]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
9ef4fa9ff9cd4375889fe1a35cbf688d9ca64d53
[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.sysdyn.ui.editor.routing.DependencyRouter;\r
34 import org.simantics.utils.datastructures.Pair;\r
35 \r
36 /**\r
37  * An element class for single connection entity elements. A connection entity\r
38  * consists of connection edge segments and branch points as its children.\r
39  * \r
40  * @author Tuukka Lehtonen\r
41  */\r
42 public class DependencyConnectionFactory extends ElementFactoryAdapter {\r
43 \r
44     public static final ElementClass CLASS = SysdynConnectionClass.CLASS;\r
45 \r
46     @Override\r
47     public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType, final AsyncProcedure<ElementClass> procedure) {\r
48         DiagramResource dr = graph.getService(DiagramResource.class);\r
49         graph.forSingleType(elementType, dr.Connection, new AsyncProcedure<Resource>() {\r
50             @Override\r
51             public void exception(AsyncReadGraph graph, Throwable throwable) {\r
52                 procedure.exception(graph, throwable);\r
53             }\r
54             @Override\r
55             public void execute(AsyncReadGraph graph, Resource connectionType) {\r
56                 procedure.execute(graph, SysdynConnectionClass.CLASS.newClassWith(false, new StaticObjectAdapter(connectionType)));\r
57             }\r
58         });\r
59     }\r
60 \r
61     @Override\r
62     public void load(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, final Resource elementResource,\r
63             final IElement element, final AsyncProcedure<IElement> procedure) {\r
64         \r
65         final AtomicInteger ready = new AtomicInteger(1);\r
66         final ConcurrentSkipListMap<String, Pair<Resource, Object>> properties = new ConcurrentSkipListMap<String, Pair<Resource, Object>>();\r
67         \r
68         element.setHint(DiagramHints.ROUTE_ALGORITHM, new DependencyRouter());\r
69 \r
70         graph.forEachPredicate(elementResource, new AsyncMultiProcedure<Resource>() {\r
71 \r
72                         @Override\r
73                         public void exception(AsyncReadGraph graph, Throwable throwable) {\r
74                                 throwable.printStackTrace();\r
75                         }\r
76 \r
77                         @Override\r
78                         public void execute(AsyncReadGraph graph, final Resource property) {\r
79                                 \r
80                                 ready.incrementAndGet();\r
81                                 Layer0 l0;\r
82                                 try {\r
83                                         l0 = Layer0.getInstance(graph.getSession());\r
84                                 } catch (DatabaseException e) {\r
85                                         e.printStackTrace();\r
86                                         return;\r
87                                 }\r
88                                 graph.forIsSubrelationOf(property, l0.HasProperty, new AsyncProcedure<Boolean>() {\r
89 \r
90                                         @Override\r
91                                         public void exception(AsyncReadGraph graph, Throwable throwable) {\r
92                                                 throwable.printStackTrace();\r
93                                         }\r
94 \r
95                                         @Override\r
96                                         public void execute(AsyncReadGraph graph, final Boolean isProperty) {\r
97                                                 \r
98                                                 if(isProperty) {\r
99                                                         \r
100                                                         graph.forPossibleRelatedValue(elementResource, property, new AsyncProcedure<Object>() {\r
101 \r
102                                                                 @Override\r
103                                                                 public void exception(AsyncReadGraph graph, Throwable throwable) {\r
104                                                                         throwable.printStackTrace();\r
105                                                                 }\r
106 \r
107                                                                 @Override\r
108                                                                 public void execute(AsyncReadGraph graph, final Object value) {\r
109 \r
110                                                                         Layer0 l0;\r
111                                                                         try {\r
112                                                                                 l0 = Layer0.getInstance(graph.getSession());\r
113                                                                         } catch (DatabaseException e) {\r
114                                                                                 e.printStackTrace();\r
115                                                                                 return;\r
116                                                                         }\r
117                                                                         \r
118                                                                         graph.forPossibleRelatedValue(property, l0.HasName, Bindings.STRING, new AsyncProcedure<String>() {\r
119 \r
120                                                                                 @Override\r
121                                                                                 public void exception(AsyncReadGraph graph, Throwable throwable) {\r
122                                                                                         throwable.printStackTrace();\r
123                                                                                 }\r
124 \r
125                                                                                 @Override\r
126                                                                                 public void execute(AsyncReadGraph graph, String name) {\r
127                                                                                         \r
128                                                                                         properties.put(name, Pair.make(property, value));\r
129 //                                                                                      System.out.println("load properties " + name + " => " + value);\r
130                                                                                         if(ready.decrementAndGet() == 0) {\r
131                                                                                         element.setHint(DiagramHints.PROPERTIES, new HashMap<String, Pair<Resource, Object>>(properties));\r
132                                                                                         procedure.execute(graph, element);\r
133                                                                                         }\r
134                                                                                         \r
135                                                                                 }\r
136                                                                                 \r
137                                                                         });\r
138                                                                         \r
139                                                                 }\r
140                                                                 \r
141                                                         });\r
142                                                         \r
143                                                         \r
144                                                 } else {\r
145 \r
146                                                         if(ready.decrementAndGet() == 0) {\r
147                                                         element.setHint(DiagramHints.PROPERTIES, new HashMap<String, Pair<Resource, Object>>(properties));\r
148                                                         procedure.execute(graph, element);\r
149                                                         }\r
150                                                         \r
151                                                 }\r
152                                                 \r
153                                         }\r
154                                         \r
155                                 });\r
156                                 \r
157                         }\r
158 \r
159                         @Override\r
160                         public void finished(AsyncReadGraph graph) {\r
161                                 \r
162                                 if(ready.decrementAndGet() == 0) {\r
163                                 element.setHint(DiagramHints.PROPERTIES, new HashMap<String, Object>(properties));\r
164                                 procedure.execute(graph, element);\r
165                                 }\r
166                                 \r
167                         }\r
168                 \r
169         });\r
170         \r
171     }\r
172 \r
173 }\r