001/*
002 * Copyright (c) 2003-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2015-07-15 00:23:30 +0000 (Wed, 15 Jul 2015) $' 
007 * '$Revision: 33543 $'
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.moml;
031
032import java.util.Collection;
033import java.util.Vector;
034
035import ptolemy.kernel.util.IllegalActionException;
036import ptolemy.kernel.util.NameDuplicationException;
037import ptolemy.kernel.util.NamedObj;
038import ptolemy.kernel.util.Settable;
039import ptolemy.kernel.util.StringAttribute;
040import ptolemy.kernel.util.ValueListener;
041import ptolemy.kernel.util.Workspace;
042
043/**
044 * This implements a DependencyAttribute for moml properties.
045 * 
046 *@author berkley
047 *@created Aug. 19, 2005
048 */
049public class PortAttribute extends StringAttribute {
050        /** container for the value */
051        private String value = null;
052        /** container for valueListeners assigned to this attribute */
053        private Vector<ValueListener> valueListeners = new Vector<ValueListener>();
054
055        /** Constructor */
056        public PortAttribute() {
057                super();
058        }
059
060        /**
061         * Constructor
062         * 
063         *@param container
064         *            Description of the Parameter
065         *@param name
066         *            Description of the Parameter
067         *@exception IllegalActionException
068         *                Description of the Exception
069         *@exception NameDuplicationException
070         *                Description of the Exception
071         */
072        public PortAttribute(NamedObj container, String name)
073                        throws IllegalActionException, NameDuplicationException {
074                super(container, name);
075        }
076
077        /**
078         * Constructor
079         * 
080         *@param workspace
081         *            Description of the Parameter
082         */
083        public PortAttribute(Workspace workspace) {
084                super(workspace);
085        }
086
087           /** Clone the object into the specified workspace. */
088    @Override
089    public Object clone(Workspace workspace) throws CloneNotSupportedException {
090        PortAttribute newObject = (PortAttribute) super.clone(workspace);
091        newObject.valueListeners = new Vector<ValueListener>();
092        return newObject;
093    }
094
095        /**
096         * returns the default expression which is null
097         * 
098         *@return The defaultExpression value
099         */
100        @Override
101    public String getDefaultExpression() {
102                return null;
103        }
104
105        /**
106         * set the value of this id
107         * 
108         *@param expression
109         *            The new expression value
110         */
111        @Override
112    public void setExpression(String expression) {
113                // System.out.println("setting NamedObjId to " + expression);
114                value = expression;
115                // set the value
116
117                for (int i = 0; i < valueListeners.size(); i++) {
118                        // notify any listeners of the change
119                        ValueListener listener = valueListeners
120                                        .elementAt(i);
121                        listener.valueChanged(this);
122                }
123        }
124
125        /**
126         * return the value of the id
127         * 
128         *@return The expression value
129         */
130        @Override
131    public String getExpression() {
132                return value;
133        }
134
135        /**
136         * add a valueListener
137         * 
138         *@param listener
139         *            The feature to be added to the ValueListener attribute
140         */
141        @Override
142    public void addValueListener(ValueListener listener) {
143                valueListeners.add(listener);
144        }
145
146        /**
147         * NamedObjIds should be invisible to the user
148         * 
149         *@return The visibility value
150         */
151        @Override
152    public Settable.Visibility getVisibility() {
153                return NONE;
154        }
155
156        /**
157         * this method does not change the visibility. NamedObjId should only be
158         * invisible
159         * 
160         *@param visibility
161         *            The new visibility value
162         */
163        @Override
164    public void setVisibility(Settable.Visibility visibility) {
165                // do nothing....we don't want the visibility getting changed
166        }
167
168        /**
169         * remove the indicated listener
170         * 
171         *@param listener
172         *            Description of the Parameter
173         */
174        @Override
175    public void removeValueListener(ValueListener listener) {
176                valueListeners.remove(listener);
177        }
178
179        /** validate the expression. */
180        @Override
181    public Collection validate() {
182                // don't need to do anything here
183                return null;
184        }
185
186        /**
187         * Description of the Method
188         * 
189         *@param obj
190         *            Description of the Parameter
191         *@return Description of the Return Value
192         */
193        @Override
194    public boolean equals(Object obj) {
195                if (!(obj instanceof NamedObjId)) {
196                        return false;
197                }
198                NamedObjId objId = (NamedObjId) obj;
199                String str = objId.getExpression();
200                if (this.getExpression() == null) {
201                        if (str != null) {
202                                return false;
203                        }
204                        return true;
205                }
206                return this.getExpression().equals(objId.getExpression());
207        }
208}