]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
fcfb5445929e89de715b49dcb441a5915f28ca5d
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010, 2012 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.elements.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.ReadGraph;\r
21 import org.simantics.db.Resource;\r
22 import org.simantics.db.exception.DatabaseException;\r
23 import org.simantics.db.procedure.AsyncMultiProcedure;\r
24 import org.simantics.db.procedure.AsyncProcedure;\r
25 import org.simantics.db.procedure.SyncProcedure;\r
26 import org.simantics.diagram.G2DUtils;\r
27 import org.simantics.diagram.adapter.ElementFactoryAdapter;\r
28 import org.simantics.diagram.stubs.DiagramResource;\r
29 import org.simantics.diagram.stubs.G2DResource;\r
30 import org.simantics.g2d.canvas.ICanvasContext;\r
31 import org.simantics.g2d.diagram.DiagramHints;\r
32 import org.simantics.g2d.diagram.IDiagram;\r
33 import org.simantics.g2d.element.ElementClass;\r
34 import org.simantics.g2d.element.ElementHints;\r
35 import org.simantics.g2d.element.IElement;\r
36 import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;\r
37 import org.simantics.layer0.Layer0;\r
38 import org.simantics.sysdyn.ui.editor.routing.DependencyRouter;\r
39 import org.simantics.utils.datastructures.Pair;\r
40 \r
41 /**\r
42  * An element class for single connection entity elements. A connection entity\r
43  * consists of connection edge segments and branch points as its children.\r
44  * \r
45  * @author Tuukka Lehtonen\r
46  */\r
47 public class DependencyConnectionFactory extends ElementFactoryAdapter {\r
48 \r
49     public static final ElementClass CLASS = SysdynConnectionClass.CLASS;\r
50 \r
51     @Override\r
52     public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType, final AsyncProcedure<ElementClass> procedure) {\r
53         procedure.execute(graph, SysdynConnectionClass.CLASS.newClassWith(false, new StaticObjectAdapter(elementType)));\r
54     }\r
55 \r
56     @Override\r
57     protected Resource getElementClassBaseType(AsyncReadGraph graph) {\r
58         return graph.getService(DiagramResource.class).Connection;\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, DependencyRouter.INSTANCE);\r
69         \r
70         G2DResource G2D;\r
71         try {\r
72             G2D = G2DResource.getInstance(graph.getSession());\r
73         } catch (DatabaseException e) {\r
74             e.printStackTrace();\r
75             return;\r
76         }\r
77         \r
78         // Find possible font\r
79         graph.forPossibleObject(elementResource, G2D.HasFont, new SyncProcedure<Resource>() {\r
80 \r
81             @Override\r
82             public void execute(ReadGraph graph, Resource result) throws DatabaseException {\r
83                 if(result != null) {\r
84                     element.setHint(ElementHints.KEY_FONT, G2DUtils.getFont(graph, result));\r
85                 }\r
86             }\r
87 \r
88             @Override\r
89             public void exception(ReadGraph graph, Throwable throwable) throws DatabaseException {\r
90                 throwable.printStackTrace();\r
91             }\r
92         });\r
93         \r
94         // Find possible color\r
95         graph.forPossibleObject(elementResource, G2D.HasColor, new SyncProcedure<Resource>() {\r
96 \r
97             @Override\r
98             public void execute(ReadGraph graph, Resource result) throws DatabaseException {\r
99                 if(result != null) {\r
100                     element.setHint(ElementHints.KEY_TEXT_COLOR, G2DUtils.getColor(graph, result));\r
101                 }\r
102             }\r
103 \r
104             @Override\r
105             public void exception(ReadGraph graph, Throwable throwable) throws DatabaseException {\r
106                 throwable.printStackTrace();\r
107             }\r
108         });\r
109         \r
110 \r
111         // A complicated-looking procedure for obtaining all HasProperties to properties map\r
112         graph.forEachPredicate(elementResource, new AsyncMultiProcedure<Resource>() {\r
113 \r
114                         @Override\r
115                         public void exception(AsyncReadGraph graph, Throwable throwable) {\r
116                                 throwable.printStackTrace();\r
117                         }\r
118 \r
119                         @Override\r
120                         public void execute(AsyncReadGraph graph, final Resource property) {\r
121                                 \r
122                                 ready.incrementAndGet();\r
123                                 Layer0 l0;\r
124                                 try {\r
125                                         l0 = Layer0.getInstance(graph.getSession());\r
126                                 } catch (DatabaseException e) {\r
127                                         e.printStackTrace();\r
128                                         return;\r
129                                 }\r
130                                 \r
131                                 graph.forIsSubrelationOf(property, l0.HasProperty, new AsyncProcedure<Boolean>() {\r
132 \r
133                                         @Override\r
134                                         public void exception(AsyncReadGraph graph, Throwable throwable) {\r
135                                                 throwable.printStackTrace();\r
136                                         }\r
137 \r
138                                         @Override\r
139                                         public void execute(AsyncReadGraph graph, final Boolean isProperty) {\r
140                                                 \r
141                                                 if(isProperty) {\r
142                                                         \r
143                                                         graph.forPossibleRelatedValue(elementResource, property, new AsyncProcedure<Object>() {\r
144 \r
145                                                                 @Override\r
146                                                                 public void exception(AsyncReadGraph graph, Throwable throwable) {\r
147                                                                         throwable.printStackTrace();\r
148                                                                 }\r
149 \r
150                                                                 @Override\r
151                                                                 public void execute(AsyncReadGraph graph, final Object value) {\r
152 \r
153                                                                         Layer0 l0;\r
154                                                                         try {\r
155                                                                                 l0 = Layer0.getInstance(graph.getSession());\r
156                                                                         } catch (DatabaseException e) {\r
157                                                                                 e.printStackTrace();\r
158                                                                                 return;\r
159                                                                         }\r
160                                                                         \r
161                                                                         graph.forPossibleRelatedValue(property, l0.HasName, Bindings.STRING, new AsyncProcedure<String>() {\r
162 \r
163                                                                                 @Override\r
164                                                                                 public void exception(AsyncReadGraph graph, Throwable throwable) {\r
165                                                                                         throwable.printStackTrace();\r
166                                                                                 }\r
167 \r
168                                                                                 @Override\r
169                                                                                 public void execute(AsyncReadGraph graph, String name) {\r
170                                                                                         \r
171                                                                                         properties.put(name, Pair.make(property, value));\r
172                                                                                         if(ready.decrementAndGet() == 0) {\r
173                                                                                         element.setHint(DiagramHints.PROPERTIES, new HashMap<String, Pair<Resource, Object>>(properties));\r
174                                                                                         procedure.execute(graph, element);\r
175                                                                                         }\r
176                                                                                         \r
177                                                                                 }\r
178                                                                                 \r
179                                                                         });\r
180                                                                         \r
181                                                                 }\r
182                                                                 \r
183                                                         });\r
184                                                         \r
185                                                         \r
186                                                 } else {\r
187 \r
188                                                         if(ready.decrementAndGet() == 0) {\r
189                                                         element.setHint(DiagramHints.PROPERTIES, new HashMap<String, Pair<Resource, Object>>(properties));\r
190                                                         procedure.execute(graph, element);\r
191                                                         }\r
192                                                         \r
193                                                 }\r
194                                                 \r
195                                         }\r
196                                         \r
197                                 });\r
198                         }\r
199 \r
200                         @Override\r
201                         public void finished(AsyncReadGraph graph) {\r
202                                 \r
203                                 if(ready.decrementAndGet() == 0) {\r
204                                 element.setHint(DiagramHints.PROPERTIES, new HashMap<String, Object>(properties));\r
205                                 procedure.execute(graph, element);\r
206                                 }\r
207                                 \r
208                         }\r
209                 \r
210         });\r
211         \r
212     }\r
213 \r
214 }\r