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