1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.graph.impl.contribution;
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;
28 public abstract class FinalCheckedStateContributionImpl {
30 protected final PrimitiveQueryUpdater updater;
31 private final ResourceQuery<CheckedState> labelQuery;
32 private final Procedure<CheckedState> procedure;
34 final private NodeContext context;
35 final private BuiltinKeys.CheckedStateKey key;
37 private CheckedState state = null;
39 public Object getIdentity(CheckedStateKey key) {
43 public CheckedState getState() {
47 final DataSource<AsyncReadGraph> source = updater.getDataSource(AsyncReadGraph.class);
48 assert(source != null);
50 source.schedule(graph -> {
51 if(procedure instanceof Listener<?>)
52 graph.asyncRequest(labelQuery, (Listener<CheckedState>)procedure);
54 graph.asyncRequest(labelQuery, procedure);
63 public FinalCheckedStateContributionImpl(final PrimitiveQueryUpdater updater, final NodeContext context, final CheckedStateKey key) {
65 this.updater = updater;
66 this.context = context;
69 labelQuery = new ResourceQuery<CheckedState>(getIdentity(key), context) {
72 public CheckedState perform(ReadGraph graph) throws DatabaseException {
74 return getState(graph, context);
75 } catch (DatabaseException e) {
77 } catch (Throwable t) {
78 ErrorLogger.defaultLogError("LazyGraphLabeler.labelQuery produced unexpected exception.", t);
84 public String toString() {
85 return FinalCheckedStateContributionImpl.this + " with context " + context;
90 procedure = createProcedure();
94 protected Procedure<CheckedState> createProcedure() {
96 return new Procedure<CheckedState>() {
99 public void execute(CheckedState result) {
100 replaceResult(result);
104 public void exception(Throwable t) {
105 ErrorLogger.defaultLogError("LazyContributionImpl.childQuery failed, see exception for details.", t);
112 protected void replaceResult(CheckedState result) {
114 updater.scheduleReplace(context, key, state);
117 abstract public CheckedState getState(ReadGraph graph, NodeContext context) throws DatabaseException;