MediaTracker.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /* MediaTracker.java -- Class used for keeping track of images
  2. Copyright (C) 1999, 2002 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.awt;
  32. import java.util.ArrayList;
  33. import java.awt.image.ImageObserver;
  34. /**
  35. * This class is used for keeping track of the status of various media
  36. * objects.
  37. *
  38. * @author Aaron M. Renn (arenn@urbanophile.com)
  39. * @author Bryce McKinlay
  40. */
  41. public class MediaTracker implements java.io.Serializable
  42. {
  43. public static final int LOADING = 1 << 0;
  44. public static final int ABORTED = 1 << 1;
  45. public static final int ERRORED = 1 << 2;
  46. public static final int COMPLETE = 1 << 3;
  47. Component target;
  48. MediaEntry head;
  49. static final long serialVersionUID = -483174189758638095L;
  50. // FIXME: The serialized form documentation says MediaEntry is a
  51. // serializable field, but the serialized form of MediaEntry itself
  52. // doesn't appear to be documented.
  53. class MediaEntry implements ImageObserver
  54. {
  55. int id;
  56. Image image;
  57. MediaEntry next;
  58. int status;
  59. int width;
  60. int height;
  61. public boolean imageUpdate(Image img, int flags, int x, int y,
  62. int width, int height)
  63. {
  64. if ((flags & ABORT) != 0)
  65. status = ABORTED & COMPLETE;
  66. else if ((flags & ERROR) != 0)
  67. status = ERRORED & COMPLETE;
  68. else if ((flags & ALLBITS) != 0)
  69. status = COMPLETE;
  70. else
  71. status = LOADING;
  72. synchronized (MediaTracker.this)
  73. {
  74. MediaTracker.this.notifyAll();
  75. }
  76. return ((status & COMPLETE) != 0);
  77. }
  78. }
  79. public MediaTracker(Component c)
  80. {
  81. target = c;
  82. }
  83. public void addImage(Image image, int id)
  84. {
  85. MediaEntry e = new MediaEntry();
  86. e.id = id;
  87. e.image = image;
  88. e.next = head;
  89. head = e;
  90. // Start tracking image status.
  91. target.checkImage(image, e);
  92. }
  93. public void addImage(Image image, int id, int width, int height)
  94. {
  95. MediaEntry e = new MediaEntry();
  96. e.id = id;
  97. e.image = image;
  98. e.next = head;
  99. e.width = width;
  100. e.height = height;
  101. head = e;
  102. // Start tracking image status.
  103. target.checkImage(image, width, height, e);
  104. }
  105. public boolean checkAll()
  106. {
  107. return checkAll(false);
  108. }
  109. public boolean checkAll(boolean load)
  110. {
  111. MediaEntry e = head;
  112. boolean result = true;
  113. while (e != null)
  114. {
  115. if ((e.status & COMPLETE) == 0)
  116. {
  117. if (load)
  118. {
  119. result = false;
  120. if (e.status == 0)
  121. {
  122. target.prepareImage(e.image, e);
  123. e.status = LOADING;
  124. }
  125. }
  126. else
  127. return false;
  128. }
  129. e = e.next;
  130. }
  131. return result;
  132. }
  133. public boolean isErrorAny()
  134. {
  135. MediaEntry e = head;
  136. while (e != null)
  137. {
  138. if ((e.status & ERRORED) != 0)
  139. return true;
  140. e = e.next;
  141. }
  142. return false;
  143. }
  144. public Object[] getErrorsAny()
  145. {
  146. MediaEntry e = head;
  147. ArrayList result = null;
  148. while (e != null)
  149. {
  150. if ((e.status & ERRORED) != 0)
  151. {
  152. if (result == null)
  153. result = new ArrayList();
  154. result.add(e.image);
  155. }
  156. e = e.next;
  157. }
  158. if (result == null)
  159. return null;
  160. else
  161. return result.toArray();
  162. }
  163. public void waitForAll() throws InterruptedException
  164. {
  165. synchronized (this)
  166. {
  167. while (checkAll(true) == false)
  168. wait();
  169. }
  170. }
  171. public boolean waitForAll(long ms) throws InterruptedException
  172. {
  173. long start = System.currentTimeMillis();
  174. synchronized (this)
  175. {
  176. while (!checkAll(true))
  177. wait(ms);
  178. }
  179. if ((System.currentTimeMillis() - start) < ms)
  180. return true;
  181. else
  182. return false;
  183. }
  184. public int statusAll(boolean load)
  185. {
  186. int result = 0;
  187. MediaEntry e = head;
  188. while (e != null)
  189. {
  190. if (load && e.status == 0)
  191. {
  192. target.prepareImage(e.image, e);
  193. e.status = LOADING;
  194. }
  195. result |= e.status;
  196. e = e.next;
  197. }
  198. return result;
  199. }
  200. public boolean checkID(int id)
  201. {
  202. return checkID(id, false);
  203. }
  204. public boolean checkID(int id, boolean load)
  205. {
  206. MediaEntry e = head;
  207. boolean result = true;
  208. while (e != null)
  209. {
  210. if (e.id == id && ((e.status & COMPLETE) == 0))
  211. {
  212. if (load)
  213. {
  214. result = false;
  215. if (e.status == 0)
  216. {
  217. target.prepareImage(e.image, e);
  218. e.status = LOADING;
  219. }
  220. }
  221. else
  222. return false;
  223. }
  224. e = e.next;
  225. }
  226. return result;
  227. }
  228. public boolean isErrorID(int id)
  229. {
  230. MediaEntry e = head;
  231. while (e != null)
  232. {
  233. if (e.id == id && ((e.status & ERRORED) != 0))
  234. return true;
  235. e = e.next;
  236. }
  237. return false;
  238. }
  239. public Object[] getErrorsID(int id)
  240. {
  241. MediaEntry e = head;
  242. ArrayList result = null;
  243. while (e != null)
  244. {
  245. if (e.id == id && ((e.status & ERRORED) != 0))
  246. {
  247. if (result == null)
  248. result = new ArrayList();
  249. result.add(e.image);
  250. }
  251. e = e.next;
  252. }
  253. if (result == null)
  254. return null;
  255. else
  256. return result.toArray();
  257. }
  258. public void waitForID(int id) throws InterruptedException
  259. {
  260. MediaEntry e = head;
  261. synchronized (this)
  262. {
  263. while (checkID (id, true) == false)
  264. wait();
  265. }
  266. }
  267. public boolean waitForID(int id, long ms) throws InterruptedException
  268. {
  269. MediaEntry e = head;
  270. long start = System.currentTimeMillis();
  271. synchronized (this)
  272. {
  273. while (checkID (id, true) == false)
  274. wait(ms);
  275. }
  276. if ((System.currentTimeMillis() - start) < ms)
  277. return true;
  278. else
  279. return false;
  280. }
  281. public int statusID(int id, boolean load)
  282. {
  283. int result = 0;
  284. MediaEntry e = head;
  285. while (e != null)
  286. {
  287. if (e.id == id)
  288. {
  289. if (load && e.status == 0)
  290. {
  291. target.prepareImage(e.image, e);
  292. e.status = LOADING;
  293. }
  294. result |= e.status;
  295. }
  296. e = e.next;
  297. }
  298. return result;
  299. }
  300. public void removeImage(Image image)
  301. {
  302. MediaEntry e = head;
  303. MediaEntry prev = null;
  304. while (e != null)
  305. {
  306. if (e.image == image)
  307. {
  308. if (prev == null)
  309. head = e.next;
  310. else
  311. prev.next = e.next;
  312. }
  313. prev = e;
  314. e = e.next;
  315. }
  316. }
  317. public void removeImage(Image image, int id)
  318. {
  319. MediaEntry e = head;
  320. MediaEntry prev = null;
  321. while (e != null)
  322. {
  323. if (e.id == id && e.image == image)
  324. {
  325. if (prev == null)
  326. head = e.next;
  327. else
  328. prev.next = e.next;
  329. }
  330. else
  331. prev = e;
  332. e = e.next;
  333. }
  334. }
  335. public void removeImage(Image image, int id, int width, int height)
  336. {
  337. MediaEntry e = head;
  338. MediaEntry prev = null;
  339. while (e != null)
  340. {
  341. if (e.id == id && e.image == image
  342. && e.width == width && e.height == height)
  343. {
  344. if (prev == null)
  345. head = e.next;
  346. else
  347. prev.next = e.next;
  348. }
  349. else
  350. prev = e;
  351. e = e.next;
  352. }
  353. }
  354. }