001/* A string parameter that changes the name of its container to its value.
002
003 Copyright (c) 2007-2015 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027
028 */
029package ptolemy.data.expr;
030
031import java.util.Collection;
032
033import ptolemy.kernel.util.IllegalActionException;
034import ptolemy.kernel.util.NameDuplicationException;
035import ptolemy.kernel.util.NamedObj;
036
037///////////////////////////////////////////////////////////////////
038//// NameParameter
039
040/**
041 <p>This subclass of StringParameter uses its value to set the name of
042 its container.
043
044 @author Elaine Cheong
045 @version $Id$
046@since Ptolemy II 6.1
047 @Pt.ProposedRating Red (celaine)
048 @Pt.AcceptedRating Red (celaine)
049 */
050public class NameParameter extends StringParameter {
051    /** Construct a parameter with the given name contained by the specified
052     *  entity. The container argument must not be null, or a
053     *  NullPointerException will be thrown.  This parameter will use the
054     *  workspace of the container for synchronization and version counts.
055     *  If the name argument is null, then the name is set to the empty string.
056     *  The object is not added to the list of objects in the workspace
057     *  unless the container is null.
058     *  Increment the version of the workspace.
059     *  @param container The container.
060     *  @param name The name of the parameter.
061     *  @exception IllegalActionException If the parameter is not of an
062     *   acceptable class for the container.
063     *  @exception NameDuplicationException If the name coincides with
064     *   a parameter already in the container.
065     */
066    public NameParameter(NamedObj container, String name)
067            throws IllegalActionException, NameDuplicationException {
068        super(container, name);
069    }
070
071    ///////////////////////////////////////////////////////////////////
072    ////                         public methods                    ////
073
074    /** If this variable is not lazy (the default) then evaluate
075     *  the expression contained in this variable, and notify any
076     *  value dependents. If those are not lazy, then they too will
077     *  be evaluated.  Also, if the variable is not lazy, then
078     *  notify its container, if there is one, by calling its
079     *  attributeChanged() method.
080     *  <p>
081     *  If this variable is lazy, then mark this variable and any
082     *  of its value dependents as needing evaluation and for any
083     *  value dependents that are not lazy, evaluate them.
084     *  Note that if there are no value dependents,
085     *  or if they are all lazy, then this will not
086     *  result in evaluation of this variable, and hence will not ensure
087     *  that the expression giving its value is valid.  Call getToken()
088     *  or getType() to accomplish that.
089     *  <p>
090     *  This method differs from its base class implementation in that
091     *  if its string value is not empty, then it also attempts to set
092     *  the name of its container to the string value.
093     *  @return The current list of value listeners, which are evaluated
094     *   as a consequence of this call to validate().
095     *  @exception IllegalActionException If this variable or a
096     *   variable dependent on this variable cannot be evaluated (and is
097     *   not lazy) and the model error handler throws an exception.
098     *   Also thrown if the change is not acceptable to the container.
099     *   Also thrown if there is a NameDuplicationException when setting
100     *   the name of the container.
101     */
102    @Override
103    public Collection validate() throws IllegalActionException {
104        Collection result = null;
105        result = super.validate();
106        NamedObj container = getContainer();
107        if (container != null) {
108            try {
109                String newName = stringValue();
110                if (newName != null && !newName.equals("")) {
111                    container.setName(stringValue());
112                }
113            } catch (NameDuplicationException ex) {
114                throw new IllegalActionException(this, ex,
115                        "Could not set name of container.");
116            }
117        }
118        return result;
119    }
120
121}