]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LazyParametrizedGraphLabeler.java b/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LazyParametrizedGraphLabeler.java
new file mode 100644 (file)
index 0000000..67e9b78
--- /dev/null
@@ -0,0 +1,100 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.browsing.ui.graph.impl;\r
+\r
+import java.util.Map;\r
+\r
+import org.simantics.browsing.ui.BuiltinKeys;\r
+import org.simantics.browsing.ui.DataSource;\r
+import org.simantics.browsing.ui.NodeContext;\r
+import org.simantics.browsing.ui.PrimitiveQueryUpdater;\r
+import org.simantics.browsing.ui.common.labelers.LabelerContent;\r
+import org.simantics.browsing.ui.common.labelers.LabelerStub;\r
+import org.simantics.browsing.ui.graph.impl.request.ParametrizedResourceQuery;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.procedure.Listener;\r
+\r
+/**\r
+ * A version of LazyLabeler that uses a set of arbitrary parameters to describe\r
+ * the label resource query within the class.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ * \r
+ * @see LazyGraphLabeler a non-parametrized version (more light-weight)\r
+ */\r
+public abstract class LazyParametrizedGraphLabeler extends LabelerStub {\r
+\r
+    private final ParametrizedResourceQuery<LabelerContent> labelQuery;\r
+    private final Listener<LabelerContent>     labelProcedure;\r
+    private final PrimitiveQueryUpdater                     updater;\r
+\r
+    // DEBUG\r
+    static int debugCounter = 0;\r
+\r
+    public LazyParametrizedGraphLabeler(final PrimitiveQueryUpdater updater, final NodeContext context, final BuiltinKeys.LabelerKey key, Object... parameters) {\r
+        this.updater = updater;\r
+        this.labelQuery = new ParametrizedResourceQuery<LabelerContent>(getClass(), context, parameters) {\r
+            @Override\r
+            public LabelerContent perform(ReadGraph graph) throws DatabaseException {\r
+                return new LabelerContent(category(graph), labels(graph));\r
+            }\r
+\r
+        };\r
+        this.labelProcedure = new Listener<LabelerContent>() {\r
+\r
+            @Override\r
+            public void execute(LabelerContent result) {\r
+                setContent(result);\r
+                updater.scheduleReplace(context, key, LazyParametrizedGraphLabeler.this);\r
+            }\r
+\r
+            @Override\r
+            public void exception(Throwable t) {\r
+                t.printStackTrace();\r
+            }\r
+\r
+            @Override\r
+            public boolean isDisposed() {\r
+                return updater.isDisposed();\r
+            }\r
+\r
+        };\r
+    }\r
+\r
+    @Override\r
+    public Map<String, String> getLabels() {\r
+//        new Exception().printStackTrace();\r
+        if (content == LabelerContent.NO_CONTENT) {\r
+            final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);\r
+\r
+            source.schedule(graph -> {\r
+                try {\r
+                    graph.syncRequest(labelQuery, labelProcedure);\r
+                } catch (DatabaseException e) {\r
+                    e.printStackTrace();\r
+                }\r
+            });\r
+        }\r
+\r
+        return content.labels;\r
+    }\r
+\r
+    // OVERRIDE\r
+\r
+    public abstract Map<String, String> labels(ReadGraph graph) throws DatabaseException;\r
+\r
+    public int category(ReadGraph graph) {\r
+        return content.category;\r
+    }\r
+\r
+}\r