001/* An attribute for specifying that a parameter is edited with a check box.
002
003 Copyright (c) 1998-2014 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 */
028package ptolemy.actor.gui.style;
029
030import ptolemy.actor.gui.PtolemyQuery;
031import ptolemy.data.BooleanToken;
032import ptolemy.data.Token;
033import ptolemy.data.expr.Parameter;
034import ptolemy.kernel.util.IllegalActionException;
035import ptolemy.kernel.util.NameDuplicationException;
036import ptolemy.kernel.util.NamedObj;
037import ptolemy.kernel.util.Settable;
038import ptolemy.kernel.util.Workspace;
039
040///////////////////////////////////////////////////////////////////
041//// CheckBoxStyle
042
043/**
044 This attribute annotates user settable attributes to specify
045 a checkbox style for configuring the containing attribute.
046 This style is only valid for boolean valued attributes, so this class
047 expects that the container will be an instance of Parameter that contains
048 a boolean token.
049
050 @see ptolemy.actor.gui.EditorPaneFactory
051 @see ParameterEditorStyle
052 @author Steve Neuendorffer and Edward A. Lee
053 @version $Id$
054 @since Ptolemy II 1.0
055 @Pt.ProposedRating Green (neuendor)
056 @Pt.AcceptedRating Yellow (neuendor)
057 */
058public class CheckBoxStyle extends ParameterEditorStyle {
059    /** Construct an attribute in the default workspace with an empty string
060     *  as its name.
061     *  The object is added to the directory of the workspace.
062     *  Increment the version number of the workspace.
063     */
064    public CheckBoxStyle() {
065        super();
066    }
067
068    /** Construct an attribute in the given workspace with an empty string
069     *  as its name.
070     *  The object is added to the directory of the workspace.
071     *  Increment the version number of the workspace.
072     *  @param workspace The workspace that will contain the attribute
073     *  that is being constructed.
074     */
075    public CheckBoxStyle(Workspace workspace) {
076        // This constructor is needed for Shallow codegen to work.
077        super(workspace);
078    }
079
080    /** Construct an attribute with the specified container and name.
081     *  @param container The container.
082     *  @param name The name of the attribute.
083     *  @exception IllegalActionException If the attribute is not of an
084     *   acceptable attribute for the container, or if the container
085     *   is not an instance of Parameter.
086     *  @exception NameDuplicationException If the name coincides with
087     *   an attribute already in the container.
088     */
089    public CheckBoxStyle(Parameter container, String name)
090            throws IllegalActionException, NameDuplicationException {
091        super(container, name);
092    }
093
094    ///////////////////////////////////////////////////////////////////
095    ////                         public methods                    ////
096
097    /** Return true if this style is acceptable for the given parameter.
098     *  @param param The attribute that this annotates.
099     *  @return True if the argument is a parameter that contains a
100     *  boolean token.
101     */
102    @Override
103    public boolean acceptable(Settable param) {
104        if (!(param instanceof Parameter)) {
105            return false;
106        }
107
108        try {
109            Token current = ((Parameter) param).getToken();
110
111            if (current instanceof BooleanToken) {
112                return true;
113            } else {
114                return false;
115            }
116        } catch (IllegalActionException ex) {
117            return false;
118        }
119    }
120
121    /** Create a new check box entry in the given query associated with the
122     *  parameter containing this style.  The name of the entry is
123     *  the name of the parameter.  Attach the parameter to the created entry.
124     *
125     *  @param query The query into which to add the entry.
126     *  @exception IllegalActionException If the containing parameter
127     *  does not contain a boolean token.
128     */
129    @Override
130    public void addEntry(PtolemyQuery query) throws IllegalActionException {
131        String name = getContainer().getName();
132
133        if (!(getContainer() instanceof Parameter)) {
134            throw new IllegalActionException(getContainer(),
135                    "CheckBoxStyle can only be "
136                            + "contained by instances of Parameter.");
137        }
138
139        Parameter param = (Parameter) getContainer();
140        Token current = param.getToken();
141
142        if (!(current instanceof BooleanToken)) {
143            throw new IllegalActionException(getContainer(),
144                    "CheckBoxStyle can only be "
145                            + "used for boolean-valued parameters");
146        }
147
148        query.addCheckBox(name, param.getDisplayName(),
149                ((BooleanToken) current).booleanValue());
150        query.attachParameter(param, name);
151    }
152
153    /** Override the base class to check that the container is
154     *  an instance of parameter.
155     *  @param container The container to attach this attribute to.
156     *  @exception IllegalActionException If this attribute and container
157     *   are not in the same workspace, or
158     *   the proposed container would result in recursive containment, or
159     *   the proposed container is not an instance of Parameter.
160     *  @exception NameDuplicationException If the container already has
161     *   an attribute with the name of this attribute.
162     */
163    @Override
164    public void setContainer(NamedObj container)
165            throws IllegalActionException, NameDuplicationException {
166        if (container != null && !(container instanceof Parameter)) {
167            throw new IllegalActionException(this, container,
168                    "CheckBoxStyle can only be contained by a Parameter.");
169        }
170
171        super.setContainer(container);
172    }
173}