]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LazyGraphLabeler.java b/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LazyGraphLabeler.java
new file mode 100644 (file)
index 0000000..73fe008
--- /dev/null
@@ -0,0 +1,119 @@
+/*******************************************************************************\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.Collections;\r
+import java.util.Map;\r
+\r
+import org.simantics.browsing.ui.BuiltinKeys.LabelerKey;\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.ResourceQuery;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.procedure.Listener;\r
+import org.simantics.utils.ui.ErrorLogger;\r
+\r
+public abstract class LazyGraphLabeler extends LabelerStub {\r
+\r
+    protected final PrimitiveQueryUpdater       updater;\r
+    private final ResourceQuery<LabelerContent> labelQuery;\r
+    private final Listener<LabelerContent>      procedure;\r
+\r
+    public Object getIdentity(LabelerKey key) {\r
+        return key;\r
+    }\r
+\r
+    public LazyGraphLabeler(final PrimitiveQueryUpdater updater, final NodeContext context, final LabelerKey key) {\r
+        this.updater = updater;\r
+\r
+        labelQuery = new ResourceQuery<LabelerContent>(getIdentity(key), context) {\r
+\r
+            @Override\r
+            public LabelerContent perform(ReadGraph graph) throws DatabaseException {\r
+                try {\r
+                    int cat = category(graph);\r
+                    Map<String, String> lbls = labels(graph);\r
+                    // Make sure that null is not returned.\r
+                    if (lbls == null)\r
+                        throw new NullPointerException("LazyGraphLabeler.labels is not allowed to return null, but " + LazyGraphLabeler.this.getClass() + " just did it");\r
+                    return new LabelerContent(cat, lbls);\r
+                } catch (DatabaseException e) {\r
+                    throw e;\r
+                } catch (Throwable t) {\r
+                    ErrorLogger.defaultLogError("LazyGraphLabeler.labelQuery produced unexpected exception.", t);\r
+                    return LabelerContent.NO_CONTENT;\r
+                }\r
+            }\r
+\r
+            @Override\r
+            public String toString() {\r
+                return LazyGraphLabeler.this + " with context " + context;\r
+            }\r
+\r
+        };\r
+\r
+        procedure = new Listener<LabelerContent>() {\r
+\r
+            @Override\r
+            public void execute(LabelerContent result) {\r
+                setContent(result);\r
+                updater.scheduleReplace(context, key, LazyGraphLabeler.this);\r
+            }\r
+\r
+            @Override\r
+            public boolean isDisposed() {\r
+                return updater.isDisposed();\r
+            }\r
+\r
+            @Override\r
+            public void exception(Throwable t) {\r
+                System.err.println("LazyGraphLabeler2: ");\r
+                t.printStackTrace();\r
+            }\r
+\r
+        };\r
+    }\r
+\r
+    @Override\r
+    public Map<String, String> getLabels() {\r
+\r
+        if (content == LabelerContent.NO_CONTENT) {\r
+\r
+            final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);\r
+            assert(source != null);\r
+            source.schedule(graph -> graph.asyncRequest(labelQuery, procedure));\r
+\r
+        }\r
+\r
+        if(content == null) return Collections.emptyMap();\r
+\r
+        return content.labels;\r
+\r
+    }\r
+\r
+    // OVERRIDE\r
+\r
+    public abstract Map<String, String> labels(ReadGraph graph) throws DatabaseException;\r
+\r
+    public int category(ReadGraph graph) throws DatabaseException {\r
+\r
+        if(content == null) return 0;\r
+\r
+        return content.category;\r
+\r
+    }\r
+\r
+}\r