001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.swing;
019:
020: import java.awt.Component;
021: import java.awt.Graphics;
022: import java.awt.IllegalComponentStateException;
023: import java.awt.Image;
024: import java.awt.MediaTracker;
025: import java.awt.Panel;
026: import java.awt.Toolkit;
027: import java.awt.image.ImageObserver;
028: import java.io.Serializable;
029: import java.net.URL;
030: import java.util.Locale;
031: import javax.accessibility.Accessible;
032: import javax.accessibility.AccessibleContext;
033: import javax.accessibility.AccessibleIcon;
034: import javax.accessibility.AccessibleRole;
035: import javax.accessibility.AccessibleStateSet;
036: import org.apache.harmony.luni.util.NotImplementedException;
037:
038: /**
039: * <p>
040: * <i>ImageIcon</i>
041: * </p>
042: * <h3>Implementation Notes:</h3>
043: * <ul>
044: * <li>The <code>serialVersionUID</code> fields are explicitly declared as a performance
045: * optimization, not as a guarantee of serialization compatibility.</li>
046: * </ul>
047: */
048: public class ImageIcon implements Icon, Serializable, Accessible {
049: private static final long serialVersionUID = -6101950798829449111L;
050:
051: protected class AccessibleImageIcon extends AccessibleContext
052: implements AccessibleIcon, Serializable {
053: private static final long serialVersionUID = -860693743697825660L;
054:
055: protected AccessibleImageIcon() {
056: }
057:
058: @Override
059: public Accessible getAccessibleChild(int i) {
060: return null;
061: }
062:
063: @Override
064: public int getAccessibleChildrenCount() {
065: return 0;
066: }
067:
068: @Override
069: public Accessible getAccessibleParent() {
070: return null;
071: }
072:
073: @Override
074: public int getAccessibleIndexInParent() {
075: return -1;
076: }
077:
078: @Override
079: public AccessibleRole getAccessibleRole() {
080: return AccessibleRole.ICON;
081: }
082:
083: @Override
084: public AccessibleStateSet getAccessibleStateSet() {
085: return null;
086: }
087:
088: @Override
089: public Locale getLocale() throws IllegalComponentStateException {
090: return null;
091: }
092:
093: public void setAccessibleIconDescription(String description) {
094: setDescription(description);
095: }
096:
097: public String getAccessibleIconDescription() {
098: return getDescription();
099: }
100:
101: public int getAccessibleIconWidth() {
102: return getIconWidth();
103: }
104:
105: public int getAccessibleIconHeight() {
106: return getIconHeight();
107: }
108: }
109:
110: protected static final Component component;
111:
112: protected static final MediaTracker tracker;
113:
114: private ImageObserver observer;
115:
116: private AccessibleContext accessibleContext;
117:
118: private Image image;
119:
120: private String description;
121:
122: /**
123: * Unique id is to enable image loading tracking via MediaTracker
124: */
125: private final int id = getUniqueID();
126:
127: /**
128: * The last unique ID assigned.
129: */
130: private static int lastAssignedID;
131: static {
132: component = new Panel();
133: tracker = new MediaTracker(component);
134: lastAssignedID = 0;
135: }
136:
137: /**
138: * 'generates' unique ids for icons
139: */
140: private final static int getUniqueID() {
141: return lastAssignedID++;
142: }
143:
144: public ImageIcon(URL url, String description) {
145: this (url);
146: this .description = description;
147: }
148:
149: public ImageIcon(String fileName, String description) {
150: this (fileName);
151: this .description = description;
152: }
153:
154: public ImageIcon(Image image, String description) {
155: this (image);
156: this .description = description;
157: }
158:
159: public ImageIcon(byte[] imageData, String description) {
160: this (imageData);
161: this .description = description;
162: }
163:
164: public ImageIcon(URL url) {
165: image = Toolkit.getDefaultToolkit().createImage(url);
166: loadImage(image);
167: description = url.toString();
168: }
169:
170: public ImageIcon(String fileName) {
171: image = Toolkit.getDefaultToolkit().createImage(fileName);
172: loadImage(image);
173: description = fileName;
174: }
175:
176: public ImageIcon(Image image) {
177: this .image = image;
178: loadImage(image);
179: Object comment = image.getProperty("comment", observer);
180: if (comment instanceof String) {
181: description = (String) comment;
182: }
183: }
184:
185: public ImageIcon(byte[] imageData) {
186: image = Toolkit.getDefaultToolkit().createImage(imageData);
187: loadImage(image);
188: Object comment = image.getProperty("comment", observer);
189: if (comment instanceof String) {
190: description = (String) comment;
191: }
192: }
193:
194: public ImageIcon() {
195: }
196:
197: public synchronized void paintIcon(Component c, Graphics g, int x,
198: int y) {
199: g.drawImage(image, x, y, (observer != null) ? observer : c);
200: }
201:
202: public AccessibleContext getAccessibleContext() {
203: return (accessibleContext == null) ? (accessibleContext = new AccessibleImageIcon())
204: : accessibleContext;
205: }
206:
207: public void setDescription(String description) {
208: this .description = description;
209: }
210:
211: @Override
212: public String toString() {
213: return description != null ? description : super .toString();
214: }
215:
216: public String getDescription() {
217: return description;
218: }
219:
220: public void setImageObserver(ImageObserver observer) {
221: this .observer = observer;
222: }
223:
224: public ImageObserver getImageObserver() {
225: return observer;
226: }
227:
228: public void setImage(Image newImage) {
229: tracker.removeImage(image);
230: image = newImage;
231: tracker.addImage(image, id);
232: }
233:
234: protected void loadImage(Image image) {
235: tracker.addImage(image, id);
236: try {
237: tracker.waitForID(id);
238: } catch (InterruptedException e) {
239: // reset the interrupt state for the current thread
240: Thread.currentThread().interrupt();
241: }
242: }
243:
244: public Image getImage() {
245: return image;
246: }
247:
248: public int getImageLoadStatus() {
249: return tracker.statusID(id, false);
250: }
251:
252: public int getIconWidth() {
253: return (image != null) ? image.getWidth(observer) : -1;
254: }
255:
256: public int getIconHeight() {
257: return (image != null) ? image.getHeight(observer) : -1;
258: }
259: }
|