]> gerrit.simantics Code Review - simantics/platform.git/blob
03512a9c4c723178dda3d1eb8af2021b30bcdc45
[simantics/platform.git] /
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.actions;
13
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;
24
25
26 /**
27  * @author Antti Villberg
28  */
29 abstract public class ExplorerDynamicMenuContribution<T> extends DynamicMenuContribution {
30
31         private static final IContributionItem[] NONE = {};
32
33         @Override
34         protected IContributionItem[] getContributionItems(ReadGraph graph, Object[] selection) throws DatabaseException {
35
36                 T input = computeInput(graph, selection);
37
38                 if(input == null) return NONE;
39
40                 return new IContributionItem[] {
41                                 new ActionContributionItem(new Helper(graph, input))
42                 };
43
44         }
45
46         public class Helper extends Action {
47
48                 private final T input;
49                 protected final Session        session;
50
51                 public Helper(ReadGraph graph, T input) throws DatabaseException {
52                         super(getName(graph, input), getImage(graph, input));
53                         this.session = graph.getSession();
54                         this.input = input;
55                 }
56
57                 @Override
58                 public void run() {
59                         perform(session, input);
60                 }
61
62         }
63
64         protected ImageDescriptor silk(String name) {
65                 return BundleUtils.getImageDescriptorFromBundle(Platform.getBundle("com.famfamfam.silk"), "/icons/" + name);
66         }
67
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;
72
73 }