]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/ContextualHelp.java
Expose helpContextId resolving to other implementations
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / modelBrowser / handlers / ContextualHelp.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2018 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *     Semantum Oy - #7116 regression fix
12  *     Semantum Oy - gitlab #147 - expose getPossibleId implementation
13  *******************************************************************************/
14 package org.simantics.modeling.ui.modelBrowser.handlers;
15
16 import org.eclipse.core.commands.AbstractHandler;
17 import org.eclipse.core.commands.ExecutionEvent;
18 import org.eclipse.core.commands.ExecutionException;
19 import org.eclipse.jface.viewers.ISelection;
20 import org.eclipse.ui.PlatformUI;
21 import org.eclipse.ui.handlers.HandlerUtil;
22 import org.simantics.Simantics;
23 import org.simantics.databoard.Bindings;
24 import org.simantics.db.ReadGraph;
25 import org.simantics.db.Resource;
26 import org.simantics.db.common.request.UniqueRead;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.db.layer0.variable.Variable;
29 import org.simantics.modeling.ModelingResources;
30 import org.simantics.modeling.PropertyVariables;
31 import org.simantics.ui.selection.WorkbenchSelectionUtils;
32 import org.simantics.utils.ui.AdaptionUtils;
33 import org.slf4j.LoggerFactory;
34
35 public class ContextualHelp extends AbstractHandler {
36
37     private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ContextualHelp.class);
38
39     private static String getPossibleId(ExecutionEvent event) {
40         try {
41             ISelection sel = HandlerUtil.getCurrentSelection(event);
42             Resource resource = WorkbenchSelectionUtils.getPossibleResource(sel);
43             Variable variable = WorkbenchSelectionUtils.getPossibleVariable(sel);
44             if (sel.isEmpty() && resource == null && variable == null)
45                 return null;
46
47             return Simantics.getSession().syncRequest(new UniqueRead<String>() {
48                 @Override
49                 public String perform(ReadGraph graph) throws DatabaseException {
50                     return getPossibleId(graph, resource, variable, sel);
51                 }
52             });
53         } catch (DatabaseException e) {
54             LOGGER.error("", e);
55             return null;
56         }
57     }
58
59     @Override
60     public Object execute(ExecutionEvent event) throws ExecutionException {
61         String id = getPossibleId(event);
62         if (id != null)
63             PlatformUI.getWorkbench().getHelpSystem().displayHelp(id);
64         return null;
65     }
66
67     public static String getPossibleId(ReadGraph graph, Resource resource, Variable variable, ISelection sel) throws DatabaseException {
68         ModelingResources MOD = ModelingResources.getInstance(graph);
69         if (resource != null) {
70             Resource component = graph.getPossibleObject(resource, MOD.ElementToComponent);
71             String id = component != null ? graph.getPossibleRelatedValue2(component, MOD.contextualHelpId, Bindings.STRING) : null;
72             if (id != null)
73                 return id;
74             id = graph.getPossibleRelatedValue2(resource, MOD.contextualHelpId, Bindings.STRING);
75             if (id != null)
76                 return id;
77         }
78
79         if (variable != null) {
80             String id = variable.getPossiblePropertyValue(graph, MOD.contextualHelpId, Bindings.STRING);
81             if (id != null)
82                 return id;
83         }
84
85         // TODO: consider removing this block
86         if (sel != null) {
87             PropertyVariables vars = AdaptionUtils.adaptToSingle(sel, PropertyVariables.class);
88             Variable var = vars != null ? vars.getConfiguration() : null;
89             String id = var != null ? var.getPossiblePropertyValue(graph, MOD.contextualHelpId, Bindings.STRING) : null;
90             if (id != null)
91                 return id;
92         }
93
94         return null;
95     }
96
97 }