]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/ElementFactoryAdapter.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / adapter / ElementFactoryAdapter.java
diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/ElementFactoryAdapter.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/ElementFactoryAdapter.java
new file mode 100644 (file)
index 0000000..0d1eb82
--- /dev/null
@@ -0,0 +1,73 @@
+/*******************************************************************************\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.diagram.adapter;\r
+\r
+import org.simantics.db.AsyncReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;\r
+import org.simantics.db.procedure.AsyncProcedure;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.g2d.canvas.ICanvasContext;\r
+import org.simantics.g2d.diagram.IDiagram;\r
+import org.simantics.g2d.element.ElementClass;\r
+import org.simantics.g2d.element.IElement;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class ElementFactoryAdapter implements ElementFactory {\r
+\r
+    @Override\r
+    public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType,\r
+            AsyncProcedure<ElementClass> procedure) {\r
+        procedure.exception(graph, new UnsupportedOperationException(getClass().getName() + " does not support ElementFactory.create"));\r
+    }\r
+\r
+    protected Resource getElementClassBaseType(AsyncReadGraph graph) {\r
+        return graph.getService(DiagramResource.class).Element;\r
+    }\r
+\r
+    @Override\r
+    public void getClass(AsyncReadGraph graph, final ICanvasContext canvas, final IDiagram diagram,\r
+            Resource elementResource, final AsyncProcedure<ElementClass> procedure) {\r
+        // A most usual default implementation.\r
+        graph.forSingleType(elementResource, getElementClassBaseType(graph), new AsyncProcedure<Resource>() {\r
+            @Override\r
+            public void exception(AsyncReadGraph graph, Throwable throwable) {\r
+                procedure.exception(graph, throwable);\r
+            }\r
+            @Override\r
+            public void execute(AsyncReadGraph graph, Resource elementType) {\r
+                // Implements caching of element class requests.\r
+                graph.asyncRequest(new CreateElementClassRequest(ElementFactoryAdapter.this, elementType, canvas, diagram),\r
+                        new TransientCacheAsyncListener<ElementClass>() {\r
+                    @Override\r
+                    public void exception(AsyncReadGraph graph, Throwable t) {\r
+                        procedure.exception(graph, t);\r
+                    }\r
+                    @Override\r
+                    public void execute(AsyncReadGraph graph, ElementClass result) {\r
+                        //System.out.println("RESULT ELEMENTCLASS: " + System.identityHashCode(result) + ": " + result);\r
+                        procedure.execute(graph, result);\r
+                    }\r
+                });\r
+            }\r
+        });\r
+    }\r
+\r
+    @Override\r
+    public void load(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementResource,\r
+            IElement element, AsyncProcedure<IElement> procedure) {\r
+        procedure.exception(graph, new UnsupportedOperationException(getClass().getName() + " does not support ElementFactory.load"));\r
+    }\r
+\r
+}\r