]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.adapter;
13
14 import org.simantics.db.AsyncReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.common.primitiverequest.Adapter;
17 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
18 import org.simantics.db.procedure.AsyncProcedure;
19 import org.simantics.diagram.synchronization.ErrorHandler;
20 import org.simantics.g2d.canvas.ICanvasContext;
21 import org.simantics.g2d.diagram.IDiagram;
22 import org.simantics.g2d.element.ElementClass;
23 import org.simantics.g2d.element.IElement;
24 import org.simantics.scl.runtime.tuple.Tuple3;
25
26 /**
27  * @author Antti Villberg
28  */
29 public class ConnectionRequest2 extends BaseRequest2<Resource, Tuple3> {
30
31     final IDiagram diagram;
32     final ErrorHandler errorHandler;
33
34     public ConnectionRequest2(ICanvasContext canvas, IDiagram diagram, Resource resource, ErrorHandler errorHandler) {
35         super(canvas, resource);
36         this.diagram = diagram;
37         this.errorHandler = errorHandler;
38     }
39
40     @Override
41     public void perform(AsyncReadGraph graph, final AsyncProcedure<Tuple3> procedure) {
42
43         graph.forHasStatement(data, new AsyncProcedure<Boolean>() {
44
45             @Override
46             public void exception(AsyncReadGraph graph, Throwable throwable) {
47                 procedure.exception(graph, throwable);
48             }
49
50             @Override
51             public void execute(AsyncReadGraph graph, Boolean result) {
52
53                 if (!result) {
54                     procedure.execute(graph, null);
55                     return;
56                 }
57
58                 graph.asyncRequest(new Adapter<ElementFactory>(data, ElementFactory.class), new TransientCacheAsyncListener<ElementFactory>() {
59
60                     @Override
61                     public void exception(AsyncReadGraph graph, Throwable throwable) {
62                         errorHandler.error("Unexpected ElementFactory adaption failure", throwable);
63                         procedure.execute(graph, null);
64                     }
65
66                     @Override
67                     public void execute(AsyncReadGraph graph, final ElementFactory factory) {
68
69                         graph.asyncRequest(new GetElementClassRequest(factory, data, canvas, diagram), new TransientCacheAsyncListener<ElementClass>() {
70
71                             @Override
72                             public void exception(AsyncReadGraph graph, Throwable throwable) {
73                                 errorHandler.error("Unexpected ElementClass creation failure", throwable);
74                                 procedure.execute(graph, null);
75                             }
76
77                             @Override
78                             public void execute(AsyncReadGraph graph, final ElementClass ec) {
79
80                                 graph.asyncRequest(new SpawnRequest(canvas, ec, data), new TransientCacheAsyncListener<IElement>() {
81
82                                     @Override
83                                     public void exception(AsyncReadGraph graph, Throwable throwable) {
84                                         errorHandler.error("Unexpected SpawnRequest failure", throwable);
85                                         procedure.execute(graph, null);
86                                     }
87
88                                     @Override
89                                     public void execute(AsyncReadGraph graph, IElement element) {
90                                         procedure.execute(graph, new Tuple3(element, ec, factory));
91                                     }
92                                 });
93                             }
94                         });
95                     }
96                 });
97             }
98         });
99     }
100
101 }