]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LazyParametrizedGraphLabeler.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / LazyParametrizedGraphLabeler.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.Map;\r
15 \r
16 import org.simantics.browsing.ui.BuiltinKeys;\r
17 import org.simantics.browsing.ui.DataSource;\r
18 import org.simantics.browsing.ui.NodeContext;\r
19 import org.simantics.browsing.ui.PrimitiveQueryUpdater;\r
20 import org.simantics.browsing.ui.common.labelers.LabelerContent;\r
21 import org.simantics.browsing.ui.common.labelers.LabelerStub;\r
22 import org.simantics.browsing.ui.graph.impl.request.ParametrizedResourceQuery;\r
23 import org.simantics.db.ReadGraph;\r
24 import org.simantics.db.exception.DatabaseException;\r
25 import org.simantics.db.procedure.Listener;\r
26 \r
27 /**\r
28  * A version of LazyLabeler that uses a set of arbitrary parameters to describe\r
29  * the label resource query within the class.\r
30  * \r
31  * @author Tuukka Lehtonen\r
32  * \r
33  * @see LazyGraphLabeler a non-parametrized version (more light-weight)\r
34  */\r
35 public abstract class LazyParametrizedGraphLabeler extends LabelerStub {\r
36 \r
37     private final ParametrizedResourceQuery<LabelerContent> labelQuery;\r
38     private final Listener<LabelerContent>     labelProcedure;\r
39     private final PrimitiveQueryUpdater                     updater;\r
40 \r
41     // DEBUG\r
42     static int debugCounter = 0;\r
43 \r
44     public LazyParametrizedGraphLabeler(final PrimitiveQueryUpdater updater, final NodeContext context, final BuiltinKeys.LabelerKey key, Object... parameters) {\r
45         this.updater = updater;\r
46         this.labelQuery = new ParametrizedResourceQuery<LabelerContent>(getClass(), context, parameters) {\r
47             @Override\r
48             public LabelerContent perform(ReadGraph graph) throws DatabaseException {\r
49                 return new LabelerContent(category(graph), labels(graph));\r
50             }\r
51 \r
52         };\r
53         this.labelProcedure = new Listener<LabelerContent>() {\r
54 \r
55             @Override\r
56             public void execute(LabelerContent result) {\r
57                 setContent(result);\r
58                 updater.scheduleReplace(context, key, LazyParametrizedGraphLabeler.this);\r
59             }\r
60 \r
61             @Override\r
62             public void exception(Throwable t) {\r
63                 t.printStackTrace();\r
64             }\r
65 \r
66             @Override\r
67             public boolean isDisposed() {\r
68                 return updater.isDisposed();\r
69             }\r
70 \r
71         };\r
72     }\r
73 \r
74     @Override\r
75     public Map<String, String> getLabels() {\r
76 //        new Exception().printStackTrace();\r
77         if (content == LabelerContent.NO_CONTENT) {\r
78             final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);\r
79 \r
80             source.schedule(graph -> {\r
81                 try {\r
82                     graph.syncRequest(labelQuery, labelProcedure);\r
83                 } catch (DatabaseException e) {\r
84                     e.printStackTrace();\r
85                 }\r
86             });\r
87         }\r
88 \r
89         return content.labels;\r
90     }\r
91 \r
92     // OVERRIDE\r
93 \r
94     public abstract Map<String, String> labels(ReadGraph graph) throws DatabaseException;\r
95 \r
96     public int category(ReadGraph graph) {\r
97         return content.category;\r
98     }\r
99 \r
100 }\r