]> gerrit.simantics Code Review - simantics/platform.git/blob
b4d233e0882289b59862157d11f872871376e52f
[simantics/platform.git] /
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 java.util.Collections;
15 import java.util.Map;
16
17 import org.eclipse.jface.resource.ImageDescriptor;
18 import org.simantics.browsing.ui.BuiltinKeys;
19 import org.simantics.browsing.ui.BuiltinKeys.ImagerKey;
20 import org.simantics.browsing.ui.DataSource;
21 import org.simantics.browsing.ui.NodeContext;
22 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
23 import org.simantics.browsing.ui.content.Imager;
24 import org.simantics.browsing.ui.graph.impl.request.ResourceQuery;
25 import org.simantics.db.ReadGraph;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.db.layer0.exception.PendingVariableException;
28 import org.simantics.db.procedure.Listener;
29 import org.simantics.db.procedure.Procedure;
30 import org.simantics.utils.ui.ErrorLogger;
31
32 public abstract class FinalImagerContributionImpl implements Imager {
33
34     private final static Map<String, ImageDescriptor>         PENDING = Collections.emptyMap();
35
36     protected final PrimitiveQueryUpdater                     updater;
37     private final ResourceQuery<Map<String, ImageDescriptor>> imagerQuery;
38     
39     protected final NodeContext                                 context;
40     private final BuiltinKeys.ImagerKey                       key;
41
42     protected Map<String, ImageDescriptor>                      content = null;
43
44     public Object getIdentity(ImagerKey key) {
45         return key;
46     }
47
48     public FinalImagerContributionImpl(final PrimitiveQueryUpdater updater, final NodeContext context, final ImagerKey key) {
49
50         this.updater = updater;
51         this.context = context;
52         this.key = key;
53
54         imagerQuery = new ResourceQuery<Map<String, ImageDescriptor>>(getIdentity(key), context) {
55
56             @Override
57             public Map<String, ImageDescriptor> perform(ReadGraph graph) throws DatabaseException {
58                 try {
59                     return getDescriptors(graph, context);
60                 } catch (PendingVariableException e) {
61                     return PENDING;
62                 } catch (DatabaseException e) {
63                     throw e;
64                 } catch (Throwable t) {
65                     ErrorLogger.defaultLogError("LazyGraphLabeler.labelQuery produced unexpected exception.", t);
66                     return null;
67                 }
68             }
69
70             @Override
71             public String toString() {
72                 return FinalImagerContributionImpl.this + " with context " + context;
73             }
74
75         };
76
77     }
78
79     protected Procedure<Map<String, ImageDescriptor>> createProcedure() {
80
81         return new Procedure<Map<String, ImageDescriptor>>() {
82
83             @Override
84             public void execute(Map<String, ImageDescriptor> result) {
85                 replaceResult(result);
86             }
87
88             @Override
89             public void exception(Throwable t) {
90                 ErrorLogger.defaultLogError("FinalImagerContributionImpl.imagerQuery failed, see exception for details.", t);
91             }
92
93         };
94
95     }
96
97     protected void replaceResult(Map<String, ImageDescriptor> desc) {
98         content = desc;
99         updater.scheduleReplace(context, key, this);
100     }
101
102     @SuppressWarnings("unchecked")
103     @Override
104     public ImageDescriptor getImage(String key) {
105         
106         if (content == null) {
107
108             content = PENDING;
109
110             final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
111             assert(source != null);
112
113             final Procedure<Map<String, ImageDescriptor>> procedure = createProcedure();
114             source.schedule(graph -> {
115                 if(procedure instanceof Listener<?>)
116                     graph.asyncRequest(imagerQuery, (Listener<Map<String, ImageDescriptor>>)procedure);
117                 else
118                     graph.asyncRequest(imagerQuery, procedure);
119             });
120
121             return null;
122
123         }
124
125         ImageDescriptor descriptor = content.get(key);
126         return descriptor;
127     }
128
129     // OVERRIDE
130
131     public abstract Map<String, ImageDescriptor> getDescriptors(ReadGraph graph, NodeContext context) throws DatabaseException;
132
133 }