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)
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
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. */
41 import java
.util
.ArrayList
;
42 import java
.awt
.image
.ImageObserver
;
45 * This class is used for keeping track of the status of various media
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;
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
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)
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
)
101 public void addImage(Image image
, int id
)
103 MediaEntry e
= new MediaEntry();
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();
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
)
133 boolean result
= true;
137 if ((e
.status
& COMPLETE
) == 0)
144 target
.prepareImage(e
.image
, e
);
156 public boolean isErrorAny()
161 if ((e
.status
& ERRORED
) != 0)
168 public Object
[] getErrorsAny()
171 ArrayList result
= null;
174 if ((e
.status
& ERRORED
) != 0)
177 result
= new ArrayList();
185 return result
.toArray();
188 public void waitForAll() throws InterruptedException
192 while (checkAll(true) == false)
197 public boolean waitForAll(long ms
) throws InterruptedException
199 long start
= System
.currentTimeMillis();
202 while (!checkAll(true))
205 if ((System
.currentTimeMillis() - start
) < ms
)
211 public int statusAll(boolean load
)
217 if (load
&& e
.status
== 0)
219 target
.prepareImage(e
.image
, e
);
228 public boolean checkID(int id
)
230 return checkID(id
, false);
233 public boolean checkID(int id
, boolean load
)
236 boolean result
= true;
240 if (e
.id
== id
&& ((e
.status
& COMPLETE
) == 0))
247 target
.prepareImage(e
.image
, e
);
259 public boolean isErrorID(int id
)
264 if (e
.id
== id
&& ((e
.status
& ERRORED
) != 0))
271 public Object
[] getErrorsID(int id
)
274 ArrayList result
= null;
277 if (e
.id
== id
&& ((e
.status
& ERRORED
) != 0))
280 result
= new ArrayList();
288 return result
.toArray();
291 public void waitForID(int id
) throws InterruptedException
296 while (checkID (id
, true) == false)
301 public boolean waitForID(int id
, long ms
) throws InterruptedException
304 long start
= System
.currentTimeMillis();
307 while (checkID (id
, true) == false)
310 if ((System
.currentTimeMillis() - start
) < ms
)
316 public int statusID(int id
, boolean load
)
324 if (load
&& e
.status
== 0)
326 target
.prepareImage(e
.image
, e
);
336 public void removeImage(Image image
)
339 MediaEntry prev
= null;
342 if (e
.image
== image
)
354 public void removeImage(Image image
, int id
)
357 MediaEntry prev
= null;
360 if (e
.id
== id
&& e
.image
== image
)
373 public void removeImage(Image image
, int id
, int width
, int height
)
376 MediaEntry prev
= null;
379 if (e
.id
== id
&& e
.image
== image
380 && e
.width
== width
&& e
.height
== height
)