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.modeling.ui.actions;
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.simantics.db.ReadGraph;
20 import org.simantics.db.Session;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.ui.contribution.DynamicMenuContribution;
23 import org.simantics.utils.ui.BundleUtils;
27 * @author Antti Villberg
29 abstract public class ExplorerDynamicMenuContribution<T> extends DynamicMenuContribution {
31 private static final IContributionItem[] NONE = {};
34 protected IContributionItem[] getContributionItems(ReadGraph graph, Object[] selection) throws DatabaseException {
36 T input = computeInput(graph, selection);
38 if(input == null) return NONE;
40 return new IContributionItem[] {
41 new ActionContributionItem(new Helper(graph, input))
46 public class Helper extends Action {
48 private final T input;
49 protected final Session session;
51 public Helper(ReadGraph graph, T input) throws DatabaseException {
52 super(getName(graph, input), getImage(graph, input));
53 this.session = graph.getSession();
59 perform(session, input);
64 protected ImageDescriptor silk(String name) {
65 return BundleUtils.getImageDescriptorFromBundle(Platform.getBundle("com.famfamfam.silk"), "/icons/" + name); //$NON-NLS-1$ //$NON-NLS-2$
68 abstract protected T computeInput(ReadGraph graph, Object[] selection) throws DatabaseException;
69 abstract protected void perform(Session session, T input);
70 abstract protected String getName(ReadGraph graph, T input) throws DatabaseException ;
71 abstract protected ImageDescriptor getImage(ReadGraph graph, T input) throws DatabaseException;