001    /*--------------------------------------------------------------------------+
002    $Id: SimulinkInPort.java 26285 2010-02-18 11:22:54Z juergens $
003    |                                                                          |
004    | Copyright 2005-2010 Technische Universitaet Muenchen                     |
005    |                                                                          |
006    | Licensed under the Apache License, Version 2.0 (the "License");          |
007    | you may not use this file except in compliance with the License.         |
008    | You may obtain a copy of the License at                                  |
009    |                                                                          |
010    |    http://www.apache.org/licenses/LICENSE-2.0                            |
011    |                                                                          |
012    | Unless required by applicable law or agreed to in writing, software      |
013    | distributed under the License is distributed on an "AS IS" BASIS,        |
014    | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
015    | See the License for the specific language governing permissions and      |
016    | limitations under the License.                                           |
017    +--------------------------------------------------------------------------*/
018    package edu.tum.cs.simulink.model;
019    
020    import edu.tum.cs.commons.assertion.CCSMPre;
021    import edu.tum.cs.commons.assertion.PreconditionException;
022    
023    /**
024     * A Simulink inport. An inport can be connected to only one
025     * {@link SimulinkLine}.
026     * 
027     * 
028     * @author deissenb
029     * @author $Author: juergens $
030     * @version $Rev: 26285 $
031     * @levd.rating GREEN Hash: 59BC605F822657C7D41209763A60100A
032     */
033    public class SimulinkInPort extends SimulinkPortBase {
034    
035            /** The line connected to this port. */
036            private SimulinkLine line;
037    
038            /**
039             * Create simulink inport.
040             * 
041             * @param block
042             *            The block this port belongs to.
043             * @param index
044             *            The port index. This may be a number or a string like 'enable'
045             */
046            public SimulinkInPort(SimulinkBlock block, String index) {
047                    super(block, index);
048                    block.addInPort(this);
049            }
050    
051            /**
052             * Get line connected to this port.
053             * 
054             * @return the line or <code>null</code> if no line is connected.
055             */
056            public SimulinkLine getLine() {
057                    return line;
058            }
059    
060            /**
061             * Set line connected to this port. This is only called from the
062             * {@link SimulinkLine}.
063             * 
064             * @throws PreconditionException
065             *             if this port already has a line or the line's destination
066             *             port does not match this port.
067             */
068            /* package */void setLine(SimulinkLine line)
069                            throws IllegalArgumentException {
070                    CCSMPre.isTrue(this.line == null, "Port already has a line");
071                    CCSMPre
072                                    .isTrue(line.getDstPort() == this,
073                                                    "Line's port does not match.");
074                    this.line = line;
075            }
076    
077            /**
078             * Remove line. This is only called from the {@link SimulinkLine}.
079             * 
080             * @throws PreconditionException
081             *             if the provided line is not connected to this port
082             */
083            /* package */void removeLine(SimulinkLine line)
084                            throws IllegalArgumentException {
085                    CCSMPre.isTrue(line != null, "Can not remove null line.");
086                    CCSMPre.isTrue(line == this.line, "Line does not belong to this port.");
087                    this.line = null;
088            }
089    
090            /** {@inheritDoc} */
091            @Override
092            public void remove() {
093                    getBlock().removeInPort(this);
094                    if (line != null) {
095                            line.remove();
096                    }
097                    super.remove();
098            }
099    }