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