]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LazyGraphLabeler.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / LazyGraphLabeler.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.browsing.ui.graph.impl;\r
13 \r
14 import java.util.Collections;\r
15 import java.util.Map;\r
16 \r
17 import org.simantics.browsing.ui.BuiltinKeys.LabelerKey;\r
18 import org.simantics.browsing.ui.DataSource;\r
19 import org.simantics.browsing.ui.NodeContext;\r
20 import org.simantics.browsing.ui.PrimitiveQueryUpdater;\r
21 import org.simantics.browsing.ui.common.labelers.LabelerContent;\r
22 import org.simantics.browsing.ui.common.labelers.LabelerStub;\r
23 import org.simantics.browsing.ui.graph.impl.request.ResourceQuery;\r
24 import org.simantics.db.ReadGraph;\r
25 import org.simantics.db.exception.DatabaseException;\r
26 import org.simantics.db.procedure.Listener;\r
27 import org.simantics.utils.ui.ErrorLogger;\r
28 \r
29 public abstract class LazyGraphLabeler extends LabelerStub {\r
30 \r
31     protected final PrimitiveQueryUpdater       updater;\r
32     private final ResourceQuery<LabelerContent> labelQuery;\r
33     private final Listener<LabelerContent>      procedure;\r
34 \r
35     public Object getIdentity(LabelerKey key) {\r
36         return key;\r
37     }\r
38 \r
39     public LazyGraphLabeler(final PrimitiveQueryUpdater updater, final NodeContext context, final LabelerKey key) {\r
40         this.updater = updater;\r
41 \r
42         labelQuery = new ResourceQuery<LabelerContent>(getIdentity(key), context) {\r
43 \r
44             @Override\r
45             public LabelerContent perform(ReadGraph graph) throws DatabaseException {\r
46                 try {\r
47                     int cat = category(graph);\r
48                     Map<String, String> lbls = labels(graph);\r
49                     // Make sure that null is not returned.\r
50                     if (lbls == null)\r
51                         throw new NullPointerException("LazyGraphLabeler.labels is not allowed to return null, but " + LazyGraphLabeler.this.getClass() + " just did it");\r
52                     return new LabelerContent(cat, lbls);\r
53                 } catch (DatabaseException e) {\r
54                     throw e;\r
55                 } catch (Throwable t) {\r
56                     ErrorLogger.defaultLogError("LazyGraphLabeler.labelQuery produced unexpected exception.", t);\r
57                     return LabelerContent.NO_CONTENT;\r
58                 }\r
59             }\r
60 \r
61             @Override\r
62             public String toString() {\r
63                 return LazyGraphLabeler.this + " with context " + context;\r
64             }\r
65 \r
66         };\r
67 \r
68         procedure = new Listener<LabelerContent>() {\r
69 \r
70             @Override\r
71             public void execute(LabelerContent result) {\r
72                 setContent(result);\r
73                 updater.scheduleReplace(context, key, LazyGraphLabeler.this);\r
74             }\r
75 \r
76             @Override\r
77             public boolean isDisposed() {\r
78                 return updater.isDisposed();\r
79             }\r
80 \r
81             @Override\r
82             public void exception(Throwable t) {\r
83                 System.err.println("LazyGraphLabeler2: ");\r
84                 t.printStackTrace();\r
85             }\r
86 \r
87         };\r
88     }\r
89 \r
90     @Override\r
91     public Map<String, String> getLabels() {\r
92 \r
93         if (content == LabelerContent.NO_CONTENT) {\r
94 \r
95             final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);\r
96             assert(source != null);\r
97             source.schedule(graph -> graph.asyncRequest(labelQuery, procedure));\r
98 \r
99         }\r
100 \r
101         if(content == null) return Collections.emptyMap();\r
102 \r
103         return content.labels;\r
104 \r
105     }\r
106 \r
107     // OVERRIDE\r
108 \r
109     public abstract Map<String, String> labels(ReadGraph graph) throws DatabaseException;\r
110 \r
111     public int category(ReadGraph graph) throws DatabaseException {\r
112 \r
113         if(content == null) return 0;\r
114 \r
115         return content.category;\r
116 \r
117     }\r
118 \r
119 }\r