001/* An icon that copies the icon of an entity with the same container. 002 003 Copyright (c) 2003-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; 032import java.util.Iterator; 033 034import diva.canvas.CompositeFigure; 035import diva.canvas.Figure; 036import diva.canvas.toolbox.BasicRectangle; 037import ptolemy.actor.gui.ColorAttribute; 038import ptolemy.data.BooleanToken; 039import ptolemy.data.IntToken; 040import ptolemy.data.expr.Parameter; 041import ptolemy.data.type.BaseType; 042import ptolemy.kernel.ComponentEntity; 043import ptolemy.kernel.CompositeEntity; 044import ptolemy.kernel.util.IllegalActionException; 045import ptolemy.kernel.util.NameDuplicationException; 046import ptolemy.kernel.util.Nameable; 047import ptolemy.kernel.util.NamedObj; 048import ptolemy.kernel.util.SingletonConfigurableAttribute; 049import ptolemy.kernel.util.Workspace; 050 051/////////////////////////////////////////////////////////////////// 052//// CopyCatIcon 053 054/** 055 This is an icon that copies the icon of the last entity contained by 056 the same container, if there is one, and behaves like the base class 057 if not. 058 059 @author Edward A. Lee 060 @version $Id$ 061 @since Ptolemy II 4.0 062 @Pt.ProposedRating Yellow (eal) 063 @Pt.AcceptedRating Red (eal) 064 */ 065public class CopyCatIcon extends XMLIcon { 066 /** Create a new icon with the given name in the given container. 067 * @param container The container. 068 * @param name The name of the attribute. 069 * @exception IllegalActionException If the attribute is not of an 070 * acceptable class for the container. 071 * @exception NameDuplicationException If the name coincides with 072 * an attribute already in the container. 073 */ 074 public CopyCatIcon(NamedObj container, String name) 075 throws IllegalActionException, NameDuplicationException { 076 super(container, name); 077 078 echos = new Parameter(this, "echos"); 079 echos.setTypeEquals(BaseType.INT); 080 echos.setExpression("2"); 081 082 echoBoxColor = new ColorAttribute(this, "echoBoxColor"); 083 echoBoxColor.setExpression("{1.0, 1.0, 1.0, 1.0}"); 084 085 includeName = new Parameter(this, "includeName"); 086 includeName.setTypeEquals(BaseType.BOOLEAN); 087 includeName.setExpression("false"); 088 } 089 090 /////////////////////////////////////////////////////////////////// 091 //// parameters //// 092 093 /** Color of the echo boxes. This defaults to white. */ 094 public ColorAttribute echoBoxColor; 095 096 /** The number of echos of the bounding box to draw. This is an 097 * int that defaults to 2. 098 */ 099 public Parameter echos; 100 101 /** If true, include the name of the copied actor in the icon 102 * if the name is included normally in its icon. NOTE: This 103 * will not include the name if the inside actor does not 104 * have an icon attribute, but only has an _iconDescription, 105 * so it's far from perfect. This is a boolean 106 * that defaults to false. 107 */ 108 public Parameter includeName; 109 110 /////////////////////////////////////////////////////////////////// 111 //// public methods //// 112 113 /** Clone the object into the specified workspace. The new object is 114 * <i>not</i> added to the directory of that workspace (you must do this 115 * yourself if you want it there). 116 * The result is an object with no container. 117 * @param workspace The workspace for the cloned object. 118 * @exception CloneNotSupportedException Not thrown in this base class 119 * @return The new Attribute. 120 */ 121 @Override 122 public Object clone(Workspace workspace) throws CloneNotSupportedException { 123 CopyCatIcon newObject = (CopyCatIcon) super.clone(workspace); 124 newObject._originalDescription = null; 125 return newObject; 126 } 127 128 /** Create a new background figure. This method looks for entities 129 * contained by the same container, and if there are any, copies 130 * the icon of the last such entity. If there are none, then it 131 * behaves like the base class. 132 * @return A new figure. 133 */ 134 @Override 135 public Figure createBackgroundFigure() { 136 Figure result = null; 137 Nameable container = getContainer(); 138 139 if (container instanceof CompositeEntity) { 140 CompositeEntity myContainer = (CompositeEntity) container; 141 ComponentEntity entity = null; 142 Iterator entities = myContainer.entityList().iterator(); 143 144 while (entities.hasNext()) { 145 entity = (ComponentEntity) entities.next(); 146 } 147 148 try { 149 if (entity != null) { 150 // Look for an icon within the entity. 151 EditorIcon icon = null; 152 Iterator icons = entity.attributeList(EditorIcon.class) 153 .iterator(); 154 155 while (icons.hasNext()) { 156 icon = (EditorIcon) icons.next(); 157 } 158 159 if (icon != null) { 160 if (((BooleanToken) includeName.getToken()) 161 .booleanValue()) { 162 result = icon.createFigure(); 163 } else { 164 result = icon.createBackgroundFigure(); 165 } 166 } else { 167 // If there is no icon, then maybe there is an 168 // _iconDescription attribute. 169 SingletonConfigurableAttribute description = (SingletonConfigurableAttribute) entity 170 .getAttribute("_iconDescription", 171 SingletonConfigurableAttribute.class); 172 173 if (description != null) { 174 // Look for an icon description in my container. 175 SingletonConfigurableAttribute myDescription = (SingletonConfigurableAttribute) myContainer 176 .getAttribute("_iconDescription", 177 SingletonConfigurableAttribute.class); 178 179 if (myDescription != null) { 180 // Save my original description, in case I go 181 // back to having nothing inside. 182 if (_originalDescription == null) { 183 _originalDescription = myDescription 184 .getConfigureText(); 185 } 186 187 myDescription.configure(null, null, 188 description.getConfigureText()); 189 } 190 } 191 } 192 } else { 193 // Restore the original description if we don't have 194 // one now. 195 if (result == null && _originalDescription != null) { 196 // Restore the original icon description. 197 // Look for an icon description in my container. 198 SingletonConfigurableAttribute myDescription = (SingletonConfigurableAttribute) myContainer 199 .getAttribute("_iconDescription", 200 SingletonConfigurableAttribute.class); 201 202 if (myDescription != null) { 203 myDescription.configure(null, null, 204 _originalDescription); 205 } 206 } 207 } 208 } catch (Throwable throwable) { 209 // Ignore and use default icon. 210 } 211 } 212 213 // If all else fails, behave like the superclass. 214 if (result == null) { 215 result = super.createBackgroundFigure(); 216 } 217 218 // Wrap in a CompositeFigure with echos of the bounding box. 219 // Note that the bounds here are actually bigger than the 220 // bounding box, which may be OK in this case. 221 int numberOfEchos = 2; 222 try { 223 numberOfEchos = ((IntToken) echos.getToken()).intValue(); 224 } catch (IllegalActionException ex) { 225 // Ignore and use the default. 226 } 227 Rectangle2D bounds = result.getBounds(); 228 CompositeFigure composite = new CompositeFigure(); 229 for (int i = numberOfEchos; i > 0; i--) { 230 BasicRectangle rectangle = new BasicRectangle( 231 bounds.getX() + 5.0 * i - _MARGIN, 232 bounds.getY() + 5.0 * i - _MARGIN, 233 bounds.getWidth() + 2 * _MARGIN, 234 bounds.getHeight() + 2 * _MARGIN, echoBoxColor.asColor()); 235 composite.add(rectangle); 236 } 237 BasicRectangle rectangle3 = new BasicRectangle(bounds.getX() - _MARGIN, 238 bounds.getY() - _MARGIN, bounds.getWidth() + _MARGIN * 2, 239 bounds.getHeight() + _MARGIN * 2, Color.white); 240 composite.add(rectangle3); 241 composite.add(result); 242 return composite; 243 } 244 245 /////////////////////////////////////////////////////////////////// 246 //// private variables //// 247 248 /** Original description of the icon. */ 249 private String _originalDescription = null; 250 251 /** Margin around the inside icon. */ 252 private static int _MARGIN = 2; 253 254}