1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.modeling.ui.actions;
\r
14 import org.eclipse.core.runtime.Platform;
\r
15 import org.eclipse.jface.action.Action;
\r
16 import org.eclipse.jface.action.ActionContributionItem;
\r
17 import org.eclipse.jface.action.IContributionItem;
\r
18 import org.eclipse.jface.resource.ImageDescriptor;
\r
19 import org.simantics.db.ReadGraph;
\r
20 import org.simantics.db.Session;
\r
21 import org.simantics.db.exception.DatabaseException;
\r
22 import org.simantics.ui.contribution.DynamicMenuContribution;
\r
23 import org.simantics.utils.ui.BundleUtils;
\r
27 * @author Antti Villberg
\r
29 abstract public class ExplorerDynamicMenuContribution<T> extends DynamicMenuContribution {
\r
31 private static final IContributionItem[] NONE = {};
\r
34 protected IContributionItem[] getContributionItems(ReadGraph graph, Object[] selection) throws DatabaseException {
\r
36 T input = computeInput(graph, selection);
\r
38 if(input == null) return NONE;
\r
40 return new IContributionItem[] {
\r
41 new ActionContributionItem(new Helper(graph, input))
\r
46 public class Helper extends Action {
\r
48 private final T input;
\r
49 protected final Session session;
\r
51 public Helper(ReadGraph graph, T input) throws DatabaseException {
\r
52 super(getName(graph, input), getImage(graph, input));
\r
53 this.session = graph.getSession();
\r
59 perform(session, input);
\r
64 protected ImageDescriptor silk(String name) {
\r
65 return BundleUtils.getImageDescriptorFromBundle(Platform.getBundle("com.famfamfam.silk"), "/icons/" + name);
\r
68 abstract protected T computeInput(ReadGraph graph, Object[] selection) throws DatabaseException;
\r
69 abstract protected void perform(Session session, T input);
\r
70 abstract protected String getName(ReadGraph graph, T input) throws DatabaseException ;
\r
71 abstract protected ImageDescriptor getImage(ReadGraph graph, T input) throws DatabaseException;
\r