Source for javax.swing.ImageIcon

   1: /* ImageIcon.java --
   2:    Copyright (C) 2002, 2004, 2005  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package javax.swing;
  39: 
  40: import java.awt.Component;
  41: import java.awt.Graphics;
  42: import java.awt.Image;
  43: import java.awt.MediaTracker;
  44: import java.awt.Toolkit;
  45: import java.awt.image.ImageObserver;
  46: import java.io.Serializable;
  47: import java.net.URL;
  48: import java.util.Locale;
  49: 
  50: import javax.accessibility.Accessible;
  51: import javax.accessibility.AccessibleContext;
  52: import javax.accessibility.AccessibleIcon;
  53: import javax.accessibility.AccessibleRole;
  54: import javax.accessibility.AccessibleStateSet;
  55: 
  56: public class ImageIcon
  57:   implements Icon, Serializable, Accessible
  58: {
  59:   /**
  60:    * Accessibility support for ImageIcon.
  61:    */
  62:   protected class AccessibleImageIcon
  63:     extends AccessibleContext
  64:     implements AccessibleIcon, Serializable
  65:   {
  66:     private static final long serialVersionUID = 2113430526551336564L;
  67: 
  68:     /**
  69:      * Creates a new instance of AccessibleImageIcon.
  70:      */
  71:     protected AccessibleImageIcon()
  72:     {
  73:     }
  74: 
  75:     /**
  76:      * Returns the AccessibleRole of ImageIcon, which is
  77:      * {@link AccessibleRole#ICON}.
  78:      *
  79:      * @return {@link AccessibleRole#ICON}
  80:      */
  81:     public AccessibleRole getAccessibleRole()
  82:     {
  83:       return AccessibleRole.ICON;
  84:     }
  85: 
  86:     /**
  87:      * Returns the accessible state of this ImageIcon.
  88:      *
  89:      * @return the accessible state of this ImageIcon
  90:      */
  91:     public AccessibleStateSet getAccessibleStateSet()
  92:     {
  93:       // TODO: which state information from ImageIcon is returned here??
  94:       return new AccessibleStateSet();
  95:     }
  96: 
  97:     /**
  98:      * Returns the accessible parent of this object, which is <code>null</code>
  99:      * in this case, because ImageIcons have no parent.
 100:      *
 101:      * @return <code>null</code>, because ImageIcons have no parent
 102:      */
 103:     public Accessible getAccessibleParent()
 104:     {
 105:       // TODO: ImageIcons have no parent, have they ??
 106:       return null;
 107:     }
 108: 
 109:     /**
 110:      * Returns the index of this object in its accessible parent, which is
 111:      * -1 here, because ImageIcons have no accessible parent.
 112:      *
 113:      * @return -1 because ImageIcons have no parent
 114:      */
 115:     public int getAccessibleIndexInParent()
 116:     {
 117:       // TODO: do ImageIcons have parents??
 118:       return -1;
 119:     }
 120: 
 121:     /**
 122:      * Returns the number of accessible children of this component,
 123:      * which is 0, because ImageIcons have no children.
 124:      *
 125:      * @return 0 because ImageIcons have no children
 126:      */
 127:     public int getAccessibleChildrenCount()
 128:     {
 129:       return 0;
 130:     }
 131: 
 132:     /**
 133:      * Returns the accessible child at index <code>i</code>, which is
 134:      * <code>null</code> in this case because ImageIcons have no children.
 135:      *
 136:      * @param i the index of the child to be fetched
 137:      *
 138:      * @return <code>null</code> because ImageIcons have no children
 139:      */
 140:     public Accessible getAccessibleChild(int i)
 141:     {
 142:       return null;
 143:     }
 144: 
 145:     /**
 146:      * Returns the locale of this object. This returns the default locale
 147:      * that is set for the current VM.
 148:      *
 149:      * @return the locale of this object
 150:      */
 151:     public Locale getLocale()
 152:     {
 153:       return Locale.getDefault();
 154:     }
 155: 
 156:     /**
 157:      * Returns the accessible Icon description. This returns the
 158:      * actual 'description' property of the ImageIcon.
 159:      *
 160:      * @return the accessible Icon description
 161:      */
 162:     public String getAccessibleIconDescription()
 163:     {
 164:       return getDescription();
 165:     }
 166: 
 167:     /**
 168:      * Sets the accessible Icon description. This sets the
 169:      * actual 'description' property of the ImageIcon.
 170:      *
 171:      * @param newDescr the description to be set
 172:      */
 173:     public void setAccessibleIconDescription(String newDescr)
 174:     {
 175:       setDescription(newDescr);
 176:     }
 177: 
 178:     /**
 179:      * Returns the icon height. This returns the iconHeight property of
 180:      * the underlying Icon.
 181:      *
 182:      * @return the icon height
 183:      */
 184:     public int getAccessibleIconHeight()
 185:     {
 186:       return getIconHeight();
 187:     }
 188:     
 189:     /**
 190:      * Returns the icon width. This returns the iconWidth property of
 191:      * the underlying Icon.
 192:      *
 193:      * @return the icon width
 194:      */
 195:     public int getAccessibleIconWidth()
 196:     {
 197:       return getIconWidth();
 198:     }
 199:   } // AccessibleIcon
 200: 
 201:   private static final long serialVersionUID = 532615968316031794L;
 202: 
 203:   /** A dummy Component that is used in the MediaTracker. */
 204:   protected static Component component = new Component(){};
 205: 
 206:   /** The MediaTracker used to monitor the loading of images. */
 207:   protected static MediaTracker tracker = new MediaTracker(component);
 208: 
 209:   /** The ID that is used in the tracker. */
 210:   private static int id;
 211: 
 212:   Image image;
 213:   String description;
 214:   ImageObserver observer;
 215: 
 216:   /** The image loading status. */
 217:   private int loadStatus;
 218: 
 219:   /** The AccessibleContext of this ImageIcon. */
 220:   private AccessibleContext accessibleContext;
 221: 
 222:   public ImageIcon()
 223:   {
 224:   }
 225:   
 226:   public ImageIcon(String file)
 227:   {
 228:     this(file, file);
 229:   }
 230: 
 231:   public ImageIcon(String file, String description)
 232:   {
 233:     this(Toolkit.getDefaultToolkit().getImage(file), description);
 234:   }
 235: 
 236:   public ImageIcon(byte[] imageData)
 237:   {
 238:     this(imageData, null);
 239:   }
 240:   
 241:   public ImageIcon(byte[] imageData, String description)
 242:   {
 243:     this(Toolkit.getDefaultToolkit().createImage(imageData), description);
 244:   }
 245: 
 246:   public ImageIcon(URL url)
 247:   {
 248:     this(url, null);
 249:   }
 250: 
 251:   public ImageIcon(URL url, String description)
 252:   {
 253:     this(Toolkit.getDefaultToolkit().getImage(url), description);
 254:   }
 255: 
 256:   public ImageIcon(Image image)
 257:   {
 258:     this(image, null);
 259:   }
 260: 
 261:   public ImageIcon(Image image, String description)
 262:   {
 263:     setImage(image);
 264:     setDescription(description);
 265:   }
 266:     
 267:   public ImageObserver getImageObserver()
 268:   {
 269:     return observer;
 270:   }
 271:   
 272:   public void setImageObserver(ImageObserver newObserver)
 273:   {
 274:     observer = newObserver;
 275:   }
 276: 
 277:   public Image getImage()
 278:   {
 279:     return image;
 280:   }
 281: 
 282:   public void setImage(Image image)
 283:   {
 284:     loadImage(image);
 285:     this.image = image;
 286:   }
 287: 
 288:   public String getDescription()
 289:   {
 290:     return description;
 291:   }
 292: 
 293:   public void setDescription(String description)
 294:   {
 295:     this.description = description;
 296:   }
 297: 
 298:   public int getIconHeight()
 299:   {
 300:     return image.getHeight(observer);
 301:   }
 302: 
 303:   public int getIconWidth()
 304:   {
 305:     return image.getWidth(observer);
 306:   }
 307: 
 308:   public void paintIcon(Component c, Graphics g, int x, int y)
 309:   {
 310:     g.drawImage(image, x, y, observer != null ? observer : c);
 311:   }
 312: 
 313:   /**
 314:    * Loads the image and blocks until the loading operation is finished.
 315:    *
 316:    * @param image the image to be loaded
 317:    */
 318:   protected void loadImage(Image image)
 319:   {
 320:     try
 321:       {
 322:         tracker.addImage(image, id);
 323:         id++;
 324:         tracker.waitForID(id - 1);
 325:       }
 326:     catch (InterruptedException ex)
 327:       {
 328:         ; // ignore this for now
 329:       }
 330:     finally
 331:       {
 332:         loadStatus = tracker.statusID(id - 1, false);
 333:       }
 334:   }
 335: 
 336:   /**
 337:    * Returns the load status of the icon image.
 338:    *
 339:    * @return the load status of the icon image
 340:    *
 341:    * @see {@link MediaTracker.COMPLETE}
 342:    * @see {@link MediaTracker.ABORTED}
 343:    * @see {@link MediaTracker.ERRORED}
 344:    */
 345:   public int getImageLoadStatus()
 346:   {
 347:     return loadStatus;
 348:   }
 349: 
 350:   /**
 351:    * Returns the AccessibleContext for this ImageIcon.
 352:    *
 353:    * @return the AccessibleContext for this ImageIcon
 354:    */
 355:   public AccessibleContext getAccessibleContext()
 356:   {
 357:     if (accessibleContext == null)
 358:       accessibleContext = new AccessibleImageIcon();
 359:     return accessibleContext;
 360:   }
 361: }