001/* A parameter that specifies the location of its container.
002
003 Copyright (c) 2002-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 */
027package ptolemy.actor.parameters;
028
029import ptolemy.data.DoubleMatrixToken;
030import ptolemy.data.MatrixToken;
031import ptolemy.data.expr.Parameter;
032import ptolemy.data.type.BaseType;
033import ptolemy.kernel.util.IllegalActionException;
034import ptolemy.kernel.util.InternalErrorException;
035import ptolemy.kernel.util.Locatable;
036import ptolemy.kernel.util.NameDuplicationException;
037import ptolemy.kernel.util.NamedObj;
038
039///////////////////////////////////////////////////////////////////
040//// LocationParameter
041
042/**
043 A parameter that specifies the location of the center of its container.
044 This location is used when rendering the container in a Vergil diagram.
045 The parameter value is a double matrix with one row and two columns.
046 The default value is [0.0, 0.0].
047
048 @author Edward A. Lee
049 @version $Id$
050 @since Ptolemy II 3.0
051 @Pt.ProposedRating Red (eal)
052 @Pt.AcceptedRating Red (cxh)
053 */
054public class LocationParameter extends Parameter implements Locatable {
055    /** Construct a parameter with the given name contained by the specified
056     *  entity. The container argument must not be null, or a
057     *  NullPointerException will be thrown.  This parameter will use the
058     *  workspace of the container for synchronization and version counts.
059     *  If the name argument is null, then the name is set to the empty string.
060     *  The object is not added to the list of objects in the workspace
061     *  unless the container is null.
062     *  Increment the version of the workspace.
063     *  @param container The container.
064     *  @param name The name of the parameter.
065     *  @exception IllegalActionException If the parameter is not of an
066     *   acceptable class for the container.
067     *  @exception NameDuplicationException If the name coincides with
068     *   a parameter already in the container.
069     */
070    public LocationParameter(NamedObj container, String name)
071            throws IllegalActionException, NameDuplicationException {
072        super(container, name);
073        setTypeEquals(BaseType.DOUBLE_MATRIX);
074        setExpression("[0.0, 0.0]");
075    }
076
077    ///////////////////////////////////////////////////////////////////
078    ////                         public methods                    ////
079
080    /** Return a name to present to the user, which
081     *  is the same as the name returned by getName().
082     *  @return A name to present to the user.
083     */
084    @Override
085    public String getDisplayName() {
086        return getName();
087    }
088
089    /** Get the location of the center of the container in some
090     *  Cartesian coordinate system.
091     *  @return The location.
092     *  @see #setLocation(double [])
093     */
094    @Override
095    public double[] getLocation() {
096        try {
097            DoubleMatrixToken token = (DoubleMatrixToken) getToken();
098            double[][] value = token.doubleMatrix();
099            return value[0];
100        } catch (IllegalActionException ex) {
101            // Should not occur.
102            throw new InternalErrorException(ex);
103        }
104    }
105
106    /** Set the center location in some Cartesian coordinate system,
107     *  and notify the container and any value listeners of the new
108     *  location. This also propagates the value to derived objects.
109     *  @param location The location.
110     *  @exception IllegalActionException If the location is rejected.
111     *  @see #getLocation()
112     */
113    @Override
114    public void setLocation(double[] location) throws IllegalActionException {
115        double[][] value = new double[1][2];
116        value[0][0] = location[0];
117        value[0][1] = location[1];
118        setToken(new DoubleMatrixToken(value, MatrixToken.DO_NOT_COPY));
119        propagateValue();
120    }
121}