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.ReadGraph;
33 import org.simantics.db.UndoContext;
34 import org.simantics.db.common.utils.Logger;
35 import org.simantics.db.exception.DatabaseException;
36 import org.simantics.db.layer0.exception.InvalidVariableException;
37 import org.simantics.db.layer0.exception.PendingVariableException;
38 import org.simantics.db.procedure.Listener;
39 import org.simantics.db.procedure.Procedure;
40 import org.simantics.db.request.Read;
41 import org.simantics.utils.ui.ErrorLogger;
43 public abstract class FinalLabelerContributionImpl extends LabelerStub {
45 protected final PrimitiveQueryUpdater updater;
46 private final ResourceQuery<LabelerContent> labelQuery;
48 final protected NodeContext context;
49 final private BuiltinKeys.LabelerKey key;
51 public Object getIdentity(LabelerKey key) {
55 public FinalLabelerContributionImpl(final PrimitiveQueryUpdater updater, final NodeContext context, final LabelerKey key) {
57 this.updater = updater;
58 this.context = context;
61 labelQuery = new ResourceQuery<LabelerContent>(getIdentity(key), context) {
64 public LabelerContent perform(ReadGraph graph) throws DatabaseException {
66 int cat = category(graph, context);
67 Map<String, String> lbls = labels(graph, context);
68 // Make sure that null is not returned.
70 throw new NullPointerException("FinalLabelerContributionImpl.labels is not allowed to return null, but " + FinalLabelerContributionImpl.this.getClass() + " just did it");
71 return new LabelerContent(cat, lbls);
72 } catch (PendingVariableException e) {
73 return LabelerContent.NO_CONTENT;
74 } catch (InvalidVariableException e) {
75 //ErrorLogger.defaultLogError("FinalLabelerContributionImpl.labelQuery failed due to invalid variable.", e);
76 return LabelerContent.NO_CONTENT;
77 } catch (DatabaseException e) {
79 } catch (Throwable t) {
80 ErrorLogger.defaultLogError("FinalLabelerContributionImpl.labelQuery produced unexpected exception.", t);
81 return LabelerContent.NO_CONTENT;
86 public String toString() {
87 return FinalLabelerContributionImpl.this + " with context " + context;
94 protected Procedure<LabelerContent> createProcedure() {
96 return new Procedure<LabelerContent>() {
99 public void execute(LabelerContent result) {
100 replaceResult(result);
104 public void exception(Throwable t) {
105 ErrorLogger.defaultLogError("FinalLabelerContributionImpl.labelQuery failed, see exception for details.", t);
112 protected void replaceResult(LabelerContent result) {
114 updater.scheduleReplace(context, key, this);
118 public Map<String, String> getLabels() {
120 if (content == LabelerContent.NO_CONTENT) {
122 final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
123 assert(source != null);
125 final Procedure<LabelerContent> procedure = createProcedure();
127 source.schedule(graph -> {
128 if(procedure instanceof Listener<?>)
129 graph.asyncRequest(labelQuery, (Listener<LabelerContent>)procedure);
131 graph.asyncRequest(labelQuery, procedure);
136 if(content == null) return Collections.emptyMap();
138 return content.labels;
143 public Modifier getModifier(final ModificationContext modificationContext, final String columnKey) {
145 Object obj = context.getConstant(BuiltinKeys.INPUT);
146 if (obj instanceof IModifiableNode) {
147 return ((IModifiableNode) obj).getModifier(columnKey);
150 return Simantics.getSession().syncRequest(new Read<Modifier>() {
153 public Modifier perform(ReadGraph graph) throws DatabaseException {
154 UndoContext undoContext = null == modificationContext ? null :
155 (UndoContext)modificationContext.getAdapter(UndoContext.class);
156 return FinalLabelerContributionImpl.this.getModifier(graph, undoContext, context, columnKey);
160 } catch (DatabaseException e) {
161 Logger.defaultLogError(e);
168 public boolean shouldCreateToolTip(Event event, NodeContext nodeContext) {
169 return createToolTip(event, nodeContext);
173 public Composite createToolTipContentArea(Event event, Composite parent, NodeContext nodeContext) {
174 return (Composite)createToolTipContent(event, parent, nodeContext);
179 public Modifier getModifier(ReadGraph graph, UndoContext undoContext, NodeContext context, String columnKey) throws DatabaseException {
183 public abstract Map<String, String> labels(ReadGraph graph, NodeContext context) throws DatabaseException;
185 public abstract int category(ReadGraph graph, NodeContext context) throws DatabaseException;
187 public boolean createToolTip(Object event, NodeContext nodeContext) {
191 public Object createToolTipContent(Object event, Object parent, NodeContext nodeContext) {