001/* An attribute with a reference to an image. 002 003 Copyright (c) 2003-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 java.awt.Image; 031import java.awt.Toolkit; 032import java.io.IOException; 033import java.net.URL; 034 035import ptolemy.data.DoubleToken; 036import ptolemy.data.expr.FileParameter; 037import ptolemy.data.expr.Parameter; 038import ptolemy.data.type.BaseType; 039import ptolemy.kernel.util.Attribute; 040import ptolemy.kernel.util.IllegalActionException; 041import ptolemy.kernel.util.NameDuplicationException; 042import ptolemy.kernel.util.NamedObj; 043import ptolemy.kernel.util.Workspace; 044import ptolemy.util.FileUtilities; 045import ptolemy.vergil.icon.ImageIcon; 046 047/////////////////////////////////////////////////////////////////// 048//// ImageAttribute 049 050/** 051 <p>This is an attribute that is rendered as an image. Its <i>source</i> 052 parameter specifies a file containing an image (GIF, JPEG, etc.), and 053 its <i>scale</i> attribute specifies a scaling factor, as a percentage. 054 </p> 055 056 @author Edward A. Lee and Steve Neuendorffer 057 @version $Id$ 058 @since Ptolemy II 4.0 059 @Pt.ProposedRating Yellow (eal) 060 @Pt.AcceptedRating Red (cxh) 061 */ 062public class ImageAttribute extends VisibleAttribute { 063 /** Construct an attribute with the given name contained by the 064 * specified container. The container argument must not be null, or a 065 * NullPointerException will be thrown. This attribute will use the 066 * workspace of the container for synchronization and version counts. 067 * If the name argument is null, then the name is set to the empty 068 * string. Increment the version of the workspace. 069 * @param container The container. 070 * @param name The name of this attribute. 071 * @exception IllegalActionException If the attribute is not of an 072 * acceptable class for the container, or if the name contains a period. 073 * @exception NameDuplicationException If the name coincides with 074 * an attribute already in the container. 075 */ 076 public ImageAttribute(NamedObj container, String name) 077 throws IllegalActionException, NameDuplicationException { 078 super(container, name); 079 080 _icon = new ImageIcon(this, "_icon"); 081 _icon.setPersistent(false); 082 083 source = new FileParameter(this, "source"); 084 085 // Put the gif in the local directory so that it stays with this actor. 086 // Use $CLASSPATH here so that this works in Web Start. 087 source.setExpression( 088 "$CLASSPATH/ptolemy/vergil/kernel/attributes/ptIIplanetIcon.gif"); 089 090 scale = new Parameter(this, "scale"); 091 scale.setTypeEquals(BaseType.DOUBLE); 092 scale.setExpression("100.0"); 093 094 // This used to be hidden because it didn't work. It 095 // seems to work now. 096 // scale.setVisibility(Settable.EXPERT); 097 098 // Create a custom controller. 099 // NOTE: This doesn't actually work with the scale parameter. 100 // It gets overridden by the scale parameter. 101 // new ResizableAttributeControllerFactory(this, "_controllerFactory"); 102 } 103 104 /////////////////////////////////////////////////////////////////// 105 //// parameters //// 106 107 /** The scale, as a percentage. 108 * This is a double that defaults to 100.0. 109 */ 110 public Parameter scale; 111 112 /** The source image file. This is a file name or URL, where the default 113 * is "$CLASSPATH/ptolemy/vergil/kernel/attributes/ptIIplanetIcon.gif". 114 */ 115 public FileParameter source; 116 117 /////////////////////////////////////////////////////////////////// 118 //// public methods //// 119 120 /** React to a change in the source or scale attributes by changing 121 * the icon. 122 * @param attribute The attribute that changed. 123 * @exception IllegalActionException If the change is not acceptable 124 * to this container (should not be thrown). 125 */ 126 @Override 127 public void attributeChanged(Attribute attribute) 128 throws IllegalActionException { 129 if (attribute == source) { 130 URL url = null; 131 try { 132 url = FileUtilities.followRedirects(source.asURL()); 133 } catch (IOException ex) { 134 throw new IllegalActionException(this, ex, 135 "Failed to follow possible redirects for " 136 + source.asURL()); 137 } 138 Toolkit tk = Toolkit.getDefaultToolkit(); 139 Image image = tk.getImage(url); 140 _icon.setImage(image); 141 } else if (attribute == scale) { 142 double scaleValue = ((DoubleToken) scale.getToken()).doubleValue(); 143 _icon.scaleImage(scaleValue); 144 } else { 145 super.attributeChanged(attribute); 146 } 147 } 148 149 /** Clone the object into the specified workspace. The new object is 150 * <i>not</i> added to the directory of that workspace (you must do this 151 * yourself if you want it there). 152 * The result is an object with no container. 153 * @param workspace The workspace for the cloned object. 154 * @exception CloneNotSupportedException Not thrown in this base class 155 * @return The new Attribute. 156 */ 157 @Override 158 public Object clone(Workspace workspace) throws CloneNotSupportedException { 159 ImageAttribute newObject = (ImageAttribute) super.clone(workspace); 160 newObject._icon = (ImageIcon) newObject.getAttribute("_icon"); 161 162 return newObject; 163 } 164 165 /////////////////////////////////////////////////////////////////// 166 //// protected members //// 167 168 /** The image icon. */ 169 protected ImageIcon _icon; 170}