]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/ContextualHelp.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / modelBrowser / handlers / ContextualHelp.java
diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/ContextualHelp.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/ContextualHelp.java
new file mode 100644 (file)
index 0000000..4491d75
--- /dev/null
@@ -0,0 +1,81 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modeling.ui.modelBrowser.handlers;\r
+\r
+import org.eclipse.core.commands.AbstractHandler;\r
+import org.eclipse.core.commands.ExecutionEvent;\r
+import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.ui.PlatformUI;\r
+import org.eclipse.ui.handlers.HandlerUtil;\r
+import org.simantics.Simantics;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.ResourceRead;\r
+import org.simantics.db.common.utils.Logger;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.modeling.PropertyVariables;\r
+import org.simantics.ui.selection.WorkbenchSelectionUtils;\r
+import org.simantics.utils.ui.AdaptionUtils;\r
+\r
+public class ContextualHelp extends AbstractHandler {\r
+\r
+    private static String getPossibleId(ExecutionEvent event) {\r
+        String id = null;\r
+        try {\r
+            Resource element = WorkbenchSelectionUtils.getPossibleResource(event);\r
+            id = Simantics.getSession().syncRequest(new ResourceRead<String>(element) {\r
+\r
+                @Override\r
+                public String perform(ReadGraph graph) throws DatabaseException {\r
+                    ModelingResources MOD = ModelingResources.getInstance(graph);\r
+                    Resource component = graph.getPossibleObject(element, MOD.ElementToComponent);\r
+                    if (component != null)\r
+                        return graph.getPossibleRelatedValue2(component, MOD.contextualHelpId, Bindings.STRING);\r
+\r
+                    Variable var = WorkbenchSelectionUtils.getPossibleVariable(event);\r
+                    if (var != null)\r
+                        return var.getPossiblePropertyValue(graph, MOD.contextualHelpId, Bindings.STRING);\r
+\r
+                    ISelection sel = HandlerUtil.getCurrentSelection(event);\r
+                    if (sel != null) {\r
+                        PropertyVariables vars = AdaptionUtils.adaptToSingle(sel, PropertyVariables.class);\r
+                        if (vars != null) {\r
+                            var = vars.getConfiguration();\r
+                            if (var != null)\r
+                                var.getPossiblePropertyValue(graph, MOD.contextualHelpId, Bindings.STRING);\r
+                        }\r
+                    }\r
+                    return null;\r
+                }\r
+            });\r
+        } catch (DatabaseException e) {\r
+            Logger.defaultLogError(e);\r
+        }\r
+        return id;\r
+    }\r
+\r
+    @Override\r
+    public Object execute(ExecutionEvent event) throws ExecutionException {\r
+\r
+        String id = getPossibleId(event);\r
+        if (id != null)\r
+            PlatformUI.getWorkbench().getHelpSystem().displayHelp(id);\r
+\r
+        return null;\r
+\r
+    }\r
+\r
+}\r