001/* An attribute with a reference to a line.
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.Line2D;
032
033import ptolemy.data.DoubleToken;
034import ptolemy.data.expr.Parameter;
035import ptolemy.data.type.BaseType;
036import ptolemy.kernel.util.Attribute;
037import ptolemy.kernel.util.IllegalActionException;
038import ptolemy.kernel.util.NameDuplicationException;
039import ptolemy.kernel.util.NamedObj;
040import ptolemy.kernel.util.Workspace;
041
042///////////////////////////////////////////////////////////////////
043//// LineAttribute
044
045/**
046 This is an attribute that is rendered as a line.
047
048 <p>
049 This contains two parameters, <i>x</i> and <i>y</i>, which control the
050 run and rise, respectively, of the line.  Note that the origin is in the
051 upper-left hand corner, so positive x values will extend to the right, and
052 positive y values will extend downwards on the screen.</p>
053
054 @author Edward A. Lee
055 @version $Id$
056 @since Ptolemy II 4.0
057 @Pt.ProposedRating Yellow (eal)
058 @Pt.AcceptedRating Red (cxh)
059 */
060public class LineAttribute extends ShapeAttribute {
061    /** Construct an attribute with the given name contained by the
062     *  specified container. The container argument must not be null, or a
063     *  NullPointerException will be thrown.  This attribute will use the
064     *  workspace of the container for synchronization and version counts.
065     *  If the name argument is null, then the name is set to the empty
066     *  string. Increment the version of the workspace.
067     *  @param container The container.
068     *  @param name The name of this attribute.
069     *  @exception IllegalActionException If the attribute is not of an
070     *   acceptable class for the container, or if the name contains a period.
071     *  @exception NameDuplicationException If the name coincides with
072     *   an attribute already in the container.
073     */
074    public LineAttribute(NamedObj container, String name)
075            throws IllegalActionException, NameDuplicationException {
076        super(container, name);
077
078        x = new Parameter(this, "x");
079        x.setTypeEquals(BaseType.DOUBLE);
080        x.setExpression("100.0");
081
082        y = new Parameter(this, "y");
083        y.setTypeEquals(BaseType.DOUBLE);
084        y.setExpression("0.0");
085
086        // FIXME: controller for resizing.
087        // Create a custom controller.
088        // new ImageAttributeControllerFactory(this, "_controllerFactory");
089    }
090
091    ///////////////////////////////////////////////////////////////////
092    ////                         parameters                        ////
093
094    /** The horizontal extent.
095     *  This is a double that defaults to 100.0.
096     */
097    public Parameter x;
098
099    /** The y extent.
100     *  This is a double that defaults to 0.0.
101     */
102    public Parameter y;
103
104    ///////////////////////////////////////////////////////////////////
105    ////                         public methods                    ////
106
107    /** React to a changes in the attributes by changing
108     *  the icon.
109     *  @param attribute The attribute that changed.
110     *  @exception IllegalActionException If the change is not acceptable
111     *   to this container (should not be thrown).
112     */
113    @Override
114    public void attributeChanged(Attribute attribute)
115            throws IllegalActionException {
116        if (attribute == x || attribute == y) {
117            double xValue = ((DoubleToken) x.getToken()).doubleValue();
118            double yValue = ((DoubleToken) y.getToken()).doubleValue();
119            _icon.setShape(new Line2D.Double(0.0, 0.0, xValue, yValue));
120        } else {
121            super.attributeChanged(attribute);
122        }
123    }
124
125    /** Clone the object into the specified workspace. The new object is
126     *  <i>not</i> added to the directory of that workspace (you must do this
127     *  yourself if you want it there).
128     *  The result is an object with no container.
129     *  @param workspace The workspace for the cloned object.
130     *  @exception CloneNotSupportedException Not thrown in this base class
131     *  @return The new Attribute.
132     */
133    @Override
134    public Object clone(Workspace workspace) throws CloneNotSupportedException {
135        LineAttribute newObject = (LineAttribute) super.clone(workspace);
136
137        // The cloned icon ends up referring to the clonee's shape.
138        // We need to fix that here.
139        try {
140            newObject._icon.attributeChanged(x);
141        } catch (IllegalActionException e) {
142            // Should not occur.
143            throw new CloneNotSupportedException(e.getMessage());
144        }
145        return newObject;
146    }
147
148    ///////////////////////////////////////////////////////////////////
149    ////                         protected methods                 ////
150
151    /** Return a line.
152     *  @return A line.
153     */
154    @Override
155    protected Shape _getDefaultShape() {
156        return new Line2D.Double(0.0, 0.0, 20.0, 20.0);
157    }
158}