+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.diagram.adapter;\r
+\r
+import org.simantics.db.AsyncReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.procedure.guarded.GuardedAsyncProcedureWrapper;\r
+import org.simantics.db.procedure.AsyncProcedure;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;\r
+import org.simantics.g2d.canvas.ICanvasContext;\r
+import org.simantics.g2d.diagram.DiagramHints;\r
+import org.simantics.g2d.diagram.IDiagram;\r
+import org.simantics.g2d.element.ElementClass;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;\r
+import org.simantics.g2d.elementclass.connection.ConnectionClass;\r
+import org.simantics.g2d.routing.IRouter2;\r
+import org.simantics.structural.stubs.StructuralResource2;\r
+\r
+/**\r
+ * An element class for single connection entity elements. A connection entity\r
+ * consists of connection edge segments and branch points as its children.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class DefaultConnectionClassFactory extends ElementFactoryAdapter {\r
+\r
+ public static final ElementClass CLASS = ConnectionClass.CLASS;\r
+\r
+ @Override\r
+ public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType, final AsyncProcedure<ElementClass> procedure) {\r
+ procedure.execute(graph, ConnectionClass.CLASS.newClassWith(false, new StaticObjectAdapter(elementType)));\r
+ }\r
+\r
+ @Override\r
+ protected Resource getElementClassBaseType(AsyncReadGraph graph) {\r
+ return graph.getService(DiagramResource.class).Connection;\r
+ }\r
+\r
+ @Override\r
+ public void load(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, final Resource elementResource,\r
+ final IElement element, final AsyncProcedure<IElement> procedure) {\r
+ final GuardedAsyncProcedureWrapper<IElement> guard = new GuardedAsyncProcedureWrapper<IElement>(procedure, 1);\r
+\r
+ // Get custom routing algorithm for connection if necessary.\r
+ DiagramGraphUtil.getPossibleRouter(graph, elementResource, new AsyncProcedure<IRouter2>() {\r
+ @Override\r
+ public void exception(AsyncReadGraph graph, Throwable throwable) {\r
+ guard.exception(graph, throwable);\r
+ }\r
+ @Override\r
+ public void execute(AsyncReadGraph graph, IRouter2 router) {\r
+ if (router != null) {\r
+ element.setHint(DiagramHints.ROUTE_ALGORITHM, router);\r
+ procedure.execute(graph, element);\r
+ } else {\r
+ loadConnectionTypeDefaultRouting(graph, element, elementResource, guard);\r
+ }\r
+ }\r
+ });\r
+ }\r
+\r
+ protected void loadConnectionTypeDefaultRouting(AsyncReadGraph graph, final IElement element, Resource elementResource, final AsyncProcedure<IElement> guard) {\r
+ StructuralResource2 STR = graph.getService(StructuralResource2.class);\r
+ graph.forPossibleObject(elementResource, STR.HasConnectionType, new AsyncProcedure<Resource>() {\r
+ @Override\r
+ public void exception(AsyncReadGraph graph, Throwable throwable) {\r
+ guard.exception(graph, throwable);\r
+ }\r
+ @Override\r
+ public void execute(AsyncReadGraph graph, Resource connectionType) {\r
+ DiagramResource DIA = graph.getService(DiagramResource.class);\r
+ if (connectionType != null) {\r
+ graph.forPossibleObject(connectionType, DIA.HasDefaultRouting, new AsyncProcedure<Resource>() {\r
+ @Override\r
+ public void exception(AsyncReadGraph graph, Throwable throwable) {\r
+ guard.exception(graph, throwable);\r
+ }\r
+ @Override\r
+ public void execute(AsyncReadGraph graph, Resource routing) {\r
+ if (routing != null) {\r
+ graph.forPossibleAdapted(routing, IRouter2.class, new AsyncProcedure<IRouter2>() {\r
+ @Override\r
+ public void exception(AsyncReadGraph graph, Throwable throwable) {\r
+ guard.exception(graph, throwable);\r
+ }\r
+ @Override\r
+ public void execute(AsyncReadGraph graph, IRouter2 router) {\r
+ if (router != null)\r
+ element.setHint(DiagramHints.ROUTE_ALGORITHM, router);\r
+ guard.execute(graph, element);\r
+ }\r
+ });\r
+ } else {\r
+ guard.execute(graph, element);\r
+ }\r
+ }\r
+ });\r
+ } else {\r
+ guard.execute(graph, element);\r
+ }\r
+ }\r
+ });\r
+ }\r
+\r
+}\r