]> 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
Fixed all line endings of the repository
[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, 2010 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  *******************************************************************************/
12 package org.simantics.browsing.ui.graph.impl.contribution;
13
14 import java.util.Collections;
15 import java.util.Map;
16
17 import org.simantics.Simantics;
18 import org.simantics.browsing.ui.BuiltinKeys;
19 import org.simantics.browsing.ui.BuiltinKeys.LabelerKey;
20 import org.simantics.browsing.ui.DataSource;
21 import org.simantics.browsing.ui.GraphExplorer.ModificationContext;
22 import org.simantics.browsing.ui.NodeContext;
23 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
24 import org.simantics.browsing.ui.common.labelers.LabelerContent;
25 import org.simantics.browsing.ui.common.labelers.LabelerStub;
26 import org.simantics.browsing.ui.common.node.IModifiableNode;
27 import org.simantics.browsing.ui.graph.impl.request.ResourceQuery;
28 import org.simantics.db.ReadGraph;
29 import org.simantics.db.UndoContext;
30 import org.simantics.db.common.utils.Logger;
31 import org.simantics.db.exception.DatabaseException;
32 import org.simantics.db.layer0.exception.InvalidVariableException;
33 import org.simantics.db.layer0.exception.PendingVariableException;
34 import org.simantics.db.procedure.Listener;
35 import org.simantics.db.procedure.Procedure;
36 import org.simantics.db.request.Read;
37 import org.simantics.utils.ui.ErrorLogger;
38
39 public abstract class FinalLabelerContributionImpl extends LabelerStub {
40
41     protected final PrimitiveQueryUpdater       updater;
42     private final ResourceQuery<LabelerContent> labelQuery;
43
44     final protected NodeContext                context;
45     final private BuiltinKeys.LabelerKey    key;
46
47     public Object getIdentity(LabelerKey key) {
48         return key;
49     }
50
51     public FinalLabelerContributionImpl(final PrimitiveQueryUpdater updater, final NodeContext context, final LabelerKey key) {
52
53         this.updater = updater;
54         this.context = context;
55         this.key = key;
56
57         labelQuery = new ResourceQuery<LabelerContent>(getIdentity(key), context) {
58
59             @Override
60             public LabelerContent perform(ReadGraph graph) throws DatabaseException {
61                 try {
62                     int cat = category(graph, context);
63                     Map<String, String> lbls = labels(graph, context);
64                     // Make sure that null is not returned.
65                     if (lbls == null)
66                         throw new NullPointerException("FinalLabelerContributionImpl.labels is not allowed to return null, but " + FinalLabelerContributionImpl.this.getClass() + " just did it");
67                     return new LabelerContent(cat, lbls);
68                 } catch (PendingVariableException e) {
69                     return LabelerContent.NO_CONTENT;
70                 } catch (InvalidVariableException e) {
71                     //ErrorLogger.defaultLogError("FinalLabelerContributionImpl.labelQuery failed due to invalid variable.", e);
72                     return LabelerContent.NO_CONTENT;
73                 } catch (DatabaseException e) {
74                     throw e;
75                 } catch (Throwable t) {
76                     ErrorLogger.defaultLogError("FinalLabelerContributionImpl.labelQuery produced unexpected exception.", t);
77                     return LabelerContent.NO_CONTENT;
78                 }
79             }
80
81             @Override
82             public String toString() {
83                 return FinalLabelerContributionImpl.this + " with context " + context;
84             }
85
86         };
87
88     }
89
90     protected Procedure<LabelerContent> createProcedure() {
91
92         return new Procedure<LabelerContent>() {
93
94             @Override
95             public void execute(LabelerContent result) {
96                 replaceResult(result);
97             }
98
99             @Override
100             public void exception(Throwable t) {
101                 ErrorLogger.defaultLogError("FinalLabelerContributionImpl.labelQuery failed, see exception for details.", t);
102             }
103
104         };
105
106     }
107
108     protected void replaceResult(LabelerContent result) {
109         setContent(result);
110         updater.scheduleReplace(context, key, this);
111     }
112
113     @Override
114     public Map<String, String> getLabels() {
115
116         if (content == LabelerContent.NO_CONTENT) {
117
118             final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
119             assert(source != null);
120
121             final Procedure<LabelerContent> procedure = createProcedure();
122             
123             source.schedule(graph -> {
124                 if(procedure instanceof Listener<?>)
125                     graph.asyncRequest(labelQuery, (Listener<LabelerContent>)procedure);
126                 else
127                     graph.asyncRequest(labelQuery, procedure);
128             });
129
130         }
131
132         if(content == null) return Collections.emptyMap();
133
134         return content.labels;
135
136     }
137
138     @Override
139     public Modifier getModifier(final ModificationContext modificationContext, final String columnKey) {
140         
141         Object obj = context.getConstant(BuiltinKeys.INPUT);
142         if (obj instanceof IModifiableNode) {
143             return ((IModifiableNode) obj).getModifier(columnKey);
144         }
145         try {
146                         return Simantics.getSession().syncRequest(new Read<Modifier>() {
147
148                                 @Override
149                                 public Modifier perform(ReadGraph graph) throws DatabaseException {
150                                     UndoContext undoContext = null == modificationContext ? null :
151                                         (UndoContext)modificationContext.getAdapter(UndoContext.class);
152                                         return FinalLabelerContributionImpl.this.getModifier(graph, undoContext, context, columnKey);
153                                 }
154                                 
155                         });
156                 } catch (DatabaseException e) {
157                         Logger.defaultLogError(e);
158                         return null;
159                 }
160         
161     }
162
163     // OVERRIDE
164
165     public Modifier getModifier(ReadGraph graph, UndoContext undoContext, NodeContext context, String columnKey) throws DatabaseException {
166         return null;
167     }
168
169     public abstract Map<String, String> labels(ReadGraph graph, NodeContext context) throws DatabaseException;
170
171     public abstract int category(ReadGraph graph, NodeContext context) throws DatabaseException;
172
173 }