001/* An attribute representing the location of a component.
002
003 Copyright (c) 1998-2013 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.actor.gui;
029
030import java.awt.Rectangle;
031import java.awt.Window;
032
033import ptolemy.data.IntMatrixToken;
034import ptolemy.data.expr.Parameter;
035import ptolemy.kernel.util.IllegalActionException;
036import ptolemy.kernel.util.InternalErrorException;
037import ptolemy.kernel.util.NameDuplicationException;
038import ptolemy.kernel.util.NamedObj;
039import ptolemy.kernel.util.Settable;
040
041///////////////////////////////////////////////////////////////////
042//// LocationAttribute
043
044/**
045 This attribute stores the width and height of a graphical component.
046 The token in this attribute is an IntMatrixToken containing a matrix
047 of dimension 1x2, containing the width and the height, in that order.
048 By default, this attribute has visibility NONE, so the user will not
049 see it in parameter editing dialogs.
050
051 @author Edward A. Lee
052 @version $Id$
053 @since Ptolemy II 1.0
054 @Pt.ProposedRating Red (eal)
055 @Pt.AcceptedRating Red (johnr)
056 */
057public class LocationAttribute extends Parameter {
058    /** Construct an attribute with the given name contained by the specified
059     *  entity. 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 string.
063     *  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 LocationAttribute(NamedObj container, String name)
072            throws IllegalActionException, NameDuplicationException {
073        super(container, name);
074        setVisibility(Settable.NONE);
075    }
076
077    ///////////////////////////////////////////////////////////////////
078    ////                         public methods                    ////
079
080    /** Set the value of the attribute to match the location
081     *  of the specified component.
082     *  @param component The component whose location is to be recorded.
083     */
084    public void recordLocation(Window component) {
085        try {
086            Rectangle location = component.getBounds();
087            int[][] locationMatrix = new int[1][2];
088            locationMatrix[0][0] = location.x;
089            locationMatrix[0][1] = location.y;
090
091            IntMatrixToken token = new IntMatrixToken(locationMatrix);
092            setToken(token);
093        } catch (IllegalActionException ex) {
094            throw new InternalErrorException("Can't set bounds value!");
095        }
096    }
097
098    /** Set the location of the specified component to match the
099     *  current value of the attribute.  If the value of the attribute
100     *  has not been set, then do nothing.
101     *  @param component The component whose location is to be set.
102     *  @return True if successful.
103     */
104    public boolean setLocation(Window component) {
105        try {
106            IntMatrixToken token = (IntMatrixToken) getToken();
107
108            if (token != null) {
109                int x = token.getElementAt(0, 0);
110                int y = token.getElementAt(0, 1);
111
112                // NOTE: As usual with swing, it's not obvious what the
113                // right way to do this is. The following seems to work,
114                // found by trial and error.  Even then, the layout
115                // manager feels free to override it.
116                component.setLocation(x, y);
117            }
118
119            return true;
120        } catch (Exception ex) {
121            return false;
122        }
123    }
124}