]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
3f757a08a7608122d17df99c1d51bb89e504338f
[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.awt.Color;\r
15 import java.awt.Font;\r
16 import java.util.HashMap;\r
17 import java.util.concurrent.ConcurrentSkipListMap;\r
18 import java.util.concurrent.atomic.AtomicInteger;\r
19 \r
20 import org.eclipse.jface.preference.PreferenceConverter;\r
21 import org.eclipse.jface.resource.StringConverter;\r
22 import org.eclipse.swt.graphics.FontData;\r
23 import org.eclipse.swt.graphics.RGB;\r
24 import org.simantics.databoard.Bindings;\r
25 import org.simantics.db.AsyncReadGraph;\r
26 import org.simantics.db.ReadGraph;\r
27 import org.simantics.db.Resource;\r
28 import org.simantics.db.Statement;\r
29 import org.simantics.db.exception.DatabaseException;\r
30 import org.simantics.db.procedure.AsyncMultiProcedure;\r
31 import org.simantics.db.procedure.AsyncProcedure;\r
32 import org.simantics.db.procedure.SyncProcedure;\r
33 import org.simantics.diagram.G2DUtils;\r
34 import org.simantics.diagram.adapter.ElementFactoryAdapter;\r
35 import org.simantics.diagram.stubs.DiagramResource;\r
36 import org.simantics.diagram.stubs.G2DResource;\r
37 import org.simantics.g2d.canvas.ICanvasContext;\r
38 import org.simantics.g2d.diagram.DiagramHints;\r
39 import org.simantics.g2d.diagram.IDiagram;\r
40 import org.simantics.g2d.element.ElementClass;\r
41 import org.simantics.g2d.element.ElementHints;\r
42 import org.simantics.g2d.element.IElement;\r
43 import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;\r
44 import org.simantics.layer0.Layer0;\r
45 import org.simantics.sysdyn.ui.editor.routing.DependencyRouter;\r
46 import org.simantics.sysdyn.ui.preferences.SysdynDiagramPreferences;\r
47 import org.simantics.sysdyn.ui.preferences.SysdynDiagramPropertyExternalRead;\r
48 import org.simantics.utils.datastructures.Pair;\r
49 \r
50 /**\r
51  * An element class for single connection entity elements. A connection entity\r
52  * consists of connection edge segments and branch points as its children.\r
53  * \r
54  * @author Tuukka Lehtonen\r
55  */\r
56 public class DependencyConnectionFactory extends ElementFactoryAdapter {\r
57 \r
58     public static final ElementClass CLASS = SysdynConnectionClass.CLASS;\r
59 \r
60     @Override\r
61     public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType, final AsyncProcedure<ElementClass> procedure) {\r
62         procedure.execute(graph, SysdynConnectionClass.CLASS.newClassWith(false, new StaticObjectAdapter(elementType)));\r
63     }\r
64 \r
65     @Override\r
66     protected Resource getElementClassBaseType(AsyncReadGraph graph) {\r
67         return graph.getService(DiagramResource.class).Connection;\r
68     }\r
69 \r
70     @Override\r
71     public void load(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, final Resource elementResource,\r
72             final IElement element, final AsyncProcedure<IElement> procedure) {\r
73 \r
74         final AtomicInteger ready = new AtomicInteger(1);\r
75         final ConcurrentSkipListMap<String, Pair<Resource, Object>> properties = new ConcurrentSkipListMap<String, Pair<Resource, Object>>();\r
76 \r
77         element.setHint(DiagramHints.ROUTE_ALGORITHM, DependencyRouter.INSTANCE);\r
78 \r
79         G2DResource G2D;\r
80         try {\r
81             G2D = G2DResource.getInstance(graph.getSession());\r
82         } catch (DatabaseException e) {\r
83             e.printStackTrace();\r
84             return;\r
85         }\r
86 \r
87         // Find possible font\r
88         graph.forPossibleStatement(elementResource, G2D.HasFont, new SyncProcedure<Statement>() {\r
89 \r
90             @Override\r
91             public void execute(ReadGraph graph, Statement result) throws DatabaseException {\r
92                 if(result != null && !result.isAsserted(elementResource)) {\r
93                     element.setHint(ElementHints.KEY_FONT, G2DUtils.getFont(graph, result.getObject()));\r
94                 } else {\r
95                     String fontdata = graph.syncRequest(new SysdynDiagramPropertyExternalRead(new Pair<Resource, String>(elementResource, SysdynDiagramPreferences.ARROW_FONT)));\r
96                     if(fontdata != null) {\r
97                         FontData[] fdArray = PreferenceConverter.basicGetFontData(fontdata);\r
98                         if(fdArray != null) {\r
99                             if(fdArray.length == 1) {\r
100                                 FontData fd = fdArray[0];\r
101                                 if(fd != null) {\r
102                                     Font font = new Font(fd.getName(), fd.getStyle(), fd.getHeight());\r
103                                     element.setHint(ElementHints.KEY_FONT, font);\r
104                                 }\r
105                             }\r
106                         }\r
107                     }\r
108 \r
109                 }\r
110             }\r
111 \r
112             @Override\r
113             public void exception(ReadGraph graph, Throwable throwable) throws DatabaseException {\r
114                 throwable.printStackTrace();\r
115             }\r
116         });\r
117 \r
118         // Find possible color\r
119         graph.forPossibleStatement(elementResource, G2D.HasColor, new SyncProcedure<Statement>() {\r
120 \r
121             @Override\r
122             public void execute(ReadGraph graph, Statement result) throws DatabaseException {\r
123                 if(result != null && !result.isAsserted(elementResource)) {\r
124                     element.setHint(ElementHints.KEY_TEXT_COLOR, G2DUtils.getColor(graph, result.getObject()));\r
125                 } else {\r
126                     String color = graph.syncRequest(new SysdynDiagramPropertyExternalRead(new Pair<Resource, String>(elementResource, SysdynDiagramPreferences.ARROW_COLOR)));\r
127                     if(color != null) {\r
128                         RGB rgb = StringConverter.asRGB(color, null);\r
129                         if(rgb != null) {\r
130                             Color c = new Color(rgb.red, rgb.green, rgb.blue);\r
131                             element.setHint(ElementHints.KEY_TEXT_COLOR, c);\r
132                         }\r
133                     }\r
134 \r
135                 }\r
136             }\r
137 \r
138             @Override\r
139             public void exception(ReadGraph graph, Throwable throwable) throws DatabaseException {\r
140                 throwable.printStackTrace();\r
141             }\r
142         });\r
143 \r
144 \r
145         // A complicated-looking procedure for obtaining all HasProperties to properties map\r
146         graph.forEachPredicate(elementResource, new AsyncMultiProcedure<Resource>() {\r
147 \r
148             @Override\r
149             public void exception(AsyncReadGraph graph, Throwable throwable) {\r
150                 throwable.printStackTrace();\r
151             }\r
152 \r
153             @Override\r
154             public void execute(AsyncReadGraph graph, final Resource property) {\r
155 \r
156                 ready.incrementAndGet();\r
157                 Layer0 l0;\r
158                 try {\r
159                     l0 = Layer0.getInstance(graph.getSession());\r
160                 } catch (DatabaseException e) {\r
161                     e.printStackTrace();\r
162                     return;\r
163                 }\r
164 \r
165                 graph.forIsSubrelationOf(property, l0.HasProperty, new AsyncProcedure<Boolean>() {\r
166 \r
167                     @Override\r
168                     public void exception(AsyncReadGraph graph, Throwable throwable) {\r
169                         throwable.printStackTrace();\r
170                     }\r
171 \r
172                     @Override\r
173                     public void execute(AsyncReadGraph graph, final Boolean isProperty) {\r
174 \r
175                         if(isProperty) {\r
176 \r
177                             graph.forPossibleRelatedValue(elementResource, property, new AsyncProcedure<Object>() {\r
178 \r
179                                 @Override\r
180                                 public void exception(AsyncReadGraph graph, Throwable throwable) {\r
181                                     throwable.printStackTrace();\r
182                                 }\r
183 \r
184                                 @Override\r
185                                 public void execute(AsyncReadGraph graph, final Object value) {\r
186 \r
187                                     Layer0 l0;\r
188                                     try {\r
189                                         l0 = Layer0.getInstance(graph.getSession());\r
190                                     } catch (DatabaseException e) {\r
191                                         e.printStackTrace();\r
192                                         return;\r
193                                     }\r
194 \r
195                                     graph.forPossibleRelatedValue(property, l0.HasName, Bindings.STRING, new AsyncProcedure<String>() {\r
196 \r
197                                         @Override\r
198                                         public void exception(AsyncReadGraph graph, Throwable throwable) {\r
199                                             throwable.printStackTrace();\r
200                                         }\r
201 \r
202                                         @Override\r
203                                         public void execute(AsyncReadGraph graph, String name) {\r
204 \r
205                                             properties.put(name, Pair.make(property, value));\r
206                                             if(ready.decrementAndGet() == 0) {\r
207                                                 element.setHint(DiagramHints.PROPERTIES, new HashMap<String, Pair<Resource, Object>>(properties));\r
208                                                 procedure.execute(graph, element);\r
209                                             }\r
210 \r
211                                         }\r
212 \r
213                                     });\r
214 \r
215                                 }\r
216 \r
217                             });\r
218 \r
219 \r
220                         } else {\r
221 \r
222                             if(ready.decrementAndGet() == 0) {\r
223                                 element.setHint(DiagramHints.PROPERTIES, new HashMap<String, Pair<Resource, Object>>(properties));\r
224                                 procedure.execute(graph, element);\r
225                             }\r
226 \r
227                         }\r
228 \r
229                     }\r
230 \r
231                 });\r
232             }\r
233 \r
234             @Override\r
235             public void finished(AsyncReadGraph graph) {\r
236 \r
237                 if(ready.decrementAndGet() == 0) {\r
238                     element.setHint(DiagramHints.PROPERTIES, new HashMap<String, Object>(properties));\r
239                     procedure.execute(graph, element);\r
240                 }\r
241 \r
242             }\r
243 \r
244         });\r
245 \r
246     }\r
247 \r
248 }\r