Add utility class org.simantics.modeling.help.HelpContexts 05/4405/1
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Wed, 26 Aug 2020 06:59:48 +0000 (09:59 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Wed, 26 Aug 2020 06:59:48 +0000 (09:59 +0300)
The new class gathers headless code related to reading help context ids
from the database that was previously crammed into a very illogical
package org.simantics.modeling.ui.modelBrowser.handlers.

gitlab #588

Change-Id: I4cbc00919d2dfef50a779ccee7f31eeb6d61eee5

bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/ContextualHelp.java
bundles/org.simantics.modeling/META-INF/MANIFEST.MF
bundles/org.simantics.modeling/src/org/simantics/modeling/actions/Help.java
bundles/org.simantics.modeling/src/org/simantics/modeling/help/HelpContexts.java [new file with mode: 0644]

index 0d0ec7ce64d5554b46e1913ebb3cd33a21f3ddde..e7bfd35fa5477550aeb68fd0aead0342d9b2a406 100644 (file)
@@ -20,16 +20,13 @@ 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.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 {
@@ -47,7 +44,7 @@ public class ContextualHelp extends AbstractHandler {
             return Simantics.getSession().syncRequest(new UniqueRead<String>() {
                 @Override
                 public String perform(ReadGraph graph) throws DatabaseException {
-                    return getPossibleId(graph, resource, variable, sel);
+                    return HelpContexts.getPossibleId(graph, resource, variable, sel);
                 }
             });
         } catch (DatabaseException e) {
@@ -64,34 +61,12 @@ public class ContextualHelp extends AbstractHandler {
         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 {
-        ModelingResources MOD = ModelingResources.getInstance(graph);
-        if (resource != null) {
-            Resource component = graph.getPossibleObject(resource, MOD.ElementToComponent);
-            String id = component != null ? graph.getPossibleRelatedValue2(component, MOD.contextualHelpId, Bindings.STRING) : null;
-            if (id != null)
-                return id;
-            id = graph.getPossibleRelatedValue2(resource, MOD.contextualHelpId, Bindings.STRING);
-            if (id != null)
-                return id;
-        }
-
-        if (variable != null) {
-            String id = variable.getPossiblePropertyValue(graph, MOD.contextualHelpId, Bindings.STRING);
-            if (id != null)
-                return id;
-        }
-
-        // TODO: consider removing this block
-        if (sel != null) {
-            PropertyVariables vars = AdaptionUtils.adaptToSingle(sel, PropertyVariables.class);
-            Variable var = vars != null ? vars.getConfiguration() : null;
-            String id = var != null ? var.getPossiblePropertyValue(graph, MOD.contextualHelpId, Bindings.STRING) : null;
-            if (id != null)
-                return id;
-        }
-
-        return null;
+        return HelpContexts.getPossibleId(graph, resource, variable, sel);
     }
 
 }
index 067521a89ff9a38eaece27e3c91fecd48264ed0b..e00a8e1147cdac344a36d52cd45f858dff5e6b72 100644 (file)
@@ -47,6 +47,7 @@ Export-Package: org.simantics.modeling,
  org.simantics.modeling.adapters,
  org.simantics.modeling.export,
  org.simantics.modeling.flags,
+ org.simantics.modeling.help,
  org.simantics.modeling.mapping,
  org.simantics.modeling.migration,
  org.simantics.modeling.preferences,
index ba124310800bb29b87988d85144176fa19f7bc96..22327aeb3f71b833ea002af5705ca7080a87ac96 100644 (file)
@@ -16,38 +16,36 @@ import org.simantics.Simantics;
 import org.simantics.databoard.Bindings;
 import org.simantics.db.Resource;
 import org.simantics.db.common.primitiverequest.PossibleRelatedValue2;
-import org.simantics.db.common.utils.Logger;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.adapter.ActionFactory;
 import org.simantics.modeling.ModelingResources;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class Help implements ActionFactory {
 
+       private static final Logger LOGGER = LoggerFactory.getLogger(Help.class);
+
        @Override
        public Runnable create(Object target) {
-
-               if(!(target instanceof Resource))
+               if (!(target instanceof Resource))
                        return null;
 
-               final Resource resource = (Resource)target;
-               
-               return new Runnable() {
-                       @Override
-                       public void run() {
-                               
-                               try {
-                                       ModelingResources MOD = ModelingResources.getInstance(Simantics.getSession());
-                                       String id = Simantics.sync(new PossibleRelatedValue2<String>(resource, MOD.contextualHelpId, Bindings.STRING));
-                                       if(id == null) {
-                                               PlatformUI.getWorkbench().getHelpSystem().displayDynamicHelp();
-                                               return;
-                                       }
-                                       PlatformUI.getWorkbench().getHelpSystem().displayHelp(id);
-                               } catch (DatabaseException e) {
-                                       Logger.defaultLogError(e);
+               final Resource resource = (Resource) target;
+
+               return () -> {
+                       try {
+                               ModelingResources MOD = ModelingResources.getInstance(Simantics.getSession());
+                               String id = Simantics.sync(new PossibleRelatedValue2<String>(resource, MOD.contextualHelpId, Bindings.STRING));
+                               if (id == null) {
+                                       PlatformUI.getWorkbench().getHelpSystem().displayDynamicHelp();
+                                       return;
                                }
-                               
+                               PlatformUI.getWorkbench().getHelpSystem().displayHelp(id);
+                       } catch (DatabaseException e) {
+                               LOGGER.error("Failed to display help for resource {}", resource, e);
                        }
                }; 
        }
+
 }
diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/help/HelpContexts.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/help/HelpContexts.java
new file mode 100644 (file)
index 0000000..ca25cf4
--- /dev/null
@@ -0,0 +1,73 @@
+/*******************************************************************************
+ * Copyright (c) 2020 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:
+ *     Semantum Oy- initial API and implementation
+ *******************************************************************************/
+package org.simantics.modeling.help;
+
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+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.utils.ui.AdaptionUtils;
+
+/**
+ * @author Tuukka Lehtonen
+ * @since 1.46.0
+ */
+public class HelpContexts {
+
+    /**
+     * @param graph
+     * @param resource
+     * @param variable
+     * @param selection optional <code>ISelection</code> 
+     * @return
+     * @throws DatabaseException
+     */
+    public static String getPossibleId(ReadGraph graph, Resource resource, Variable variable, Object selection) throws DatabaseException {
+        ModelingResources MOD = ModelingResources.getInstance(graph);
+        if (resource != null) {
+            Resource component = graph.getPossibleObject(resource, MOD.ElementToComponent);
+            String id = component != null ? graph.getPossibleRelatedValue2(component, MOD.contextualHelpId, Bindings.STRING) : null;
+            if (id != null)
+                return id;
+            id = graph.getPossibleRelatedValue2(resource, MOD.contextualHelpId, Bindings.STRING);
+            if (id != null)
+                return id;
+        }
+
+        if (variable != null) {
+            String id = variable.getPossiblePropertyValue(graph, MOD.contextualHelpId, Bindings.STRING);
+            if (id != null)
+                return id;
+        }
+
+        // TODO: consider removing this block
+        if (selection != null) {
+            PropertyVariables vars = AdaptionUtils.adaptToSingle(selection, PropertyVariables.class);
+            Variable var = vars != null ? vars.getConfiguration() : null;
+            String id = var != null ? var.getPossiblePropertyValue(graph, MOD.contextualHelpId, Bindings.STRING) : null;
+            if (id != null)
+                return id;
+        }
+
+        return null;
+    }
+
+    public static String getPossibleId(ReadGraph graph, Variable variable, String property) throws DatabaseException {
+        ModelingResources MOD = ModelingResources.getInstance(graph);
+        Variable prop = variable != null ? variable.getPossibleProperty(graph, property) : null;
+        return prop != null ? prop.getPossiblePropertyValue(graph, MOD.contextualHelpId, Bindings.STRING) : null;
+    }
+
+}