001/* An icon specialized for states of a state machine. 002 003 Copyright (c) 2006-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.modal; 029 030import java.awt.Color; 031import java.awt.Paint; 032 033import ptolemy.actor.TypedActor; 034import ptolemy.actor.gui.ColorAttribute; 035import ptolemy.data.ArrayToken; 036import ptolemy.data.BooleanToken; 037import ptolemy.data.ScalarToken; 038import ptolemy.data.expr.Parameter; 039import ptolemy.domains.modal.kernel.State; 040import ptolemy.kernel.util.IllegalActionException; 041import ptolemy.kernel.util.NameDuplicationException; 042import ptolemy.kernel.util.NamedObj; 043import ptolemy.vergil.icon.NameIcon; 044 045/////////////////////////////////////////////////////////////////// 046//// StateIcon 047 048/** 049 An icon that displays the name of the container in an appropriately 050 sized rounded box. This is designed to be contained by an instance 051 of State, and if it is, and if the state is the initial state, then 052 the rounded box will be bold. If it is a final state, then it will 053 be double. 054 <p> 055 By default, the icon will be filled with color white, 056 unless the refinement name of the containing State is not empty, 057 in which case it returns a light green. 058 However, if this attribute contains a ColorParameter named "fill", 059 then the color of that color parameter is used instead. 060 If not, but if the container is a State and contains a 061 ColorAttribute named "fill", then that color is used.. 062 063 @author Edward A. Lee 064 @version $Id$ 065 @since Ptolemy II 8.0 066 @Pt.ProposedRating Red (eal) 067 @Pt.AcceptedRating Red (cxh) 068 */ 069public class StateIcon extends NameIcon { 070 071 /** Create a new icon with the given name in the given container. 072 * The container is required to implement Settable, or an exception 073 * will be thrown. 074 * @param container The container for this attribute. 075 * @param name The name of this attribute. 076 * @exception IllegalActionException If thrown by the parent 077 * class or while setting an attribute. 078 * @exception NameDuplicationException If the name coincides with 079 * an attribute already in the container. 080 */ 081 public StateIcon(NamedObj container, String name) 082 throws NameDuplicationException, IllegalActionException { 083 super(container, name); 084 085 // Change the default rounding to 20. 086 rounding.setExpression("20"); 087 } 088 089 /** Return the paint to use to fill the icon. 090 * By default, this class returns Color.white, unless the refinement name 091 * is not empty, in which case it returns a light green. 092 * However, if this attribute contains a ColorParameter named "fill", 093 * then the color of that color parameter is returned instead. 094 * In addition, if the container is a State and contains a 095 * ColorAttribute named "fill", then that color is returned. 096 * @return The paint to use to fill the icon. 097 */ 098 @Override 099 protected Paint _getFill() { 100 Parameter colorParameter; 101 try { 102 colorParameter = (Parameter) getAttribute("fill", Parameter.class); 103 if (colorParameter != null) { 104 ArrayToken array = (ArrayToken) colorParameter.getToken(); 105 if (array.length() == 4) { 106 Color color = new Color( 107 (float) ((ScalarToken) array.getElement(0)) 108 .doubleValue(), 109 (float) ((ScalarToken) array.getElement(1)) 110 .doubleValue(), 111 (float) ((ScalarToken) array.getElement(2)) 112 .doubleValue(), 113 (float) ((ScalarToken) array.getElement(3)) 114 .doubleValue()); 115 return color; 116 } 117 } 118 ColorAttribute colorAttribute = (ColorAttribute) getAttribute( 119 "fill", ColorAttribute.class); 120 if (colorAttribute != null) { 121 return colorAttribute.asColor(); 122 } 123 } catch (Throwable t) { 124 // Ignore and return the default. 125 } 126 127 NamedObj container = getContainer(); 128 if (container instanceof State) { 129 try { 130 ColorAttribute colorAttribute = (ColorAttribute) container 131 .getAttribute("fill", ColorAttribute.class); 132 if (colorAttribute != null) { 133 return colorAttribute.asColor(); 134 } 135 TypedActor[] refinement = ((State) container).getRefinement(); 136 if (refinement != null && refinement.length > 0) { 137 return _REFINEMENT_COLOR; 138 } 139 } catch (IllegalActionException e) { 140 // Ignore and return the default. 141 } 142 } 143 144 return Color.white; 145 } 146 147 /** Return the line width to use in rendering the box. 148 * This returns 1.0f, unless the container is an instance of State 149 * and its <i>isInitialState</i> parameter is set to true. 150 * @return The line width to use in rendering the box. 151 */ 152 @Override 153 protected float _getLineWidth() { 154 NamedObj container = getContainer(); 155 if (container instanceof State) { 156 try { 157 if (((BooleanToken) ((State) container).isInitialState 158 .getToken()).booleanValue()) { 159 return 2.0f; 160 } 161 } catch (IllegalActionException e) { 162 // Ignore and return the default. 163 } 164 } 165 return 1.0f; 166 } 167 168 /////////////////////////////////////////////////////////////////// 169 //// private variables //// 170 171 /** The fill color for states with refinements. */ 172 private static Color _REFINEMENT_COLOR = new Color(0.8f, 1.0f, 0.8f, 1.0f); 173}