1 | /* MediaTracker.java -- Class used for keeping track of images
|
---|
2 | Copyright (C) 1999, 2002 Free Software Foundation, Inc.
|
---|
3 |
|
---|
4 | This file is part of GNU Classpath.
|
---|
5 |
|
---|
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.
|
---|
10 |
|
---|
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.
|
---|
15 |
|
---|
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.
|
---|
20 |
|
---|
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.
|
---|
25 |
|
---|
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. */
|
---|
37 |
|
---|
38 |
|
---|
39 | package java.awt;
|
---|
40 |
|
---|
41 | import java.util.ArrayList;
|
---|
42 | import java.awt.image.ImageObserver;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * This class is used for keeping track of the status of various media
|
---|
46 | * objects.
|
---|
47 | *
|
---|
48 | * @author Aaron M. Renn (arenn@urbanophile.com)
|
---|
49 | * @author Bryce McKinlay
|
---|
50 | */
|
---|
51 | public class MediaTracker implements java.io.Serializable
|
---|
52 | {
|
---|
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;
|
---|
57 |
|
---|
58 | Component target;
|
---|
59 | MediaEntry head;
|
---|
60 |
|
---|
61 | static final long serialVersionUID = -483174189758638095L;
|
---|
62 |
|
---|
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
|
---|
67 | {
|
---|
68 | int id;
|
---|
69 | Image image;
|
---|
70 | MediaEntry next;
|
---|
71 | int status;
|
---|
72 | int width;
|
---|
73 | int height;
|
---|
74 |
|
---|
75 | public boolean imageUpdate(Image img, int flags, int x, int y,
|
---|
76 | int width, int height)
|
---|
77 | {
|
---|
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;
|
---|
86 |
|
---|
87 | synchronized (MediaTracker.this)
|
---|
88 | {
|
---|
89 | MediaTracker.this.notifyAll();
|
---|
90 | }
|
---|
91 |
|
---|
92 | return ((status & COMPLETE) != 0);
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | public MediaTracker(Component c)
|
---|
97 | {
|
---|
98 | target = c;
|
---|
99 | }
|
---|
100 |
|
---|
101 | public void addImage(Image image, int id)
|
---|
102 | {
|
---|
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);
|
---|
110 | }
|
---|
111 |
|
---|
112 | public void addImage(Image image, int id, int width, int height)
|
---|
113 | {
|
---|
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);
|
---|
123 | }
|
---|
124 |
|
---|
125 | public boolean checkAll()
|
---|
126 | {
|
---|
127 | return checkAll(false);
|
---|
128 | }
|
---|
129 |
|
---|
130 | public boolean checkAll(boolean load)
|
---|
131 | {
|
---|
132 | MediaEntry e = head;
|
---|
133 | boolean result = true;
|
---|
134 |
|
---|
135 | while (e != null)
|
---|
136 | {
|
---|
137 | if ((e.status & COMPLETE) == 0)
|
---|
138 | {
|
---|
139 | if (load)
|
---|
140 | {
|
---|
141 | result = false;
|
---|
142 | if (e.status == 0)
|
---|
143 | {
|
---|
144 | target.prepareImage(e.image, e);
|
---|
145 | e.status = LOADING;
|
---|
146 | }
|
---|
147 | }
|
---|
148 | else
|
---|
149 | return false;
|
---|
150 | }
|
---|
151 | e = e.next;
|
---|
152 | }
|
---|
153 | return result;
|
---|
154 | }
|
---|
155 |
|
---|
156 | public boolean isErrorAny()
|
---|
157 | {
|
---|
158 | MediaEntry e = head;
|
---|
159 | while (e != null)
|
---|
160 | {
|
---|
161 | if ((e.status & ERRORED) != 0)
|
---|
162 | return true;
|
---|
163 | e = e.next;
|
---|
164 | }
|
---|
165 | return false;
|
---|
166 | }
|
---|
167 |
|
---|
168 | public Object[] getErrorsAny()
|
---|
169 | {
|
---|
170 | MediaEntry e = head;
|
---|
171 | ArrayList result = null;
|
---|
172 | while (e != null)
|
---|
173 | {
|
---|
174 | if ((e.status & ERRORED) != 0)
|
---|
175 | {
|
---|
176 | if (result == null)
|
---|
177 | result = new ArrayList();
|
---|
178 | result.add(e.image);
|
---|
179 | }
|
---|
180 | e = e.next;
|
---|
181 | }
|
---|
182 | if (result == null)
|
---|
183 | return null;
|
---|
184 | else
|
---|
185 | return result.toArray();
|
---|
186 | }
|
---|
187 |
|
---|
188 | public void waitForAll() throws InterruptedException
|
---|
189 | {
|
---|
190 | synchronized (this)
|
---|
191 | {
|
---|
192 | while (checkAll(true) == false)
|
---|
193 | wait();
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | public boolean waitForAll(long ms) throws InterruptedException
|
---|
198 | {
|
---|
199 | long start = System.currentTimeMillis();
|
---|
200 | synchronized (this)
|
---|
201 | {
|
---|
202 | while (!checkAll(true))
|
---|
203 | wait(ms);
|
---|
204 | }
|
---|
205 | if ((System.currentTimeMillis() - start) < ms)
|
---|
206 | return true;
|
---|
207 | else
|
---|
208 | return false;
|
---|
209 | }
|
---|
210 |
|
---|
211 | public int statusAll(boolean load)
|
---|
212 | {
|
---|
213 | int result = 0;
|
---|
214 | MediaEntry e = head;
|
---|
215 | while (e != null)
|
---|
216 | {
|
---|
217 | if (load && e.status == 0)
|
---|
218 | {
|
---|
219 | target.prepareImage(e.image, e);
|
---|
220 | e.status = LOADING;
|
---|
221 | }
|
---|
222 | result |= e.status;
|
---|
223 | e = e.next;
|
---|
224 | }
|
---|
225 | return result;
|
---|
226 | }
|
---|
227 |
|
---|
228 | public boolean checkID(int id)
|
---|
229 | {
|
---|
230 | return checkID(id, false);
|
---|
231 | }
|
---|
232 |
|
---|
233 | public boolean checkID(int id, boolean load)
|
---|
234 | {
|
---|
235 | MediaEntry e = head;
|
---|
236 | boolean result = true;
|
---|
237 |
|
---|
238 | while (e != null)
|
---|
239 | {
|
---|
240 | if (e.id == id && ((e.status & COMPLETE) == 0))
|
---|
241 | {
|
---|
242 | if (load)
|
---|
243 | {
|
---|
244 | result = false;
|
---|
245 | if (e.status == 0)
|
---|
246 | {
|
---|
247 | target.prepareImage(e.image, e);
|
---|
248 | e.status = LOADING;
|
---|
249 | }
|
---|
250 | }
|
---|
251 | else
|
---|
252 | return false;
|
---|
253 | }
|
---|
254 | e = e.next;
|
---|
255 | }
|
---|
256 | return result;
|
---|
257 | }
|
---|
258 |
|
---|
259 | public boolean isErrorID(int id)
|
---|
260 | {
|
---|
261 | MediaEntry e = head;
|
---|
262 | while (e != null)
|
---|
263 | {
|
---|
264 | if (e.id == id && ((e.status & ERRORED) != 0))
|
---|
265 | return true;
|
---|
266 | e = e.next;
|
---|
267 | }
|
---|
268 | return false;
|
---|
269 | }
|
---|
270 |
|
---|
271 | public Object[] getErrorsID(int id)
|
---|
272 | {
|
---|
273 | MediaEntry e = head;
|
---|
274 | ArrayList result = null;
|
---|
275 | while (e != null)
|
---|
276 | {
|
---|
277 | if (e.id == id && ((e.status & ERRORED) != 0))
|
---|
278 | {
|
---|
279 | if (result == null)
|
---|
280 | result = new ArrayList();
|
---|
281 | result.add(e.image);
|
---|
282 | }
|
---|
283 | e = e.next;
|
---|
284 | }
|
---|
285 | if (result == null)
|
---|
286 | return null;
|
---|
287 | else
|
---|
288 | return result.toArray();
|
---|
289 | }
|
---|
290 |
|
---|
291 | public void waitForID(int id) throws InterruptedException
|
---|
292 | {
|
---|
293 | MediaEntry e = head;
|
---|
294 | synchronized (this)
|
---|
295 | {
|
---|
296 | while (checkID (id, true) == false)
|
---|
297 | wait();
|
---|
298 | }
|
---|
299 | }
|
---|
300 |
|
---|
301 | public boolean waitForID(int id, long ms) throws InterruptedException
|
---|
302 | {
|
---|
303 | MediaEntry e = head;
|
---|
304 | long start = System.currentTimeMillis();
|
---|
305 | synchronized (this)
|
---|
306 | {
|
---|
307 | while (checkID (id, true) == false)
|
---|
308 | wait(ms);
|
---|
309 | }
|
---|
310 | if ((System.currentTimeMillis() - start) < ms)
|
---|
311 | return true;
|
---|
312 | else
|
---|
313 | return false;
|
---|
314 | }
|
---|
315 |
|
---|
316 | public int statusID(int id, boolean load)
|
---|
317 | {
|
---|
318 | int result = 0;
|
---|
319 | MediaEntry e = head;
|
---|
320 | while (e != null)
|
---|
321 | {
|
---|
322 | if (e.id == id)
|
---|
323 | {
|
---|
324 | if (load && e.status == 0)
|
---|
325 | {
|
---|
326 | target.prepareImage(e.image, e);
|
---|
327 | e.status = LOADING;
|
---|
328 | }
|
---|
329 | result |= e.status;
|
---|
330 | }
|
---|
331 | e = e.next;
|
---|
332 | }
|
---|
333 | return result;
|
---|
334 | }
|
---|
335 |
|
---|
336 | public void removeImage(Image image)
|
---|
337 | {
|
---|
338 | MediaEntry e = head;
|
---|
339 | MediaEntry prev = null;
|
---|
340 | while (e != null)
|
---|
341 | {
|
---|
342 | if (e.image == image)
|
---|
343 | {
|
---|
344 | if (prev == null)
|
---|
345 | head = e.next;
|
---|
346 | else
|
---|
347 | prev.next = e.next;
|
---|
348 | }
|
---|
349 | prev = e;
|
---|
350 | e = e.next;
|
---|
351 | }
|
---|
352 | }
|
---|
353 |
|
---|
354 | public void removeImage(Image image, int id)
|
---|
355 | {
|
---|
356 | MediaEntry e = head;
|
---|
357 | MediaEntry prev = null;
|
---|
358 | while (e != null)
|
---|
359 | {
|
---|
360 | if (e.id == id && e.image == image)
|
---|
361 | {
|
---|
362 | if (prev == null)
|
---|
363 | head = e.next;
|
---|
364 | else
|
---|
365 | prev.next = e.next;
|
---|
366 | }
|
---|
367 | else
|
---|
368 | prev = e;
|
---|
369 | e = e.next;
|
---|
370 | }
|
---|
371 | }
|
---|
372 |
|
---|
373 | public void removeImage(Image image, int id, int width, int height)
|
---|
374 | {
|
---|
375 | MediaEntry e = head;
|
---|
376 | MediaEntry prev = null;
|
---|
377 | while (e != null)
|
---|
378 | {
|
---|
379 | if (e.id == id && e.image == image
|
---|
380 | && e.width == width && e.height == height)
|
---|
381 | {
|
---|
382 | if (prev == null)
|
---|
383 | head = e.next;
|
---|
384 | else
|
---|
385 | prev.next = e.next;
|
---|
386 | }
|
---|
387 | else
|
---|
388 | prev = e;
|
---|
389 | e = e.next;
|
---|
390 | }
|
---|
391 | }
|
---|
392 | }
|
---|