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)
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
.awt
.image
.ImageObserver
;
42 import java
.util
.ArrayList
;
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)
84 else if ((flags
& SOMEBITS
) != 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
)
106 public void addImage(Image image
, int id
)
108 MediaEntry e
= new MediaEntry();
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();
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
)
140 boolean result
= true;
144 if ((e
.status
& COMPLETE
) == 0)
151 target
.prepareImage(e
.image
, e
);
163 public boolean isErrorAny()
168 if ((e
.status
& ERRORED
) != 0)
175 public Object
[] getErrorsAny()
178 ArrayList result
= null;
181 if ((e
.status
& ERRORED
) != 0)
184 result
= new ArrayList();
192 return result
.toArray();
195 public void waitForAll() throws InterruptedException
199 while (checkAll(true) == false)
204 public boolean waitForAll(long ms
) throws InterruptedException
206 long start
= System
.currentTimeMillis();
209 while (!checkAll(true))
212 if ((System
.currentTimeMillis() - start
) < ms
)
218 public int statusAll(boolean load
)
224 if (load
&& e
.status
== 0)
226 target
.prepareImage(e
.image
, e
);
235 public boolean checkID(int id
)
237 return checkID(id
, false);
240 public boolean checkID(int id
, boolean load
)
243 boolean result
= true;
247 if (e
.id
== id
&& ((e
.status
& COMPLETE
) == 0))
254 target
.prepareImage(e
.image
, e
);
266 public boolean isErrorID(int id
)
271 if (e
.id
== id
&& ((e
.status
& ERRORED
) != 0))
278 public Object
[] getErrorsID(int id
)
281 ArrayList result
= null;
284 if (e
.id
== id
&& ((e
.status
& ERRORED
) != 0))
287 result
= new ArrayList();
295 return result
.toArray();
298 public void waitForID(int id
) throws InterruptedException
303 while (checkID (id
, true) == false)
308 public boolean waitForID(int id
, long ms
) throws InterruptedException
311 long start
= System
.currentTimeMillis();
314 while (checkID (id
, true) == false)
317 if ((System
.currentTimeMillis() - start
) < ms
)
323 public int statusID(int id
, boolean load
)
331 if (load
&& e
.status
== 0)
333 target
.prepareImage(e
.image
, e
);
343 public void removeImage(Image image
)
346 MediaEntry prev
= null;
349 if (e
.image
== image
)
361 public void removeImage(Image image
, int id
)
364 MediaEntry prev
= null;
367 if (e
.id
== id
&& e
.image
== image
)
380 public void removeImage(Image image
, int id
, int width
, int height
)
383 MediaEntry prev
= null;
386 if (e
.id
== id
&& e
.image
== image
387 && e
.width
== width
&& e
.height
== height
)