1 package org.simantics.modeling.ui.actions;
3 import java.lang.reflect.InvocationTargetException;
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;
30 * @author Tuukka Lehtonen
32 public abstract class FlagOperationHandler extends AbstractHandler {
34 protected abstract void perform(IProgressMonitor monitor, WriteGraph graph, List<Resource> flags,
35 ICanvasContext canvasContext) throws DatabaseException;
38 public Object execute(ExecutionEvent event) throws ExecutionException {
39 //System.out.println("mergeFlags");
40 ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService()
43 IWorkbenchPart part = HandlerUtil.getActivePartChecked(event);
44 final ICanvasContext canvasContext = (ICanvasContext) part.getAdapter(ICanvasContext.class);
46 if (selection instanceof IStructuredSelection) {
47 final List<Resource> flags = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
49 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
50 new ProgressMonitorDialog(shell).run(true, false, new IRunnableWithProgress() {
52 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
53 runWithProgress(monitor, flags, canvasContext);
56 } catch (InvocationTargetException e) {
57 ExceptionUtils.logAndShowError(e.getTargetException());
58 } catch (InterruptedException e) {
64 protected void runWithProgress(final IProgressMonitor monitor, final List<Resource> flags, final ICanvasContext canvasContext) throws InvocationTargetException {
66 Simantics.getSession().sync(new WriteRequest() {
68 public void perform(WriteGraph graph) throws DatabaseException {
69 graph.markUndoPoint();
70 FlagOperationHandler.this.perform(monitor, graph, flags, canvasContext);
73 } catch (CancelTransactionException e) {
75 } catch (DatabaseException e) {
76 throw new InvocationTargetException(e);