001/* A parameter that is in string mode and has a list of choices.
002
003 Copyright (c) 2003-2009 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.IllegalActionException;
032import ptolemy.kernel.util.NameDuplicationException;
033import ptolemy.kernel.util.NamedObj;
034
035//////////////////////////////////////////////////////////////////////////
036//// ChoiceParameter
037
038/**
039 This parameter contains a string value and has a list of acceptable choices.
040
041 @author Thomas Huining Feng
042 @version $Id$
043 @since Ptolemy II 8.0
044 @Pt.ProposedRating Red (tfeng)
045 @Pt.AcceptedRating Red (tfeng)
046 */
047public class ChoiceParameter extends StringParameter {
048
049    /** Construct a parameter with the given name contained by the specified
050     *  entity. The container argument must not be null, or a
051     *  NullPointerException will be thrown.  This parameter will use the
052     *  workspace of the container for synchronization and version counts.
053     *  If the name argument is null, then the name is set to the empty string.
054     *  The object is not added to the list of objects in the workspace
055     *  unless the container is null.
056     *  Increment the version of the workspace.
057     *  @param container The container.
058     *  @param name The name of the parameter.
059     *  @param enumClass The enum class that contains the choices.
060     *  @exception IllegalActionException If the parameter is not of an
061     *   acceptable class for the container.
062     *  @exception NameDuplicationException If the name coincides with
063     *   a parameter already in the container.
064     */
065    public ChoiceParameter(NamedObj container, String name, Class<?> enumClass)
066            throws IllegalActionException, NameDuplicationException {
067        super(container, name);
068        if (!enumClass.isEnum()) {
069            throw new IllegalActionException("Only a Java enum class is "
070                    + "accepted as parameter to enumClass.");
071        }
072        int i = 0;
073        for (Object enumObject : enumClass.getEnumConstants()) {
074            String value = enumObject.toString();
075            addChoice(value);
076            if (i++ == 0) {
077                setExpression(value);
078            }
079        }
080        _enumClass = enumClass;
081    }
082
083    /** Get the choice as a member of the enum class. The string value of that
084     *  member (returned with its toString() method) is equal to the string
085     *  value contained by this parameter.
086     *
087     *  @return The chosen member of the enum class.
088     */
089    public Object getChosenValue() {
090        String expression = getExpression();
091        for (Object enumObject : _enumClass.getEnumConstants()) {
092            if (expression.equals(enumObject.toString())) {
093                return enumObject;
094            }
095        }
096        return null;
097    }
098
099    private Class<?> _enumClass;
100}