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.ImageDecoratorKey;
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.ImageDecorator;
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.layer0.exception.PendingVariableException;
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 FinalImageDecoratorContributionImpl implements ImageDecorator {
29 final ImageDecorator FRESH = new ImageDecorator.Stub();
30 final ImageDecorator PENDING = new ImageDecorator.Stub();
31 final ImageDecorator NO_DECORATION = new ImageDecorator.Stub();
33 protected final PrimitiveQueryUpdater updater;
34 private final ResourceQuery<ImageDecorator> imageQuery;
35 private final ImageDecoratorKey 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 ImageDecorator decorator = FRESH;
44 final protected NodeContext context;
46 public Object getIdentity(ImageDecoratorKey key) {
50 private void request() {
52 final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
53 assert(source != null);
55 final Procedure<ImageDecorator> procedure = createProcedure();
57 source.schedule(graph -> {
58 if(procedure instanceof Listener<?>)
59 graph.asyncRequest(imageQuery, (Listener<ImageDecorator>)procedure);
61 graph.asyncRequest(imageQuery, procedure);
67 public <Image> Image decorateImage(Image image, String column, int itemIndex) {
68 if(FRESH == decorator) {
72 return decorator.decorateImage(image, column, itemIndex);
75 public FinalImageDecoratorContributionImpl(final PrimitiveQueryUpdater updater, final NodeContext context, final ImageDecoratorKey key) {
77 this.updater = updater;
78 this.context = context;
81 imageQuery = new ResourceQuery<ImageDecorator>(getIdentity(key), context) {
84 public ImageDecorator perform(ReadGraph graph) throws DatabaseException {
86 return getDecorator(graph, context);
87 } catch (PendingVariableException e) {
89 } catch (DatabaseException e) {
91 } catch (Throwable t) {
92 ErrorLogger.defaultLogError("FinalImageDecoratorContributionImpl.imageQuery produced unexpected exception.", t);
98 public String toString() {
99 return FinalImageDecoratorContributionImpl.this + " with context " + context;
106 protected Procedure<ImageDecorator> createProcedure() {
108 return new Procedure<ImageDecorator>() {
111 public void execute(ImageDecorator result) {
112 replaceResult(result);
116 public void exception(Throwable t) {
117 ErrorLogger.defaultLogError("FinalImageDecoratorContributionImpl.imageQuery failed, see exception for details.", t);
124 protected void replaceResult(ImageDecorator result) {
125 // Never let decorator become null, use a stub decorator instead.
127 result = NO_DECORATION;
130 updater.scheduleReplace(context, key, this);
135 public abstract ImageDecorator getDecorator(ReadGraph graph, NodeContext context) throws DatabaseException;