]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/ContextualHelp.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / modelBrowser / handlers / ContextualHelp.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 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  *******************************************************************************/
12 package org.simantics.modeling.ui.modelBrowser.handlers;
13
14 import org.eclipse.core.commands.AbstractHandler;
15 import org.eclipse.core.commands.ExecutionEvent;
16 import org.eclipse.core.commands.ExecutionException;
17 import org.eclipse.jface.viewers.ISelection;
18 import org.eclipse.ui.PlatformUI;
19 import org.eclipse.ui.handlers.HandlerUtil;
20 import org.simantics.Simantics;
21 import org.simantics.databoard.Bindings;
22 import org.simantics.db.ReadGraph;
23 import org.simantics.db.Resource;
24 import org.simantics.db.common.request.ResourceRead;
25 import org.simantics.db.common.utils.Logger;
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
33 public class ContextualHelp extends AbstractHandler {
34
35     private static String getPossibleId(ExecutionEvent event) {
36         String id = null;
37         try {
38             Resource element = WorkbenchSelectionUtils.getPossibleResource(event);
39             id = Simantics.getSession().syncRequest(new ResourceRead<String>(element) {
40
41                 @Override
42                 public String perform(ReadGraph graph) throws DatabaseException {
43                     ModelingResources MOD = ModelingResources.getInstance(graph);
44                     Resource component = graph.getPossibleObject(element, MOD.ElementToComponent);
45                     if (component != null)
46                         return graph.getPossibleRelatedValue2(component, MOD.contextualHelpId, Bindings.STRING);
47
48                     Variable var = WorkbenchSelectionUtils.getPossibleVariable(event);
49                     if (var != null)
50                         return var.getPossiblePropertyValue(graph, MOD.contextualHelpId, Bindings.STRING);
51
52                     ISelection sel = HandlerUtil.getCurrentSelection(event);
53                     if (sel != null) {
54                         PropertyVariables vars = AdaptionUtils.adaptToSingle(sel, PropertyVariables.class);
55                         if (vars != null) {
56                             var = vars.getConfiguration();
57                             if (var != null)
58                                 var.getPossiblePropertyValue(graph, MOD.contextualHelpId, Bindings.STRING);
59                         }
60                     }
61                     return null;
62                 }
63             });
64         } catch (DatabaseException e) {
65             Logger.defaultLogError(e);
66         }
67         return id;
68     }
69
70     @Override
71     public Object execute(ExecutionEvent event) throws ExecutionException {
72
73         String id = getPossibleId(event);
74         if (id != null)
75             PlatformUI.getWorkbench().getHelpSystem().displayHelp(id);
76
77         return null;
78
79     }
80
81 }