001/* An icon that renders the value of an attribute 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.geom.Rectangle2D; 031 032import javax.swing.SwingConstants; 033 034import diva.canvas.Figure; 035import diva.canvas.toolbox.BasicRectangle; 036import diva.canvas.toolbox.LabelFigure; 037import ptolemy.actor.gui.ColorAttribute; 038import ptolemy.kernel.util.IllegalActionException; 039import ptolemy.kernel.util.NameDuplicationException; 040import ptolemy.kernel.util.NamedObj; 041 042/////////////////////////////////////////////////////////////////// 043//// BoxedValueIcon 044 045/** 046 An icon that displays the value of an attribute of the container in a box 047 that resizes according to the width of the attribute value. 048 049 <p>If the value is long, then the value is truncated and ends with "...". 050 See {@link ptolemy.util.StringUtilities#truncateString(String, int, int)}. 051 This is done so as to avoid Consts with overly long icons.</p> 052 053 <p>The attribute is assumed to be an instance of Settable, and its name 054 is given by the parameter <i>attributeName</i>.</p> 055 056 @author Edward A. Lee 057 @version $Id$ 058 @since Ptolemy II 2.0 059 @Pt.ProposedRating Yellow (eal) 060 @Pt.AcceptedRating Red (johnr) 061 */ 062public class BoxedValueIcon extends AttributeValueIcon { 063 // See http://bugzilla.ecoinformatics.org/show_bug.cgi?id=5607 064 // for problems with long Consts. 065 066 /** Create a new icon with the given name in the given container. 067 * The container is required to implement Settable, or an exception 068 * will be thrown. 069 * @param container The container for this attribute. 070 * @param name The name of this attribute. 071 * @exception IllegalActionException If thrown by the parent 072 * class or while setting an attribute 073 * @exception NameDuplicationException If the name coincides with 074 * an attribute already in the container. 075 */ 076 public BoxedValueIcon(NamedObj container, String name) 077 throws NameDuplicationException, IllegalActionException { 078 super(container, name); 079 080 boxColor = new ColorAttribute(this, "boxColor"); 081 boxColor.setExpression("{1.0, 1.0, 1.0, 1.0}"); 082 } 083 084 /////////////////////////////////////////////////////////////////// 085 //// parameters //// 086 087 /** Color of the box. This defaults to white. */ 088 public ColorAttribute boxColor; 089 090 /////////////////////////////////////////////////////////////////// 091 //// public methods //// 092 093 /** Create a new background figure. This overrides the base class 094 * to draw a box around the value display, where the width of the 095 * box depends on the value. 096 * @return A new figure. 097 */ 098 @Override 099 public Figure createBackgroundFigure() { 100 String displayString = _displayString(); 101 double width = 60; 102 double height = 30; 103 104 if (displayString != null) { 105 // Measure width of the text. Unfortunately, this 106 // requires generating a label figure that we will not use. 107 LabelFigure label = new LabelFigure(displayString, _labelFont, 1.0, 108 SwingConstants.CENTER); 109 Rectangle2D stringBounds = label.getBounds(); 110 111 // NOTE: Padding of 20. Quantize the height so that 112 // snap to grid still works. 113 width = Math.floor(stringBounds.getWidth()) + 20; 114 height = Math.floor(stringBounds.getHeight()) + 10; 115 116 if (width < 40) { 117 // Too small. Annoying. 118 width = 40; 119 } 120 if (height < 20) { 121 height = 20; 122 } 123 } 124 125 return new BasicRectangle(0, 0, width, height, boxColor.asColor(), 1); 126 } 127}