]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/ExplorerDynamicMenuContribution.java
Sync git svn branch with SVN repository r33319.
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / actions / ExplorerDynamicMenuContribution.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.modeling.ui.actions;\r
13 \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
24 \r
25 \r
26 /**\r
27  * @author Antti Villberg\r
28  */\r
29 abstract public class ExplorerDynamicMenuContribution<T> extends DynamicMenuContribution {\r
30 \r
31         private static final IContributionItem[] NONE = {};\r
32 \r
33         @Override\r
34         protected IContributionItem[] getContributionItems(ReadGraph graph, Object[] selection) throws DatabaseException {\r
35 \r
36                 T input = computeInput(graph, selection);\r
37 \r
38                 if(input == null) return NONE;\r
39 \r
40                 return new IContributionItem[] {\r
41                                 new ActionContributionItem(new Helper(graph, input))\r
42                 };\r
43 \r
44         }\r
45 \r
46         public class Helper extends Action {\r
47 \r
48                 private final T input;\r
49                 protected final Session        session;\r
50 \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
54                         this.input = input;\r
55                 }\r
56 \r
57                 @Override\r
58                 public void run() {\r
59                         perform(session, input);\r
60                 }\r
61 \r
62         }\r
63 \r
64         protected ImageDescriptor silk(String name) {\r
65                 return BundleUtils.getImageDescriptorFromBundle(Platform.getBundle("com.famfamfam.silk"), "/icons/" + name);\r
66         }\r
67 \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
72 \r
73 }\r