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