001/* An attribute for specifying that a parameter is edited with a
002 TextArea (multi-line).
003
004 Copyright (c) 2002-2014 The Regents of the University of California.
005 All rights reserved.
006 Permission is hereby granted, without written agreement and without
007 license or royalty fees, to use, copy, modify, and distribute this
008 software and its documentation for any purpose, provided that the above
009 copyright notice and the following two paragraphs appear in all copies
010 of this software.
011
012 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
013 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
014 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
015 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
016 SUCH DAMAGE.
017
018 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
021 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
022 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
023 ENHANCEMENTS, OR MODIFICATIONS.
024
025 PT_COPYRIGHT_VERSION_2
026 COPYRIGHTENDKEY
027
028 */
029package ptolemy.actor.gui.style;
030
031import javax.swing.JTextArea;
032
033import ptolemy.actor.gui.PtolemyQuery;
034import ptolemy.data.IntToken;
035import ptolemy.data.expr.Parameter;
036import ptolemy.data.type.BaseType;
037import ptolemy.kernel.util.IllegalActionException;
038import ptolemy.kernel.util.InternalErrorException;
039import ptolemy.kernel.util.NameDuplicationException;
040import ptolemy.kernel.util.NamedObj;
041import ptolemy.kernel.util.Settable;
042
043///////////////////////////////////////////////////////////////////
044//// TextStyle
045
046/**
047 This attribute annotates user settable attributes to specify an
048 arbitrary multi-line text area style for configuring the containing
049 attribute.  This style can be used with any Settable attribute.
050
051 @see ptolemy.actor.gui.EditorPaneFactory
052 @see ParameterEditorStyle
053 @author Zoltan Kemenczy, Research in Motion Ltd
054 @version $Id$
055 @since Ptolemy II 2.1
056 @Pt.ProposedRating Red (zkemenczy)
057 @Pt.AcceptedRating Red (cxh)
058 */
059public class TextStyle extends ParameterEditorStyle {
060    /** Construct an attribute in the default workspace with an empty string
061     *  as its name. This constructor is for testing only.
062     *  The object is added to the directory of the workspace.
063     *  Increment the version number of the workspace.
064     *  @exception IllegalActionException If the height or width
065     *  attribute is not of an acceptable attribute for the container,
066     *  or if the container is not an instance of Settable.
067     *  @exception NameDuplicationException If the height or width
068     *  Parameter name coincides with an attribute already in the
069     *  container.
070     */
071    public TextStyle() throws IllegalActionException, NameDuplicationException {
072        // Note: StyleConfigurer calls this constructor when
073        // configure -> preferences -> text is selected.
074        super();
075        _initialize();
076    }
077
078    /** Construct an attribute with the specified container and name.
079     *  @param container The container.
080     *  @param name The name of the attribute.
081     *  @exception IllegalActionException If the attribute is not of an
082     *   acceptable attribute for the container, or if the container
083     *   is not an instance of Settable.
084     *  @exception NameDuplicationException If the name coincides with
085     *   an attribute already in the container.
086     */
087    public TextStyle(NamedObj container, String name)
088            throws IllegalActionException, NameDuplicationException {
089        super(container, name);
090        _initialize();
091    }
092
093    ///////////////////////////////////////////////////////////////////
094    ////                         parameters                        ////
095
096    /** The height (in lines) of the text box. This is an integer
097     *  that defaults to 10.
098     */
099    public Parameter height;
100
101    /** The width (in characters) of the text box. This is an integer
102     *  that defaults to 30.
103     */
104    public Parameter width;
105
106    ///////////////////////////////////////////////////////////////////
107    ////                         public methods                    ////
108
109    /** Return true if this style is acceptable for the given parameter.
110     *  @param param The attribute that this annotates.
111     *  @return True.
112     */
113    @Override
114    public boolean acceptable(Settable param) {
115        return true;
116    }
117
118    /** Create a new type-in line
119     *  entry in the given query associated with the
120     *  attribute containing this style.  The name of the entry is
121     *  the name of the attribute.  Attach the attribute to the created entry.
122     *
123     *  @param query The query into which to add the entry.
124     */
125    @Override
126    public void addEntry(PtolemyQuery query) {
127        Settable container = (Settable) getContainer();
128        String name = container.getName();
129        String defaultValue = "";
130        defaultValue = container.getExpression();
131
132        try {
133            int heightValue = ((IntToken) height.getToken()).intValue();
134            int widthValue = ((IntToken) width.getToken()).intValue();
135            JTextArea area = query.addTextArea(name, container.getDisplayName(),
136                    defaultValue,
137                    PtolemyQuery.preferredBackgroundColor(container),
138                    PtolemyQuery.preferredForegroundColor(container),
139                    heightValue, widthValue);
140            // Adjust the editability if the container is NOT_EDITABLE
141            // and _expertMode is not set.
142            query.adjustEditable(container, area);
143            query.attachParameter(container, name);
144            if (container.getVisibility() == Settable.NOT_EDITABLE) {
145                // If the user has selected expert mode, then they can
146                // set the editor of a NOT_EDITABLE to be a TextStyle.
147                // However, we still don't let them edit it.
148                area.setEditable(false);
149            }
150        } catch (IllegalActionException e) {
151            throw new InternalErrorException(e);
152        }
153    }
154
155    ///////////////////////////////////////////////////////////////////
156    ////                         private methods                   ////
157    // Initialize height and width.  This method is called by the
158    // constructors so as to avoid code duplication.
159    private void _initialize()
160            throws IllegalActionException, NameDuplicationException {
161        height = new Parameter(this, "height");
162        height.setToken("10");
163        height.setTypeEquals(BaseType.INT);
164
165        width = new Parameter(this, "width");
166        width.setToken("30");
167        width.setTypeEquals(BaseType.INT);
168    }
169}