]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/contribution/FinalLabelDecoratorContributionImpl.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / contribution / FinalLabelDecoratorContributionImpl.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.browsing.ui.graph.impl.contribution;\r
13 \r
14 import org.simantics.browsing.ui.BuiltinKeys.LabelDecoratorKey;\r
15 import org.simantics.browsing.ui.DataSource;\r
16 import org.simantics.browsing.ui.NodeContext;\r
17 import org.simantics.browsing.ui.PrimitiveQueryUpdater;\r
18 import org.simantics.browsing.ui.content.LabelDecorator;\r
19 import org.simantics.browsing.ui.graph.impl.request.ResourceQuery;\r
20 import org.simantics.db.ReadGraph;\r
21 import org.simantics.db.exception.DatabaseException;\r
22 import org.simantics.db.procedure.Listener;\r
23 import org.simantics.db.procedure.Procedure;\r
24 import org.simantics.utils.ui.ErrorLogger;\r
25 \r
26 public abstract class FinalLabelDecoratorContributionImpl extends LabelDecorator.Stub {\r
27 \r
28     final LabelDecorator FRESH = new LabelDecorator.Stub();\r
29     final LabelDecorator PENDING = new LabelDecorator.Stub();\r
30     final LabelDecorator NO_DECORATION = new LabelDecorator.Stub();\r
31 \r
32     protected final PrimitiveQueryUpdater       updater;\r
33     private final ResourceQuery<LabelDecorator> labelQuery;\r
34     private final LabelDecoratorKey             key;\r
35 \r
36     /**\r
37      * The current decorator result produced for this contribution. The value\r
38      * should never be <code>null</code>, but one of {@link #FRESH},\r
39      * {@link #PENDING}, {@link #NO_DECORATION} instead.\r
40      */\r
41     protected LabelDecorator                      decorator = FRESH;\r
42 \r
43     final protected NodeContext                   context;\r
44 \r
45     public Object getIdentity(LabelDecoratorKey key) {\r
46         return key;\r
47     }\r
48 \r
49     private void request() {\r
50 \r
51         final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);\r
52         assert(source != null);\r
53         \r
54         final Procedure<LabelDecorator> procedure = createProcedure();\r
55 \r
56         source.schedule(graph -> {\r
57             if(procedure instanceof Listener<?>)\r
58                 graph.asyncRequest(labelQuery, (Listener<LabelDecorator>)procedure);\r
59             else\r
60                 graph.asyncRequest(labelQuery, procedure);\r
61         });\r
62 \r
63     }\r
64 \r
65 \r
66 \r
67     @Override\r
68     public <Color_> Color_ decorateBackground(Color_ color, String column, int itemIndex) {\r
69         if(FRESH == decorator) {\r
70             decorator = PENDING;\r
71             request();\r
72         }\r
73         return decorator.decorateBackground(color, column, itemIndex);\r
74     }\r
75 \r
76     @Override\r
77     public <Font_> Font_ decorateFont(Font_ font, String column, int itemIndex) {\r
78         if(FRESH == decorator) {\r
79             decorator = PENDING;\r
80             request();\r
81         }\r
82         return decorator.decorateFont(font, column, itemIndex);\r
83     }\r
84 \r
85     @Override\r
86     public String decorateLabel(String label, String column, int itemIndex) {\r
87         if(FRESH == decorator) {\r
88             decorator = PENDING;\r
89             request();\r
90         }\r
91         return decorator.decorateLabel(label, column, itemIndex);\r
92     }\r
93 \r
94     @Override\r
95     public <Color_> Color_ decorateForeground(Color_ color, String column, int itemIndex) {\r
96         if(FRESH == decorator) {\r
97             decorator = PENDING;\r
98             request();\r
99         }\r
100         return decorator.decorateForeground(color, column, itemIndex);\r
101     }\r
102 \r
103     public FinalLabelDecoratorContributionImpl(final PrimitiveQueryUpdater updater, final NodeContext context, final LabelDecoratorKey key) {\r
104 \r
105         this.updater = updater;\r
106         this.context = context;\r
107         this.key = key;\r
108 \r
109         labelQuery = new ResourceQuery<LabelDecorator>(getIdentity(key), context) {\r
110 \r
111             @Override\r
112             public LabelDecorator perform(ReadGraph graph) throws DatabaseException {\r
113                 try {\r
114                     return getDecorator(graph, context);\r
115                 } catch (DatabaseException e) {\r
116                     throw e;\r
117                 } catch (Throwable t) {\r
118                     ErrorLogger.defaultLogError("LazyGraphLabeler.labelQuery produced unexpected exception.", t);\r
119                     return null;\r
120                 }\r
121             }\r
122 \r
123             @Override\r
124             public String toString() {\r
125                 return FinalLabelDecoratorContributionImpl.this + " with context " + context;\r
126             }\r
127 \r
128         };\r
129 \r
130     }\r
131 \r
132     protected Procedure<LabelDecorator> createProcedure() {\r
133 \r
134         return new Procedure<LabelDecorator>() {\r
135 \r
136             @Override\r
137             public void execute(LabelDecorator result) {\r
138                 replaceResult(result);\r
139             }\r
140 \r
141             @Override\r
142             public void exception(Throwable t) {\r
143                 ErrorLogger.defaultLogError("LazyContributionImpl.childQuery failed, see exception for details.", t);\r
144             }\r
145 \r
146         };\r
147 \r
148     }\r
149 \r
150     protected void replaceResult(LabelDecorator result) {\r
151         // Never let decorator become null, use a stub decorator instead.\r
152         if (result == null)\r
153             result = NO_DECORATION;\r
154 \r
155         decorator = result;\r
156         updater.scheduleReplace(context, key, this);\r
157     }\r
158 \r
159     // OVERRIDE\r
160 \r
161     public abstract LabelDecorator getDecorator(ReadGraph graph, NodeContext context) throws DatabaseException;\r
162 \r
163 }\r