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.LabelDecoratorKey;
15 import org.simantics.browsing.ui.DataSource;
16 import org.simantics.browsing.ui.NodeContext;
17 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
18 import org.simantics.browsing.ui.content.LabelDecorator;
19 import org.simantics.browsing.ui.graph.impl.request.ResourceQuery;
20 import org.simantics.db.AsyncReadGraph;
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 FinalLabelDecoratorContributionImpl extends LabelDecorator.Stub {
29 final LabelDecorator FRESH = new LabelDecorator.Stub();
30 final LabelDecorator PENDING = new LabelDecorator.Stub();
31 final LabelDecorator NO_DECORATION = new LabelDecorator.Stub();
33 protected final PrimitiveQueryUpdater updater;
34 private final ResourceQuery<LabelDecorator> labelQuery;
35 private final LabelDecoratorKey key;
38 * The current decorator result produced for this contribution. The value
39 * should never be <code>null</code>, but one of {@link #FRESH},
40 * {@link #PENDING}, {@link #NO_DECORATION} instead.
42 protected LabelDecorator decorator = FRESH;
44 final protected NodeContext context;
46 public Object getIdentity(LabelDecoratorKey key) {
50 private void request() {
52 final DataSource<AsyncReadGraph> source = updater.getDataSource(AsyncReadGraph.class);
53 assert(source != null);
55 final Procedure<LabelDecorator> procedure = createProcedure();
57 source.schedule(graph -> {
58 if(procedure instanceof Listener<?>)
59 graph.asyncRequest(labelQuery, (Listener<LabelDecorator>)procedure);
61 graph.asyncRequest(labelQuery, procedure);
69 public <Color_> Color_ decorateBackground(Color_ color, String column, int itemIndex) {
70 if(FRESH == decorator) {
74 return decorator.decorateBackground(color, column, itemIndex);
78 public <Font_> Font_ decorateFont(Font_ font, String column, int itemIndex) {
79 if(FRESH == decorator) {
83 return decorator.decorateFont(font, column, itemIndex);
87 public String decorateLabel(String label, String column, int itemIndex) {
88 if(FRESH == decorator) {
92 return decorator.decorateLabel(label, column, itemIndex);
96 public <Color_> Color_ decorateForeground(Color_ color, String column, int itemIndex) {
97 if(FRESH == decorator) {
101 return decorator.decorateForeground(color, column, itemIndex);
104 public FinalLabelDecoratorContributionImpl(final PrimitiveQueryUpdater updater, final NodeContext context, final LabelDecoratorKey key) {
106 this.updater = updater;
107 this.context = context;
110 labelQuery = new ResourceQuery<LabelDecorator>(getIdentity(key), context) {
113 public LabelDecorator perform(ReadGraph graph) throws DatabaseException {
115 return getDecorator(graph, context);
116 } catch (DatabaseException e) {
118 } catch (Throwable t) {
119 ErrorLogger.defaultLogError("LazyGraphLabeler.labelQuery produced unexpected exception.", t);
125 public String toString() {
126 return FinalLabelDecoratorContributionImpl.this + " with context " + context;
133 protected Procedure<LabelDecorator> createProcedure() {
135 return new Procedure<LabelDecorator>() {
138 public void execute(LabelDecorator result) {
139 replaceResult(result);
143 public void exception(Throwable t) {
144 ErrorLogger.defaultLogError("LazyContributionImpl.childQuery failed, see exception for details.", t);
151 protected void replaceResult(LabelDecorator result) {
152 // Never let decorator become null, use a stub decorator instead.
154 result = NO_DECORATION;
157 updater.scheduleReplace(context, key, this);
162 public abstract LabelDecorator getDecorator(ReadGraph graph, NodeContext context) throws DatabaseException;