001/* A GUI property that is associated with an action.
002
003 Copyright (c) 2008-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.properties;
029
030import java.net.URL;
031
032import ptolemy.data.BooleanToken;
033import ptolemy.data.expr.Parameter;
034import ptolemy.data.type.BaseType;
035import ptolemy.kernel.util.Configurable;
036import ptolemy.kernel.util.IllegalActionException;
037import ptolemy.kernel.util.InternalErrorException;
038import ptolemy.kernel.util.NameDuplicationException;
039import ptolemy.kernel.util.NamedObj;
040
041//////////////////////////////////////////////////////////////////////////
042//// ActionGUIProperty
043
044/**
045 A GUI property that is associated with an action.
046
047 @author Thomas Huining Feng
048 @version $Id$
049 @since Ptolemy II 8.0
050 @Pt.ProposedRating Red (tfeng)
051 @Pt.AcceptedRating Red (tfeng)
052 */
053public abstract class ActionGUIProperty extends GUIProperty
054        implements Configurable {
055
056    /** Construct a GUI property with the given name contained by the specified
057     *  entity. The container argument must not be null, or a
058     *  NullPointerException will be thrown.  This attribute will use the
059     *  workspace of the container for synchronization and version counts.
060     *  If the name argument is null, then the name is set to the empty string.
061     *  Increment the version of the workspace.
062     *  @param container The container.
063     *  @param name The name of this attribute.
064     *  @exception IllegalActionException If the attribute is not of an
065     *   acceptable class for the container, or if the name contains a period.
066     *  @exception NameDuplicationException If the name coincides with
067     *   an attribute already in the container.
068     */
069    public ActionGUIProperty(NamedObj container, String name)
070            throws IllegalActionException, NameDuplicationException {
071        super(container, name);
072
073        parse = new Parameter(this, "parse");
074        parse.setTypeEquals(BaseType.BOOLEAN);
075        parse.setToken(BooleanToken.FALSE);
076
077        _action = _createAction();
078    }
079
080    /** Configure the object with data from the specified input source
081     *  (a URL) and/or textual data.  The object should interpret the
082     *  source first, if it is specified, followed by the literal text,
083     *  if that is specified.  The new configuration should usually
084     *  override any old configuration wherever possible, in order to
085     *  ensure that the current state can be successfully retrieved.
086     *  <p>
087     *  This method is defined to throw a very general exception to allow
088     *  classes that implement the interface to use whatever exceptions
089     *  are appropriate.
090     *  @param base The base relative to which references within the input
091     *   are found, or null if this is not known, or there is none.
092     *  @param source The input source, which specifies a URL, or null
093     *   if none.
094     *  @param text Configuration information given as text, or null if
095     *   none.
096     *  @exception Exception If something goes wrong.
097     */
098    @Override
099    public void configure(URL base, String source, String text)
100            throws Exception {
101        _action.configure(base, source, text);
102    }
103
104    /** Return the input source that was specified the last time the configure
105     *  method was called.
106     *  @return The string representation of the input URL, or null if the
107     *  no source has been used to configure this object, or null if no
108     *  external source need be used to configure this object.
109     */
110    @Override
111    public String getConfigureSource() {
112        return _action.getConfigureSource();
113    }
114
115    /** Return the text string that represents the current configuration of
116     *  this object.  Note that any configuration that was previously
117     *  specified using the source attribute need not be represented here
118     *  as well.
119     *  @return A configuration string, or null if no configuration
120     *  has been used to configure this object, or null if no
121     *  configuration string need be used to configure this object.
122     */
123    @Override
124    public String getConfigureText() {
125        return _action.getConfigureText();
126    }
127
128    /** Perform this action. In this base class, if a source
129     *  file is specified in the configuration of this item, e.g.:
130     *  <pre>
131     *    &lt;configure source="some_file.xml"&gt;
132     *    &lt;/configure&gt;
133     *  </pre>
134     *  then the source is read and its contents are used as the moml text.
135     *  The moml text can also be given directly:
136     *  <pre>
137     *    &lt;configure&gt;
138     *      &lt;entity name="C" class="ptolemy.actor.lib.Const"&gt;
139     *      &lt;/entity&gt;
140     *    &lt;/configure&gt;
141     *  </pre>
142     *
143     *  Depending on whether the {@link #parse} parameter is true or false,
144     *  the moml text may be parsed first or not. If it is parsed, the
145     *  returned NamedObj is used to generate a new moml string to be
146     *  applied to the model in the current tableau (the nearest tableau
147     *  that contains this GUI property). If it is not parsed, then the moml
148     *  text is directly applied to the model.
149     */
150    public void perform() {
151        if (parse == null) {
152            return;
153        }
154        try {
155            boolean parse = ((BooleanToken) this.parse.getToken())
156                    .booleanValue();
157            _action.perform(parse);
158        } catch (Exception e) {
159            throw new InternalErrorException(e);
160        }
161    }
162
163    /** A Boolean parameter that determines whether the moml text should be
164     *  parsed before applying to the current model in the {@link #perform()}
165     *  method.
166     */
167    public Parameter parse;
168
169    /** Create the action to be used in this property.
170     *
171     *  @return The action.
172     *  @exception IllegalActionException If the attribute is not of an
173     *   acceptable class for the container, or if the name contains a period.
174     *  @exception NameDuplicationException If the name coincides with
175     *   an attribute already in the container.
176     */
177    protected GUIAction _createAction()
178            throws IllegalActionException, NameDuplicationException {
179        return new GUIAction(this, "_actionHandler");
180    }
181
182    /** The action.
183     */
184    protected GUIAction _action;
185}