]> 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 df01fe5d267207d55881afff88007f6273545df3..e7bfd35fa5477550aeb68fd0aead0342d9b2a406 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * 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
@@ -8,6 +8,8 @@
  *
  * 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;
 
@@ -18,64 +20,53 @@ import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.handlers.HandlerUtil;
 import org.simantics.Simantics;
-import org.simantics.databoard.Bindings;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
-import org.simantics.db.common.request.ResourceRead;
-import org.simantics.db.common.utils.Logger;
+import org.simantics.db.common.request.UniqueRead;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.variable.Variable;
-import org.simantics.modeling.ModelingResources;
-import org.simantics.modeling.PropertyVariables;
+import org.simantics.modeling.help.HelpContexts;
 import org.simantics.ui.selection.WorkbenchSelectionUtils;
-import org.simantics.utils.ui.AdaptionUtils;
+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) {
-        String id = null;
         try {
-            Resource element = WorkbenchSelectionUtils.getPossibleResource(event);
-            id = Simantics.getSession().syncRequest(new ResourceRead<String>(element) {
+            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 {
-                    ModelingResources MOD = ModelingResources.getInstance(graph);
-                    Resource component = graph.getPossibleObject(element, MOD.ElementToComponent);
-                    if (component != null)
-                        return graph.getPossibleRelatedValue2(component, MOD.contextualHelpId, Bindings.STRING);
-
-                    Variable var = WorkbenchSelectionUtils.getPossibleVariable(event);
-                    if (var != null)
-                        return var.getPossiblePropertyValue(graph, MOD.contextualHelpId, Bindings.STRING);
-
-                    ISelection sel = HandlerUtil.getCurrentSelection(event);
-                    if (sel != null) {
-                        PropertyVariables vars = AdaptionUtils.adaptToSingle(sel, PropertyVariables.class);
-                        if (vars != null) {
-                            var = vars.getConfiguration();
-                            if (var != null)
-                                var.getPossiblePropertyValue(graph, MOD.contextualHelpId, Bindings.STRING);
-                        }
-                    }
-                    return null;
+                    return HelpContexts.getPossibleId(graph, resource, variable, sel);
                 }
             });
         } catch (DatabaseException e) {
-            Logger.defaultLogError(e);
+            LOGGER.error("", e);
+            return null;
         }
-        return id;
     }
 
     @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);
     }
 
 }