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.AsyncReadGraph;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.layer0.exception.PendingVariableException;
24 import org.simantics.db.procedure.Listener;
25 import org.simantics.db.procedure.Procedure;
26 import org.simantics.utils.ui.ErrorLogger;
28 public abstract class FinalImageDecoratorContributionImpl implements ImageDecorator {
30 final ImageDecorator FRESH = new ImageDecorator.Stub();
31 final ImageDecorator PENDING = new ImageDecorator.Stub();
32 final ImageDecorator NO_DECORATION = new ImageDecorator.Stub();
34 protected final PrimitiveQueryUpdater updater;
35 private final ResourceQuery<ImageDecorator> imageQuery;
36 private final ImageDecoratorKey key;
39 * The current decorator result produced for this contribution. The value
40 * should never be <code>null</code>, but one of {@link #FRESH},
41 * {@link #PENDING}, {@link #NO_DECORATION} instead.
43 protected ImageDecorator decorator = FRESH;
45 final protected NodeContext context;
47 public Object getIdentity(ImageDecoratorKey key) {
51 private void request() {
53 final DataSource<AsyncReadGraph> source = updater.getDataSource(AsyncReadGraph.class);
54 assert(source != null);
56 final Procedure<ImageDecorator> procedure = createProcedure();
58 source.schedule(graph -> {
59 if(procedure instanceof Listener<?>)
60 graph.asyncRequest(imageQuery, (Listener<ImageDecorator>)procedure);
62 graph.asyncRequest(imageQuery, procedure);
68 public <Image> Image decorateImage(Image image, String column, int itemIndex) {
69 if(FRESH == decorator) {
73 return decorator.decorateImage(image, column, itemIndex);
76 public FinalImageDecoratorContributionImpl(final PrimitiveQueryUpdater updater, final NodeContext context, final ImageDecoratorKey key) {
78 this.updater = updater;
79 this.context = context;
82 imageQuery = new ResourceQuery<ImageDecorator>(getIdentity(key), context) {
85 public ImageDecorator perform(ReadGraph graph) throws DatabaseException {
87 return getDecorator(graph, context);
88 } catch (PendingVariableException e) {
90 } catch (DatabaseException e) {
92 } catch (Throwable t) {
93 ErrorLogger.defaultLogError("FinalImageDecoratorContributionImpl.imageQuery produced unexpected exception.", t);
99 public String toString() {
100 return FinalImageDecoratorContributionImpl.this + " with context " + context;
107 protected Procedure<ImageDecorator> createProcedure() {
109 return new Procedure<ImageDecorator>() {
112 public void execute(ImageDecorator result) {
113 replaceResult(result);
117 public void exception(Throwable t) {
118 ErrorLogger.defaultLogError("FinalImageDecoratorContributionImpl.imageQuery failed, see exception for details.", t);
125 protected void replaceResult(ImageDecorator result) {
126 // Never let decorator become null, use a stub decorator instead.
128 result = NO_DECORATION;
131 updater.scheduleReplace(context, key, this);
136 public abstract ImageDecorator getDecorator(ReadGraph graph, NodeContext context) throws DatabaseException;