]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/help/HelpContexts.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / help / HelpContexts.java
1 /*******************************************************************************
2  * Copyright (c) 2020 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  *     Semantum Oy- initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.help;
13
14 import org.simantics.databoard.Bindings;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.layer0.variable.Variable;
19 import org.simantics.modeling.ModelingResources;
20 import org.simantics.modeling.PropertyVariables;
21 import org.simantics.utils.ui.AdaptionUtils;
22
23 /**
24  * @author Tuukka Lehtonen
25  * @since 1.46.0
26  */
27 public class HelpContexts {
28
29     /**
30      * @param graph
31      * @param resource
32      * @param variable
33      * @param selection optional <code>ISelection</code> 
34      * @return
35      * @throws DatabaseException
36      */
37     public static String getPossibleId(ReadGraph graph, Resource resource, Variable variable, Object selection) throws DatabaseException {
38         ModelingResources MOD = ModelingResources.getInstance(graph);
39         if (resource != null) {
40             Resource component = graph.getPossibleObject(resource, MOD.ElementToComponent);
41             String id = component != null ? graph.getPossibleRelatedValue2(component, MOD.contextualHelpId, Bindings.STRING) : null;
42             if (id != null)
43                 return id;
44             id = graph.getPossibleRelatedValue2(resource, MOD.contextualHelpId, Bindings.STRING);
45             if (id != null)
46                 return id;
47         }
48
49         if (variable != null) {
50             String id = variable.getPossiblePropertyValue(graph, MOD.contextualHelpId, Bindings.STRING);
51             if (id != null)
52                 return id;
53         }
54
55         // TODO: consider removing this block
56         if (selection != null) {
57             PropertyVariables vars = AdaptionUtils.adaptToSingle(selection, PropertyVariables.class);
58             Variable var = vars != null ? vars.getConfiguration() : null;
59             String id = var != null ? var.getPossiblePropertyValue(graph, MOD.contextualHelpId, Bindings.STRING) : null;
60             if (id != null)
61                 return id;
62         }
63
64         return null;
65     }
66
67     public static String getPossibleId(ReadGraph graph, Variable variable, String property) throws DatabaseException {
68         ModelingResources MOD = ModelingResources.getInstance(graph);
69         Variable prop = variable != null ? variable.getPossibleProperty(graph, property) : null;
70         return prop != null ? prop.getPossiblePropertyValue(graph, MOD.contextualHelpId, Bindings.STRING) : null;
71     }
72
73 }