]> 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
3459990009ede231ba7259c4d8535638dd363a5c
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / contribution / FinalLabelDecoratorContributionImpl.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 org.simantics.browsing.ui.BuiltinKeys.LabelDecoratorKey;
15 import org.simantics.browsing.ui.DataSource;
16 import org.simantics.browsing.ui.NodeContext;
17 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
18 import org.simantics.browsing.ui.content.LabelDecorator;
19 import org.simantics.browsing.ui.graph.impl.request.ResourceQuery;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.procedure.Listener;
23 import org.simantics.db.procedure.Procedure;
24 import org.simantics.utils.ui.ErrorLogger;
25
26 public abstract class FinalLabelDecoratorContributionImpl extends LabelDecorator.Stub {
27
28     final LabelDecorator FRESH = new LabelDecorator.Stub();
29     final LabelDecorator PENDING = new LabelDecorator.Stub();
30     final LabelDecorator NO_DECORATION = new LabelDecorator.Stub();
31
32     protected final PrimitiveQueryUpdater       updater;
33     private final ResourceQuery<LabelDecorator> labelQuery;
34     private final LabelDecoratorKey             key;
35
36     /**
37      * The current decorator result produced for this contribution. The value
38      * should never be <code>null</code>, but one of {@link #FRESH},
39      * {@link #PENDING}, {@link #NO_DECORATION} instead.
40      */
41     protected LabelDecorator                      decorator = FRESH;
42
43     final protected NodeContext                   context;
44
45     public Object getIdentity(LabelDecoratorKey key) {
46         return key;
47     }
48
49     private void request() {
50
51         final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
52         assert(source != null);
53         
54         final Procedure<LabelDecorator> procedure = createProcedure();
55
56         source.schedule(graph -> {
57             if(procedure instanceof Listener<?>)
58                 graph.asyncRequest(labelQuery, (Listener<LabelDecorator>)procedure);
59             else
60                 graph.asyncRequest(labelQuery, procedure);
61         });
62
63     }
64
65
66
67     @Override
68     public <Color_> Color_ decorateBackground(Color_ color, String column, int itemIndex) {
69         if(FRESH == decorator) {
70             decorator = PENDING;
71             request();
72         }
73         return decorator.decorateBackground(color, column, itemIndex);
74     }
75
76     @Override
77     public <Font_> Font_ decorateFont(Font_ font, String column, int itemIndex) {
78         if(FRESH == decorator) {
79             decorator = PENDING;
80             request();
81         }
82         return decorator.decorateFont(font, column, itemIndex);
83     }
84
85     @Override
86     public String decorateLabel(String label, String column, int itemIndex) {
87         if(FRESH == decorator) {
88             decorator = PENDING;
89             request();
90         }
91         return decorator.decorateLabel(label, column, itemIndex);
92     }
93
94     @Override
95     public <Color_> Color_ decorateForeground(Color_ color, String column, int itemIndex) {
96         if(FRESH == decorator) {
97             decorator = PENDING;
98             request();
99         }
100         return decorator.decorateForeground(color, column, itemIndex);
101     }
102
103     public FinalLabelDecoratorContributionImpl(final PrimitiveQueryUpdater updater, final NodeContext context, final LabelDecoratorKey key) {
104
105         this.updater = updater;
106         this.context = context;
107         this.key = key;
108
109         labelQuery = new ResourceQuery<LabelDecorator>(getIdentity(key), context) {
110
111             @Override
112             public LabelDecorator perform(ReadGraph graph) throws DatabaseException {
113                 try {
114                     return getDecorator(graph, context);
115                 } catch (DatabaseException e) {
116                     throw e;
117                 } catch (Throwable t) {
118                     ErrorLogger.defaultLogError("LazyGraphLabeler.labelQuery produced unexpected exception.", t);
119                     return null;
120                 }
121             }
122
123             @Override
124             public String toString() {
125                 return FinalLabelDecoratorContributionImpl.this + " with context " + context;
126             }
127
128         };
129
130     }
131
132     protected Procedure<LabelDecorator> createProcedure() {
133
134         return new Procedure<LabelDecorator>() {
135
136             @Override
137             public void execute(LabelDecorator result) {
138                 replaceResult(result);
139             }
140
141             @Override
142             public void exception(Throwable t) {
143                 ErrorLogger.defaultLogError("LazyContributionImpl.childQuery failed, see exception for details.", t);
144             }
145
146         };
147
148     }
149
150     protected void replaceResult(LabelDecorator result) {
151         // Never let decorator become null, use a stub decorator instead.
152         if (result == null)
153             result = NO_DECORATION;
154
155         decorator = result;
156         updater.scheduleReplace(context, key, this);
157     }
158
159     // OVERRIDE
160
161     public abstract LabelDecorator getDecorator(ReadGraph graph, NodeContext context) throws DatabaseException;
162
163 }