--- /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.g2d.element.handler.impl;\r
+\r
+import java.awt.Shape;\r
+import java.awt.geom.AffineTransform;\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+\r
+import org.simantics.g2d.diagram.handler.Topology.Terminal;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.g2d.element.handler.TerminalLayout;\r
+import org.simantics.g2d.element.handler.TerminalTopology;\r
+import org.simantics.g2d.utils.geom.DirectionSet;\r
+import org.simantics.utils.datastructures.ValueUtils;\r
+\r
+/**\r
+ * \r
+ * \r
+ * Example:\r
+ * \r
+ * TerminalHandler th = \r
+ * StaticTerminals.compile(\r
+ * topTerminal, 0.0, -10.0, DirectionSet.N,\r
+ * bottomTerminal, 0.0, 10.0, DirectionSet.S,\r
+ * leftTerminal, -10.0, 0.0, DirectionSet.W,\r
+ * rightTerminal, 20.0, 0.0, DirectionSet.E);\r
+ * \r
+ * @author Toni Kalajainen\r
+ */\r
+public class StaticTerminals implements TerminalLayout, TerminalTopology {\r
+\r
+ private static final long serialVersionUID = -7324592260579191478L;\r
+\r
+ /**\r
+ * Compile terminal handler with static terminals.\r
+ * The xytArray is an varargs array which consists of sequences of\r
+ * (terminal, x-position, y-position, DirectionSet).\r
+ * \r
+ * @param xytArray an array of (x, y, terminal) triples\r
+ * @return terminal reference\r
+ */\r
+ public static TerminalLayout compile(Object ... xytArray)\r
+ {\r
+ return new StaticTerminals(xytArray);\r
+ }\r
+ \r
+ private class TerminalImpl implements Terminal {\r
+ double x, y;\r
+ DirectionSet ds;\r
+ Terminal t;\r
+ }\r
+ \r
+ Map<Terminal, TerminalImpl> terminalMap = new HashMap<Terminal, TerminalImpl>();\r
+ ArrayList<Terminal> terminals = new ArrayList<Terminal>();\r
+ \r
+ StaticTerminals(Object ... xytArray)\r
+ {\r
+ assert(xytArray.length % 4 == 0);\r
+ for (int i=0; i<xytArray.length/4; i++)\r
+ {\r
+ TerminalImpl ti = new TerminalImpl();\r
+ ti.t = (Terminal) xytArray[i*4+0];\r
+ ti.x = ValueUtils.toDoubleScalar( xytArray[i*4+1] );\r
+ ti.y = ValueUtils.toDoubleScalar( xytArray[i*4+2] );\r
+ DirectionSet ds = (DirectionSet) xytArray[i*4+3];\r
+ ti.ds = ds;\r
+ terminals.add(ti.t);\r
+ terminalMap.put(ti.t, ti);\r
+ }\r
+ } \r
+\r
+ @Override\r
+ public AffineTransform getTerminalPosition(IElement node, Terminal t) {\r
+ TerminalImpl ti = terminalMap.get(t);\r
+ if (ti==null) return null;\r
+ AffineTransform result = new AffineTransform();\r
+ result.setToTranslation(ti.x, ti.y);\r
+ return result;\r
+ }\r
+\r
+ @Override\r
+ public void getTerminals(IElement e, Collection<Terminal> result) {\r
+ result.addAll(terminals);\r
+ }\r
+\r
+ @Override\r
+ public boolean getTerminalDirection(IElement node, Terminal t, DirectionSet directions) {\r
+ TerminalImpl ti = terminalMap.get(t);\r
+ if (ti==null) return false;\r
+ directions.addAll(ti.ds);\r
+ return true;\r
+ }\r
+\r
+ @Override\r
+ public Shape getTerminalShape(IElement node, Terminal t) {\r
+ return null;\r
+ }\r
+ \r
+}\r