]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/DefaultConnectionClassFactory.java
Logger fixes after merge commit:fdbe8762
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / adapter / DefaultConnectionClassFactory.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in 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.diagram.adapter;\r
13 \r
14 import org.simantics.db.AsyncReadGraph;\r
15 import org.simantics.db.Resource;\r
16 import org.simantics.db.common.procedure.guarded.GuardedAsyncProcedureWrapper;\r
17 import org.simantics.db.procedure.AsyncProcedure;\r
18 import org.simantics.diagram.stubs.DiagramResource;\r
19 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;\r
20 import org.simantics.g2d.canvas.ICanvasContext;\r
21 import org.simantics.g2d.diagram.DiagramHints;\r
22 import org.simantics.g2d.diagram.IDiagram;\r
23 import org.simantics.g2d.element.ElementClass;\r
24 import org.simantics.g2d.element.IElement;\r
25 import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;\r
26 import org.simantics.g2d.elementclass.connection.ConnectionClass;\r
27 import org.simantics.g2d.routing.IRouter2;\r
28 import org.simantics.structural.stubs.StructuralResource2;\r
29 \r
30 /**\r
31  * An element class for single connection entity elements. A connection entity\r
32  * consists of connection edge segments and branch points as its children.\r
33  * \r
34  * @author Tuukka Lehtonen\r
35  */\r
36 public class DefaultConnectionClassFactory extends ElementFactoryAdapter {\r
37 \r
38     public static final ElementClass CLASS = ConnectionClass.CLASS;\r
39 \r
40     @Override\r
41     public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType, final AsyncProcedure<ElementClass> procedure) {\r
42         procedure.execute(graph, ConnectionClass.CLASS.newClassWith(false, new StaticObjectAdapter(elementType)));\r
43     }\r
44 \r
45     @Override\r
46     protected Resource getElementClassBaseType(AsyncReadGraph graph) {\r
47         return graph.getService(DiagramResource.class).Connection;\r
48     }\r
49 \r
50     @Override\r
51     public void load(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, final Resource elementResource,\r
52             final IElement element, final AsyncProcedure<IElement> procedure) {\r
53         final GuardedAsyncProcedureWrapper<IElement> guard = new GuardedAsyncProcedureWrapper<IElement>(procedure, 1);\r
54 \r
55         // Get custom routing algorithm for connection if necessary.\r
56         DiagramGraphUtil.getPossibleRouter(graph, elementResource, new AsyncProcedure<IRouter2>() {\r
57             @Override\r
58             public void exception(AsyncReadGraph graph, Throwable throwable) {\r
59                 guard.exception(graph, throwable);\r
60             }\r
61             @Override\r
62             public void execute(AsyncReadGraph graph, IRouter2 router) {\r
63                 if (router != null) {\r
64                     element.setHint(DiagramHints.ROUTE_ALGORITHM, router);\r
65                     procedure.execute(graph, element);\r
66                 } else {\r
67                     loadConnectionTypeDefaultRouting(graph, element, elementResource, guard);\r
68                 }\r
69             }\r
70         });\r
71     }\r
72 \r
73     protected void loadConnectionTypeDefaultRouting(AsyncReadGraph graph, final IElement element, Resource elementResource, final AsyncProcedure<IElement> guard) {\r
74         StructuralResource2 STR = graph.getService(StructuralResource2.class);\r
75         graph.forPossibleObject(elementResource, STR.HasConnectionType, new AsyncProcedure<Resource>() {\r
76             @Override\r
77             public void exception(AsyncReadGraph graph, Throwable throwable) {\r
78                 guard.exception(graph, throwable);\r
79             }\r
80             @Override\r
81             public void execute(AsyncReadGraph graph, Resource connectionType) {\r
82                 DiagramResource DIA = graph.getService(DiagramResource.class);\r
83                 if (connectionType != null) {\r
84                     graph.forPossibleObject(connectionType, DIA.HasDefaultRouting, new AsyncProcedure<Resource>() {\r
85                         @Override\r
86                         public void exception(AsyncReadGraph graph, Throwable throwable) {\r
87                             guard.exception(graph, throwable);\r
88                         }\r
89                         @Override\r
90                         public void execute(AsyncReadGraph graph, Resource routing) {\r
91                             if (routing != null) {\r
92                                 graph.forPossibleAdapted(routing, IRouter2.class, new AsyncProcedure<IRouter2>() {\r
93                                     @Override\r
94                                     public void exception(AsyncReadGraph graph, Throwable throwable) {\r
95                                         guard.exception(graph, throwable);\r
96                                     }\r
97                                     @Override\r
98                                     public void execute(AsyncReadGraph graph, IRouter2 router) {\r
99                                         if (router != null)\r
100                                             element.setHint(DiagramHints.ROUTE_ALGORITHM, router);\r
101                                         guard.execute(graph, element);\r
102                                     }\r
103                                 });\r
104                             } else {\r
105                                 guard.execute(graph, element);\r
106                             }\r
107                         }\r
108                     });\r
109                 } else {\r
110                     guard.execute(graph, element);\r
111                 }\r
112             }\r
113         });\r
114     }\r
115 \r
116 }\r