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