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