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