|
12 | 12 | import java.util.ArrayList; |
13 | 13 | import java.util.Collections; |
14 | 14 | import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.Map.Entry; |
| 17 | +import java.util.TreeMap; |
| 18 | + |
15 | 19 | import org.eclipse.core.runtime.IProgressMonitor; |
16 | 20 | import org.eclipse.core.runtime.IStatus; |
17 | 21 | import org.eclipse.core.runtime.Status; |
|
30 | 34 | import org.eclipse.jface.action.IMenuManager; |
31 | 35 | import org.eclipse.jface.action.IToolBarManager; |
32 | 36 | import org.eclipse.jface.action.MenuManager; |
33 | | -import org.eclipse.jface.action.Separator; |
34 | 37 | import org.eclipse.jface.viewers.IStructuredSelection; |
35 | 38 | import org.eclipse.jface.viewers.ITableLabelProvider; |
36 | 39 | import org.eclipse.jface.viewers.LabelProvider; |
37 | 40 | import org.eclipse.jface.viewers.StructuredSelection; |
38 | 41 | import org.eclipse.jface.viewers.TableViewer; |
39 | 42 | import org.eclipse.jface.viewers.ViewerComparator; |
| 43 | +import org.eclipse.jface.window.Window; |
40 | 44 | import org.eclipse.swt.SWT; |
41 | 45 | import org.eclipse.swt.graphics.Image; |
42 | 46 | import org.eclipse.swt.widgets.Composite; |
43 | 47 | import org.eclipse.swt.widgets.Display; |
44 | 48 | import org.eclipse.swt.widgets.Menu; |
45 | 49 | import org.eclipse.ui.IActionBars; |
46 | | -import org.eclipse.ui.IWorkbenchActionConstants; |
47 | 50 | import org.eclipse.ui.PlatformUI; |
| 51 | +import org.eclipse.ui.dialogs.ElementListSelectionDialog; |
48 | 52 | import org.eclipse.ui.part.ViewPart; |
49 | 53 |
|
50 | 54 | public class ValidationView extends ViewPart { |
@@ -167,54 +171,101 @@ private void fillLocalPullDown(IMenuManager manager) { |
167 | 171 | } |
168 | 172 |
|
169 | 173 | private void fillContextMenu(IMenuManager manager) { |
170 | | - UnsatisfiedConstraint unsatisfiedConstraint = (UnsatisfiedConstraint)((StructuredSelection) viewer.getSelection()).getFirstElement(); |
171 | | - if (unsatisfiedConstraint == null) return; |
172 | | - |
173 | | - for (FixInstance fixInstance : unsatisfiedConstraint.getFixes()) { |
174 | | - manager.add(new PerformFixAction(unsatisfiedConstraint, fixInstance)); |
| 174 | + StructuredSelection selection = (StructuredSelection) viewer.getSelection(); |
| 175 | + UnsatisfiedConstraint unsatisfiedConstraint = (UnsatisfiedConstraint) selection.getFirstElement(); |
| 176 | + |
| 177 | + if (unsatisfiedConstraint != null && !unsatisfiedConstraint.getFixes().isEmpty()) { |
| 178 | + manager.add(new QuickFixAction(unsatisfiedConstraint)); |
175 | 179 | } |
176 | | - |
177 | | - // Other plug-ins can contribute there actions here |
178 | | - manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); |
179 | 180 | } |
180 | | - |
181 | | - class PerformFixAction extends Action { |
182 | | - UnsatisfiedConstraint unsatisfiedConstraint = null; |
183 | | - FixInstance fixInstance = null; |
184 | | - |
185 | | - public PerformFixAction(UnsatisfiedConstraint unsatisfiedConstraint, FixInstance fixInstance) { |
186 | | - this.unsatisfiedConstraint = unsatisfiedConstraint; |
187 | | - this.fixInstance = fixInstance; |
188 | | - this.setImageDescriptor(EvlPlugin.getDefault().getImageDescriptor("icons/fix.gif")); |
189 | | - try { |
190 | | - this.setText(fixInstance.getTitle()); |
| 181 | + |
| 182 | + protected class QuickFixAction extends Action { |
| 183 | + protected class ComputeFixTitlesJob extends Job { |
| 184 | + protected ComputeFixTitlesJob(String name) { |
| 185 | + super(name); |
191 | 186 | } |
192 | | - catch (EolRuntimeException e) { |
193 | | - module.getContext().getErrorStream().println(e.toString()); |
194 | | - this.setText("An exception occured while evaluating the title of the fix"); |
| 187 | + |
| 188 | + @Override |
| 189 | + protected IStatus run(IProgressMonitor monitor) { |
| 190 | + final Map<String, FixInstance> fixesByTitle = new TreeMap<>(); |
| 191 | + for (FixInstance fixInstance : unsatisfiedConstraint.getFixes()) { |
| 192 | + try { |
| 193 | + fixesByTitle.put(fixInstance.getTitle(), fixInstance); |
| 194 | + } catch (EolRuntimeException e) { |
| 195 | + return Status.error(e.getMessage(), e); |
| 196 | + } |
| 197 | + } |
| 198 | + PlatformUI.getWorkbench().getDisplay().asyncExec(() -> { |
| 199 | + showQuickFixDialog(unsatisfiedConstraint, fixesByTitle); |
| 200 | + }); |
| 201 | + return Status.OK_STATUS; |
| 202 | + } |
| 203 | + |
| 204 | + @SuppressWarnings("unchecked") |
| 205 | + protected void showQuickFixDialog(UnsatisfiedConstraint unsatisfiedConstraint, final Map<String, FixInstance> fixesByTitle) { |
| 206 | + ElementListSelectionDialog dialog = new ElementListSelectionDialog( |
| 207 | + getViewSite().getShell(), new MapEntryLabelProvider()); |
| 208 | + |
| 209 | + dialog.setElements(fixesByTitle.entrySet().toArray()); |
| 210 | + dialog.setTitle("Select a quick fix"); |
| 211 | + dialog.setMultipleSelection(false); |
| 212 | + if (dialog.open() == Window.OK) { |
| 213 | + Entry<String, FixInstance> selected = (Map.Entry<String, FixInstance>) dialog.getFirstResult(); |
| 214 | + if (selected != null) { |
| 215 | + // Need to run fix from a non-UI background job (in case it is debugged) |
| 216 | + new RunFixJob("Run fix", selected).schedule(); |
| 217 | + } |
| 218 | + } |
195 | 219 | } |
196 | 220 | } |
197 | | - |
| 221 | + |
| 222 | + protected class RunFixJob extends Job { |
| 223 | + private final Entry<String, FixInstance> selected; |
| 224 | + |
| 225 | + protected RunFixJob(String name, Entry<String, FixInstance> selected) { |
| 226 | + super(name); |
| 227 | + this.selected = selected; |
| 228 | + } |
| 229 | + |
| 230 | + @Override |
| 231 | + protected IStatus run(IProgressMonitor monitor) { |
| 232 | + try { |
| 233 | + selected.getValue().perform(); |
| 234 | + unsatisfiedConstraint.setFixed(true); |
| 235 | + PlatformUI.getWorkbench().getDisplay().asyncExec(() -> { |
| 236 | + setDone(!existUnsatisfiedConstraintsToFix()); |
| 237 | + viewer.refresh(); |
| 238 | + }); |
| 239 | + } catch (Exception e) { |
| 240 | + module.getContext().getErrorStream().println(e.toString()); |
| 241 | + return Status.error(e.getMessage()); |
| 242 | + } |
| 243 | + return Status.OK_STATUS; |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + protected class MapEntryLabelProvider extends LabelProvider { |
| 248 | + @SuppressWarnings("unchecked") |
| 249 | + @Override |
| 250 | + public String getText(Object element) { |
| 251 | + if (element instanceof Map.Entry) { |
| 252 | + return ((Map.Entry<String, Object>) element).getKey(); |
| 253 | + } |
| 254 | + return super.getText(element); |
| 255 | + } |
| 256 | + } |
| 257 | + |
| 258 | + private final UnsatisfiedConstraint unsatisfiedConstraint; |
| 259 | + |
| 260 | + public QuickFixAction(UnsatisfiedConstraint unsatisfiedConstraint) { |
| 261 | + super("Quick Fix..."); |
| 262 | + this.unsatisfiedConstraint = unsatisfiedConstraint; |
| 263 | + } |
| 264 | + |
198 | 265 | @Override |
199 | 266 | public void run() { |
200 | | - // Need to run fix from a non-UI background job (in case it is debugged) |
201 | | - new Job("Run fix") { |
202 | | - @Override |
203 | | - protected IStatus run(IProgressMonitor monitor) { |
204 | | - try { |
205 | | - fixInstance.perform(); |
206 | | - unsatisfiedConstraint.setFixed(true); |
207 | | - PlatformUI.getWorkbench().getDisplay().asyncExec(() -> { |
208 | | - setDone(!existUnsatisfiedConstraintsToFix()); |
209 | | - viewer.refresh(); |
210 | | - }); |
211 | | - } catch (Exception e) { |
212 | | - module.getContext().getErrorStream().println(e.toString()); |
213 | | - return Status.error(e.getMessage()); |
214 | | - } |
215 | | - return Status.OK_STATUS; |
216 | | - } |
217 | | - }.schedule(); |
| 267 | + // Need to compute fix titles from a background job (could have a breakpoint) |
| 268 | + new ComputeFixTitlesJob("Compute fix titles").schedule(); |
218 | 269 | } |
219 | 270 | } |
220 | 271 |
|
|
0 commit comments