Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / java / awt / MediaTracker.java
blobe69832d11a07cbe32eca232b920eff19ead89223
1 /* MediaTracker.java -- Class used for keeping track of images
2 Copyright (C) 1999, 2002i, 2004 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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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. */
39 package java.awt;
41 import java.awt.image.ImageObserver;
42 import java.util.ArrayList;
44 /**
45 * This class is used for keeping track of the status of various media
46 * objects.
48 * @author Aaron M. Renn (arenn@urbanophile.com)
49 * @author Bryce McKinlay
51 public class MediaTracker implements java.io.Serializable
53 public static final int LOADING = 1 << 0;
54 public static final int ABORTED = 1 << 1;
55 public static final int ERRORED = 1 << 2;
56 public static final int COMPLETE = 1 << 3;
58 Component target;
59 MediaEntry head;
61 static final long serialVersionUID = -483174189758638095L;
63 // FIXME: The serialized form documentation says MediaEntry is a
64 // serializable field, but the serialized form of MediaEntry itself
65 // doesn't appear to be documented.
66 class MediaEntry implements ImageObserver
68 int id;
69 Image image;
70 MediaEntry next;
71 int status;
72 int width;
73 int height;
75 public boolean imageUpdate(Image img, int flags, int x, int y,
76 int width, int height)
78 if ((flags & ABORT) != 0)
79 status = ABORTED | COMPLETE;
80 else if ((flags & ERROR) != 0)
81 status = ERRORED | COMPLETE;
82 else if ((flags & ALLBITS) != 0)
83 status = COMPLETE;
84 else if ((flags & SOMEBITS) != 0)
85 status = LOADING;
86 else
87 status = 0;
89 if ((status & COMPLETE) == COMPLETE)
91 synchronized (MediaTracker.this)
93 MediaTracker.this.notifyAll();
96 // If status is not COMPLETE then we need more updates.
97 return (status & COMPLETE) == 0;
101 public MediaTracker(Component c)
103 target = c;
106 public void addImage(Image image, int id)
108 MediaEntry e = new MediaEntry();
109 e.id = id;
110 e.image = image;
111 e.next = head;
112 head = e;
113 // Start tracking image status.
114 int flags = target.checkImage(image, e);
115 e.imageUpdate(image, flags, -1, -1, -1, -1);
118 public void addImage(Image image, int id, int width, int height)
120 MediaEntry e = new MediaEntry();
121 e.id = id;
122 e.image = image;
123 e.next = head;
124 e.width = width;
125 e.height = height;
126 head = e;
127 // Start tracking image status.
128 int flags = target.checkImage(image, width, height, e);
129 e.imageUpdate(image, flags, -1, -1, width, height);
132 public boolean checkAll()
134 return checkAll(false);
137 public boolean checkAll(boolean load)
139 MediaEntry e = head;
140 boolean result = true;
142 while (e != null)
144 if ((e.status & COMPLETE) == 0)
146 if (load)
148 result = false;
149 if (e.status == 0)
151 target.prepareImage(e.image, e);
152 e.status = LOADING;
155 else
156 return false;
158 e = e.next;
160 return result;
163 public boolean isErrorAny()
165 MediaEntry e = head;
166 while (e != null)
168 if ((e.status & ERRORED) != 0)
169 return true;
170 e = e.next;
172 return false;
175 public Object[] getErrorsAny()
177 MediaEntry e = head;
178 ArrayList result = null;
179 while (e != null)
181 if ((e.status & ERRORED) != 0)
183 if (result == null)
184 result = new ArrayList();
185 result.add(e.image);
187 e = e.next;
189 if (result == null)
190 return null;
191 else
192 return result.toArray();
195 public void waitForAll() throws InterruptedException
197 synchronized (this)
199 while (checkAll(true) == false)
200 wait();
204 public boolean waitForAll(long ms) throws InterruptedException
206 long start = System.currentTimeMillis();
207 synchronized (this)
209 while (!checkAll(true))
210 wait(ms);
212 if ((System.currentTimeMillis() - start) < ms)
213 return true;
214 else
215 return false;
218 public int statusAll(boolean load)
220 int result = 0;
221 MediaEntry e = head;
222 while (e != null)
224 if (load && e.status == 0)
226 target.prepareImage(e.image, e);
227 e.status = LOADING;
229 result |= e.status;
230 e = e.next;
232 return result;
235 public boolean checkID(int id)
237 return checkID(id, false);
240 public boolean checkID(int id, boolean load)
242 MediaEntry e = head;
243 boolean result = true;
245 while (e != null)
247 if (e.id == id && ((e.status & COMPLETE) == 0))
249 if (load)
251 result = false;
252 if (e.status == 0)
254 target.prepareImage(e.image, e);
255 e.status = LOADING;
258 else
259 return false;
261 e = e.next;
263 return result;
266 public boolean isErrorID(int id)
268 MediaEntry e = head;
269 while (e != null)
271 if (e.id == id && ((e.status & ERRORED) != 0))
272 return true;
273 e = e.next;
275 return false;
278 public Object[] getErrorsID(int id)
280 MediaEntry e = head;
281 ArrayList result = null;
282 while (e != null)
284 if (e.id == id && ((e.status & ERRORED) != 0))
286 if (result == null)
287 result = new ArrayList();
288 result.add(e.image);
290 e = e.next;
292 if (result == null)
293 return null;
294 else
295 return result.toArray();
298 public void waitForID(int id) throws InterruptedException
300 MediaEntry e = head;
301 synchronized (this)
303 while (checkID (id, true) == false)
304 wait();
308 public boolean waitForID(int id, long ms) throws InterruptedException
310 MediaEntry e = head;
311 long start = System.currentTimeMillis();
312 synchronized (this)
314 while (checkID (id, true) == false)
315 wait(ms);
317 if ((System.currentTimeMillis() - start) < ms)
318 return true;
319 else
320 return false;
323 public int statusID(int id, boolean load)
325 int result = 0;
326 MediaEntry e = head;
327 while (e != null)
329 if (e.id == id)
331 if (load && e.status == 0)
333 target.prepareImage(e.image, e);
334 e.status = LOADING;
336 result |= e.status;
338 e = e.next;
340 return result;
343 public void removeImage(Image image)
345 MediaEntry e = head;
346 MediaEntry prev = null;
347 while (e != null)
349 if (e.image == image)
351 if (prev == null)
352 head = e.next;
353 else
354 prev.next = e.next;
356 prev = e;
357 e = e.next;
361 public void removeImage(Image image, int id)
363 MediaEntry e = head;
364 MediaEntry prev = null;
365 while (e != null)
367 if (e.id == id && e.image == image)
369 if (prev == null)
370 head = e.next;
371 else
372 prev.next = e.next;
374 else
375 prev = e;
376 e = e.next;
380 public void removeImage(Image image, int id, int width, int height)
382 MediaEntry e = head;
383 MediaEntry prev = null;
384 while (e != null)
386 if (e.id == id && e.image == image
387 && e.width == width && e.height == height)
389 if (prev == null)
390 head = e.next;
391 else
392 prev.next = e.next;
394 else
395 prev = e;
396 e = e.next;