1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.diagram.contribution;
14 import org.eclipse.core.runtime.Platform;
15 import org.eclipse.jface.action.Action;
16 import org.eclipse.jface.action.ActionContributionItem;
17 import org.eclipse.jface.action.IContributionItem;
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.eclipse.swt.widgets.Menu;
20 import org.eclipse.ui.IWorkbenchPart;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Session;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.g2d.canvas.ICanvasContext;
25 import org.simantics.g2d.diagram.participant.Selection;
26 import org.simantics.ui.contribution.DynamicMenuContribution;
27 import org.simantics.utils.threads.ThreadUtils;
28 import org.simantics.utils.ui.BundleUtils;
29 import org.simantics.utils.ui.workbench.WorkbenchUtils;
33 * @author Antti Villberg
35 abstract public class DiagramDynamicMenuContribution<T> extends DynamicMenuContribution {
37 private static final IContributionItem[] NONE = {};
39 private ICanvasContext canvas;
42 public void fill(Menu menu, int index) {
43 // Need to grab active part here, we're still in the SWT thread.
44 IWorkbenchPart activePart = WorkbenchUtils.getActiveWorkbenchPart();
45 if (activePart == null)
47 ICanvasContext ctx = (ICanvasContext) activePart.getAdapter(ICanvasContext.class);
53 super.fill(menu, index);
60 protected IContributionItem[] getContributionItems(ReadGraph graph, Object[] selection) throws DatabaseException {
62 T input = computeInput(graph, selection);
64 if(input == null) return NONE;
66 return new IContributionItem[] {
67 new ActionContributionItem(new Helper(graph.getSession(), canvas, input))
72 public class Helper extends Action {
74 private final T input;
75 protected final Session session;
76 protected final ICanvasContext context;
78 public Helper(Session session, ICanvasContext context, T input) {
79 super(getName(), getImage());
80 this.session = session;
81 this.context = context;
87 perform(session, context, input);
88 ThreadUtils.asyncExec(context.getThreadAccess(), new Runnable() {
91 if (context.isDisposed())
93 Selection selection = context.getAtMostOneItemOfClass(Selection.class);
94 if (selection != null) {
95 // This prevents workbench selection from being left over.
96 // Also prevents scene graph crap from being left on the screen.
105 protected ImageDescriptor silk(String name) {
106 return BundleUtils.getImageDescriptorFromBundle(Platform.getBundle("com.famfamfam.silk"), "/icons/" + name);
109 abstract protected T computeInput(ReadGraph graph, Object[] selection) throws DatabaseException;
110 abstract protected void perform(Session session, ICanvasContext context, T input);
111 abstract protected String getName();
112 abstract protected ImageDescriptor getImage();