]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/impl/StaticTerminals.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / element / handler / impl / StaticTerminals.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.g2d.element.handler.impl;\r
13 \r
14 import java.awt.Shape;\r
15 import java.awt.geom.AffineTransform;\r
16 import java.util.ArrayList;\r
17 import java.util.Collection;\r
18 import java.util.HashMap;\r
19 import java.util.Map;\r
20 \r
21 import org.simantics.g2d.diagram.handler.Topology.Terminal;\r
22 import org.simantics.g2d.element.IElement;\r
23 import org.simantics.g2d.element.handler.TerminalLayout;\r
24 import org.simantics.g2d.element.handler.TerminalTopology;\r
25 import org.simantics.g2d.utils.geom.DirectionSet;\r
26 import org.simantics.utils.datastructures.ValueUtils;\r
27 \r
28 /**\r
29  * \r
30  * \r
31  * Example:\r
32  * \r
33  *   TerminalHandler th = \r
34  *      StaticTerminals.compile(\r
35  *              topTerminal,    0.0, -10.0, DirectionSet.N,\r
36  *              bottomTerminal, 0.0,  10.0, DirectionSet.S,\r
37  *              leftTerminal,  -10.0,  0.0, DirectionSet.W,\r
38  *          rightTerminal,  20.0,  0.0, DirectionSet.E);\r
39  * \r
40  * @author Toni Kalajainen\r
41  */\r
42 public class StaticTerminals implements TerminalLayout, TerminalTopology {\r
43 \r
44     private static final long serialVersionUID = -7324592260579191478L;\r
45 \r
46     /**\r
47          * Compile terminal handler with static terminals.\r
48          * The xytArray is an varargs array which consists of sequences of\r
49          *  (terminal, x-position, y-position, DirectionSet).\r
50          * \r
51          * @param xytArray an array of (x, y, terminal) triples\r
52          * @return terminal reference\r
53          */\r
54         public static TerminalLayout compile(Object ... xytArray)\r
55         {\r
56                 return new StaticTerminals(xytArray);\r
57         }\r
58         \r
59         private class TerminalImpl implements Terminal {\r
60                 double x, y;\r
61                 DirectionSet ds;\r
62                 Terminal t;\r
63         }\r
64         \r
65         Map<Terminal, TerminalImpl> terminalMap = new HashMap<Terminal, TerminalImpl>();\r
66         ArrayList<Terminal> terminals = new ArrayList<Terminal>();\r
67         \r
68         StaticTerminals(Object ... xytArray)\r
69         {\r
70                 assert(xytArray.length % 4 == 0);\r
71                 for (int i=0; i<xytArray.length/4; i++)\r
72                 {\r
73                         TerminalImpl ti = new TerminalImpl();\r
74                         ti.t = (Terminal) xytArray[i*4+0];\r
75                         ti.x = ValueUtils.toDoubleScalar( xytArray[i*4+1] );\r
76                         ti.y = ValueUtils.toDoubleScalar( xytArray[i*4+2] );\r
77                         DirectionSet ds = (DirectionSet) xytArray[i*4+3];\r
78                         ti.ds = ds;\r
79                         terminals.add(ti.t);\r
80                         terminalMap.put(ti.t, ti);\r
81                 }\r
82         }       \r
83 \r
84         @Override\r
85         public AffineTransform getTerminalPosition(IElement node, Terminal t) {\r
86                 TerminalImpl ti = terminalMap.get(t);\r
87                 if (ti==null) return null;\r
88                 AffineTransform result = new AffineTransform();\r
89                 result.setToTranslation(ti.x, ti.y);\r
90                 return result;\r
91         }\r
92 \r
93         @Override\r
94         public void getTerminals(IElement e, Collection<Terminal> result) {\r
95                 result.addAll(terminals);\r
96         }\r
97 \r
98         @Override\r
99         public boolean getTerminalDirection(IElement node, Terminal t, DirectionSet directions) {\r
100                 TerminalImpl ti = terminalMap.get(t);\r
101                 if (ti==null) return false;\r
102                 directions.addAll(ti.ds);\r
103                 return true;\r
104         }\r
105 \r
106         @Override\r
107         public Shape getTerminalShape(IElement node, Terminal t) {\r
108                 return null;\r
109         }\r
110         \r
111 }\r