Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / actions / Help.java
1 /*******************************************************************************
2  * Copyright (c) 2016 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.actions;
13
14 import org.eclipse.ui.PlatformUI;
15 import org.simantics.Simantics;
16 import org.simantics.databoard.Bindings;
17 import org.simantics.db.Resource;
18 import org.simantics.db.common.primitiverequest.PossibleRelatedValue2;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.layer0.adapter.ActionFactory;
21 import org.simantics.modeling.ModelingResources;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public class Help implements ActionFactory {
26
27         private static final Logger LOGGER = LoggerFactory.getLogger(Help.class);
28
29         @Override
30         public Runnable create(Object target) {
31                 if (!(target instanceof Resource))
32                         return null;
33
34                 final Resource resource = (Resource) target;
35
36                 return () -> {
37                         try {
38                                 ModelingResources MOD = ModelingResources.getInstance(Simantics.getSession());
39                                 String id = Simantics.sync(new PossibleRelatedValue2<String>(resource, MOD.contextualHelpId, Bindings.STRING));
40                                 if (id == null) {
41                                         PlatformUI.getWorkbench().getHelpSystem().displayDynamicHelp();
42                                         return;
43                                 }
44                                 PlatformUI.getWorkbench().getHelpSystem().displayHelp(id);
45                         } catch (DatabaseException e) {
46                                 LOGGER.error("Failed to display help for resource {}", resource, e);
47                         }
48                 }; 
49         }
50
51 }