Project

General

Profile

Bug #5266 » ValueIcon.java

Sean Riddle, 10/17/2012 04:10 PM

 
1
/* An icon that renders the value of the container.
2

    
3
 Copyright (c) 1999-2011 The Regents of the University of California.
4
 All rights reserved.
5
 Permission is hereby granted, without written agreement and without
6
 license or royalty fees, to use, copy, modify, and distribute this
7
 software and its documentation for any purpose, provided that the above
8
 copyright notice and the following two paragraphs appear in all copies
9
 of this software.
10

    
11
 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
12
 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
13
 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
14
 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
15
 SUCH DAMAGE.
16

    
17
 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
18
 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19
 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
20
 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
21
 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
22
 ENHANCEMENTS, OR MODIFICATIONS.
23

    
24
 PT_COPYRIGHT_VERSION_2
25
 COPYRIGHTENDKEY
26

    
27
 */
28
package ptolemy.vergil.icon;
29

    
30
import java.awt.Color;
31
import java.awt.Font;
32
import java.util.List;
33

    
34
import javax.swing.SwingConstants;
35

    
36
import ptolemy.actor.gui.ColorAttribute;
37
import ptolemy.data.IntToken;
38
import ptolemy.data.expr.Parameter;
39
import ptolemy.data.type.BaseType;
40
import ptolemy.kernel.util.*;
41
import ptolemy.util.StringUtilities;
42
import diva.canvas.CompositeFigure;
43
import diva.canvas.Figure;
44
import diva.canvas.toolbox.BasicEllipse;
45
import diva.canvas.toolbox.LabelFigure;
46

    
47
///////////////////////////////////////////////////////////////////
48
//// ValueIcon
49

    
50
/**
51
 An icon that displays a bullet, the name, and, if the container is
52
 an instance of settable, its value.
53

    
54
 @author Edward A. Lee, Steve Neuendorffer
55
 @version $Id: ValueIcon.java 64753 2012-10-02 02:05:53Z cxh $
56
 @since Ptolemy II 2.0
57
 @Pt.ProposedRating Yellow (eal)
58
 @Pt.AcceptedRating Red (johnr)
59
 */
60
public class ValueIcon extends XMLIcon {
61
    /** Create a new icon with the given name in the given container.
62
     *  The container is required to implement Settable, or an exception
63
     *  will be thrown.
64
     *  @param container The container for this attribute.
65
     *  @param name The name of this attribute.
66
     *  @exception IllegalActionException If thrown by the parent
67
     *  class or while setting an attribute.
68
     *  @exception NameDuplicationException If the name coincides with
69
     *   an attribute already in the container.
70
     */
71
    public ValueIcon(NamedObj container, String name)
72
            throws NameDuplicationException, IllegalActionException {
73
        super(container, name);
74

    
75
        displayWidth = new Parameter(this, "displayWidth");
76
        displayWidth.setExpression("60");
77
        displayWidth.setTypeEquals(BaseType.INT);
78
    }
79

    
80
    ///////////////////////////////////////////////////////////////////
81
    ////                         parameters                        ////
82

    
83
    /** The number of characters to display. This is an integer, with
84
     *  default value 60.
85
     */
86
    public Parameter displayWidth;
87

    
88
    ///////////////////////////////////////////////////////////////////
89
    ////                         public methods                    ////
90

    
91
    /** Create a background figure based on this icon, which is a text
92
     *  element with the name of the container, a colon, and its value.
93
     *  @return A figure for this icon.
94
     */
95
    public Figure createBackgroundFigure() {
96
        return createFigure();
97
    }
98

    
99
    /** Create a new Diva figure that visually represents this icon.
100
     *  The figure will be an instance of LabelFigure that renders the
101
     *  container name and value, separated by a colon.
102
     *  @return A new CompositeFigure consisting of the label.
103
     */
104
    public Figure createFigure() {
105
        CompositeFigure background = new CompositeFigure(
106
                super.createBackgroundFigure());
107
        Nameable container = getContainer();
108

    
109
        // Patch from Sean Riddle so that attributes that have _hideName
110
        // are handled in a manner similar to how actors handle _hideName.
111
        // See http://bugzilla.ecoinformatics.org/show_bug.cgi?id=4903
112
        // and http://bugzilla.ecoinformatics.org/show_bug.cgi?id=5266
113
        // and http://bugzilla.ecoinformatics.org/show_bug.cgi?id=5642.
114
        if (!_isPropertySet(getContainer(), "_hideName")) {
115
            if (container instanceof Settable) {
116
                String name = container.getDisplayName();
117
                String value = ((Settable) container).getExpression();
118
                int width = 60;
119
                try {
120
                    width = ((IntToken) displayWidth.getToken()).intValue();
121
                } catch (IllegalActionException ex) {
122
                    throw new InternalErrorException(this, ex,
123
                            "Failed to get the width?");
124
                }
125
                String truncated = StringUtilities
126
                    .truncateString(value, width, 1);
127
				LabelFigure label;
128
				if (container instanceof Parameter) {
129
					label = new LabelFigure(truncated,
130
							_labelFont, 1.0, SwingConstants.SOUTH_WEST);
131
				}
132
				else {
133
					label = new LabelFigure(name + ": "
134
							+ truncated,
135
							_labelFont, 1.0, SwingConstants.SOUTH_WEST);
136

    
137
				}
138
                background.add(label);
139
                return background;
140
            } else {
141
                String name = container.getName();
142
                LabelFigure label = new LabelFigure(name, _labelFont, 1.0,
143
                        SwingConstants.SOUTH_WEST);
144
                background.add(label);
145
                return background;
146
			}
147
        }
148
		else {
149
            return background;
150
        }
151
    }
152

    
153
    ///////////////////////////////////////////////////////////////////
154
    ////                         protected methods                 ////
155

    
156
    /** Create a new default background figure, which is a bullet.
157
     *  @return A figure representing a bullet.
158
     */
159
    protected Figure _createDefaultBackgroundFigure() {
160
        Color color = Color.black;
161
        List colorAttributes = attributeList(ColorAttribute.class);
162

    
163
        if (colorAttributes.size() > 0) {
164
            ColorAttribute colorAttribute = (ColorAttribute) colorAttributes
165
                    .get(0);
166
            color = colorAttribute.asColor();
167
        }
168

    
169
        return new BasicEllipse(-10, -6, 6, 6, color, 1);
170
    }
171

    
172
    ///////////////////////////////////////////////////////////////////
173
    ////                         private members                   ////
174
    private static Font _labelFont = new Font("SansSerif", Font.PLAIN, 12);
175
}
(5-5/5)