]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/DefaultConnectionClassFactory.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / adapter / DefaultConnectionClassFactory.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.adapter;
13
14 import org.simantics.db.AsyncReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.common.procedure.guarded.GuardedAsyncProcedureWrapper;
17 import org.simantics.db.procedure.AsyncProcedure;
18 import org.simantics.diagram.stubs.DiagramResource;
19 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
20 import org.simantics.g2d.canvas.ICanvasContext;
21 import org.simantics.g2d.diagram.DiagramHints;
22 import org.simantics.g2d.diagram.IDiagram;
23 import org.simantics.g2d.element.ElementClass;
24 import org.simantics.g2d.element.IElement;
25 import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;
26 import org.simantics.g2d.elementclass.connection.ConnectionClass;
27 import org.simantics.g2d.routing.IRouter2;
28 import org.simantics.structural.stubs.StructuralResource2;
29
30 /**
31  * An element class for single connection entity elements. A connection entity
32  * consists of connection edge segments and branch points as its children.
33  * 
34  * @author Tuukka Lehtonen
35  */
36 public class DefaultConnectionClassFactory extends ElementFactoryAdapter {
37
38     public static final ElementClass CLASS = ConnectionClass.CLASS;
39
40     @Override
41     public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType, final AsyncProcedure<ElementClass> procedure) {
42         procedure.execute(graph, ConnectionClass.CLASS.newClassWith(false, new StaticObjectAdapter(elementType)));
43     }
44
45     @Override
46     protected Resource getElementClassBaseType(AsyncReadGraph graph) {
47         return graph.getService(DiagramResource.class).Connection;
48     }
49
50     @Override
51     public void load(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, final Resource elementResource,
52             final IElement element, final AsyncProcedure<IElement> procedure) {
53         final GuardedAsyncProcedureWrapper<IElement> guard = new GuardedAsyncProcedureWrapper<IElement>(procedure, 1);
54
55         // Get custom routing algorithm for connection if necessary.
56         DiagramGraphUtil.getPossibleRouter(graph, elementResource, new AsyncProcedure<IRouter2>() {
57             @Override
58             public void exception(AsyncReadGraph graph, Throwable throwable) {
59                 guard.exception(graph, throwable);
60             }
61             @Override
62             public void execute(AsyncReadGraph graph, IRouter2 router) {
63                 if (router != null) {
64                     element.setHint(DiagramHints.ROUTE_ALGORITHM, router);
65                     procedure.execute(graph, element);
66                 } else {
67                     loadConnectionTypeDefaultRouting(graph, element, elementResource, guard);
68                 }
69             }
70         });
71     }
72
73     protected void loadConnectionTypeDefaultRouting(AsyncReadGraph graph, final IElement element, Resource elementResource, final AsyncProcedure<IElement> guard) {
74         StructuralResource2 STR = graph.getService(StructuralResource2.class);
75         graph.forPossibleObject(elementResource, STR.HasConnectionType, new AsyncProcedure<Resource>() {
76             @Override
77             public void exception(AsyncReadGraph graph, Throwable throwable) {
78                 guard.exception(graph, throwable);
79             }
80             @Override
81             public void execute(AsyncReadGraph graph, Resource connectionType) {
82                 DiagramResource DIA = graph.getService(DiagramResource.class);
83                 if (connectionType != null) {
84                     graph.forPossibleObject(connectionType, DIA.HasDefaultRouting, new AsyncProcedure<Resource>() {
85                         @Override
86                         public void exception(AsyncReadGraph graph, Throwable throwable) {
87                             guard.exception(graph, throwable);
88                         }
89                         @Override
90                         public void execute(AsyncReadGraph graph, Resource routing) {
91                             if (routing != null) {
92                                 graph.forPossibleAdapted(routing, IRouter2.class, new AsyncProcedure<IRouter2>() {
93                                     @Override
94                                     public void exception(AsyncReadGraph graph, Throwable throwable) {
95                                         guard.exception(graph, throwable);
96                                     }
97                                     @Override
98                                     public void execute(AsyncReadGraph graph, IRouter2 router) {
99                                         if (router != null)
100                                             element.setHint(DiagramHints.ROUTE_ALGORITHM, router);
101                                         guard.execute(graph, element);
102                                     }
103                                 });
104                             } else {
105                                 guard.execute(graph, element);
106                             }
107                         }
108                     });
109                 } else {
110                     guard.execute(graph, element);
111                 }
112             }
113         });
114     }
115
116 }