]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LazyGraphLabeler.java
Multiple reader thread support for db client
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / LazyGraphLabeler.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;
13
14 import java.util.Collections;
15 import java.util.Map;
16
17 import org.simantics.browsing.ui.BuiltinKeys.LabelerKey;
18 import org.simantics.browsing.ui.DataSource;
19 import org.simantics.browsing.ui.NodeContext;
20 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
21 import org.simantics.browsing.ui.common.labelers.LabelerContent;
22 import org.simantics.browsing.ui.common.labelers.LabelerStub;
23 import org.simantics.browsing.ui.graph.impl.request.ResourceQuery;
24 import org.simantics.db.AsyncReadGraph;
25 import org.simantics.db.ReadGraph;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.db.procedure.Listener;
28 import org.simantics.utils.ui.ErrorLogger;
29 import org.slf4j.Logger;
30
31 public abstract class LazyGraphLabeler extends LabelerStub {
32
33     protected final PrimitiveQueryUpdater       updater;
34     private final ResourceQuery<LabelerContent> labelQuery;
35     private final Listener<LabelerContent>      procedure;
36
37     public Object getIdentity(LabelerKey key) {
38         return key;
39     }
40
41     public LazyGraphLabeler(final PrimitiveQueryUpdater updater, final NodeContext context, final LabelerKey key) {
42         this.updater = updater;
43
44         labelQuery = new ResourceQuery<LabelerContent>(getIdentity(key), context) {
45
46             @Override
47             public LabelerContent perform(ReadGraph graph) throws DatabaseException {
48                 try {
49                     int cat = category(graph);
50                     Map<String, String> lbls = labels(graph);
51                     // Make sure that null is not returned.
52                     if (lbls == null)
53                         throw new NullPointerException("LazyGraphLabeler.labels is not allowed to return null, but " + LazyGraphLabeler.this.getClass() + " just did it");
54                     return new LabelerContent(cat, lbls);
55                 } catch (DatabaseException e) {
56                     throw e;
57                 } catch (Throwable t) {
58                     ErrorLogger.defaultLogError("LazyGraphLabeler.labelQuery produced unexpected exception.", t);
59                     return LabelerContent.NO_CONTENT;
60                 }
61             }
62
63             @Override
64             public String toString() {
65                 return LazyGraphLabeler.this + " with context " + context;
66             }
67
68         };
69
70         procedure = new Listener<LabelerContent>() {
71
72             @Override
73             public void execute(LabelerContent result) {
74                 setContent(result);
75                 updater.scheduleReplace(context, key, LazyGraphLabeler.this);
76             }
77
78             @Override
79             public boolean isDisposed() {
80                 return updater.isDisposed();
81             }
82
83             @Override
84             public void exception(Throwable t) {
85                 getLogger().error("LazyGraphLabeler2: ", t);
86             }
87
88         };
89     }
90
91     @Override
92     public Map<String, String> getLabels() {
93
94         if (content == LabelerContent.NO_CONTENT) {
95
96             final DataSource<AsyncReadGraph> source = updater.getDataSource(AsyncReadGraph.class);
97             assert(source != null);
98             source.schedule(graph -> graph.asyncRequest(labelQuery, procedure));
99
100         }
101
102         if(content == null) return Collections.emptyMap();
103
104         return content.labels;
105
106     }
107
108     // OVERRIDE
109
110     public abstract Map<String, String> labels(ReadGraph graph) throws DatabaseException;
111
112     public int category(ReadGraph graph) throws DatabaseException {
113
114         if(content == null) return 0;
115
116         return content.category;
117
118     }
119
120     public abstract Logger getLogger();
121 }