]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/ConnectionRequest2.java
Bringing layers back to life
[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 java.util.List;
15
16 import org.simantics.db.AsyncReadGraph;
17 import org.simantics.db.Resource;
18 import org.simantics.db.common.primitiverequest.Adapter;
19 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
20 import org.simantics.db.procedure.AsyncProcedure;
21 import org.simantics.diagram.synchronization.ErrorHandler;
22 import org.simantics.g2d.canvas.ICanvasContext;
23 import org.simantics.g2d.diagram.IDiagram;
24 import org.simantics.g2d.diagram.handler.SubstituteElementClass;
25 import org.simantics.g2d.element.ElementClass;
26 import org.simantics.g2d.element.IElement;
27 import org.simantics.scl.runtime.tuple.Tuple3;
28
29 /**
30  * @author Antti Villberg
31  */
32 public class ConnectionRequest2 extends BaseRequest2<Resource, Tuple3> {
33
34     final IDiagram diagram;
35     final ErrorHandler errorHandler;
36
37     public ConnectionRequest2(ICanvasContext canvas, IDiagram diagram, Resource resource, ErrorHandler errorHandler) {
38         super(canvas, resource);
39         this.diagram = diagram;
40         this.errorHandler = errorHandler;
41     }
42
43     @Override
44     public void perform(AsyncReadGraph graph, final AsyncProcedure<Tuple3> procedure) {
45
46         graph.forHasStatement(data, new AsyncProcedure<Boolean>() {
47
48             @Override
49             public void exception(AsyncReadGraph graph, Throwable throwable) {
50                 procedure.exception(graph, throwable);
51             }
52
53             @Override
54             public void execute(AsyncReadGraph graph, Boolean result) {
55
56                 if (!result) {
57                     procedure.execute(graph, null);
58                     return;
59                 }
60
61                 graph.asyncRequest(new Adapter<ElementFactory>(data, ElementFactory.class), new TransientCacheAsyncListener<ElementFactory>() {
62
63                     @Override
64                     public void exception(AsyncReadGraph graph, Throwable throwable) {
65                         errorHandler.error("Unexpected ElementFactory adaption failure", throwable);
66                         procedure.execute(graph, null);
67                     }
68
69                     @Override
70                     public void execute(AsyncReadGraph graph, final ElementFactory factory) {
71
72                         graph.asyncRequest(new GetElementClassRequest(factory, data, canvas, diagram), new TransientCacheAsyncListener<ElementClass>() {
73
74                             @Override
75                             public void exception(AsyncReadGraph graph, Throwable throwable) {
76                                 errorHandler.error("Unexpected ElementClass creation failure", throwable);
77                                 procedure.execute(graph, null);
78                             }
79
80                             @Override
81                             public void execute(AsyncReadGraph graph, ElementClass mutableClazz) {
82
83                                 List<SubstituteElementClass> substitutes = diagram.getDiagramClass().getItemsByClass(SubstituteElementClass.class);
84                                 for (SubstituteElementClass subs : substitutes) {
85                                     mutableClazz = subs.substitute(diagram, mutableClazz);
86                                 }
87                                 final ElementClass ec = mutableClazz;
88
89                                 graph.asyncRequest(new SpawnRequest(canvas, ec, data), new TransientCacheAsyncListener<IElement>() {
90
91                                     @Override
92                                     public void exception(AsyncReadGraph graph, Throwable throwable) {
93                                         errorHandler.error("Unexpected SpawnRequest failure", throwable);
94                                         procedure.execute(graph, null);
95                                     }
96
97                                     @Override
98                                     public void execute(AsyncReadGraph graph, IElement element) {
99                                         procedure.execute(graph, new Tuple3(element, ec, factory));
100                                     }
101                                 });
102                             }
103                         });
104                     }
105                 });
106             }
107         });
108     }
109
110 }