001/* An attribute with a reference to a rectangle.
002
003 Copyright (c) 2003-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.vergil.kernel.attributes;
029
030import java.awt.Shape;
031import java.awt.geom.Rectangle2D;
032import java.awt.geom.RoundRectangle2D;
033
034import ptolemy.data.DoubleToken;
035import ptolemy.data.expr.Parameter;
036import ptolemy.data.type.BaseType;
037import ptolemy.kernel.util.Attribute;
038import ptolemy.kernel.util.IllegalActionException;
039import ptolemy.kernel.util.NameDuplicationException;
040import ptolemy.kernel.util.NamedObj;
041import ptolemy.kernel.util.Workspace;
042
043///////////////////////////////////////////////////////////////////
044//// RectangleAttribute
045
046/**
047 <p>
048 This is an attribute that is rendered as a rectangle.
049 </p>
050 @author Edward A. Lee
051 @version $Id$
052 @since Ptolemy II 4.0
053 @Pt.ProposedRating Yellow (eal)
054 @Pt.AcceptedRating Red (cxh)
055 */
056public class RectangleAttribute extends FilledShapeAttribute {
057    /** Construct an attribute with the given name contained by the
058     *  specified container. The container argument must not be null, or a
059     *  NullPointerException will be thrown.  This attribute will use the
060     *  workspace of the container for synchronization and version counts.
061     *  If the name argument is null, then the name is set to the empty
062     *  string. Increment the version of the workspace.
063     *  @param container The container.
064     *  @param name The name of this attribute.
065     *  @exception IllegalActionException If the attribute is not of an
066     *   acceptable class for the container, or if the name contains a period.
067     *  @exception NameDuplicationException If the name coincides with
068     *   an attribute already in the container.
069     */
070    public RectangleAttribute(NamedObj container, String name)
071            throws IllegalActionException, NameDuplicationException {
072        super(container, name);
073
074        rounding = new Parameter(this, "rounding");
075        rounding.setTypeEquals(BaseType.DOUBLE);
076        rounding.setExpression("0.0");
077    }
078
079    ///////////////////////////////////////////////////////////////////
080    ////                         parameters                        ////
081
082    /** The amount of rounding of the corners.
083     *  This is a double that defaults to 0.0, which indicates no rounding.
084     */
085    public Parameter rounding;
086
087    ///////////////////////////////////////////////////////////////////
088    ////                         public methods                    ////
089
090    /** React to a changes in the attributes by changing
091     *  the icon.
092     *  @param attribute The attribute that changed.
093     *  @exception IllegalActionException If the change is not acceptable
094     *   to this container (should not be thrown).
095     */
096    @Override
097    public void attributeChanged(Attribute attribute)
098            throws IllegalActionException {
099        if (attribute == rounding) {
100            // Make sure that the new rounding value is valid.
101            double roundingValue = ((DoubleToken) rounding.getToken())
102                    .doubleValue();
103
104            if (roundingValue < 0.0) {
105                throw new IllegalActionException(this,
106                        "Invalid rounding value. Required to be non-negative.");
107            }
108
109            if (roundingValue != _roundingValue) {
110                _roundingValue = roundingValue;
111                _icon.setShape(_newShape());
112            }
113        } else {
114            super.attributeChanged(attribute);
115        }
116    }
117
118    /** Clone the object into the specified workspace. The new object is
119     *  <i>not</i> added to the directory of that workspace (you must do this
120     *  yourself if you want it there).
121     *  The result is an object with no container.
122     *  @param workspace The workspace for the cloned object.
123     *  @exception CloneNotSupportedException Not thrown in this base class
124     *  @return The new Attribute.
125     */
126    @Override
127    public Object clone(Workspace workspace) throws CloneNotSupportedException {
128        RectangleAttribute newObject = (RectangleAttribute) super.clone(
129                workspace);
130
131        // The cloned icon ends up referring to the clonee's shape.
132        // We need to fix that here. Do not use the _newShape() method
133        // of the clone, however, because it may refer to parameters that
134        // have not been created yet. Instead, use this object to generate
135        // the new shape for the clone.
136        newObject._icon.setShape(_newShape());
137        return newObject;
138    }
139
140    ///////////////////////////////////////////////////////////////////
141    ////                         protected methods                 ////
142
143    /** Return the a new rectangle given a new width and height.
144     *  @return A new shape.
145     */
146    @Override
147    protected Shape _newShape() {
148        double roundingValue = 0.0;
149
150        try {
151            if (rounding != null && rounding.getToken() != null) {
152                roundingValue = ((DoubleToken) rounding.getToken())
153                        .doubleValue();
154            }
155        } catch (IllegalActionException ex) {
156            // Ignore and use default.
157        }
158
159        double x = 0.0;
160        double y = 0.0;
161        double width = _widthValue;
162        double height = _heightValue;
163
164        /* Let Diva's notion of centering handle this.
165        if (_centeredValue) {
166            x = -width * 0.5;
167            y = -height * 0.5;
168        }
169        */
170
171        if (roundingValue == 0.0) {
172            return new Rectangle2D.Double(x, y, width, height);
173        } else {
174            return new RoundRectangle2D.Double(x, y, width, height,
175                    roundingValue, roundingValue);
176        }
177    }
178
179    ///////////////////////////////////////////////////////////////////
180    ////                        protected members                  ////
181
182    /** Most recent value of the rounding parameter. */
183    protected double _roundingValue = 0.0;
184}