Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / javax / swing / ImageIcon.java
blob9e6265830a337f7ecb8d62e0ba9c429ed796ddb1
1 /* ImageIcon.java --
2 Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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.
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.
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. */
38 package javax.swing;
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;
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;
56 /**
57 * An {@link Icon} implementation that is backed by an {@link Image}.
59 public class ImageIcon
60 implements Icon, Serializable, Accessible
62 /**
63 * Accessibility support for ImageIcon.
65 protected class AccessibleImageIcon
66 extends AccessibleContext
67 implements AccessibleIcon, Serializable
69 private static final long serialVersionUID = 2113430526551336564L;
71 /**
72 * Creates a new instance of AccessibleImageIcon.
74 protected AccessibleImageIcon()
76 // Nothing to do here.
79 /**
80 * Returns the AccessibleRole of ImageIcon, which is
81 * {@link AccessibleRole#ICON}.
83 * @return {@link AccessibleRole#ICON}
85 public AccessibleRole getAccessibleRole()
87 return AccessibleRole.ICON;
90 /**
91 * Returns the accessible state of this ImageIcon.
93 * @return the accessible state of this ImageIcon
95 public AccessibleStateSet getAccessibleStateSet()
97 // TODO: which state information from ImageIcon is returned here??
98 return new AccessibleStateSet();
102 * Returns the accessible parent of this object, which is <code>null</code>
103 * in this case, because ImageIcons have no parent.
105 * @return <code>null</code>, because ImageIcons have no parent
107 public Accessible getAccessibleParent()
109 // TODO: ImageIcons have no parent, have they ??
110 return null;
114 * Returns the index of this object in its accessible parent, which is
115 * -1 here, because ImageIcons have no accessible parent.
117 * @return -1 because ImageIcons have no parent
119 public int getAccessibleIndexInParent()
121 // TODO: do ImageIcons have parents??
122 return -1;
126 * Returns the number of accessible children of this component,
127 * which is 0, because ImageIcons have no children.
129 * @return 0 because ImageIcons have no children
131 public int getAccessibleChildrenCount()
133 return 0;
137 * Returns the accessible child at index <code>i</code>, which is
138 * <code>null</code> in this case because ImageIcons have no children.
140 * @param i the index of the child to be fetched
142 * @return <code>null</code> because ImageIcons have no children
144 public Accessible getAccessibleChild(int i)
146 return null;
150 * Returns the locale of this object. This returns the default locale
151 * that is set for the current VM.
153 * @return the locale of this object
155 public Locale getLocale()
157 return Locale.getDefault();
161 * Returns the accessible Icon description. This returns the
162 * actual 'description' property of the ImageIcon.
164 * @return the accessible Icon description
166 public String getAccessibleIconDescription()
168 return getDescription();
172 * Sets the accessible Icon description. This sets the
173 * actual 'description' property of the ImageIcon.
175 * @param newDescr the description to be set
177 public void setAccessibleIconDescription(String newDescr)
179 setDescription(newDescr);
183 * Returns the icon height. This returns the iconHeight property of
184 * the underlying Icon.
186 * @return the icon height
188 public int getAccessibleIconHeight()
190 return getIconHeight();
194 * Returns the icon width. This returns the iconWidth property of
195 * the underlying Icon.
197 * @return the icon width
199 public int getAccessibleIconWidth()
201 return getIconWidth();
203 } // AccessibleIcon
205 private static final long serialVersionUID = 532615968316031794L;
207 /** A dummy Component that is used in the MediaTracker. */
208 protected static final Component component = new Component()
210 // No need to implement this.
213 /** The MediaTracker used to monitor the loading of images. */
214 protected static final MediaTracker tracker = new MediaTracker(component);
216 /** The ID that is used in the tracker. */
217 private static int id;
219 Image image;
220 String description;
221 ImageObserver observer;
223 /** The image loading status. */
224 private int loadStatus;
226 /** The AccessibleContext of this ImageIcon. */
227 private AccessibleContext accessibleContext;
230 * Creates an ImageIcon without any properties set.
232 public ImageIcon()
234 // Nothing to do here.
238 * Constructs an ImageIcon given a filename. The icon's description
239 * is initially set to the filename itself. A filename of "" means
240 * create a blank icon.
242 * @param filename name of file to load or "" for a blank icon
244 public ImageIcon(String filename)
246 this(filename, filename);
250 * Constructs an ImageIcon from the given filename, setting its
251 * description to the given description. A filename of "" means
252 * create a blank icon.
254 * @param filename name of file to load or "" for a blank icon
255 * @param description human-readable description of this icon
257 public ImageIcon(String filename, String description)
259 this(Toolkit.getDefaultToolkit().getImage(filename), description);
263 * Creates an ImageIcon from the given byte array without any
264 * description set.
266 public ImageIcon(byte[] imageData)
268 this(imageData, null);
272 * Creates an ImageIcon from the given byte array and sets the given
273 * description.
275 public ImageIcon(byte[] imageData, String description)
277 this(Toolkit.getDefaultToolkit().createImage(imageData), description);
281 * Creates an ImageIcon from the given URL without any description
282 * set.
284 public ImageIcon(URL url)
286 this(url, null);
290 * Creates an ImageIcon from the given URL and sets the given
291 * description.
293 public ImageIcon(URL url, String description)
295 this(Toolkit.getDefaultToolkit().getImage(url), description);
299 * Creates an ImageIcon from the given Image without any description
300 * set.
302 public ImageIcon(Image image)
304 this(image, null);
308 * Creates an ImageIcon from the given Image and sets the given
309 * description.
311 public ImageIcon(Image image, String description)
313 setImage(image);
314 setDescription(description);
318 * Returns the ImageObserver that is used for all Image
319 * operations. Defaults to null when not explicitly set.
321 public ImageObserver getImageObserver()
323 return observer;
327 * Sets the ImageObserver that will be used for all Image
328 * operations. Can be set to null (the default) when no observer is
329 * needed.
331 public void setImageObserver(ImageObserver newObserver)
333 observer = newObserver;
337 * Returns the backing Image for this ImageIcon. Might be set to
338 * null in which case no image is shown.
340 public Image getImage()
342 return image;
346 * Explicitly sets the backing Image for this ImageIcon. Will call
347 * loadImage() to make sure that the Image is completely loaded
348 * before returning.
350 public void setImage(Image image)
352 loadImage(image);
353 this.image = image;
357 * Returns a human readable description for this ImageIcon or null
358 * when no description is set or available.
360 public String getDescription()
362 return description;
366 * Sets a human readable description for this ImageIcon. Can be set
367 * to null when no description is available.
369 public void setDescription(String description)
371 this.description = description;
375 * Returns the the height of the backing Image, or -1 if the backing
376 * Image is null. The getHeight() method of the Image will be called
377 * with the set observer of this ImageIcon.
379 public int getIconHeight()
381 if (image == null)
382 return -1;
384 return image.getHeight(observer);
388 * Returns the the width of the backing Image, or -1 if the backing
389 * Image is null. The getWidth() method of the Image will be called
390 * with the set observer of this ImageIcon.
392 public int getIconWidth()
394 if (image == null)
395 return -1;
397 return image.getWidth(observer);
401 * Calls <code>g.drawImage()</code> on the backing Image using the
402 * set observer of this ImageIcon. If the set observer is null, the
403 * given Component is used as observer.
405 public void paintIcon(Component c, Graphics g, int x, int y)
407 g.drawImage(image, x, y, observer != null ? observer : c);
411 * Loads the image and blocks until the loading operation is finished.
413 * @param image the image to be loaded
415 protected void loadImage(Image image)
419 tracker.addImage(image, id);
420 id++;
421 tracker.waitForID(id - 1);
423 catch (InterruptedException ex)
425 // Ignore this for now.
427 finally
429 loadStatus = tracker.statusID(id - 1, false);
434 * Returns the load status of the icon image.
436 * @return the load status of the icon image
438 * @see MediaTracker#COMPLETE
439 * @see MediaTracker#ABORTED
440 * @see MediaTracker#ERRORED
442 public int getImageLoadStatus()
444 return loadStatus;
448 * Returns the AccessibleContext for this ImageIcon.
450 * @return the AccessibleContext for this ImageIcon
452 public AccessibleContext getAccessibleContext()
454 if (accessibleContext == null)
455 accessibleContext = new AccessibleImageIcon();
456 return accessibleContext;