001/* An icon that renders the value of an attribute of the container. 002 003 Copyright (c) 2006-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.geom.Rectangle2D; 032 033import javax.swing.SwingConstants; 034 035import diva.canvas.Figure; 036import diva.canvas.toolbox.BasicRectangle; 037import diva.canvas.toolbox.LabelFigure; 038import ptolemy.data.expr.SingletonParameter; 039import ptolemy.kernel.util.IllegalActionException; 040import ptolemy.kernel.util.NameDuplicationException; 041import ptolemy.kernel.util.NamedObj; 042 043/////////////////////////////////////////////////////////////////// 044//// ActorNameIcon 045 046/** 047 An icon that displays the parameter <i>_displayedName</i>. 048 049 This differs from {@link ptolemy.vergil.icon.BoxedValueIcon} in that 050 it uses the _displayedName parameter and hides the name of the 051 container. 052 053 @author Elaine Cheong 054 @version $Id$ 055 @since Ptolemy II 5.2 056 @Pt.ProposedRating Red (celaine) 057 @Pt.AcceptedRating Red (celaine) 058 */ 059public class ActorNameIcon extends BoxedValueIcon { 060 061 /** Create a new icon with the given name in the given container. 062 * The container is required to implement Settable, or an exception 063 * will be thrown. 064 * @param container The container for this attribute. 065 * @param name The name of this attribute. 066 * @exception IllegalActionException If thrown by the parent 067 * class or while setting an attribute 068 * @exception NameDuplicationException If the name coincides with 069 * an attribute already in the container. 070 */ 071 public ActorNameIcon(NamedObj container, String name) 072 throws NameDuplicationException, IllegalActionException { 073 super(container, name); 074 075 // Set the name of the parameter that will contain the displayed name. 076 this.attributeName.setExpression("_displayedName"); 077 078 displayWidth.setExpression("20"); 079 } 080 081 /////////////////////////////////////////////////////////////////// 082 //// public methods //// 083 084 /** Create a new background figure. This overrides the base class 085 * to with a different display height and hides the name of the 086 * container if it exists. 087 * @return A new figure. 088 */ 089 @Override 090 public Figure createBackgroundFigure() { 091 String displayString = _displayString(); 092 double width = 60; 093 double height = 30; 094 095 if (displayString != null) { 096 // Measure width of the text. Unfortunately, this 097 // requires generating a label figure that we will not use. 098 LabelFigure label = new LabelFigure(displayString, _labelFont, 1.0, 099 SwingConstants.CENTER); 100 Rectangle2D stringBounds = label.getBounds(); 101 102 // NOTE: Padding of 20. Quantize the height so that 103 // snap to grid still works. 104 width = Math.floor(stringBounds.getWidth()) + 20; 105 height = Math.floor(stringBounds.getHeight()) + 20; 106 107 // If the container name exists, hide it. 108 if (displayString.trim().length() > 0) { 109 try { 110 SingletonParameter hideName = new SingletonParameter( 111 this.getContainer(), "_hideName"); 112 hideName.setExpression("true"); 113 } catch (Exception ex) { 114 if (!_printedMessage) { 115 _printedMessage = true; 116 System.out.println( 117 "Failed to create the background figure. " 118 + ex); 119 } 120 } 121 } 122 } 123 124 return new BasicRectangle(0, 0, width, height, Color.white, 1); 125 } 126 127 /** True if we have printed a message about failing to create 128 * background figures. 129 */ 130 private boolean _printedMessage; 131}