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.ReadGraph;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.procedure.Listener;
23 import org.simantics.db.procedure.Procedure;
24 import org.simantics.utils.ui.ErrorLogger;
26 public abstract class FinalLabelDecoratorContributionImpl extends LabelDecorator.Stub {
28 final LabelDecorator FRESH = new LabelDecorator.Stub();
29 final LabelDecorator PENDING = new LabelDecorator.Stub();
30 final LabelDecorator NO_DECORATION = new LabelDecorator.Stub();
32 protected final PrimitiveQueryUpdater updater;
33 private final ResourceQuery<LabelDecorator> labelQuery;
34 private final LabelDecoratorKey key;
37 * The current decorator result produced for this contribution. The value
38 * should never be <code>null</code>, but one of {@link #FRESH},
39 * {@link #PENDING}, {@link #NO_DECORATION} instead.
41 protected LabelDecorator decorator = FRESH;
43 final protected NodeContext context;
45 public Object getIdentity(LabelDecoratorKey key) {
49 private void request() {
51 final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
52 assert(source != null);
54 final Procedure<LabelDecorator> procedure = createProcedure();
56 source.schedule(graph -> {
57 if(procedure instanceof Listener<?>)
58 graph.asyncRequest(labelQuery, (Listener<LabelDecorator>)procedure);
60 graph.asyncRequest(labelQuery, procedure);
68 public <Color_> Color_ decorateBackground(Color_ color, String column, int itemIndex) {
69 if(FRESH == decorator) {
73 return decorator.decorateBackground(color, column, itemIndex);
77 public <Font_> Font_ decorateFont(Font_ font, String column, int itemIndex) {
78 if(FRESH == decorator) {
82 return decorator.decorateFont(font, column, itemIndex);
86 public String decorateLabel(String label, String column, int itemIndex) {
87 if(FRESH == decorator) {
91 return decorator.decorateLabel(label, column, itemIndex);
95 public <Color_> Color_ decorateForeground(Color_ color, String column, int itemIndex) {
96 if(FRESH == decorator) {
100 return decorator.decorateForeground(color, column, itemIndex);
103 public FinalLabelDecoratorContributionImpl(final PrimitiveQueryUpdater updater, final NodeContext context, final LabelDecoratorKey key) {
105 this.updater = updater;
106 this.context = context;
109 labelQuery = new ResourceQuery<LabelDecorator>(getIdentity(key), context) {
112 public LabelDecorator perform(ReadGraph graph) throws DatabaseException {
114 return getDecorator(graph, context);
115 } catch (DatabaseException e) {
117 } catch (Throwable t) {
118 ErrorLogger.defaultLogError("LazyGraphLabeler.labelQuery produced unexpected exception.", t);
124 public String toString() {
125 return FinalLabelDecoratorContributionImpl.this + " with context " + context;
132 protected Procedure<LabelDecorator> createProcedure() {
134 return new Procedure<LabelDecorator>() {
137 public void execute(LabelDecorator result) {
138 replaceResult(result);
142 public void exception(Throwable t) {
143 ErrorLogger.defaultLogError("LazyContributionImpl.childQuery failed, see exception for details.", t);
150 protected void replaceResult(LabelDecorator result) {
151 // Never let decorator become null, use a stub decorator instead.
153 result = NO_DECORATION;
156 updater.scheduleReplace(context, key, this);
161 public abstract LabelDecorator getDecorator(ReadGraph graph, NodeContext context) throws DatabaseException;