001/* An attribute with a reference to an ellipse.
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.Arc2D;
032import java.awt.geom.Rectangle2D;
033
034import ptolemy.data.DoubleToken;
035import ptolemy.data.expr.Parameter;
036import ptolemy.data.expr.StringParameter;
037import ptolemy.data.type.BaseType;
038import ptolemy.kernel.util.Attribute;
039import ptolemy.kernel.util.IllegalActionException;
040import ptolemy.kernel.util.NameDuplicationException;
041import ptolemy.kernel.util.NamedObj;
042import ptolemy.kernel.util.Workspace;
043
044///////////////////////////////////////////////////////////////////
045//// ArcAttribute
046
047/**
048 <p>This is an attribute that is rendered as an ellipse.
049 Unlike the base class, by default, an ellipse is centered on its origin.</p>
050
051 @author Edward A. Lee
052 @version $Id$
053 @since Ptolemy II 10.0
054 @Pt.ProposedRating Yellow (eal)
055 @Pt.AcceptedRating Red (cxh)
056 */
057public class ArcAttribute extends FilledShapeAttribute {
058    /** Construct an attribute with the given name contained by the
059     *  specified container. The container argument must not be null, or a
060     *  NullPointerException will be thrown.  This attribute will use the
061     *  workspace of the container for synchronization and version counts.
062     *  If the name argument is null, then the name is set to the empty
063     *  string. Increment the version of the workspace.
064     *  @param container The container.
065     *  @param name The name of this attribute.
066     *  @exception IllegalActionException If the attribute is not of an
067     *   acceptable class for the container, or if the name contains a period.
068     *  @exception NameDuplicationException If the name coincides with
069     *   an attribute already in the container.
070     */
071    public ArcAttribute(NamedObj container, String name)
072            throws IllegalActionException, NameDuplicationException {
073        super(container, name);
074
075        type = new StringParameter(this, "type");
076        type.setExpression("pie");
077        type.addChoice("chord");
078        type.addChoice("open");
079        type.addChoice("pie");
080
081        start = new Parameter(this, "start");
082        start.setTypeEquals(BaseType.DOUBLE);
083        start.setExpression("0.0");
084
085        extent = new Parameter(this, "extent");
086        extent.setTypeEquals(BaseType.DOUBLE);
087        extent.setExpression("90.0");
088    }
089
090    ///////////////////////////////////////////////////////////////////
091    ////                         parameters                        ////
092
093    /** The angular extent of the angle of the arc in degrees. This is a double
094     *  that defaults to 90.0.
095     */
096    public Parameter extent;
097
098    /** The starting angle of the arc in degrees. This is a double
099     *  that defaults to 0.0.
100     */
101    public Parameter start;
102
103    /** The type of the arc, which should be one of "chord", "open", or "pie".
104     *  The default is "pie".
105     */
106    public StringParameter type;
107
108    ///////////////////////////////////////////////////////////////////
109    ////                         public method                     ////
110
111    /** React to changes in attribute values.
112     *  @param attribute The attribute that changed.
113     *  @exception IllegalActionException If thrown while getting the
114     *  value of an attribute.
115     */
116    @Override
117    public void attributeChanged(Attribute attribute)
118            throws IllegalActionException {
119        if (attribute == type) {
120            String typeValue = type.stringValue();
121            int typeDesignator = Arc2D.PIE;
122            if (typeValue.equals("chord")) {
123                typeDesignator = Arc2D.CHORD;
124            } else if (typeValue.equals("open")) {
125                typeDesignator = Arc2D.OPEN;
126            }
127            if (typeDesignator != _typeDesignator) {
128                _typeDesignator = typeDesignator;
129                _icon.setShape(_newShape());
130            }
131        } else if (attribute == start) {
132            double startValue = ((DoubleToken) start.getToken()).doubleValue();
133            if (startValue != _start) {
134                _start = startValue;
135                _icon.setShape(_newShape());
136            }
137        } else if (attribute == extent) {
138            double extentValue = ((DoubleToken) extent.getToken())
139                    .doubleValue();
140            if (extentValue != _extent) {
141                _extent = extentValue;
142                _icon.setShape(_newShape());
143            }
144        } else {
145            super.attributeChanged(attribute);
146        }
147    }
148
149    /** Clone the object into the specified workspace. The new object is
150     *  <i>not</i> added to the directory of that workspace (you must do this
151     *  yourself if you want it there).
152     *  The result is an object with no container.
153     *  @param workspace The workspace for the cloned object.
154     *  @exception CloneNotSupportedException Not thrown in this base class
155     *  @return The new Attribute.
156     */
157    @Override
158    public Object clone(Workspace workspace) throws CloneNotSupportedException {
159        ArcAttribute newObject = (ArcAttribute) super.clone(workspace);
160
161        // The cloned icon ends up referring to the clonee's shape.
162        // We need to fix that here. Do not use the _newShape() method
163        // of the clone, however, because it may refer to parameters that
164        // have not been created yet. Instead, use this object to generate
165        // the new shape for the clone.
166        newObject._icon.setShape(_newShape());
167        return newObject;
168    }
169
170    ///////////////////////////////////////////////////////////////////
171    ////                         protected methods                 ////
172
173    /** Return a circle.
174     *  @return A Circle.
175     */
176    @Override
177    protected Shape _getDefaultShape() {
178        Rectangle2D bounds = new Rectangle2D.Double(0, 0, _widthValue,
179                _heightValue);
180        return new Arc2D.Double(bounds, 0, 90, Arc2D.PIE);
181    }
182
183    /** Return the a new ellipse given a new width and height.
184     *  @return A new shape.
185     */
186    @Override
187    protected Shape _newShape() {
188        Rectangle2D bounds = new Rectangle2D.Double(0, 0, _widthValue,
189                _heightValue);
190        return new Arc2D.Double(bounds, _start, _extent, _typeDesignator);
191    }
192
193    ///////////////////////////////////////////////////////////////////
194    ////                         private fields                    ////
195
196    /** The value of the extent parameter. */
197    private double _extent = 90.0;
198
199    /** The value of the type parameter. */
200    private double _start = 0.0;
201
202    /** The value of the type parameter. */
203    private int _typeDesignator = Arc2D.PIE;
204}