001/*
002 * Copyright (c) 2005-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.sms;
031
032import java.util.Collection;
033import java.util.Iterator;
034
035import ptolemy.kernel.Entity;
036import ptolemy.kernel.util.IllegalActionException;
037import ptolemy.kernel.util.NameDuplicationException;
038import ptolemy.kernel.util.NamedObj;
039import ptolemy.kernel.util.Settable;
040import ptolemy.kernel.util.Workspace;
041
042/**
043 * A KeplerVirtualPortReference refers to a kepler virtual port within its
044 * containing entity. The virutal port is referenced by its name within the
045 * context of the entity (actor).
046 * 
047 * @author Shawn Bowers
048 * @created June 14, 2005
049 * @see ptolemy.kernel.Port
050 */
051public class KeplerVirtualIOPortReference extends KeplerIOPortReference {
052
053        /** Constructor */
054        public KeplerVirtualIOPortReference() {
055                super();
056        }
057
058        /**
059         * Constructor
060         * 
061         * @param container
062         *            Description of the Parameter
063         * @param name
064         *            The value of the property
065         * @exception IllegalActionException
066         *                Description of the Exception
067         * @exception NameDuplicationException
068         *                Description of the Exception
069         */
070        public KeplerVirtualIOPortReference(NamedObj container, String name)
071                        throws IllegalActionException, NameDuplicationException {
072                super(container, name);
073        }
074
075        /**
076         * Constructor
077         * 
078         * @param workspace
079         *            Description of the Parameter
080         */
081        public KeplerVirtualIOPortReference(Workspace workspace) {
082                super(workspace);
083        }
084
085        /**
086         * Return the default expression which is null
087         * 
088         * @return The defaultExpression value. Currently if not given, then
089         *         unknown.
090         */
091        public String getDefaultExpression() {
092                return null;
093        }
094
095        /**
096         * @return The virtual port being referenced.
097         */
098        public Object getPort() {
099                // get the container, etc.
100                String portName = getExpression();
101
102                // iterate to first entity container
103                NamedObj container = getContainer();
104                while (container != null && !(container instanceof Entity))
105                        container = container.getContainer();
106
107                // couldn't find a container
108                if (container == null)
109                        return null;
110
111                Entity actor = (Entity) container;
112                for (Iterator ports = actor.attributeList().iterator(); ports.hasNext();) {
113                        Object obj = ports.next();
114                        if (obj instanceof KeplerVirtualIOPort) {
115                                KeplerVirtualIOPort port = (KeplerVirtualIOPort) obj;
116                                if (portName == null && port.getName() == null)
117                                        return port;
118                                else if (portName.equals(port.getName()))
119                                        return port;
120                        }
121                }
122                return null;
123        }
124
125        /**
126         * Sets the reference to the given virtual port.
127         * 
128         * @param vport
129         *            The virtual port that this reference will refer to. The
130         *            virtual port must be within the same container.
131         */
132        public void setPort(KeplerVirtualIOPort port) throws IllegalActionException {
133                this.setExpression(port.getName());
134        }
135
136        /**
137         * Set visibility to NOT_EDITABLE
138         * 
139         * @return The visibility value
140         */
141        public Settable.Visibility getVisibility() {
142                // return NONE; // this should be changed?
143                // return ptolemy.kernel.util.Settable.FULL;
144                return ptolemy.kernel.util.Settable.NOT_EDITABLE;
145        }
146
147        /**
148         * The visibility cannot be changed. This method does nothing.
149         * 
150         * @param visibility
151         *            The new visibility value
152         */
153        public void setVisibility(Settable.Visibility visibility) {
154                // do nothing....we don't want the visibility getting changed
155        }
156
157        /**
158         * Validate the expression. The expression is valid if it has a
159         * corresponding virtual port.
160         */
161        public Collection validate() throws IllegalActionException {
162                // want to make sure the port exists ...
163                if (getPort() == null) {
164                        String msg = "Could not find virtual port '" + getExpression()
165                                        + "'";
166                        throw new IllegalActionException(this, msg);
167                }
168                return null;
169        }
170
171        /**
172         * References are equal when they refer to the same port.
173         */
174        public boolean equals(Object obj) {
175                if (!(obj instanceof KeplerVirtualIOPortReference))
176                        return false;
177                KeplerVirtualIOPortReference ref = (KeplerVirtualIOPortReference) obj;
178                if (getPort() == null)
179                        return false;
180                return getPort().equals(ref.getPort());
181        }
182
183} // KeplerVirtualIOPortReference