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)
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
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
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. */
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;
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 public class ImageIcon
57 implements Icon, Serializable, Accessible
60 * Accessibility support for ImageIcon.
62 protected class AccessibleImageIcon
63 extends AccessibleContext
64 implements AccessibleIcon, Serializable
66 private static final long serialVersionUID = 2113430526551336564L;
69 * Creates a new instance of AccessibleImageIcon.
71 protected AccessibleImageIcon()
76 * Returns the AccessibleRole of ImageIcon, which is
77 * {@link AccessibleRole#ICON}.
79 * @return {@link AccessibleRole#ICON}
81 public AccessibleRole getAccessibleRole()
83 return AccessibleRole.ICON;
87 * Returns the accessible state of this ImageIcon.
89 * @return the accessible state of this ImageIcon
91 public AccessibleStateSet getAccessibleStateSet()
93 // TODO: which state information from ImageIcon is returned here??
94 return new AccessibleStateSet();
98 * Returns the accessible parent of this object, which is <code>null</code>
99 * in this case, because ImageIcons have no parent.
101 * @return <code>null</code>, because ImageIcons have no parent
103 public Accessible getAccessibleParent()
105 // TODO: ImageIcons have no parent, have they ??
110 * Returns the index of this object in its accessible parent, which is
111 * -1 here, because ImageIcons have no accessible parent.
113 * @return -1 because ImageIcons have no parent
115 public int getAccessibleIndexInParent()
117 // TODO: do ImageIcons have parents??
122 * Returns the number of accessible children of this component,
123 * which is 0, because ImageIcons have no children.
125 * @return 0 because ImageIcons have no children
127 public int getAccessibleChildrenCount()
133 * Returns the accessible child at index <code>i</code>, which is
134 * <code>null</code> in this case because ImageIcons have no children.
136 * @param i the index of the child to be fetched
138 * @return <code>null</code> because ImageIcons have no children
140 public Accessible getAccessibleChild(int i)
146 * Returns the locale of this object. This returns the default locale
147 * that is set for the current VM.
149 * @return the locale of this object
151 public Locale getLocale()
153 return Locale.getDefault();
157 * Returns the accessible Icon description. This returns the
158 * actual 'description' property of the ImageIcon.
160 * @return the accessible Icon description
162 public String getAccessibleIconDescription()
164 return getDescription();
168 * Sets the accessible Icon description. This sets the
169 * actual 'description' property of the ImageIcon.
171 * @param newDescr the description to be set
173 public void setAccessibleIconDescription(String newDescr)
175 setDescription(newDescr);
179 * Returns the icon height. This returns the iconHeight property of
180 * the underlying Icon.
182 * @return the icon height
184 public int getAccessibleIconHeight()
186 return getIconHeight();
190 * Returns the icon width. This returns the iconWidth property of
191 * the underlying Icon.
193 * @return the icon width
195 public int getAccessibleIconWidth()
197 return getIconWidth();
201 private static final long serialVersionUID = 532615968316031794L;
203 /** A dummy Component that is used in the MediaTracker. */
204 protected static Component component = new Component(){};
206 /** The MediaTracker used to monitor the loading of images. */
207 protected static MediaTracker tracker = new MediaTracker(component);
209 /** The ID that is used in the tracker. */
210 private static int id;
214 ImageObserver observer;
216 /** The image loading status. */
217 private int loadStatus;
219 /** The AccessibleContext of this ImageIcon. */
220 private AccessibleContext accessibleContext;
226 public ImageIcon(String file)
231 public ImageIcon(String file, String description)
233 this(Toolkit.getDefaultToolkit().getImage(file), description);
236 public ImageIcon(byte[] imageData)
238 this(imageData, null);
241 public ImageIcon(byte[] imageData, String description)
243 this(Toolkit.getDefaultToolkit().createImage(imageData), description);
246 public ImageIcon(URL url)
251 public ImageIcon(URL url, String description)
253 this(Toolkit.getDefaultToolkit().getImage(url), description);
256 public ImageIcon(Image image)
261 public ImageIcon(Image image, String description)
264 setDescription(description);
267 public ImageObserver getImageObserver()
272 public void setImageObserver(ImageObserver newObserver)
274 observer = newObserver;
277 public Image getImage()
282 public void setImage(Image image)
288 public String getDescription()
293 public void setDescription(String description)
295 this.description = description;
298 public int getIconHeight()
300 return image.getHeight(observer);
303 public int getIconWidth()
305 return image.getWidth(observer);
308 public void paintIcon(Component c, Graphics g, int x, int y)
310 g.drawImage(image, x, y, observer != null ? observer : c);
314 * Loads the image and blocks until the loading operation is finished.
316 * @param image the image to be loaded
318 protected void loadImage(Image image)
322 tracker.addImage(image, id);
324 tracker.waitForID(id - 1);
326 catch (InterruptedException ex)
328 ; // ignore this for now
332 loadStatus = tracker.statusID(id - 1, false);
337 * Returns the load status of the icon image.
339 * @return the load status of the icon image
341 * @see {@link MediaTracker.COMPLETE}
342 * @see {@link MediaTracker.ABORTED}
343 * @see {@link MediaTracker.ERRORED}
345 public int getImageLoadStatus()
351 * Returns the AccessibleContext for this ImageIcon.
353 * @return the AccessibleContext for this ImageIcon
355 public AccessibleContext getAccessibleContext()
357 if (accessibleContext == null)
358 accessibleContext = new AccessibleImageIcon();
359 return accessibleContext;