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 java.util.Collections;
17 import org.simantics.Simantics;
18 import org.simantics.browsing.ui.BuiltinKeys;
19 import org.simantics.browsing.ui.BuiltinKeys.LabelerKey;
20 import org.simantics.browsing.ui.DataSource;
21 import org.simantics.browsing.ui.GraphExplorer.ModificationContext;
22 import org.simantics.browsing.ui.NodeContext;
23 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
24 import org.simantics.browsing.ui.common.labelers.LabelerContent;
25 import org.simantics.browsing.ui.common.labelers.LabelerStub;
26 import org.simantics.browsing.ui.common.node.IModifiableNode;
27 import org.simantics.browsing.ui.graph.impl.request.ResourceQuery;
28 import org.simantics.db.AsyncReadGraph;
29 import org.simantics.db.ReadGraph;
30 import org.simantics.db.UndoContext;
31 import org.simantics.db.common.utils.Logger;
32 import org.simantics.db.exception.DatabaseException;
33 import org.simantics.db.layer0.exception.InvalidVariableException;
34 import org.simantics.db.layer0.exception.PendingVariableException;
35 import org.simantics.db.procedure.Listener;
36 import org.simantics.db.procedure.Procedure;
37 import org.simantics.db.request.Read;
38 import org.simantics.utils.ui.ErrorLogger;
40 public abstract class FinalLabelerContributionImpl extends LabelerStub {
42 protected final PrimitiveQueryUpdater updater;
43 private final ResourceQuery<LabelerContent> labelQuery;
45 final protected NodeContext context;
46 final private BuiltinKeys.LabelerKey key;
48 public Object getIdentity(LabelerKey key) {
52 public FinalLabelerContributionImpl(final PrimitiveQueryUpdater updater, final NodeContext context, final LabelerKey key) {
54 this.updater = updater;
55 this.context = context;
58 labelQuery = new ResourceQuery<LabelerContent>(getIdentity(key), context) {
61 public LabelerContent perform(ReadGraph graph) throws DatabaseException {
63 int cat = category(graph, context);
64 Map<String, String> lbls = labels(graph, context);
65 // Make sure that null is not returned.
67 throw new NullPointerException("FinalLabelerContributionImpl.labels is not allowed to return null, but " + FinalLabelerContributionImpl.this.getClass() + " just did it");
68 return new LabelerContent(cat, lbls);
69 } catch (PendingVariableException e) {
70 return LabelerContent.NO_CONTENT;
71 } catch (InvalidVariableException e) {
72 //ErrorLogger.defaultLogError("FinalLabelerContributionImpl.labelQuery failed due to invalid variable.", e);
73 return LabelerContent.NO_CONTENT;
74 } catch (DatabaseException e) {
76 } catch (Throwable t) {
77 ErrorLogger.defaultLogError("FinalLabelerContributionImpl.labelQuery produced unexpected exception.", t);
78 return LabelerContent.NO_CONTENT;
83 public String toString() {
84 return FinalLabelerContributionImpl.this + " with context " + context;
91 protected Procedure<LabelerContent> createProcedure() {
93 return new Procedure<LabelerContent>() {
96 public void execute(LabelerContent result) {
97 replaceResult(result);
101 public void exception(Throwable t) {
102 ErrorLogger.defaultLogError("FinalLabelerContributionImpl.labelQuery failed, see exception for details.", t);
109 protected void replaceResult(LabelerContent result) {
111 updater.scheduleReplace(context, key, this);
115 public Map<String, String> getLabels() {
117 if (content == LabelerContent.NO_CONTENT) {
119 final DataSource<AsyncReadGraph> source = updater.getDataSource(AsyncReadGraph.class);
120 assert(source != null);
122 final Procedure<LabelerContent> procedure = createProcedure();
124 source.schedule(graph -> {
125 if(procedure instanceof Listener<?>)
126 graph.asyncRequest(labelQuery, (Listener<LabelerContent>)procedure);
128 graph.asyncRequest(labelQuery, procedure);
133 if(content == null) return Collections.emptyMap();
135 return content.labels;
140 public Modifier getModifier(final ModificationContext modificationContext, final String columnKey) {
142 Object obj = context.getConstant(BuiltinKeys.INPUT);
143 if (obj instanceof IModifiableNode) {
144 return ((IModifiableNode) obj).getModifier(columnKey);
147 return Simantics.getSession().syncRequest(new Read<Modifier>() {
150 public Modifier perform(ReadGraph graph) throws DatabaseException {
151 UndoContext undoContext = null == modificationContext ? null :
152 (UndoContext)modificationContext.getAdapter(UndoContext.class);
153 return FinalLabelerContributionImpl.this.getModifier(graph, undoContext, context, columnKey);
157 } catch (DatabaseException e) {
158 Logger.defaultLogError(e);
166 public Modifier getModifier(ReadGraph graph, UndoContext undoContext, NodeContext context, String columnKey) throws DatabaseException {
170 public abstract Map<String, String> labels(ReadGraph graph, NodeContext context) throws DatabaseException;
172 public abstract int category(ReadGraph graph, NodeContext context) throws DatabaseException;