1 package org.simantics.modeling.ui.actions;
\r
3 import java.lang.reflect.InvocationTargetException;
\r
4 import java.util.List;
\r
6 import org.eclipse.core.commands.AbstractHandler;
\r
7 import org.eclipse.core.commands.ExecutionEvent;
\r
8 import org.eclipse.core.commands.ExecutionException;
\r
9 import org.eclipse.core.runtime.IProgressMonitor;
\r
10 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
\r
11 import org.eclipse.jface.operation.IRunnableWithProgress;
\r
12 import org.eclipse.jface.viewers.ISelection;
\r
13 import org.eclipse.jface.viewers.IStructuredSelection;
\r
14 import org.eclipse.swt.widgets.Shell;
\r
15 import org.eclipse.ui.IWorkbenchPart;
\r
16 import org.eclipse.ui.PlatformUI;
\r
17 import org.eclipse.ui.handlers.HandlerUtil;
\r
18 import org.simantics.Simantics;
\r
19 import org.simantics.db.Resource;
\r
20 import org.simantics.db.WriteGraph;
\r
21 import org.simantics.db.common.request.WriteRequest;
\r
22 import org.simantics.db.exception.CancelTransactionException;
\r
23 import org.simantics.db.exception.DatabaseException;
\r
24 import org.simantics.db.layer0.SelectionHints;
\r
25 import org.simantics.g2d.canvas.ICanvasContext;
\r
26 import org.simantics.utils.ui.ExceptionUtils;
\r
27 import org.simantics.utils.ui.ISelectionUtils;
\r
30 * @author Tuukka Lehtonen
\r
32 public abstract class FlagOperationHandler extends AbstractHandler {
\r
34 protected abstract void perform(IProgressMonitor monitor, WriteGraph graph, List<Resource> flags,
\r
35 ICanvasContext canvasContext) throws DatabaseException;
\r
38 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
39 //System.out.println("mergeFlags");
\r
40 ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService()
\r
43 IWorkbenchPart part = HandlerUtil.getActivePartChecked(event);
\r
44 final ICanvasContext canvasContext = (ICanvasContext) part.getAdapter(ICanvasContext.class);
\r
46 if (selection instanceof IStructuredSelection) {
\r
47 final List<Resource> flags = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
\r
49 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
\r
50 new ProgressMonitorDialog(shell).run(true, false, new IRunnableWithProgress() {
\r
52 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
\r
53 runWithProgress(monitor, flags, canvasContext);
\r
56 } catch (InvocationTargetException e) {
\r
57 ExceptionUtils.logAndShowError(e.getTargetException());
\r
58 } catch (InterruptedException e) {
\r
64 protected void runWithProgress(final IProgressMonitor monitor, final List<Resource> flags, final ICanvasContext canvasContext) throws InvocationTargetException {
\r
66 Simantics.getSession().sync(new WriteRequest() {
\r
68 public void perform(WriteGraph graph) throws DatabaseException {
\r
69 graph.markUndoPoint();
\r
70 FlagOperationHandler.this.perform(monitor, graph, flags, canvasContext);
\r
73 } catch (CancelTransactionException e) {
\r
75 } catch (DatabaseException e) {
\r
76 throw new InvocationTargetException(e);
\r