Add utility class org.simantics.modeling.help.HelpContexts
[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.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.help.HelpContexts;
29 import org.simantics.ui.selection.WorkbenchSelectionUtils;
30 import org.slf4j.LoggerFactory;
31
32 public class ContextualHelp extends AbstractHandler {
33
34     private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ContextualHelp.class);
35
36     private static String getPossibleId(ExecutionEvent event) {
37         try {
38             ISelection sel = HandlerUtil.getCurrentSelection(event);
39             Resource resource = WorkbenchSelectionUtils.getPossibleResource(sel);
40             Variable variable = WorkbenchSelectionUtils.getPossibleVariable(sel);
41             if (sel.isEmpty() && resource == null && variable == null)
42                 return null;
43
44             return Simantics.getSession().syncRequest(new UniqueRead<String>() {
45                 @Override
46                 public String perform(ReadGraph graph) throws DatabaseException {
47                     return HelpContexts.getPossibleId(graph, resource, variable, sel);
48                 }
49             });
50         } catch (DatabaseException e) {
51             LOGGER.error("", e);
52             return null;
53         }
54     }
55
56     @Override
57     public Object execute(ExecutionEvent event) throws ExecutionException {
58         String id = getPossibleId(event);
59         if (id != null)
60             PlatformUI.getWorkbench().getHelpSystem().displayHelp(id);
61         return null;
62     }
63
64     /**
65      * @deprecated use {@link HelpContexts#getPossibleId(ReadGraph, Resource, Variable, Object)} instead
66      */
67     @Deprecated
68     public static String getPossibleId(ReadGraph graph, Resource resource, Variable variable, ISelection sel) throws DatabaseException {
69         return HelpContexts.getPossibleId(graph, resource, variable, sel);
70     }
71
72 }