]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/ConnectionRequest2.java
Multiple readers in db client
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / adapter / ConnectionRequest2.java
diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/ConnectionRequest2.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/ConnectionRequest2.java
new file mode 100644 (file)
index 0000000..36a01b5
--- /dev/null
@@ -0,0 +1,101 @@
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.diagram.adapter;
+
+import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.primitiverequest.Adapter;
+import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
+import org.simantics.db.procedure.AsyncProcedure;
+import org.simantics.diagram.synchronization.ErrorHandler;
+import org.simantics.g2d.canvas.ICanvasContext;
+import org.simantics.g2d.diagram.IDiagram;
+import org.simantics.g2d.element.ElementClass;
+import org.simantics.g2d.element.IElement;
+import org.simantics.scl.runtime.tuple.Tuple3;
+
+/**
+ * @author Antti Villberg
+ */
+public class ConnectionRequest2 extends BaseRequest2<Resource, Tuple3> {
+
+    final IDiagram diagram;
+    final ErrorHandler errorHandler;
+
+    public ConnectionRequest2(ICanvasContext canvas, IDiagram diagram, Resource resource, ErrorHandler errorHandler) {
+        super(canvas, resource);
+        this.diagram = diagram;
+        this.errorHandler = errorHandler;
+    }
+
+    @Override
+    public void perform(AsyncReadGraph graph, final AsyncProcedure<Tuple3> procedure) {
+
+        graph.forHasStatement(data, new AsyncProcedure<Boolean>() {
+
+            @Override
+            public void exception(AsyncReadGraph graph, Throwable throwable) {
+                procedure.exception(graph, throwable);
+            }
+
+            @Override
+            public void execute(AsyncReadGraph graph, Boolean result) {
+
+                if (!result) {
+                    procedure.execute(graph, null);
+                    return;
+                }
+
+                graph.asyncRequest(new Adapter<ElementFactory>(data, ElementFactory.class), new TransientCacheAsyncListener<ElementFactory>() {
+
+                    @Override
+                    public void exception(AsyncReadGraph graph, Throwable throwable) {
+                        errorHandler.error("Unexpected ElementFactory adaption failure", throwable);
+                        procedure.execute(graph, null);
+                    }
+
+                    @Override
+                    public void execute(AsyncReadGraph graph, final ElementFactory factory) {
+
+                        graph.asyncRequest(new GetElementClassRequest(factory, data, canvas, diagram), new TransientCacheAsyncListener<ElementClass>() {
+
+                            @Override
+                            public void exception(AsyncReadGraph graph, Throwable throwable) {
+                                errorHandler.error("Unexpected ElementClass creation failure", throwable);
+                                procedure.execute(graph, null);
+                            }
+
+                            @Override
+                            public void execute(AsyncReadGraph graph, final ElementClass ec) {
+
+                                graph.asyncRequest(new SpawnRequest(canvas, ec, data), new TransientCacheAsyncListener<IElement>() {
+
+                                    @Override
+                                    public void exception(AsyncReadGraph graph, Throwable throwable) {
+                                        errorHandler.error("Unexpected SpawnRequest failure", throwable);
+                                        procedure.execute(graph, null);
+                                    }
+
+                                    @Override
+                                    public void execute(AsyncReadGraph graph, IElement element) {
+                                        procedure.execute(graph, new Tuple3(element, ec, factory));
+                                    }
+                                });
+                            }
+                        });
+                    }
+                });
+            }
+        });
+    }
+
+}