001/* A parameter that is in string mode and has a list of choices.
002
003 Copyright (c) 2003-2018 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 ptolemy.kernel.util.Attribute;
032import ptolemy.kernel.util.IllegalActionException;
033import ptolemy.kernel.util.NameDuplicationException;
034import ptolemy.kernel.util.NamedObj;
035
036///////////////////////////////////////////////////////////////////
037//// ChoiceStringParameter
038
039/**
040 This parameter contains a string value and has a list of acceptable choices.
041 The acceptable choices are contained in the parameter _choices_, a string
042 containing a comma-separated list of choices.
043
044 @author Edward A. Lee
045 @version $Id$
046 @since Ptolemy II 11.0
047 @Pt.ProposedRating Red (tfeng)
048 @Pt.AcceptedRating Red (tfeng)
049 */
050public class ChoiceStringParameter extends StringParameter {
051
052    /** Construct a parameter with the given name contained by the specified
053     *  entity. The container argument must not be null, or a
054     *  NullPointerException will be thrown.  This parameter will use the
055     *  workspace of the container for synchronization and version counts.
056     *  If the name argument is null, then the name is set to the empty string.
057     *  The object is not added to the list of objects in the workspace
058     *  unless the container is null.
059     *  Increment the version of the workspace.
060     *  @param container The container.
061     *  @param name The name of the parameter.
062     *  @param enumClass The enum class that contains the choices.
063     *  @exception IllegalActionException If the parameter is not of an
064     *   acceptable class for the container.
065     *  @exception NameDuplicationException If the name coincides with
066     *   a parameter already in the container.
067     */
068    public ChoiceStringParameter(NamedObj container, String name)
069            throws IllegalActionException, NameDuplicationException {
070        super(container, name);
071        choices = new StringParameter(this, "choices");
072    }
073
074    ///////////////////////////////////////////////////////////////////
075    ////                         public variables                  ////
076
077    /** A comma-separated list of choices. */
078    public StringParameter choices;
079
080    ///////////////////////////////////////////////////////////////////
081    ////                         public methods                    ////
082
083    /** React to a change in an attribute.  This method is called by
084     *  a contained attribute when its value changes.  This overrides
085     *  the base class to check whether the attribute is choices, and if
086     *  so, it removes all previously specified choices and adds the ones
087     *  given in choices.
088     *  @param attribute The attribute that changed.
089     *  @exception IllegalActionException If the change is not acceptable
090     *   to this container.
091     */
092    @Override
093    public void attributeChanged(Attribute attribute)
094            throws IllegalActionException {
095        super.attributeChanged(attribute);
096        if (attribute == choices) {
097            removeAllChoices();
098            String spec = choices.stringValue();
099            String[] choices = spec.split(",");
100            for (int i = 0; i < choices.length; i++) {
101                addChoice(choices[i].trim());
102            }
103        }
104    }
105}