]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/NodeRequest2.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / adapter / NodeRequest2.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 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * @author Antti Villberg
33  */
34 public class NodeRequest2 extends BaseRequest2<Resource, Tuple3> {
35
36     private static final Logger LOGGER = LoggerFactory.getLogger(NodeRequest2.class);
37
38     final IDiagram diagram;
39
40     public NodeRequest2(ICanvasContext canvas, IDiagram diagram, Resource resource) {
41         super(canvas, resource);
42         this.diagram = diagram;
43     }
44
45     @Override
46     public void perform(AsyncReadGraph graph, final AsyncProcedure<Tuple3> procedure) {
47         // Keep this code from crashing the whole DB client by unexpected
48         // throwing of NPE's somewhere in the following code that leads to
49         // procedure not getting called properly.
50         if (diagram == null) {
51             procedure.exception(graph, new NullPointerException("null diagram specified for resource " + data));
52             return;
53         }
54
55         final ErrorHandler eh = ElementFactoryUtil.getErrorHandler(diagram);
56
57         graph.forHasStatement(data, new AsyncProcedure<Boolean>() {
58
59             @Override
60             public void exception(AsyncReadGraph graph, Throwable throwable) {
61                 eh.error("NodeRequest.forHasStatement failed", throwable);
62                 procedure.execute(graph, null);
63             }
64
65             @Override
66             public void execute(AsyncReadGraph graph, Boolean result) {
67
68                 if(!result) {
69                     procedure.execute(graph, null);
70                     return;
71                 }
72
73                 graph.asyncRequest(new Adapter<ElementFactory>(data, ElementFactory.class), new TransientCacheAsyncListener<ElementFactory>() {
74
75                     @Override
76                     public void exception(AsyncReadGraph graph, Throwable throwable) {
77                         eh.error("NodeRequest.asyncRequest(Adapter<ElementFactory>) failed", throwable);
78                         procedure.execute(graph, null);
79                     }
80
81                     @Override
82                     public void execute(AsyncReadGraph graph, final ElementFactory factory) {
83
84                         graph.asyncRequest(new GetElementClassRequest(factory, data, canvas, diagram), new TransientCacheAsyncListener<ElementClass>() {
85
86                             @Override
87                             public void exception(AsyncReadGraph graph, Throwable throwable) {
88                                 LOGGER.error("Unexpected error in GetElementClassRequest", throwable);
89                                 procedure.execute(graph, null);
90                             }
91
92                             @Override
93                             public void execute(AsyncReadGraph graph, ElementClass mutableClazz) {
94                                 List<SubstituteElementClass> substitutes = diagram.getDiagramClass().getItemsByClass(SubstituteElementClass.class);
95                                 for (SubstituteElementClass subs : substitutes) {
96                                     mutableClazz = subs.substitute(diagram, mutableClazz);
97                                 }
98                                 final ElementClass clazz = mutableClazz;
99                                 graph.asyncRequest(new SpawnRequest(canvas, clazz, data), new TransientCacheAsyncListener<IElement>() {
100
101                                     @Override
102                                     public void exception(AsyncReadGraph graph, Throwable throwable) {
103                                         LOGGER.error("Unexpected error in SpawnRequest", throwable);
104                                         procedure.execute(graph, null);
105                                     }
106
107                                     @Override
108                                     public void execute(AsyncReadGraph graph, IElement element) {
109                                         procedure.execute(graph, new Tuple3(element, clazz, factory));
110                                     }
111
112                                 });
113
114                             }
115
116                         });
117
118                     }
119
120                 });
121
122             }
123
124         });
125
126     }
127
128 }