001/* An attribute for a visible text annotation. 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.actor.gui.style.TextStyle; 031import ptolemy.kernel.util.Attribute; 032import ptolemy.kernel.util.IllegalActionException; 033import ptolemy.kernel.util.NameDuplicationException; 034import ptolemy.kernel.util.NamedObj; 035import ptolemy.kernel.util.StringAttribute; 036import ptolemy.vergil.basic.RelativeLocatable; 037 038/////////////////////////////////////////////////////////////////// 039//// TextAttribute 040 041/** 042 <p> 043 This is an attribute that is rendered as text annotation. 044 </p> 045 @author Edward A. Lee 046 @version $Id$ 047 @since Ptolemy II 4.0 048 @Pt.ProposedRating Yellow (eal) 049 @Pt.AcceptedRating Red (cxh) 050 */ 051public class TextAttribute extends AbstractTextAttribute 052 implements RelativeLocatable { 053 054 // FIXME: It may be possible to make a base class implement 055 // RelativeLocatable, but right now, if we do that, ShapeAttribute 056 // gets messed up because it includes in its bounding box the line 057 // that gets drawn to the relative object. So for now, we restrict 058 // the relative locatable capability to text attributes. 059 060 /** Construct an attribute with the given name contained by the 061 * specified container. The container argument must not be null, or a 062 * NullPointerException will be thrown. This attribute will use the 063 * workspace of the container for synchronization and version counts. 064 * If the name argument is null, then the name is set to the empty 065 * string. Increment the version of the workspace. 066 * @param container The container. 067 * @param name The name of this attribute. 068 * @exception IllegalActionException If the attribute is not of an 069 * acceptable class for the container, or if the name contains a period. 070 * @exception NameDuplicationException If the name coincides with 071 * an attribute already in the container. 072 */ 073 public TextAttribute(NamedObj container, String name) 074 throws IllegalActionException, NameDuplicationException { 075 super(container, name); 076 077 // Don't use StringParameter here because variable 078 // substitution would be strange. 079 text = new StringAttribute(this, "text"); 080 text.setExpression("Double click to edit text."); 081 082 TextStyle style = new TextStyle(text, "_style"); 083 style.height.setExpression("15"); 084 style.width.setExpression("40"); 085 } 086 087 /////////////////////////////////////////////////////////////////// 088 //// parameters //// 089 090 /** The text. This is a string that defaults to 091 * "Double click to edit text." 092 */ 093 public StringAttribute text; 094 095 /////////////////////////////////////////////////////////////////// 096 //// public methods //// 097 098 /** React to a changes in the attributes by changing the icon. 099 * @param attribute The attribute that changed. 100 * @exception IllegalActionException If the change is not acceptable 101 * to this container (should not be thrown). 102 */ 103 @Override 104 public void attributeChanged(Attribute attribute) 105 throws IllegalActionException { 106 if (attribute == text) { 107 _icon.setText(text.getExpression()); 108 } else { 109 super.attributeChanged(attribute); 110 } 111 } 112}