]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/NodeRequest.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / adapter / NodeRequest.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.diagram.adapter;\r
13 \r
14 import org.simantics.db.AsyncReadGraph;\r
15 import org.simantics.db.Resource;\r
16 import org.simantics.db.common.primitiverequest.Adapter;\r
17 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;\r
18 import org.simantics.db.procedure.AsyncProcedure;\r
19 import org.simantics.db.procedure.Listener;\r
20 import org.simantics.diagram.synchronization.ErrorHandler;\r
21 import org.simantics.g2d.canvas.ICanvasContext;\r
22 import org.simantics.g2d.diagram.IDiagram;\r
23 import org.simantics.g2d.element.ElementClass;\r
24 import org.simantics.g2d.element.IElement;\r
25 \r
26 /**\r
27  * @author Antti Villberg\r
28  */\r
29 public class NodeRequest extends BaseRequest2<Resource, IElement> {\r
30 \r
31     final IDiagram diagram;\r
32     final Listener<IElement> loadListener;\r
33 \r
34     public NodeRequest(ICanvasContext canvas, IDiagram diagram, Resource resource, Listener<IElement> loadListener) {\r
35         super(canvas, resource);\r
36         this.diagram = diagram;\r
37         this.loadListener = loadListener;\r
38     }\r
39 \r
40     @Override\r
41     public void perform(AsyncReadGraph graph, final AsyncProcedure<IElement> procedure) {\r
42         // Keep this code from crashing the whole DB client by unexpected\r
43         // throwing of NPE's somewhere in the following code that leads to\r
44         // procedure not getting called properly.\r
45         if (diagram == null) {\r
46             procedure.exception(graph, new NullPointerException("null diagram specified for resource " + data));\r
47             return;\r
48         }\r
49 \r
50 //        System.out.println("NodeRequest2 " + data);\r
51 //        graph.asyncRequest(new SafeName(data), new Procedure<String>() {\r
52 //            @Override\r
53 //            public void exception(Throwable t) {\r
54 //            }\r
55 //            @Override\r
56 //            public void execute(String result) {\r
57 //                System.out.println("NodeRequest2 "  + result);\r
58 //            }\r
59 //        });\r
60 \r
61         final ErrorHandler eh = ElementFactoryUtil.getErrorHandler(diagram);\r
62 \r
63         graph.forHasStatement(data, new AsyncProcedure<Boolean>() {\r
64 \r
65             @Override\r
66             public void exception(AsyncReadGraph graph, Throwable throwable) {\r
67                 eh.error("NodeRequest.forHasStatement failed", throwable);\r
68                 procedure.execute(graph, null);\r
69             }\r
70 \r
71             @Override\r
72             public void execute(AsyncReadGraph graph, Boolean result) {\r
73 \r
74                 if(!result) {\r
75                     procedure.execute(graph, null);\r
76                     return;\r
77                 }\r
78 \r
79                 graph.asyncRequest(new Adapter<ElementFactory>(data, ElementFactory.class), new TransientCacheAsyncListener<ElementFactory>() {\r
80 \r
81                     @Override\r
82                     public void exception(AsyncReadGraph graph, Throwable throwable) {\r
83                         eh.error("NodeRequest.asyncRequest(Adapter<ElementFactory>) failed", throwable);\r
84                         procedure.execute(graph, null);\r
85                     }\r
86 \r
87                     @Override\r
88                     public void execute(AsyncReadGraph graph, final ElementFactory factory) {\r
89 \r
90 //                        graph.asyncRequest(new ResourceToURI(data), new Procedure<String>() {\r
91 //                            @Override\r
92 //                            public void exception(Throwable t) {\r
93 //                            }\r
94 //                            @Override\r
95 //                            public void execute(String result) {\r
96 //                                System.out.println("NodeRequest2 factory for "  + result + " -> " + factory);\r
97 //                            }\r
98 //                        });\r
99 \r
100                         graph.asyncRequest(new GetElementClassRequest(factory, data, canvas, diagram), new TransientCacheAsyncListener<ElementClass>() {\r
101 \r
102                             @Override\r
103                             public void exception(AsyncReadGraph graph, Throwable throwable) {\r
104                                 throwable.printStackTrace();\r
105                                 procedure.execute(graph, null);\r
106                             }\r
107 \r
108                             @Override\r
109                             public void execute(AsyncReadGraph graph, final ElementClass clazz) {\r
110 \r
111                                 graph.asyncRequest(new SpawnRequest(canvas, clazz, data), new TransientCacheAsyncListener<IElement>() {\r
112 \r
113                                     @Override\r
114                                     public void exception(AsyncReadGraph graph, Throwable throwable) {\r
115                                         throwable.printStackTrace();\r
116                                         procedure.execute(graph, null);\r
117                                     }\r
118 \r
119                                     @Override\r
120                                     public void execute(AsyncReadGraph graph, IElement element) {\r
121                                         procedure.execute(graph, element);\r
122 \r
123                                         if (loadListener != null) {\r
124                                             //System.out.println("LoadRequest[" + (loadCounter++) + "] for " + data + ": " + element);\r
125                                             graph.asyncRequest(new LoadRequest(canvas, diagram, factory, clazz, data), loadListener);\r
126                                         } else {\r
127                                             //System.out.println("Spawn[" + (spawnCounter++) + "] for " + data + ": " + element);\r
128                                             factory.load(graph, canvas, diagram, data, element, new AsyncProcedure<IElement>() {\r
129                                                 @Override\r
130                                                 public void exception(AsyncReadGraph graph, Throwable throwable) {\r
131                                                     // TODO: proper logging\r
132                                                     throwable.printStackTrace();\r
133                                                 }\r
134                                                 @Override\r
135                                                 public void execute(AsyncReadGraph graph, IElement result) {\r
136                                                     // Loading complete, don't care.\r
137                                                 }\r
138                                             });\r
139                                         }\r
140                                     }\r
141 \r
142                                 });\r
143 \r
144                             }\r
145 \r
146                         });\r
147 \r
148 //                        graph.asyncRequest(new SafeName(data), new Procedure<String>() {\r
149 //                            @Override\r
150 //                            public void exception(Throwable t) {\r
151 //                            }\r
152 //                            @Override\r
153 //                            public void execute(String result) {\r
154 //                                System.out.println("NodeRequest2 factory "  + result + " " + factory.getClass().getName());\r
155 //                            }\r
156 //                        });\r
157 \r
158                     }\r
159 \r
160                 });\r
161 \r
162             }\r
163 \r
164         });\r
165 \r
166     }\r
167 \r
168     static int loadCounter = 0;\r
169     static int spawnCounter = 0;\r
170 \r
171 }\r