]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/EdgeRequest.java
651228ce1d7756f10516430c6bbc8c71d355cece
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / adapter / EdgeRequest.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.common.procedure.adapter.ListenerSupport;
16 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
17 import org.simantics.db.procedure.AsyncProcedure;
18 import org.simantics.diagram.content.EdgeResource;
19 import org.simantics.diagram.stubs.DiagramResource;
20 import org.simantics.diagram.synchronization.ErrorHandler;
21 import org.simantics.g2d.canvas.ICanvasContext;
22 import org.simantics.g2d.diagram.IDiagram;
23 import org.simantics.g2d.element.ElementClass;
24 import org.simantics.g2d.element.ElementHints;
25 import org.simantics.g2d.element.IElement;
26 import org.simantics.g2d.element.impl.Element;
27
28 /**
29  * @author Antti Villberg
30  */
31 public class EdgeRequest extends BaseRequest2<EdgeResource, IElement> {
32
33     final ErrorHandler errorHandler;
34     final ListenerSupport listenerSupport;
35     final IDiagram diagram;
36     final ConnectionSegmentAdapter adapter;
37
38     public EdgeRequest(ICanvasContext canvas, ErrorHandler errorHandler, ListenerSupport listenerSupport, IDiagram diagram, ConnectionSegmentAdapter adapter, EdgeResource resource) {
39         super(canvas, resource);
40
41         assert(adapter != null);
42
43         this.errorHandler = errorHandler;
44         this.listenerSupport = listenerSupport;
45         this.diagram = diagram;
46         this.adapter = adapter;
47     }
48
49     @Override
50     public void perform(AsyncReadGraph graph, final AsyncProcedure<IElement> procedure) {
51 //        final ITask task = ThreadLogger.getInstance().begin("NodeRequest2 " + data.getResourceId());
52         final DiagramResource dr = graph.getService(DiagramResource.class);
53
54         graph.forHasStatement(data.first(), new AsyncProcedure<Boolean>() {
55
56             @Override
57             public void exception(AsyncReadGraph graph, Throwable throwable) {
58                 procedure.exception(graph, throwable);
59             }
60
61             @Override
62             public void execute(AsyncReadGraph graph, Boolean result) {
63                 if (!result) {
64                     procedure.execute(graph, null);
65                     return;
66                 }
67
68                 graph.asyncRequest(new ConnectionInfoRequest(dr, data), new TransientCacheAsyncListener<ConnectionInfo>() {
69
70                     @Override
71                     public void exception(AsyncReadGraph graph, Throwable throwable) {
72                         errorHandler.error("ConnectionInfoRequest failure", throwable);
73                     }
74
75                     @Override
76                     public void execute(AsyncReadGraph graph, final ConnectionInfo info) {
77                         adapter.getClass(graph, data, info, listenerSupport, canvas, diagram, new AsyncProcedure<ElementClass>() {
78                             @Override
79                             public void exception(AsyncReadGraph graph, Throwable throwable) {
80                                 errorHandler.error("ConnectionSegmentAdapter(" + adapter + ").getClass failure", throwable);
81                             }
82
83                             @Override
84                             public void execute(AsyncReadGraph graph, final ElementClass clazz) {
85                                 if (clazz == null) {
86                                     procedure.execute(graph, null);
87                                     return;
88                                 }
89
90 //                                final ITask task = ThreadLogger.getInstance().begin("SpawnEdge");
91                                 graph.asyncRequest(new SpawnEdgeRequest(canvas, clazz, data), new AsyncProcedure<IElement>() {
92                                     @Override
93                                     public void exception(AsyncReadGraph graph, Throwable throwable) {
94                                         errorHandler.error("SpawnEdgeRequest failure", throwable);
95                                     }
96
97                                     @Override
98                                     public void execute(AsyncReadGraph graph, IElement element) {
99 //                                        task.finish();
100                                         // Tell the procedure to use this element
101                                         procedure.execute(graph, element);
102
103                                         // Create a separate element for
104                                         // loading the edge which
105                                         // will then be updated into
106                                         // element by the listener in
107                                         // the connection segment adapter.
108                                         IElement loadingElement = Element.spawnNew(clazz);
109                                         loadingElement.setHint(ElementHints.KEY_OBJECT, data);
110
111                                         adapter.load(graph, data, info, listenerSupport, canvas, diagram, loadingElement);
112                                     }
113                                 });
114                             }
115                         });
116                     }
117                 });
118             }
119         });
120     }
121
122 }