]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/contribution/FinalImageDecoratorContributionImpl.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / contribution / FinalImageDecoratorContributionImpl.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.graph.impl.contribution;
13
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;
26
27 public abstract class FinalImageDecoratorContributionImpl implements ImageDecorator {
28
29     final ImageDecorator FRESH = new ImageDecorator.Stub();
30     final ImageDecorator PENDING = new ImageDecorator.Stub();
31     final ImageDecorator NO_DECORATION = new ImageDecorator.Stub();
32
33     protected final PrimitiveQueryUpdater       updater;
34     private final ResourceQuery<ImageDecorator> imageQuery;
35     private final ImageDecoratorKey             key;
36
37     /**
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.
41      */
42     protected ImageDecorator                      decorator = FRESH;
43
44     final protected NodeContext                   context;
45
46     public Object getIdentity(ImageDecoratorKey key) {
47         return key;
48     }
49
50     private void request() {
51
52         final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
53         assert(source != null);
54
55         final Procedure<ImageDecorator> procedure = createProcedure();
56         
57         source.schedule(graph -> {
58             if(procedure instanceof Listener<?>)
59                 graph.asyncRequest(imageQuery, (Listener<ImageDecorator>)procedure);
60             else
61                 graph.asyncRequest(imageQuery, procedure);
62         });
63
64     }
65
66     @Override
67     public <Image> Image decorateImage(Image image, String column, int itemIndex) {
68         if(FRESH == decorator) {
69             decorator = PENDING;
70             request();
71         }
72         return decorator.decorateImage(image, column, itemIndex);
73     }
74
75     public FinalImageDecoratorContributionImpl(final PrimitiveQueryUpdater updater, final NodeContext context, final ImageDecoratorKey key) {
76
77         this.updater = updater;
78         this.context = context;
79         this.key = key;
80
81         imageQuery = new ResourceQuery<ImageDecorator>(getIdentity(key), context) {
82
83             @Override
84             public ImageDecorator perform(ReadGraph graph) throws DatabaseException {
85                 try {
86                     return getDecorator(graph, context);
87                 } catch (PendingVariableException e) {
88                     return PENDING;
89                 } catch (DatabaseException e) {
90                     throw e;
91                 } catch (Throwable t) {
92                     ErrorLogger.defaultLogError("FinalImageDecoratorContributionImpl.imageQuery produced unexpected exception.", t);
93                     return null;
94                 }
95             }
96
97             @Override
98             public String toString() {
99                 return FinalImageDecoratorContributionImpl.this + " with context " + context;
100             }
101
102         };
103         
104     }
105
106     protected Procedure<ImageDecorator> createProcedure() {
107
108         return new Procedure<ImageDecorator>() {
109
110             @Override
111             public void execute(ImageDecorator result) {
112                 replaceResult(result);
113             }
114
115             @Override
116             public void exception(Throwable t) {
117                 ErrorLogger.defaultLogError("FinalImageDecoratorContributionImpl.imageQuery failed, see exception for details.", t);
118             }
119
120         };
121
122     }
123
124     protected void replaceResult(ImageDecorator result) {
125         // Never let decorator become null, use a stub decorator instead.
126         if (result == null)
127             result = NO_DECORATION;
128
129         decorator = result;
130         updater.scheduleReplace(context, key, this);
131     }
132
133     // OVERRIDE
134
135     public abstract ImageDecorator getDecorator(ReadGraph graph, NodeContext context) throws DatabaseException;
136
137 }