]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/FlagOperationHandler.java
(refs #7561) Allow extra parameters to the application of type alias
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / actions / FlagOperationHandler.java
1 package org.simantics.modeling.ui.actions;
2
3 import java.lang.reflect.InvocationTargetException;
4 import java.util.List;
5
6 import org.eclipse.core.commands.AbstractHandler;
7 import org.eclipse.core.commands.ExecutionEvent;
8 import org.eclipse.core.commands.ExecutionException;
9 import org.eclipse.core.runtime.IProgressMonitor;
10 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
11 import org.eclipse.jface.operation.IRunnableWithProgress;
12 import org.eclipse.jface.viewers.ISelection;
13 import org.eclipse.jface.viewers.IStructuredSelection;
14 import org.eclipse.swt.widgets.Shell;
15 import org.eclipse.ui.IWorkbenchPart;
16 import org.eclipse.ui.PlatformUI;
17 import org.eclipse.ui.handlers.HandlerUtil;
18 import org.simantics.Simantics;
19 import org.simantics.db.Resource;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.common.request.WriteRequest;
22 import org.simantics.db.exception.CancelTransactionException;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.layer0.SelectionHints;
25 import org.simantics.g2d.canvas.ICanvasContext;
26 import org.simantics.utils.ui.ExceptionUtils;
27 import org.simantics.utils.ui.ISelectionUtils;
28
29 /**
30  * @author Tuukka Lehtonen
31  */
32 public abstract class FlagOperationHandler extends AbstractHandler {
33
34     protected abstract void perform(IProgressMonitor monitor, WriteGraph graph, List<Resource> flags,
35             ICanvasContext canvasContext) throws DatabaseException;
36
37     @Override
38     public Object execute(ExecutionEvent event) throws ExecutionException {
39         //System.out.println("mergeFlags");
40         ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService()
41             .getSelection();
42
43         IWorkbenchPart part = HandlerUtil.getActivePartChecked(event);
44         final ICanvasContext canvasContext = (ICanvasContext) part.getAdapter(ICanvasContext.class);
45
46         if (selection instanceof IStructuredSelection) {
47             final List<Resource> flags = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
48             try {
49                 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
50                 new ProgressMonitorDialog(shell).run(true, false, new IRunnableWithProgress() {
51                     @Override
52                     public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
53                         runWithProgress(monitor, flags, canvasContext);
54                     }
55                 });
56             } catch (InvocationTargetException e) {
57                 ExceptionUtils.logAndShowError(e.getTargetException());
58             } catch (InterruptedException e) {
59             }
60         }
61         return null;
62     }
63
64     protected void runWithProgress(final IProgressMonitor monitor, final List<Resource> flags, final ICanvasContext canvasContext) throws InvocationTargetException {
65         try {
66             Simantics.getSession().sync(new WriteRequest() {
67                 @Override
68                 public void perform(WriteGraph graph) throws DatabaseException {
69                     graph.markUndoPoint();
70                     FlagOperationHandler.this.perform(monitor, graph, flags, canvasContext);
71                 }
72             });
73         } catch (CancelTransactionException e) {
74             // OK.
75         } catch (DatabaseException e) {
76             throw new InvocationTargetException(e);
77         } finally {
78             monitor.done();
79         }
80     }
81
82 }