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