]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/NodeRequest.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / adapter / NodeRequest.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.Resource;
16 import org.simantics.db.common.primitiverequest.Adapter;
17 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
18 import org.simantics.db.procedure.AsyncProcedure;
19 import org.simantics.db.procedure.Listener;
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.IElement;
25
26 /**
27  * @author Antti Villberg
28  */
29 public class NodeRequest extends BaseRequest2<Resource, IElement> {
30
31     final IDiagram diagram;
32     final Listener<IElement> loadListener;
33
34     public NodeRequest(ICanvasContext canvas, IDiagram diagram, Resource resource, Listener<IElement> loadListener) {
35         super(canvas, resource);
36         this.diagram = diagram;
37         this.loadListener = loadListener;
38     }
39
40     @Override
41     public void perform(AsyncReadGraph graph, final AsyncProcedure<IElement> procedure) {
42         // Keep this code from crashing the whole DB client by unexpected
43         // throwing of NPE's somewhere in the following code that leads to
44         // procedure not getting called properly.
45         if (diagram == null) {
46             procedure.exception(graph, new NullPointerException("null diagram specified for resource " + data));
47             return;
48         }
49
50 //        System.out.println("NodeRequest2 " + data);
51 //        graph.asyncRequest(new SafeName(data), new Procedure<String>() {
52 //            @Override
53 //            public void exception(Throwable t) {
54 //            }
55 //            @Override
56 //            public void execute(String result) {
57 //                System.out.println("NodeRequest2 "  + result);
58 //            }
59 //        });
60
61         final ErrorHandler eh = ElementFactoryUtil.getErrorHandler(diagram);
62
63         graph.forHasStatement(data, new AsyncProcedure<Boolean>() {
64
65             @Override
66             public void exception(AsyncReadGraph graph, Throwable throwable) {
67                 eh.error("NodeRequest.forHasStatement failed", throwable);
68                 procedure.execute(graph, null);
69             }
70
71             @Override
72             public void execute(AsyncReadGraph graph, Boolean result) {
73
74                 if(!result) {
75                     procedure.execute(graph, null);
76                     return;
77                 }
78
79                 graph.asyncRequest(new Adapter<ElementFactory>(data, ElementFactory.class), new TransientCacheAsyncListener<ElementFactory>() {
80
81                     @Override
82                     public void exception(AsyncReadGraph graph, Throwable throwable) {
83                         eh.error("NodeRequest.asyncRequest(Adapter<ElementFactory>) failed", throwable);
84                         procedure.execute(graph, null);
85                     }
86
87                     @Override
88                     public void execute(AsyncReadGraph graph, final ElementFactory factory) {
89
90 //                        graph.asyncRequest(new ResourceToURI(data), new Procedure<String>() {
91 //                            @Override
92 //                            public void exception(Throwable t) {
93 //                            }
94 //                            @Override
95 //                            public void execute(String result) {
96 //                                System.out.println("NodeRequest2 factory for "  + result + " -> " + factory);
97 //                            }
98 //                        });
99
100                         graph.asyncRequest(new GetElementClassRequest(factory, data, canvas, diagram), new TransientCacheAsyncListener<ElementClass>() {
101
102                             @Override
103                             public void exception(AsyncReadGraph graph, Throwable throwable) {
104                                 throwable.printStackTrace();
105                                 procedure.execute(graph, null);
106                             }
107
108                             @Override
109                             public void execute(AsyncReadGraph graph, final ElementClass clazz) {
110
111                                 graph.asyncRequest(new SpawnRequest(canvas, clazz, data), new TransientCacheAsyncListener<IElement>() {
112
113                                     @Override
114                                     public void exception(AsyncReadGraph graph, Throwable throwable) {
115                                         throwable.printStackTrace();
116                                         procedure.execute(graph, null);
117                                     }
118
119                                     @Override
120                                     public void execute(AsyncReadGraph graph, IElement element) {
121                                         procedure.execute(graph, element);
122
123                                         if (loadListener != null) {
124                                             //System.out.println("LoadRequest[" + (loadCounter++) + "] for " + data + ": " + element);
125                                             graph.asyncRequest(new LoadRequest(canvas, diagram, factory, clazz, data), loadListener);
126                                         } else {
127                                             //System.out.println("Spawn[" + (spawnCounter++) + "] for " + data + ": " + element);
128                                             factory.load(graph, canvas, diagram, data, element, new AsyncProcedure<IElement>() {
129                                                 @Override
130                                                 public void exception(AsyncReadGraph graph, Throwable throwable) {
131                                                     // TODO: proper logging
132                                                     throwable.printStackTrace();
133                                                 }
134                                                 @Override
135                                                 public void execute(AsyncReadGraph graph, IElement result) {
136                                                     // Loading complete, don't care.
137                                                 }
138                                             });
139                                         }
140                                     }
141
142                                 });
143
144                             }
145
146                         });
147
148 //                        graph.asyncRequest(new SafeName(data), new Procedure<String>() {
149 //                            @Override
150 //                            public void exception(Throwable t) {
151 //                            }
152 //                            @Override
153 //                            public void execute(String result) {
154 //                                System.out.println("NodeRequest2 factory "  + result + " " + factory.getClass().getName());
155 //                            }
156 //                        });
157
158                     }
159
160                 });
161
162             }
163
164         });
165
166     }
167
168     static int loadCounter = 0;
169     static int spawnCounter = 0;
170
171 }