]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/impl/Terminals.java
Merge branch 'feature/funcwrite'
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / element / handler / impl / Terminals.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2017 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  *     Semantum Oy - #7119 refactoring
12  *******************************************************************************/
13 package org.simantics.g2d.element.handler.impl;
14
15 import gnu.trove.map.hash.THashMap;
16
17 import java.awt.Shape;
18 import java.awt.geom.AffineTransform;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Map;
22
23 import org.simantics.g2d.diagram.handler.Topology.Terminal;
24 import org.simantics.g2d.element.IElement;
25 import org.simantics.g2d.element.handler.TerminalLayout;
26 import org.simantics.g2d.element.handler.TerminalTopology;
27 import org.simantics.g2d.utils.geom.DirectionSet;
28
29 /**
30  * @author Tuukka Lehtonen
31  */
32 public class Terminals implements TerminalLayout, TerminalTopology {
33
34     private static final long serialVersionUID = -6532093690907028016L;
35
36     protected final Map<Terminal, ObjectTerminal> terminalMap = new THashMap<>();
37     protected final ArrayList<Terminal>           terminals   = new ArrayList<>();
38
39     public Terminals(Collection<ObjectTerminal> ts) {
40         for (ObjectTerminal ti : ts) {
41             terminals.add(ti);
42             terminalMap.put(ti, ti);
43         }
44     }
45
46     @Override
47     public AffineTransform getTerminalPosition(IElement node, Terminal t) {
48         ObjectTerminal ti = terminalMap.get(t);
49         if (ti == null)
50             return null;
51         return new AffineTransform(ti.getTransform());
52     }
53
54     @Override
55     public void getTerminals(IElement e, Collection<Terminal> result) {
56         result.addAll(terminals);
57     }
58
59     @Override
60     public boolean getTerminalDirection(IElement node, Terminal t, DirectionSet directions) {
61         ObjectTerminal ti = terminalMap.get(t);
62         if (ti == null)
63             return false;
64         directions.addAll(ti.getDirections());
65         return true;
66     }
67
68     @Override
69     public Shape getTerminalShape(IElement node, Terminal t) {
70         ObjectTerminal ti = terminalMap.get(t);
71         if (ti == null)
72             return null;
73         return ti.getShape();
74     }
75
76     @Override
77     public int hashCode() {
78         return terminalMap.hashCode();
79     }
80
81     @Override
82     public boolean equals(Object obj) {
83         if (this == obj)
84             return true;
85         if (obj == null)
86             return false;
87         if (getClass() != obj.getClass())
88             return false;
89         Terminals other = (Terminals) obj;
90         return terminalMap.equals(other.terminalMap);
91     }
92
93 }