]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/contribution/FinalCheckedStateContributionImpl.java
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 / FinalCheckedStateContributionImpl.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;
15 import org.simantics.browsing.ui.BuiltinKeys.CheckedStateKey;
16 import org.simantics.browsing.ui.CheckedState;
17 import org.simantics.browsing.ui.DataSource;
18 import org.simantics.browsing.ui.NodeContext;
19 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
20 import org.simantics.browsing.ui.graph.impl.request.ResourceQuery;
21 import org.simantics.db.AsyncReadGraph;
22 import org.simantics.db.ReadGraph;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.procedure.Listener;
25 import org.simantics.db.procedure.Procedure;
26 import org.simantics.utils.ui.ErrorLogger;
27
28 public abstract class FinalCheckedStateContributionImpl {
29
30     protected final PrimitiveQueryUpdater       updater;
31     private final ResourceQuery<CheckedState> labelQuery;
32     private final Procedure<CheckedState>      procedure;
33
34     final private NodeContext                context;
35     final private BuiltinKeys.CheckedStateKey    key;
36     
37     private CheckedState state = null;
38
39     public Object getIdentity(CheckedStateKey key) {
40         return key;
41     }
42
43     public CheckedState getState() {
44         
45         if (state == null) {
46
47             final DataSource<AsyncReadGraph> source = updater.getDataSource(AsyncReadGraph.class);
48             assert(source != null);
49
50             source.schedule(graph -> {
51                 if(procedure instanceof Listener<?>)
52                     graph.asyncRequest(labelQuery, (Listener<CheckedState>)procedure);
53                 else
54                     graph.asyncRequest(labelQuery, procedure);
55             });
56                 
57         }
58         
59         return state;
60         
61     }
62     
63     public FinalCheckedStateContributionImpl(final PrimitiveQueryUpdater updater, final NodeContext context, final CheckedStateKey key) {
64
65         this.updater = updater;
66         this.context = context;
67         this.key = key;
68
69         labelQuery = new ResourceQuery<CheckedState>(getIdentity(key), context) {
70
71             @Override
72             public CheckedState perform(ReadGraph graph) throws DatabaseException {
73                 try {
74                         return getState(graph, context);
75                 } catch (DatabaseException e) {
76                     throw e;
77                 } catch (Throwable t) {
78                     ErrorLogger.defaultLogError("LazyGraphLabeler.labelQuery produced unexpected exception.", t);
79                     return null;
80                 }
81             }
82
83             @Override
84             public String toString() {
85                 return FinalCheckedStateContributionImpl.this + " with context " + context;
86             }
87
88         };
89
90         procedure = createProcedure();
91
92     }
93
94     protected Procedure<CheckedState> createProcedure() {
95
96         return new Procedure<CheckedState>() {
97
98             @Override
99             public void execute(CheckedState result) {
100                 replaceResult(result);
101             }
102
103             @Override
104             public void exception(Throwable t) {
105                 ErrorLogger.defaultLogError("LazyContributionImpl.childQuery failed, see exception for details.", t);
106             }
107
108         };
109
110     }
111
112     protected void replaceResult(CheckedState result) {
113         state = result;
114         updater.scheduleReplace(context, key, state);
115     }
116
117     abstract public CheckedState getState(ReadGraph graph, NodeContext context) throws DatabaseException;
118     
119
120 }