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