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