1 /*******************************************************************************
2 * Copyright (c) 2007, 2018 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 * Semantum Oy - gitlab #146 - tooltip support
12 *******************************************************************************/
13 package org.simantics.browsing.ui.graph.impl.contribution;
15 import java.util.Collections;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Event;
20 import org.simantics.Simantics;
21 import org.simantics.browsing.ui.BuiltinKeys;
22 import org.simantics.browsing.ui.BuiltinKeys.LabelerKey;
23 import org.simantics.browsing.ui.DataSource;
24 import org.simantics.browsing.ui.GraphExplorer.ModificationContext;
25 import org.simantics.browsing.ui.NodeContext;
26 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
27 import org.simantics.browsing.ui.common.labelers.LabelerContent;
28 import org.simantics.browsing.ui.common.labelers.LabelerStub;
29 import org.simantics.browsing.ui.common.node.IModifiableNode;
30 import org.simantics.browsing.ui.graph.impl.contributor.labeler.ColumnLabelerContributorImpl;
31 import org.simantics.browsing.ui.graph.impl.request.ResourceQuery;
32 import org.simantics.db.AsyncReadGraph;
33 import org.simantics.db.ReadGraph;
34 import org.simantics.db.UndoContext;
35 import org.simantics.db.common.utils.Logger;
36 import org.simantics.db.exception.DatabaseException;
37 import org.simantics.db.layer0.exception.InvalidVariableException;
38 import org.simantics.db.layer0.exception.PendingVariableException;
39 import org.simantics.db.procedure.Listener;
40 import org.simantics.db.procedure.Procedure;
41 import org.simantics.db.request.Read;
42 import org.simantics.utils.ui.ErrorLogger;
44 public abstract class FinalLabelerContributionImpl extends LabelerStub {
46 protected final PrimitiveQueryUpdater updater;
47 private final ResourceQuery<LabelerContent> labelQuery;
49 final protected NodeContext context;
50 final private BuiltinKeys.LabelerKey key;
52 public Object getIdentity(LabelerKey key) {
56 public FinalLabelerContributionImpl(final PrimitiveQueryUpdater updater, final NodeContext context, final LabelerKey key) {
58 this.updater = updater;
59 this.context = context;
62 labelQuery = new ResourceQuery<LabelerContent>(getIdentity(key), context) {
65 public LabelerContent perform(ReadGraph graph) throws DatabaseException {
67 int cat = category(graph, context);
68 Map<String, String> lbls = labels(graph, context);
69 // Make sure that null is not returned.
71 throw new NullPointerException("FinalLabelerContributionImpl.labels is not allowed to return null, but " + FinalLabelerContributionImpl.this.getClass() + " just did it");
72 return new LabelerContent(cat, lbls);
73 } catch (PendingVariableException e) {
74 return LabelerContent.NO_CONTENT;
75 } catch (InvalidVariableException e) {
76 //ErrorLogger.defaultLogError("FinalLabelerContributionImpl.labelQuery failed due to invalid variable.", e);
77 return LabelerContent.NO_CONTENT;
78 } catch (DatabaseException e) {
80 } catch (Throwable t) {
81 ErrorLogger.defaultLogError("FinalLabelerContributionImpl.labelQuery produced unexpected exception.", t);
82 return LabelerContent.NO_CONTENT;
87 public String toString() {
88 return FinalLabelerContributionImpl.this + " with context " + context;
95 protected Procedure<LabelerContent> createProcedure() {
97 return new Procedure<LabelerContent>() {
100 public void execute(LabelerContent result) {
101 replaceResult(result);
105 public void exception(Throwable t) {
106 ErrorLogger.defaultLogError("FinalLabelerContributionImpl.labelQuery failed, see exception for details.", t);
113 protected void replaceResult(LabelerContent result) {
115 updater.scheduleReplace(context, key, this);
119 public Map<String, String> getLabels() {
121 if (content == LabelerContent.NO_CONTENT) {
123 final DataSource<AsyncReadGraph> source = updater.getDataSource(AsyncReadGraph.class);
124 assert(source != null);
126 final Procedure<LabelerContent> procedure = createProcedure();
128 source.schedule(graph -> {
129 if(procedure instanceof Listener<?>)
130 graph.asyncRequest(labelQuery, (Listener<LabelerContent>)procedure);
132 graph.asyncRequest(labelQuery, procedure);
137 if(content == null) return Collections.emptyMap();
139 return content.labels;
144 public Modifier getModifier(final ModificationContext modificationContext, final String columnKey) {
146 Object obj = context.getConstant(BuiltinKeys.INPUT);
147 if (obj instanceof IModifiableNode) {
148 return ((IModifiableNode) obj).getModifier(columnKey);
151 return Simantics.getSession().syncRequest(new Read<Modifier>() {
154 public Modifier perform(ReadGraph graph) throws DatabaseException {
155 UndoContext undoContext = null == modificationContext ? null :
156 (UndoContext)modificationContext.getAdapter(UndoContext.class);
157 return FinalLabelerContributionImpl.this.getModifier(graph, undoContext, context, columnKey);
161 } catch (DatabaseException e) {
162 Logger.defaultLogError(e);
169 public boolean shouldCreateToolTip(Event event, NodeContext nodeContext) {
170 return createToolTip(event, nodeContext);
174 public Composite createToolTipContentArea(Event event, Composite parent, NodeContext nodeContext) {
175 return (Composite)createToolTipContent(event, parent, nodeContext);
180 public Modifier getModifier(ReadGraph graph, UndoContext undoContext, NodeContext context, String columnKey) throws DatabaseException {
184 public abstract Map<String, String> labels(ReadGraph graph, NodeContext context) throws DatabaseException;
186 public abstract int category(ReadGraph graph, NodeContext context) throws DatabaseException;
188 public boolean createToolTip(Object event, NodeContext nodeContext) {
192 public Object createToolTipContent(Object event, Object parent, NodeContext nodeContext) {