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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.browsing.ui.graph.impl.contribution;
\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
27 public abstract class FinalCheckedStateContributionImpl {
\r
29 protected final PrimitiveQueryUpdater updater;
\r
30 private final ResourceQuery<CheckedState> labelQuery;
\r
31 private final Procedure<CheckedState> procedure;
\r
33 final private NodeContext context;
\r
34 final private BuiltinKeys.CheckedStateKey key;
\r
36 private CheckedState state = null;
\r
38 public Object getIdentity(CheckedStateKey key) {
\r
42 public CheckedState getState() {
\r
44 if (state == null) {
\r
46 final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
\r
47 assert(source != null);
\r
49 source.schedule(graph -> {
\r
50 if(procedure instanceof Listener<?>)
\r
51 graph.asyncRequest(labelQuery, (Listener<CheckedState>)procedure);
\r
53 graph.asyncRequest(labelQuery, procedure);
\r
62 public FinalCheckedStateContributionImpl(final PrimitiveQueryUpdater updater, final NodeContext context, final CheckedStateKey key) {
\r
64 this.updater = updater;
\r
65 this.context = context;
\r
68 labelQuery = new ResourceQuery<CheckedState>(getIdentity(key), context) {
\r
71 public CheckedState perform(ReadGraph graph) throws DatabaseException {
\r
73 return getState(graph, context);
\r
74 } catch (DatabaseException e) {
\r
76 } catch (Throwable t) {
\r
77 ErrorLogger.defaultLogError("LazyGraphLabeler.labelQuery produced unexpected exception.", t);
\r
83 public String toString() {
\r
84 return FinalCheckedStateContributionImpl.this + " with context " + context;
\r
89 procedure = createProcedure();
\r
93 protected Procedure<CheckedState> createProcedure() {
\r
95 return new Procedure<CheckedState>() {
\r
98 public void execute(CheckedState result) {
\r
99 replaceResult(result);
\r
103 public void exception(Throwable t) {
\r
104 ErrorLogger.defaultLogError("LazyContributionImpl.childQuery failed, see exception for details.", t);
\r
111 protected void replaceResult(CheckedState result) {
\r
113 updater.scheduleReplace(context, key, state);
\r
116 abstract public CheckedState getState(ReadGraph graph, NodeContext context) throws DatabaseException;
\r