2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / awt / MediaTracker.java
blobb1157349b5b6c3e424146bd9d6b3898ee7800acf
1 /* MediaTracker.java -- Class used for keeping track of images
2 Copyright (C) 1999, 2002 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.util.ArrayList;
42 import java.awt.image.ImageObserver;
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
85 status = LOADING;
87 synchronized (MediaTracker.this)
89 MediaTracker.this.notifyAll();
91 // If status is not COMPLETE then we need more updates.
92 return (status & COMPLETE) == 0;
96 public MediaTracker(Component c)
98 target = c;
101 public void addImage(Image image, int id)
103 MediaEntry e = new MediaEntry();
104 e.id = id;
105 e.image = image;
106 e.next = head;
107 head = e;
108 // Start tracking image status.
109 target.checkImage(image, e);
112 public void addImage(Image image, int id, int width, int height)
114 MediaEntry e = new MediaEntry();
115 e.id = id;
116 e.image = image;
117 e.next = head;
118 e.width = width;
119 e.height = height;
120 head = e;
121 // Start tracking image status.
122 target.checkImage(image, width, height, e);
125 public boolean checkAll()
127 return checkAll(false);
130 public boolean checkAll(boolean load)
132 MediaEntry e = head;
133 boolean result = true;
135 while (e != null)
137 if ((e.status & COMPLETE) == 0)
139 if (load)
141 result = false;
142 if (e.status == 0)
144 target.prepareImage(e.image, e);
145 e.status = LOADING;
148 else
149 return false;
151 e = e.next;
153 return result;
156 public boolean isErrorAny()
158 MediaEntry e = head;
159 while (e != null)
161 if ((e.status & ERRORED) != 0)
162 return true;
163 e = e.next;
165 return false;
168 public Object[] getErrorsAny()
170 MediaEntry e = head;
171 ArrayList result = null;
172 while (e != null)
174 if ((e.status & ERRORED) != 0)
176 if (result == null)
177 result = new ArrayList();
178 result.add(e.image);
180 e = e.next;
182 if (result == null)
183 return null;
184 else
185 return result.toArray();
188 public void waitForAll() throws InterruptedException
190 synchronized (this)
192 while (checkAll(true) == false)
193 wait();
197 public boolean waitForAll(long ms) throws InterruptedException
199 long start = System.currentTimeMillis();
200 synchronized (this)
202 while (!checkAll(true))
203 wait(ms);
205 if ((System.currentTimeMillis() - start) < ms)
206 return true;
207 else
208 return false;
211 public int statusAll(boolean load)
213 int result = 0;
214 MediaEntry e = head;
215 while (e != null)
217 if (load && e.status == 0)
219 target.prepareImage(e.image, e);
220 e.status = LOADING;
222 result |= e.status;
223 e = e.next;
225 return result;
228 public boolean checkID(int id)
230 return checkID(id, false);
233 public boolean checkID(int id, boolean load)
235 MediaEntry e = head;
236 boolean result = true;
238 while (e != null)
240 if (e.id == id && ((e.status & COMPLETE) == 0))
242 if (load)
244 result = false;
245 if (e.status == 0)
247 target.prepareImage(e.image, e);
248 e.status = LOADING;
251 else
252 return false;
254 e = e.next;
256 return result;
259 public boolean isErrorID(int id)
261 MediaEntry e = head;
262 while (e != null)
264 if (e.id == id && ((e.status & ERRORED) != 0))
265 return true;
266 e = e.next;
268 return false;
271 public Object[] getErrorsID(int id)
273 MediaEntry e = head;
274 ArrayList result = null;
275 while (e != null)
277 if (e.id == id && ((e.status & ERRORED) != 0))
279 if (result == null)
280 result = new ArrayList();
281 result.add(e.image);
283 e = e.next;
285 if (result == null)
286 return null;
287 else
288 return result.toArray();
291 public void waitForID(int id) throws InterruptedException
293 MediaEntry e = head;
294 synchronized (this)
296 while (checkID (id, true) == false)
297 wait();
301 public boolean waitForID(int id, long ms) throws InterruptedException
303 MediaEntry e = head;
304 long start = System.currentTimeMillis();
305 synchronized (this)
307 while (checkID (id, true) == false)
308 wait(ms);
310 if ((System.currentTimeMillis() - start) < ms)
311 return true;
312 else
313 return false;
316 public int statusID(int id, boolean load)
318 int result = 0;
319 MediaEntry e = head;
320 while (e != null)
322 if (e.id == id)
324 if (load && e.status == 0)
326 target.prepareImage(e.image, e);
327 e.status = LOADING;
329 result |= e.status;
331 e = e.next;
333 return result;
336 public void removeImage(Image image)
338 MediaEntry e = head;
339 MediaEntry prev = null;
340 while (e != null)
342 if (e.image == image)
344 if (prev == null)
345 head = e.next;
346 else
347 prev.next = e.next;
349 prev = e;
350 e = e.next;
354 public void removeImage(Image image, int id)
356 MediaEntry e = head;
357 MediaEntry prev = null;
358 while (e != null)
360 if (e.id == id && e.image == image)
362 if (prev == null)
363 head = e.next;
364 else
365 prev.next = e.next;
367 else
368 prev = e;
369 e = e.next;
373 public void removeImage(Image image, int id, int width, int height)
375 MediaEntry e = head;
376 MediaEntry prev = null;
377 while (e != null)
379 if (e.id == id && e.image == image
380 && e.width == width && e.height == height)
382 if (prev == null)
383 head = e.next;
384 else
385 prev.next = e.next;
387 else
388 prev = e;
389 e = e.next;