001/* An attribute with a reference to a rectangle.
002
003 Copyright (c) 2004-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 ptolemy.kernel.util.IllegalActionException;
031import ptolemy.kernel.util.NameDuplicationException;
032import ptolemy.kernel.util.NamedObj;
033
034///////////////////////////////////////////////////////////////////
035//// AttributeExpressionAttribute
036
037/**
038 This is a text attribute whose text string is derived from the
039 expression of a parameter.  <p>
040 @author Steve Neuendorffer
041 @version $Id$
042 @since Ptolemy II 4.1
043 @Pt.ProposedRating Yellow (eal)
044 @Pt.AcceptedRating Red (cxh)
045 */
046public class AttributeExpressionAttribute extends AttributeValueAttribute {
047    /** Construct an attribute with the given name contained by the
048     *  specified container. The container argument must not be null, or a
049     *  NullPointerException will be thrown.  This attribute will use the
050     *  workspace of the container for synchronization and version counts.
051     *  If the name argument is null, then the name is set to the empty
052     *  string. Increment the version of the workspace.
053     *  @param container The container.
054     *  @param name The name of this attribute.
055     *  @exception IllegalActionException If the attribute is not of an
056     *   acceptable class for the container, or if the name contains a period.
057     *  @exception NameDuplicationException If the name coincides with
058     *   an attribute already in the container.
059     */
060    public AttributeExpressionAttribute(NamedObj container, String name)
061            throws IllegalActionException, NameDuplicationException {
062        super(container, name);
063    }
064
065    ///////////////////////////////////////////////////////////////////
066    ////                         protected methods                 ////
067
068    /** Return the a new string that contains the expression of the
069     *  referred to attribute.
070     *  @return A new shape.
071     */
072    @Override
073    protected String _getText() {
074        if (_attribute != null) {
075            String value = _attribute.getExpression();
076            String truncated = value;
077            int width = _displayWidth;
078
079            if (value.length() > width) {
080                truncated = value.substring(0, width) + "...";
081            }
082
083            if (truncated.length() == 0) {
084                truncated = " ";
085            }
086
087            return truncated;
088        }
089
090        return "???";
091    }
092}