]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/contribution/FinalLabelerContributionImpl.java
Allow column specific tooltips in the GraphExplorer
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / contribution / FinalLabelerContributionImpl.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 - gitlab #146 - tooltip support
12  *******************************************************************************/
13 package org.simantics.browsing.ui.graph.impl.contribution;
14
15 import java.util.Collections;
16 import java.util.Map;
17
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;
42
43 public abstract class FinalLabelerContributionImpl extends LabelerStub {
44
45     protected final PrimitiveQueryUpdater       updater;
46     private final ResourceQuery<LabelerContent> labelQuery;
47
48     final protected NodeContext                context;
49     final private BuiltinKeys.LabelerKey    key;
50
51     public Object getIdentity(LabelerKey key) {
52         return key;
53     }
54
55     public FinalLabelerContributionImpl(final PrimitiveQueryUpdater updater, final NodeContext context, final LabelerKey key) {
56
57         this.updater = updater;
58         this.context = context;
59         this.key = key;
60
61         labelQuery = new ResourceQuery<LabelerContent>(getIdentity(key), context) {
62
63             @Override
64             public LabelerContent perform(ReadGraph graph) throws DatabaseException {
65                 try {
66                     int cat = category(graph, context);
67                     Map<String, String> lbls = labels(graph, context);
68                     // Make sure that null is not returned.
69                     if (lbls == null)
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) {
78                     throw e;
79                 } catch (Throwable t) {
80                     ErrorLogger.defaultLogError("FinalLabelerContributionImpl.labelQuery produced unexpected exception.", t);
81                     return LabelerContent.NO_CONTENT;
82                 }
83             }
84
85             @Override
86             public String toString() {
87                 return FinalLabelerContributionImpl.this + " with context " + context;
88             }
89
90         };
91
92     }
93
94     protected Procedure<LabelerContent> createProcedure() {
95
96         return new Procedure<LabelerContent>() {
97
98             @Override
99             public void execute(LabelerContent result) {
100                 replaceResult(result);
101             }
102
103             @Override
104             public void exception(Throwable t) {
105                 ErrorLogger.defaultLogError("FinalLabelerContributionImpl.labelQuery failed, see exception for details.", t);
106             }
107
108         };
109
110     }
111
112     protected void replaceResult(LabelerContent result) {
113         setContent(result);
114         updater.scheduleReplace(context, key, this);
115     }
116
117     @Override
118     public Map<String, String> getLabels() {
119
120         if (content == LabelerContent.NO_CONTENT) {
121
122             final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
123             assert(source != null);
124
125             final Procedure<LabelerContent> procedure = createProcedure();
126             
127             source.schedule(graph -> {
128                 if(procedure instanceof Listener<?>)
129                     graph.asyncRequest(labelQuery, (Listener<LabelerContent>)procedure);
130                 else
131                     graph.asyncRequest(labelQuery, procedure);
132             });
133
134         }
135
136         if(content == null) return Collections.emptyMap();
137
138         return content.labels;
139
140     }
141
142     @Override
143     public Modifier getModifier(final ModificationContext modificationContext, final String columnKey) {
144         
145         Object obj = context.getConstant(BuiltinKeys.INPUT);
146         if (obj instanceof IModifiableNode) {
147             return ((IModifiableNode) obj).getModifier(columnKey);
148         }
149         try {
150                         return Simantics.getSession().syncRequest(new Read<Modifier>() {
151
152                                 @Override
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);
157                                 }
158                                 
159                         });
160                 } catch (DatabaseException e) {
161                         Logger.defaultLogError(e);
162                         return null;
163                 }
164         
165     }
166
167     @Override
168     public boolean shouldCreateToolTip(Event event, NodeContext nodeContext) {
169         return createToolTip(event, nodeContext);
170     }
171
172     @Override
173     public Composite createToolTipContentArea(Event event, Composite parent, NodeContext nodeContext) {
174         return (Composite)createToolTipContent(event, parent, nodeContext);
175     }
176
177     // OVERRIDE
178
179     public Modifier getModifier(ReadGraph graph, UndoContext undoContext, NodeContext context, String columnKey) throws DatabaseException {
180         return null;
181     }
182
183     public abstract Map<String, String> labels(ReadGraph graph, NodeContext context) throws DatabaseException;
184
185     public abstract int category(ReadGraph graph, NodeContext context) throws DatabaseException;
186
187     public boolean createToolTip(Object event, NodeContext nodeContext) {
188         return false;
189     }
190
191     public Object createToolTipContent(Object event, Object parent, NodeContext nodeContext) {
192         return null;
193     }
194
195 }