]> gerrit.simantics Code Review - simantics/platform.git/blob - modelBrowser/handlers/ContextualHelp.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / modelBrowser / handlers / ContextualHelp.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2017 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  *******************************************************************************/
13 package org.simantics.modeling.ui.modelBrowser.handlers;
14
15 import org.eclipse.core.commands.AbstractHandler;
16 import org.eclipse.core.commands.ExecutionEvent;
17 import org.eclipse.core.commands.ExecutionException;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.ui.PlatformUI;
20 import org.eclipse.ui.handlers.HandlerUtil;
21 import org.simantics.Simantics;
22 import org.simantics.databoard.Bindings;
23 import org.simantics.db.ReadGraph;
24 import org.simantics.db.Resource;
25 import org.simantics.db.common.request.UniqueRead;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.db.layer0.variable.Variable;
28 import org.simantics.modeling.ModelingResources;
29 import org.simantics.modeling.PropertyVariables;
30 import org.simantics.ui.selection.WorkbenchSelectionUtils;
31 import org.simantics.utils.ui.AdaptionUtils;
32 import org.slf4j.LoggerFactory;
33
34 public class ContextualHelp extends AbstractHandler {
35
36     private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ContextualHelp.class);
37
38     private static String getPossibleId(ExecutionEvent event) {
39         try {
40             ISelection sel = HandlerUtil.getCurrentSelection(event);
41             Resource resource = WorkbenchSelectionUtils.getPossibleResource(sel);
42             Variable variable = WorkbenchSelectionUtils.getPossibleVariable(sel);
43             if (sel.isEmpty() && resource == null && variable == null)
44                 return null;
45
46             return Simantics.getSession().syncRequest(new UniqueRead<String>() {
47                 @Override
48                 public String perform(ReadGraph graph) throws DatabaseException {
49                     ModelingResources MOD = ModelingResources.getInstance(graph);
50                     if (resource != null) {
51                         Resource component = graph.getPossibleObject(resource, MOD.ElementToComponent);
52                         String id = component != null ? graph.getPossibleRelatedValue2(component, MOD.contextualHelpId, Bindings.STRING) : null;
53                         if (id != null)
54                             return id;
55                         id = graph.getPossibleRelatedValue2(resource, MOD.contextualHelpId, Bindings.STRING);
56                         if (id != null)
57                             return id;
58                     }
59
60                     if (variable != null) {
61                         String id = variable.getPossiblePropertyValue(graph, MOD.contextualHelpId, Bindings.STRING);
62                         if (id != null)
63                             return id;
64                     }
65
66                     // TODO: consider removing this block
67                     if (sel != null) {
68                         PropertyVariables vars = AdaptionUtils.adaptToSingle(sel, PropertyVariables.class);
69                         Variable var = vars != null ? vars.getConfiguration() : null;
70                         String id = var != null ? var.getPossiblePropertyValue(graph, MOD.contextualHelpId, Bindings.STRING) : null;
71                         if (id != null)
72                             return id;
73                     }
74
75                     return null;
76                 }
77             });
78         } catch (DatabaseException e) {
79             LOGGER.error("", e);
80             return null;
81         }
82     }
83
84     @Override
85     public Object execute(ExecutionEvent event) throws ExecutionException {
86         String id = getPossibleId(event);
87         if (id != null)
88             PlatformUI.getWorkbench().getHelpSystem().displayHelp(id);
89         return null;
90     }
91
92 }