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