]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/ConnectionRequest.java
Bringing layers back to life
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / adapter / ConnectionRequest.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.db.procedure.Listener;
22 import org.simantics.diagram.synchronization.ErrorHandler;
23 import org.simantics.g2d.canvas.ICanvasContext;
24 import org.simantics.g2d.diagram.IDiagram;
25 import org.simantics.g2d.diagram.handler.SubstituteElementClass;
26 import org.simantics.g2d.element.ElementClass;
27 import org.simantics.g2d.element.IElement;
28
29 /**
30  * @author Antti Villberg
31  */
32 public class ConnectionRequest extends BaseRequest2<Resource, IElement> {
33
34     final IDiagram diagram;
35     final Listener<IElement> loadListener;
36     final ErrorHandler errorHandler;
37
38     public ConnectionRequest(ICanvasContext canvas, IDiagram diagram, Resource resource, ErrorHandler errorHandler, Listener<IElement> loadListener) {
39         super(canvas, resource);
40         this.diagram = diagram;
41         this.errorHandler = errorHandler;
42         this.loadListener = loadListener;
43     }
44
45     @Override
46     public void perform(AsyncReadGraph graph, final AsyncProcedure<IElement> procedure) {
47
48         graph.forHasStatement(data, new AsyncProcedure<Boolean>() {
49
50             @Override
51             public void exception(AsyncReadGraph graph, Throwable throwable) {
52                 procedure.exception(graph, throwable);
53             }
54
55             @Override
56             public void execute(AsyncReadGraph graph, Boolean result) {
57
58                 if (!result) {
59                     procedure.execute(graph, null);
60                     return;
61                 }
62
63                 graph.asyncRequest(new Adapter<ElementFactory>(data, ElementFactory.class), new TransientCacheAsyncListener<ElementFactory>() {
64
65                     @Override
66                     public void exception(AsyncReadGraph graph, Throwable throwable) {
67                         errorHandler.error("Unexpected ElementFactory adaption failure", throwable);
68                         procedure.execute(graph, null);
69                     }
70
71                     @Override
72                     public void execute(AsyncReadGraph graph, final ElementFactory factory) {
73
74                         graph.asyncRequest(new GetElementClassRequest(factory, data, canvas, diagram), new TransientCacheAsyncListener<ElementClass>() {
75
76                             @Override
77                             public void exception(AsyncReadGraph graph, Throwable throwable) {
78                                 errorHandler.error("Unexpected ElementClass creation failure", throwable);
79                                 procedure.execute(graph, null);
80                             }
81
82                             @Override
83                             public void execute(AsyncReadGraph graph, ElementClass mutableClazz) {
84
85                                 List<SubstituteElementClass> substitutes = diagram.getDiagramClass().getItemsByClass(SubstituteElementClass.class);
86                                 for (SubstituteElementClass subs : substitutes) {
87                                     mutableClazz = subs.substitute(diagram, mutableClazz);
88                                 }
89                                 final ElementClass ec = mutableClazz;
90                                 
91                                 graph.asyncRequest(new SpawnRequest(canvas, ec, data), new TransientCacheAsyncListener<IElement>() {
92
93                                     @Override
94                                     public void exception(AsyncReadGraph graph, Throwable throwable) {
95                                         errorHandler.error("Unexpected SpawnRequest failure", throwable);
96                                         procedure.execute(graph, null);
97                                     }
98
99                                     @Override
100                                     public void execute(AsyncReadGraph graph, IElement element) {
101
102                                         procedure.execute(graph, element);
103
104                                         if (loadListener != null) {
105                                             //System.out.println("LoadRequest[" + (loadCounter++) + "] for " + data + ": " + element);
106                                             graph.asyncRequest(new LoadRequest(canvas, diagram, factory, ec, data), loadListener);
107                                         } else {
108                                             //System.out.println("Spawn[" + (spawnCounter++) + "] for " + data + ": " + element);
109                                             factory.load(graph, canvas, diagram, data, element, new AsyncProcedure<IElement>() {
110                                                 @Override
111                                                 public void exception(AsyncReadGraph graph, Throwable throwable) {
112                                                     errorHandler.error("Unexpected ElementFactory.load failure", throwable);
113                                                 }
114                                                 @Override
115                                                 public void execute(AsyncReadGraph graph, IElement result) {
116                                                     // Loading complete, don't care.
117                                                 }
118                                             });
119                                         }
120                                     }
121                                 });
122                             }
123                         });
124                     }
125                 });
126             }
127         });
128     }
129
130 }