]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/ContextualHelp.java
Add utility class org.simantics.modeling.help.HelpContexts
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / modelBrowser / handlers / ContextualHelp.java
index 4491d75a0a9f1945ea0c8e5dc4c82fcb6d38cd6c..e7bfd35fa5477550aeb68fd0aead0342d9b2a406 100644 (file)
@@ -1,81 +1,72 @@
-/*******************************************************************************\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
+/*******************************************************************************
+ * Copyright (c) 2007, 2018 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *     Semantum Oy - #7116 regression fix
+ *     Semantum Oy - gitlab #147 - expose getPossibleId implementation
+ *******************************************************************************/
+package org.simantics.modeling.ui.modelBrowser.handlers;
+
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.handlers.HandlerUtil;
+import org.simantics.Simantics;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.request.UniqueRead;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.modeling.help.HelpContexts;
+import org.simantics.ui.selection.WorkbenchSelectionUtils;
+import org.slf4j.LoggerFactory;
+
+public class ContextualHelp extends AbstractHandler {
+
+    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ContextualHelp.class);
+
+    private static String getPossibleId(ExecutionEvent event) {
+        try {
+            ISelection sel = HandlerUtil.getCurrentSelection(event);
+            Resource resource = WorkbenchSelectionUtils.getPossibleResource(sel);
+            Variable variable = WorkbenchSelectionUtils.getPossibleVariable(sel);
+            if (sel.isEmpty() && resource == null && variable == null)
+                return null;
+
+            return Simantics.getSession().syncRequest(new UniqueRead<String>() {
+                @Override
+                public String perform(ReadGraph graph) throws DatabaseException {
+                    return HelpContexts.getPossibleId(graph, resource, variable, sel);
+                }
+            });
+        } catch (DatabaseException e) {
+            LOGGER.error("", e);
+            return null;
+        }
+    }
+
+    @Override
+    public Object execute(ExecutionEvent event) throws ExecutionException {
+        String id = getPossibleId(event);
+        if (id != null)
+            PlatformUI.getWorkbench().getHelpSystem().displayHelp(id);
+        return null;
+    }
+
+    /**
+     * @deprecated use {@link HelpContexts#getPossibleId(ReadGraph, Resource, Variable, Object)} instead
+     */
+    @Deprecated
+    public static String getPossibleId(ReadGraph graph, Resource resource, Variable variable, ISelection sel) throws DatabaseException {
+        return HelpContexts.getPossibleId(graph, resource, variable, sel);
+    }
+
+}