1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g2d.element.handler.impl;
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;
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;
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);
40 * @author Toni Kalajainen
42 public class StaticTerminals implements TerminalLayout, TerminalTopology {
44 private static final long serialVersionUID = -7324592260579191478L;
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).
51 * @param xytArray an array of (x, y, terminal) triples
52 * @return terminal reference
54 public static TerminalLayout compile(Object ... xytArray)
56 return new StaticTerminals(xytArray);
59 private class TerminalImpl implements Terminal {
65 Map<Terminal, TerminalImpl> terminalMap = new HashMap<Terminal, TerminalImpl>();
66 ArrayList<Terminal> terminals = new ArrayList<Terminal>();
68 StaticTerminals(Object ... xytArray)
70 assert(xytArray.length % 4 == 0);
71 for (int i=0; i<xytArray.length/4; i++)
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];
80 terminalMap.put(ti.t, ti);
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);
94 public void getTerminals(IElement e, Collection<Terminal> result) {
95 result.addAll(terminals);
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);
107 public Shape getTerminalShape(IElement node, Terminal t) {