001/* An icon that renders the value of the container.
002
003 Copyright (c) 1999-2016 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.icon;
029
030import java.awt.Color;
031import java.awt.Font;
032import java.util.List;
033
034import javax.swing.SwingConstants;
035
036import diva.canvas.CompositeFigure;
037import diva.canvas.Figure;
038import diva.canvas.toolbox.BasicEllipse;
039import diva.canvas.toolbox.LabelFigure;
040import ptolemy.actor.gui.ColorAttribute;
041import ptolemy.data.IntToken;
042import ptolemy.data.expr.Parameter;
043import ptolemy.data.type.BaseType;
044import ptolemy.kernel.util.IllegalActionException;
045import ptolemy.kernel.util.NameDuplicationException;
046import ptolemy.kernel.util.Nameable;
047import ptolemy.kernel.util.NamedObj;
048import ptolemy.kernel.util.Settable;
049import ptolemy.util.StringUtilities;
050
051///////////////////////////////////////////////////////////////////
052//// ValueIcon
053
054/**
055 An icon that displays a bullet, the name, and, if the container is
056 an instance of settable, its value.
057
058 @author Edward A. Lee, Steve Neuendorffer
059 @version $Id$
060 @since Ptolemy II 2.0
061 @Pt.ProposedRating Yellow (eal)
062 @Pt.AcceptedRating Red (johnr)
063 */
064public class ValueIcon extends XMLIcon {
065    /** Create a new icon with the given name in the given container.
066     *  The container is required to implement Settable, or an exception
067     *  will be thrown.
068     *  @param container The container for this attribute.
069     *  @param name The name of this attribute.
070     *  @exception IllegalActionException If thrown by the parent
071     *  class or while setting an attribute.
072     *  @exception NameDuplicationException If the name coincides with
073     *   an attribute already in the container.
074     */
075    public ValueIcon(NamedObj container, String name)
076            throws NameDuplicationException, IllegalActionException {
077        super(container, name);
078
079        displayWidth = new Parameter(this, "displayWidth");
080        displayWidth.setExpression("80");
081        displayWidth.setTypeEquals(BaseType.INT);
082
083        numberOfLines = new Parameter(this, "numberOfLines");
084        numberOfLines.setExpression("1");
085        numberOfLines.setTypeEquals(BaseType.INT);
086    }
087
088    ///////////////////////////////////////////////////////////////////
089    ////                         parameters                        ////
090
091    /** The number of characters to display. This is an integer, with
092     *  default value 60.
093     */
094    public Parameter displayWidth;
095
096    /** The number of lines to display. This is an integer, with default
097     *  value 1.
098     */
099    public Parameter numberOfLines;
100
101    ///////////////////////////////////////////////////////////////////
102    ////                         public methods                    ////
103
104    /** Create a background figure based on this icon, which is a text
105     *  element with the name of the container, a colon, and its value.
106     *  @return A figure for this icon.
107     */
108    @Override
109    public Figure createBackgroundFigure() {
110        return createFigure();
111    }
112
113    /** Create a new Diva figure that visually represents this icon.
114     *  The figure will be an instance of LabelFigure that renders the
115     *  container name and value, separated by a colon.
116     *  @return A new CompositeFigure consisting of the label.
117     */
118    @Override
119    public Figure createFigure() {
120        CompositeFigure background = new CompositeFigure(
121                super.createBackgroundFigure());
122        Nameable container = getContainer();
123
124        if (container instanceof Settable) {
125            String name = container.getDisplayName();
126            String value = ((Settable) container).getExpression();
127            int width = 60;
128            int lines = 1;
129            try {
130                width = ((IntToken) displayWidth.getToken()).intValue();
131            } catch (IllegalActionException e) {
132                // This should not happen.
133            }
134            try {
135                lines = ((IntToken) numberOfLines.getToken()).intValue();
136            } catch (IllegalActionException e) {
137                // This should not happen.
138            }
139            String truncated = StringUtilities.truncateString(value, width,
140                    lines);
141            LabelFigure label = new LabelFigure(name + ": " + truncated,
142                    _labelFont, 1.0, SwingConstants.SOUTH_WEST);
143            background.add(label);
144            return background;
145        } else {
146            String name = container.getName();
147            LabelFigure label = new LabelFigure(name, _labelFont, 1.0,
148                    SwingConstants.SOUTH_WEST);
149            background.add(label);
150            return background;
151        }
152    }
153
154    ///////////////////////////////////////////////////////////////////
155    ////                         protected methods                 ////
156
157    /** Create a new default background figure, which is a bullet.
158     *  @return A figure representing a bullet.
159     */
160    @Override
161    protected Figure _createDefaultBackgroundFigure() {
162        Color color = Color.black;
163        List colorAttributes = attributeList(ColorAttribute.class);
164
165        if (colorAttributes.size() > 0) {
166            ColorAttribute colorAttribute = (ColorAttribute) colorAttributes
167                    .get(0);
168            color = colorAttribute.asColor();
169        }
170
171        return new BasicEllipse(-10, -6, 6, 6, color, 1);
172    }
173
174    ///////////////////////////////////////////////////////////////////
175    ////                         private members                   ////
176    private static Font _labelFont = new Font("SansSerif", Font.PLAIN, 12);
177}