]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/ContextualHelp.java
4491d75a0a9f1945ea0c8e5dc4c82fcb6d38cd6c
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / modelBrowser / handlers / ContextualHelp.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.modelBrowser.handlers;\r
13 \r
14 import org.eclipse.core.commands.AbstractHandler;\r
15 import org.eclipse.core.commands.ExecutionEvent;\r
16 import org.eclipse.core.commands.ExecutionException;\r
17 import org.eclipse.jface.viewers.ISelection;\r
18 import org.eclipse.ui.PlatformUI;\r
19 import org.eclipse.ui.handlers.HandlerUtil;\r
20 import org.simantics.Simantics;\r
21 import org.simantics.databoard.Bindings;\r
22 import org.simantics.db.ReadGraph;\r
23 import org.simantics.db.Resource;\r
24 import org.simantics.db.common.request.ResourceRead;\r
25 import org.simantics.db.common.utils.Logger;\r
26 import org.simantics.db.exception.DatabaseException;\r
27 import org.simantics.db.layer0.variable.Variable;\r
28 import org.simantics.modeling.ModelingResources;\r
29 import org.simantics.modeling.PropertyVariables;\r
30 import org.simantics.ui.selection.WorkbenchSelectionUtils;\r
31 import org.simantics.utils.ui.AdaptionUtils;\r
32 \r
33 public class ContextualHelp extends AbstractHandler {\r
34 \r
35     private static String getPossibleId(ExecutionEvent event) {\r
36         String id = null;\r
37         try {\r
38             Resource element = WorkbenchSelectionUtils.getPossibleResource(event);\r
39             id = Simantics.getSession().syncRequest(new ResourceRead<String>(element) {\r
40 \r
41                 @Override\r
42                 public String perform(ReadGraph graph) throws DatabaseException {\r
43                     ModelingResources MOD = ModelingResources.getInstance(graph);\r
44                     Resource component = graph.getPossibleObject(element, MOD.ElementToComponent);\r
45                     if (component != null)\r
46                         return graph.getPossibleRelatedValue2(component, MOD.contextualHelpId, Bindings.STRING);\r
47 \r
48                     Variable var = WorkbenchSelectionUtils.getPossibleVariable(event);\r
49                     if (var != null)\r
50                         return var.getPossiblePropertyValue(graph, MOD.contextualHelpId, Bindings.STRING);\r
51 \r
52                     ISelection sel = HandlerUtil.getCurrentSelection(event);\r
53                     if (sel != null) {\r
54                         PropertyVariables vars = AdaptionUtils.adaptToSingle(sel, PropertyVariables.class);\r
55                         if (vars != null) {\r
56                             var = vars.getConfiguration();\r
57                             if (var != null)\r
58                                 var.getPossiblePropertyValue(graph, MOD.contextualHelpId, Bindings.STRING);\r
59                         }\r
60                     }\r
61                     return null;\r
62                 }\r
63             });\r
64         } catch (DatabaseException e) {\r
65             Logger.defaultLogError(e);\r
66         }\r
67         return id;\r
68     }\r
69 \r
70     @Override\r
71     public Object execute(ExecutionEvent event) throws ExecutionException {\r
72 \r
73         String id = getPossibleId(event);\r
74         if (id != null)\r
75             PlatformUI.getWorkbench().getHelpSystem().displayHelp(id);\r
76 \r
77         return null;\r
78 \r
79     }\r
80 \r
81 }\r