001/* A terminal for ports that supports multiports.
002
003 Copyright (c) 2006-2016 The Regents of the University of California.
004 All rights reserved.
005
006 Permission is hereby granted, without written agreement and without
007 license or royalty fees, to use, copy, modify, and distribute this
008 software and its documentation for any purpose, provided that the above
009 copyright notice and the following two paragraphs appear in all copies
010 of this software.
011
012 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
013 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
014 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
015 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
016 SUCH DAMAGE.
017
018 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
021 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
022 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
023 ENHANCEMENTS, OR MODIFICATIONS.
024 */
025
026package ptolemy.vergil.actor;
027
028import java.util.List;
029
030import diva.canvas.Figure;
031import diva.canvas.connector.TerminalFigure;
032import ptolemy.actor.IOPort;
033import ptolemy.kernel.ComponentRelation;
034import ptolemy.vergil.kernel.Link;
035import ptolemy.vergil.toolbox.PortSite;
036
037///////////////////////////////////////////////////////////////////
038//// PortTerminal
039
040/**
041 A terminal figure for ports that supports multiports.
042 In particular, this figure provides a method to determine
043 the "order index" of a link to the port. When multiple relations
044 are linked to a port, the order in which they are linked matters.
045 The provided method returns the position within that order.
046 <p>
047 When this is constructed, a figure is specified for the port,
048 and properties of this figure, such as its bounds, whether it
049 intersects other objects, etc., are determined by that figure.
050 The extra decorations added to support multiple connections
051 are not treated as part of the figure.
052
053 @author Edward A. Lee
054 @version $Id$
055 @since Ptolemy II 5.2
056 @see PortSite
057 @Pt.ProposedRating Yellow (eal)
058 @Pt.AcceptedRating Red (eal)
059 */
060public class PortTerminal extends TerminalFigure {
061    /** Construct a port terminal with the specified figure as
062     *  the port figure.
063     *  @param port The port.
064     *  @param figure The associated figure.
065     *  @param normal The normal direction.
066     *  @param inside True if this is external port and the terminal represents
067     *   inside connections.
068     */
069    public PortTerminal(IOPort port, Figure figure, double normal,
070            boolean inside) {
071        super(figure);
072
073        _connectSite = new PortConnectSite(figure, this, 0, normal);
074        _port = port;
075        _inside = inside;
076    }
077
078    ///////////////////////////////////////////////////////////////////
079    ////                         public methods                    ////
080
081    /** Return the number of links to relations that this port has.
082     *  @return The size of the inside or outside relations list of
083     *   the port.
084     */
085    public int getNumberOfLinks() {
086        List relations;
087
088        if (_inside) {
089            relations = _port.insideRelationList();
090        } else {
091            relations = _port.linkedRelationList();
092        }
093
094        return relations.size();
095    }
096
097    /** Return the order index of the connection represented
098     *  by the specified connector. That is, return 0 if it is
099     *  the first connection, 1 if it is the second, etc.
100     *  If the connector is not known, then return -1.
101     *  @param connector The connector.
102     *  @return The order index of the connection.
103     */
104    public int getOrderIndex(LinkManhattanConnector connector) {
105        Link link = connector.getLink();
106        ComponentRelation relation = link.getRelation();
107        List relations;
108
109        if (_inside) {
110            relations = _port.insideRelationList();
111        } else {
112            relations = _port.linkedRelationList();
113        }
114
115        return relations.indexOf(relation);
116    }
117
118    /** Return the port specified in the constructor.
119     *  @return The port for which this is a terminal.
120     */
121    public IOPort getPort() {
122        return _port;
123    }
124
125    ///////////////////////////////////////////////////////////////////
126    ////                         private variables                 ////
127
128    /** True if the terminal is an external port, and connections
129     *  represent inside connections.
130     */
131    private boolean _inside;
132
133    /** The port that owns this terminal. */
134    private IOPort _port;
135}