1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.browsing.ui.graph.impl.contribution;
\r
14 import org.simantics.browsing.ui.BuiltinKeys.LabelDecoratorKey;
\r
15 import org.simantics.browsing.ui.DataSource;
\r
16 import org.simantics.browsing.ui.NodeContext;
\r
17 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
\r
18 import org.simantics.browsing.ui.content.LabelDecorator;
\r
19 import org.simantics.browsing.ui.graph.impl.request.ResourceQuery;
\r
20 import org.simantics.db.ReadGraph;
\r
21 import org.simantics.db.exception.DatabaseException;
\r
22 import org.simantics.db.procedure.Listener;
\r
23 import org.simantics.db.procedure.Procedure;
\r
24 import org.simantics.utils.ui.ErrorLogger;
\r
26 public abstract class FinalLabelDecoratorContributionImpl extends LabelDecorator.Stub {
\r
28 final LabelDecorator FRESH = new LabelDecorator.Stub();
\r
29 final LabelDecorator PENDING = new LabelDecorator.Stub();
\r
30 final LabelDecorator NO_DECORATION = new LabelDecorator.Stub();
\r
32 protected final PrimitiveQueryUpdater updater;
\r
33 private final ResourceQuery<LabelDecorator> labelQuery;
\r
34 private final LabelDecoratorKey key;
\r
37 * The current decorator result produced for this contribution. The value
\r
38 * should never be <code>null</code>, but one of {@link #FRESH},
\r
39 * {@link #PENDING}, {@link #NO_DECORATION} instead.
\r
41 protected LabelDecorator decorator = FRESH;
\r
43 final protected NodeContext context;
\r
45 public Object getIdentity(LabelDecoratorKey key) {
\r
49 private void request() {
\r
51 final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
\r
52 assert(source != null);
\r
54 final Procedure<LabelDecorator> procedure = createProcedure();
\r
56 source.schedule(graph -> {
\r
57 if(procedure instanceof Listener<?>)
\r
58 graph.asyncRequest(labelQuery, (Listener<LabelDecorator>)procedure);
\r
60 graph.asyncRequest(labelQuery, procedure);
\r
68 public <Color_> Color_ decorateBackground(Color_ color, String column, int itemIndex) {
\r
69 if(FRESH == decorator) {
\r
70 decorator = PENDING;
\r
73 return decorator.decorateBackground(color, column, itemIndex);
\r
77 public <Font_> Font_ decorateFont(Font_ font, String column, int itemIndex) {
\r
78 if(FRESH == decorator) {
\r
79 decorator = PENDING;
\r
82 return decorator.decorateFont(font, column, itemIndex);
\r
86 public String decorateLabel(String label, String column, int itemIndex) {
\r
87 if(FRESH == decorator) {
\r
88 decorator = PENDING;
\r
91 return decorator.decorateLabel(label, column, itemIndex);
\r
95 public <Color_> Color_ decorateForeground(Color_ color, String column, int itemIndex) {
\r
96 if(FRESH == decorator) {
\r
97 decorator = PENDING;
\r
100 return decorator.decorateForeground(color, column, itemIndex);
\r
103 public FinalLabelDecoratorContributionImpl(final PrimitiveQueryUpdater updater, final NodeContext context, final LabelDecoratorKey key) {
\r
105 this.updater = updater;
\r
106 this.context = context;
\r
109 labelQuery = new ResourceQuery<LabelDecorator>(getIdentity(key), context) {
\r
112 public LabelDecorator perform(ReadGraph graph) throws DatabaseException {
\r
114 return getDecorator(graph, context);
\r
115 } catch (DatabaseException e) {
\r
117 } catch (Throwable t) {
\r
118 ErrorLogger.defaultLogError("LazyGraphLabeler.labelQuery produced unexpected exception.", t);
\r
124 public String toString() {
\r
125 return FinalLabelDecoratorContributionImpl.this + " with context " + context;
\r
132 protected Procedure<LabelDecorator> createProcedure() {
\r
134 return new Procedure<LabelDecorator>() {
\r
137 public void execute(LabelDecorator result) {
\r
138 replaceResult(result);
\r
142 public void exception(Throwable t) {
\r
143 ErrorLogger.defaultLogError("LazyContributionImpl.childQuery failed, see exception for details.", t);
\r
150 protected void replaceResult(LabelDecorator result) {
\r
151 // Never let decorator become null, use a stub decorator instead.
\r
152 if (result == null)
\r
153 result = NO_DECORATION;
\r
155 decorator = result;
\r
156 updater.scheduleReplace(context, key, this);
\r
161 public abstract LabelDecorator getDecorator(ReadGraph graph, NodeContext context) throws DatabaseException;
\r