7871192338def884b1319ef70b1acf18613f68d1
[jalviewjs.git] / site / swingjs / j2s / jssun / awt / image / ImageFetcher.js
1 Clazz.declarePackage ("jssun.awt.image");\r
2 Clazz.load (["java.lang.Thread", "$.StringBuffer"], ["jssun.awt.image.FetcherInfo", "$.ImageFetcher"], ["java.util.Vector", "jssun.awt.AppContext"], function () {\r
3 c$ = Clazz.declareType (jssun.awt.image, "ImageFetcher", Thread);\r
4 Clazz.makeConstructor (c$, \r
5  function (threadGroup, index) {\r
6 Clazz.superConstructor (this, jssun.awt.image.ImageFetcher, [threadGroup, "Image Fetcher " + index]);\r
7 this.setDaemon (true);\r
8 }, "ThreadGroup,~N");\r
9 c$.add = Clazz.defineMethod (c$, "add", \r
10 function (src) {\r
11 var info = jssun.awt.image.FetcherInfo.getFetcherInfo ();\r
12 {\r
13 if (!info.waitList.contains (src)) {\r
14 info.waitList.addElement (src);\r
15 if (info.numWaiting == 0 && info.numFetchers < info.fetchers.length) {\r
16 jssun.awt.image.ImageFetcher.createFetchers (info);\r
17 }info.waitList.notify ();\r
18 }}}, "jssun.awt.image.ImageFetchable");\r
19 c$.remove = Clazz.defineMethod (c$, "remove", \r
20 function (src) {\r
21 var info = jssun.awt.image.FetcherInfo.getFetcherInfo ();\r
22 {\r
23 if (info.waitList.contains (src)) {\r
24 info.waitList.removeElement (src);\r
25 }}}, "jssun.awt.image.ImageFetchable");\r
26 c$.isFetcher = Clazz.defineMethod (c$, "isFetcher", \r
27 function (t) {\r
28 var info = jssun.awt.image.FetcherInfo.getFetcherInfo ();\r
29 {\r
30 for (var i = 0; i < info.fetchers.length; i++) {\r
31 if (info.fetchers[i] === t) {\r
32 return true;\r
33 }}\r
34 }return false;\r
35 }, "Thread");\r
36 c$.amFetcher = Clazz.defineMethod (c$, "amFetcher", \r
37 function () {\r
38 return jssun.awt.image.ImageFetcher.isFetcher (Thread.currentThread ());\r
39 });\r
40 c$.nextImage = Clazz.defineMethod (c$, "nextImage", \r
41  function () {\r
42 var info = jssun.awt.image.FetcherInfo.getFetcherInfo ();\r
43 {\r
44 var src = null;\r
45 var end = System.currentTimeMillis () + 5000;\r
46 while (src == null) {\r
47 while (info.waitList.size () == 0) {\r
48 var now = System.currentTimeMillis ();\r
49 if (now >= end) {\r
50 return null;\r
51 }try {\r
52 info.numWaiting++;\r
53 info.waitList.wait (end - now);\r
54 } catch (e) {\r
55 if (Clazz.exceptionOf (e, InterruptedException)) {\r
56 return null;\r
57 } else {\r
58 throw e;\r
59 }\r
60 } finally {\r
61 info.numWaiting--;\r
62 }\r
63 }\r
64 src = info.waitList.elementAt (0);\r
65 info.waitList.removeElement (src);\r
66 }\r
67 return src;\r
68 }});\r
69 Clazz.overrideMethod (c$, "run", \r
70 function () {\r
71 var info = jssun.awt.image.FetcherInfo.getFetcherInfo ();\r
72 try {\r
73 this.fetchloop ();\r
74 } catch (e) {\r
75 if (Clazz.exceptionOf (e, Exception)) {\r
76 e.printStackTrace ();\r
77 } else {\r
78 throw e;\r
79 }\r
80 } finally {\r
81 {\r
82 var me = Thread.currentThread ();\r
83 for (var i = 0; i < info.fetchers.length; i++) {\r
84 if (info.fetchers[i] === me) {\r
85 info.fetchers[i] = null;\r
86 info.numFetchers--;\r
87 }}\r
88 }}\r
89 });\r
90 Clazz.defineMethod (c$, "fetchloop", \r
91  function () {\r
92 var me = Thread.currentThread ();\r
93 while (jssun.awt.image.ImageFetcher.isFetcher (me)) {\r
94 Thread.interrupted ();\r
95 me.setPriority (8);\r
96 var src = jssun.awt.image.ImageFetcher.nextImage ();\r
97 if (src == null) {\r
98 return;\r
99 }try {\r
100 src.doFetch ();\r
101 } catch (e) {\r
102 if (Clazz.exceptionOf (e, Exception)) {\r
103 System.err.println ("Uncaught error fetching image:");\r
104 e.printStackTrace ();\r
105 } else {\r
106 throw e;\r
107 }\r
108 }\r
109 jssun.awt.image.ImageFetcher.stoppingAnimation (me);\r
110 }\r
111 });\r
112 c$.startingAnimation = Clazz.defineMethod (c$, "startingAnimation", \r
113 function () {\r
114 var info = jssun.awt.image.FetcherInfo.getFetcherInfo ();\r
115 var me = Thread.currentThread ();\r
116 {\r
117 for (var i = 0; i < info.fetchers.length; i++) {\r
118 if (info.fetchers[i] === me) {\r
119 info.fetchers[i] = null;\r
120 info.numFetchers--;\r
121 me.setName ("Image Animator " + i);\r
122 if (info.waitList.size () > info.numWaiting) {\r
123 jssun.awt.image.ImageFetcher.createFetchers (info);\r
124 }return;\r
125 }}\r
126 }me.setPriority (2);\r
127 me.setName ("Image Animator");\r
128 });\r
129 c$.stoppingAnimation = Clazz.defineMethod (c$, "stoppingAnimation", \r
130  function (me) {\r
131 var info = jssun.awt.image.FetcherInfo.getFetcherInfo ();\r
132 {\r
133 var index = -1;\r
134 for (var i = 0; i < info.fetchers.length; i++) {\r
135 if (info.fetchers[i] === me) {\r
136 return;\r
137 }if (info.fetchers[i] == null) {\r
138 index = i;\r
139 }}\r
140 if (index >= 0) {\r
141 info.fetchers[index] = me;\r
142 info.numFetchers++;\r
143 me.setName ("Image Fetcher " + index);\r
144 return;\r
145 }}}, "Thread");\r
146 c$.createFetchers = Clazz.defineMethod (c$, "createFetchers", \r
147  function (info) {\r
148 var appContext = jssun.awt.AppContext.getAppContext ();\r
149 var threadGroup = appContext.getThreadGroup ();\r
150 var fetcherThreadGroup;\r
151 try {\r
152 if (threadGroup.getParent () != null) {\r
153 fetcherThreadGroup = threadGroup;\r
154 } else {\r
155 threadGroup = Thread.currentThread ().getThreadGroup ();\r
156 var parent = threadGroup.getParent ();\r
157 while ((parent != null) && (parent.getParent () != null)) {\r
158 threadGroup = parent;\r
159 parent = threadGroup.getParent ();\r
160 }\r
161 fetcherThreadGroup = threadGroup;\r
162 }} catch (e) {\r
163 if (Clazz.exceptionOf (e, SecurityException)) {\r
164 fetcherThreadGroup = appContext.getThreadGroup ();\r
165 } else {\r
166 throw e;\r
167 }\r
168 }\r
169 var fetcherGroup = fetcherThreadGroup;\r
170 for (var i = 0; i < info.fetchers.length; i++) {\r
171 if (info.fetchers[i] == null) {\r
172 info.fetchers[i] =  new jssun.awt.image.ImageFetcher (fetcherGroup, i);\r
173 info.fetchers[i].start ();\r
174 info.numFetchers++;\r
175 break;\r
176 }}\r
177 return;\r
178 }, "jssun.awt.image.FetcherInfo");\r
179 Clazz.defineStatics (c$,\r
180 "HIGH_PRIORITY", 8,\r
181 "LOW_PRIORITY", 3,\r
182 "ANIM_PRIORITY", 2,\r
183 "TIMEOUT", 5000);\r
184 c$ = Clazz.decorateAsClass (function () {\r
185 this.fetchers = null;\r
186 this.numFetchers = 0;\r
187 this.numWaiting = 0;\r
188 this.waitList = null;\r
189 Clazz.instantialize (this, arguments);\r
190 }, jssun.awt.image, "FetcherInfo");\r
191 Clazz.makeConstructor (c$, \r
192  function () {\r
193 this.fetchers =  new Array (4);\r
194 this.numFetchers = 0;\r
195 this.numWaiting = 0;\r
196 this.waitList =  new java.util.Vector ();\r
197 });\r
198 c$.getFetcherInfo = Clazz.defineMethod (c$, "getFetcherInfo", \r
199 function () {\r
200 var appContext = jssun.awt.AppContext.getAppContext ();\r
201 {\r
202 var info = appContext.get (jssun.awt.image.FetcherInfo.FETCHER_INFO_KEY);\r
203 if (info == null) {\r
204 info =  new jssun.awt.image.FetcherInfo ();\r
205 appContext.put (jssun.awt.image.FetcherInfo.FETCHER_INFO_KEY, info);\r
206 }return info;\r
207 }});\r
208 Clazz.defineStatics (c$,\r
209 "MAX_NUM_FETCHERS_PER_APPCONTEXT", 4);\r
210 c$.FETCHER_INFO_KEY = c$.prototype.FETCHER_INFO_KEY =  new StringBuffer ("FetcherInfo");\r
211 });\r