--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.diagram.contribution;\r
+\r
+import org.eclipse.core.runtime.Platform;\r
+import org.eclipse.jface.action.Action;\r
+import org.eclipse.jface.action.ActionContributionItem;\r
+import org.eclipse.jface.action.IContributionItem;\r
+import org.eclipse.jface.resource.ImageDescriptor;\r
+import org.eclipse.swt.widgets.Menu;\r
+import org.eclipse.ui.IWorkbenchPart;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.g2d.canvas.ICanvasContext;\r
+import org.simantics.g2d.diagram.participant.Selection;\r
+import org.simantics.ui.contribution.DynamicMenuContribution;\r
+import org.simantics.utils.threads.ThreadUtils;\r
+import org.simantics.utils.ui.BundleUtils;\r
+import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
+\r
+\r
+/**\r
+ * @author Antti Villberg\r
+ */\r
+abstract public class DiagramDynamicMenuContribution<T> extends DynamicMenuContribution {\r
+\r
+ private static final IContributionItem[] NONE = {};\r
+\r
+ private ICanvasContext canvas;\r
+\r
+ @Override\r
+ public void fill(Menu menu, int index) {\r
+ // Need to grab active part here, we're still in the SWT thread.\r
+ IWorkbenchPart activePart = WorkbenchUtils.getActiveWorkbenchPart();\r
+ if (activePart == null)\r
+ return;\r
+ ICanvasContext ctx = (ICanvasContext) activePart.getAdapter(ICanvasContext.class);\r
+ if (ctx == null)\r
+ return;\r
+\r
+ this.canvas = ctx;\r
+ try {\r
+ super.fill(menu, index);\r
+ } finally {\r
+ this.canvas = null;\r
+ }\r
+ }\r
+\r
+ @Override\r
+ protected IContributionItem[] getContributionItems(ReadGraph graph, Object[] selection) throws DatabaseException {\r
+\r
+ T input = computeInput(graph, selection);\r
+\r
+ if(input == null) return NONE;\r
+\r
+ return new IContributionItem[] {\r
+ new ActionContributionItem(new Helper(graph.getSession(), canvas, input))\r
+ };\r
+\r
+ }\r
+\r
+ public class Helper extends Action {\r
+\r
+ private final T input;\r
+ protected final Session session;\r
+ protected final ICanvasContext context;\r
+\r
+ public Helper(Session session, ICanvasContext context, T input) {\r
+ super(getName(), getImage());\r
+ this.session = session;\r
+ this.context = context;\r
+ this.input = input;\r
+ }\r
+\r
+ @Override\r
+ public void run() {\r
+ perform(session, context, input);\r
+ ThreadUtils.asyncExec(context.getThreadAccess(), new Runnable() {\r
+ @Override\r
+ public void run() {\r
+ if (context.isDisposed())\r
+ return;\r
+ Selection selection = context.getAtMostOneItemOfClass(Selection.class);\r
+ if (selection != null) {\r
+ // This prevents workbench selection from being left over.\r
+ // Also prevents scene graph crap from being left on the screen.\r
+ selection.clear(0);\r
+ }\r
+ }\r
+ });\r
+ }\r
+\r
+ }\r
+\r
+ protected ImageDescriptor silk(String name) {\r
+ return BundleUtils.getImageDescriptorFromBundle(Platform.getBundle("com.famfamfam.silk"), "/icons/" + name);\r
+ }\r
+\r
+ abstract protected T computeInput(ReadGraph graph, Object[] selection) throws DatabaseException;\r
+ abstract protected void perform(Session session, ICanvasContext context, T input);\r
+ abstract protected String getName();\r
+ abstract protected ImageDescriptor getImage();\r
+\r
+}\r