]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
e7bb25f474435b1a19104de941f85af2e27a7292
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010 Association for Decentralized Information Management in\r
3  * 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.sysdyn.ui.elements2.connections;\r
13 \r
14 import java.util.HashMap;\r
15 import java.util.concurrent.ConcurrentSkipListMap;\r
16 import java.util.concurrent.atomic.AtomicInteger;\r
17 \r
18 import org.simantics.databoard.Bindings;\r
19 import org.simantics.db.AsyncReadGraph;\r
20 import org.simantics.db.Resource;\r
21 import org.simantics.db.exception.DatabaseException;\r
22 import org.simantics.db.procedure.AsyncMultiProcedure;\r
23 import org.simantics.db.procedure.AsyncProcedure;\r
24 import org.simantics.diagram.adapter.ElementFactoryAdapter;\r
25 import org.simantics.diagram.stubs.DiagramResource;\r
26 import org.simantics.g2d.canvas.ICanvasContext;\r
27 import org.simantics.g2d.diagram.DiagramHints;\r
28 import org.simantics.g2d.diagram.IDiagram;\r
29 import org.simantics.g2d.element.ElementClass;\r
30 import org.simantics.g2d.element.IElement;\r
31 import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;\r
32 import org.simantics.layer0.Layer0;\r
33 import org.simantics.modeling.ModelingResources;\r
34 import org.simantics.sysdyn.SysdynResource;\r
35 import org.simantics.sysdyn.ui.editor.routing.DependencyRouter;\r
36 import org.simantics.utils.datastructures.Pair;\r
37 \r
38 /**\r
39  * An element class for single connection entity elements. A connection entity\r
40  * consists of connection edge segments and branch points as its children.\r
41  * \r
42  * @author Tuukka Lehtonen\r
43  */\r
44 public class DependencyConnectionFactory extends ElementFactoryAdapter {\r
45 \r
46     public static final ElementClass CLASS = SysdynConnectionClass.CLASS;\r
47 \r
48     @Override\r
49     public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType, final AsyncProcedure<ElementClass> procedure) {\r
50         DiagramResource dr = graph.getService(DiagramResource.class);\r
51         graph.forSingleType(elementType, dr.Connection, new AsyncProcedure<Resource>() {\r
52             @Override\r
53             public void exception(AsyncReadGraph graph, Throwable throwable) {\r
54                 procedure.exception(graph, throwable);\r
55             }\r
56             @Override\r
57             public void execute(AsyncReadGraph graph, Resource connectionType) {\r
58                 procedure.execute(graph, SysdynConnectionClass.CLASS.newClassWith(false, new StaticObjectAdapter(connectionType)));\r
59             }\r
60         });\r
61     }\r
62 \r
63     @Override\r
64     public void load(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, final Resource elementResource,\r
65             final IElement element, final AsyncProcedure<IElement> procedure) {\r
66         \r
67         final AtomicInteger ready = new AtomicInteger(1);\r
68         final ConcurrentSkipListMap<String, Pair<Resource, Object>> properties = new ConcurrentSkipListMap<String, Pair<Resource, Object>>();\r
69         \r
70         element.setHint(DiagramHints.ROUTE_ALGORITHM, new DependencyRouter());\r
71 \r
72         graph.forEachPredicate(elementResource, new AsyncMultiProcedure<Resource>() {\r
73 \r
74                         @Override\r
75                         public void exception(AsyncReadGraph graph, Throwable throwable) {\r
76                                 throwable.printStackTrace();\r
77                         }\r
78 \r
79                         @Override\r
80                         public void execute(AsyncReadGraph graph, final Resource property) {\r
81                                 \r
82                                 ready.incrementAndGet();\r
83                                 Layer0 l0;\r
84                                 ModelingResources mr;\r
85                                 try {\r
86                                         l0 = Layer0.getInstance(graph.getSession());\r
87                                         mr = ModelingResources.getInstance(graph.getSession());\r
88                                 } catch (DatabaseException e) {\r
89                                         e.printStackTrace();\r
90                                         return;\r
91                                 }\r
92                                 graph.forIsSubrelationOf(property, l0.HasProperty, new AsyncProcedure<Boolean>() {\r
93 \r
94                                         @Override\r
95                                         public void exception(AsyncReadGraph graph, Throwable throwable) {\r
96                                                 throwable.printStackTrace();\r
97                                         }\r
98 \r
99                                         @Override\r
100                                         public void execute(AsyncReadGraph graph, final Boolean isProperty) {\r
101                                                 \r
102                                                 if(isProperty) {\r
103                                                         \r
104                                                         graph.forPossibleRelatedValue(elementResource, property, new AsyncProcedure<Object>() {\r
105 \r
106                                                                 @Override\r
107                                                                 public void exception(AsyncReadGraph graph, Throwable throwable) {\r
108                                                                         throwable.printStackTrace();\r
109                                                                 }\r
110 \r
111                                                                 @Override\r
112                                                                 public void execute(AsyncReadGraph graph, final Object value) {\r
113 \r
114                                                                         Layer0 l0;\r
115                                                                         try {\r
116                                                                                 l0 = Layer0.getInstance(graph.getSession());\r
117                                                                         } catch (DatabaseException e) {\r
118                                                                                 e.printStackTrace();\r
119                                                                                 return;\r
120                                                                         }\r
121                                                                         \r
122                                                                         graph.forPossibleRelatedValue(property, l0.HasName, Bindings.STRING, new AsyncProcedure<String>() {\r
123 \r
124                                                                                 @Override\r
125                                                                                 public void exception(AsyncReadGraph graph, Throwable throwable) {\r
126                                                                                         throwable.printStackTrace();\r
127                                                                                 }\r
128 \r
129                                                                                 @Override\r
130                                                                                 public void execute(AsyncReadGraph graph, String name) {\r
131                                                                                         \r
132                                                                                         properties.put(name, Pair.make(property, value));\r
133 //                                                                                      System.out.println("load properties " + name + " => " + value);\r
134                                                                                         if(ready.decrementAndGet() == 0) {\r
135                                                                                         element.setHint(DiagramHints.PROPERTIES, new HashMap<String, Pair<Resource, Object>>(properties));\r
136                                                                                         procedure.execute(graph, element);\r
137                                                                                         }\r
138                                                                                         \r
139                                                                                 }\r
140                                                                                 \r
141                                                                         });\r
142                                                                         \r
143                                                                 }\r
144                                                                 \r
145                                                         });\r
146                                                         \r
147                                                         \r
148                                                 } else {\r
149 \r
150                                                         if(ready.decrementAndGet() == 0) {\r
151                                                         element.setHint(DiagramHints.PROPERTIES, new HashMap<String, Pair<Resource, Object>>(properties));\r
152                                                         procedure.execute(graph, element);\r
153                                                         }\r
154                                                         \r
155                                                 }\r
156                                                 \r
157                                         }\r
158                                         \r
159                                 });\r
160                                 \r
161                                 \r
162 //                              graph.forPossibleObject(elementResource, mr.DiagramConnectionToConnection,  new AsyncProcedure<Resource>() {\r
163 //\r
164 //                    @Override\r
165 //                    public void execute(AsyncReadGraph graph, Resource result) {\r
166 //                        SysdynResource sr;\r
167 //                        try {\r
168 //                            sr = SysdynResource.getInstance(graph.getSession());\r
169 //                        } catch (DatabaseException e) {\r
170 //                            e.printStackTrace();\r
171 //                            return;\r
172 //                        }\r
173 //                        graph.forPossibleRelatedValue(result, sr.Polarity, Bindings.STRING, new AsyncProcedure<String>() {\r
174 //\r
175 //                            @Override\r
176 //                            public void exception(AsyncReadGraph graph, Throwable throwable) {\r
177 //                                throwable.printStackTrace();\r
178 //                            }\r
179 //\r
180 //                            @Override\r
181 //                            public void execute(AsyncReadGraph graph, String name) {\r
182 //                                \r
183 //                                properties.put("polarity", Pair.make(property, (Object)name));\r
184 ////                              System.out.println("load properties " + name + " => " + value);\r
185 //                                if(ready.decrementAndGet() == 0) {\r
186 //                                    element.setHint(DiagramHints.PROPERTIES, new HashMap<String, Pair<Resource, Object>>(properties));\r
187 //                                    procedure.execute(graph, element);\r
188 //                                }\r
189 //                                \r
190 //                            }\r
191 //                            \r
192 //                        });\r
193 //                        \r
194 //                    }\r
195 //\r
196 //                    @Override\r
197 //                    public void exception(AsyncReadGraph graph, Throwable throwable) {\r
198 //                        throwable.printStackTrace();                        \r
199 //                    }\r
200 //\r
201 //                              });\r
202                                 \r
203                         }\r
204 \r
205                         @Override\r
206                         public void finished(AsyncReadGraph graph) {\r
207                                 \r
208                                 if(ready.decrementAndGet() == 0) {\r
209                                 element.setHint(DiagramHints.PROPERTIES, new HashMap<String, Object>(properties));\r
210                                 procedure.execute(graph, element);\r
211                                 }\r
212                                 \r
213                         }\r
214                 \r
215         });\r
216         \r
217     }\r
218 \r
219 }\r