--- /dev/null
+/*******************************************************************************\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
+\r
+import org.simantics.db.AsyncReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.procedure.adapter.AsyncProcedureAdapter;\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.SynchronizationHints;\r
+import org.simantics.diagram.synchronization.graph.TransformSynchronizer;\r
+import org.simantics.g2d.canvas.ICanvasContext;\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.BranchPoint;\r
+import org.simantics.g2d.elementclass.BranchPoint.Direction;\r
+import org.simantics.g2d.elementclass.BranchPointClass;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class BranchPointClassFactory extends ElementFactoryAdapter {\r
+\r
+ public static ElementClass create(Resource elementClass) {\r
+ return BranchPointClass.CLASS.newClassWith(false, new StaticObjectAdapter(elementClass));\r
+ }\r
+\r
+ @Override\r
+ public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType, AsyncProcedure<ElementClass> procedure) {\r
+ procedure.execute(graph, BranchPointClass.CLASS.newClassWith(false, new StaticObjectAdapter(elementType)));\r
+ }\r
+\r
+ @Override\r
+ public void load(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, final Resource element, final IElement e, final AsyncProcedure<IElement> procedure) {\r
+ e.setHint(SynchronizationHints.HINT_SYNCHRONIZER, TransformSynchronizer.INSTANCE);\r
+\r
+ final AsyncProcedure<IElement> guard = new GuardedAsyncProcedureWrapper<IElement>(procedure, 2);\r
+\r
+ final BranchPoint bp = e.getElementClass().getAtMostOneItemOfClass(BranchPoint.class);\r
+ if (bp != null) {\r
+ final DiagramResource dr = graph.getService(DiagramResource.class);\r
+ graph.forHasStatement(element, dr.Horizontal, element, new AsyncProcedureAdapter<Boolean>() {\r
+ @Override\r
+ public void exception(AsyncReadGraph graph, Throwable throwable) {\r
+ guard.exception(graph, throwable);\r
+ }\r
+ @Override\r
+ public void execute(AsyncReadGraph graph, final Boolean horizontal) {\r
+ graph.forHasStatement(element, dr.Vertical, element, new AsyncProcedure<Boolean>() {\r
+ @Override\r
+ public void exception(AsyncReadGraph graph, Throwable throwable) {\r
+ guard.exception(graph, throwable);\r
+ }\r
+ @Override\r
+ public void execute(AsyncReadGraph graph, Boolean vertical) {\r
+ Direction direction = Direction.Any;\r
+ if (horizontal ^ vertical)\r
+ direction = horizontal ? Direction.Horizontal : Direction.Vertical;\r
+ bp.setDirectionPreference(e, direction);\r
+\r
+ guard.execute(graph, e);\r
+ }\r
+ });\r
+ }\r
+ });\r
+ }\r
+\r
+ ElementFactoryUtil.readTransform(graph, element, e, guard);\r
+ }\r
+\r
+}
\ No newline at end of file