Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / j2s / java / awt / EventQueue.js
1 Clazz.declarePackage ("java.awt");
2 Clazz.load (["java.lang.Thread"], ["java.awt.EventQueueItem", "$.EventQueue", "$.Queue"], ["java.lang.Error", "java.lang.reflect.InvocationTargetException", "java.util.EmptyStackException", "java.awt.ActiveEvent", "$.Component", "$.EventDispatchThread", "$.SentEvent", "$.SequencedEvent", "$.Toolkit", "java.awt.event.ActionEvent", "$.FocusEvent", "$.InputEvent", "$.InputMethodEvent", "$.InvocationEvent", "$.KeyEvent", "$.MouseEvent", "$.PaintEvent", "$.WindowEvent", "jssun.awt.AWTAutoShutdown", "$.AppContext", "$.PeerEvent", "$.SunToolkit", "swingjs.JSToolkit"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.queues = null;
5 this.nextQueue = null;
6 this.previousQueue = null;
7 this.dispatchThread = null;
8 this.threadGroup = null;
9 this.mostRecentEventTime = 0;
10 this.currentEvent = null;
11 this.waitForID = 0;
12 this.name = null;
13 Clazz.instantialize (this, arguments);
14 }, java.awt, "EventQueue");
15 Clazz.prepareFields (c$, function () {
16 this.queues =  new Array (4);
17 this.threadGroup = Thread.currentThread ().getThreadGroup ();
18 this.mostRecentEventTime = System.currentTimeMillis ();
19 this.name = "AWT-EventQueue-" + java.awt.EventQueue.nextThreadNum ();
20 });
21 c$.nextThreadNum = Clazz.defineMethod (c$, "nextThreadNum", 
22  function () {
23 return java.awt.EventQueue.threadInitNumber++;
24 });
25 c$.noEvents = Clazz.defineMethod (c$, "noEvents", 
26 function (eventQueue) {
27 return eventQueue.noEvents ();
28 }, "java.awt.EventQueue");
29 c$.getNextQueue = Clazz.defineMethod (c$, "getNextQueue", 
30 function (eventQueue) {
31 return eventQueue.nextQueue;
32 }, "java.awt.EventQueue");
33 c$.removeSourceEvents = Clazz.defineMethod (c$, "removeSourceEvents", 
34 function (eventQueue, source, removeAllEvents) {
35 eventQueue.removeSourceEvents (source, removeAllEvents);
36 }, "java.awt.EventQueue,~O,~B");
37 Clazz.makeConstructor (c$, 
38 function () {
39 for (var i = 0; i < 4; i++) {
40 this.queues[i] =  new java.awt.Queue ();
41 }
42 });
43 Clazz.defineMethod (c$, "postEvent", 
44 function (event) {
45 jssun.awt.SunToolkit.flushPendingEvents ();
46 this.postEventPrivate (event);
47 }, "java.awt.AWTEvent");
48 Clazz.defineMethod (c$, "postEventPrivate", 
49 function (theEvent) {
50 theEvent.isPosted = true;
51 {
52 if (this.dispatchThread == null && this.nextQueue == null) {
53 if (theEvent.getSource () === jssun.awt.AWTAutoShutdown.getInstance ()) {
54 return;
55 } else {
56 this.initDispatchThread ();
57 }}if (this.nextQueue != null) {
58 this.nextQueue.postEventPrivate (theEvent);
59 return;
60 }this.postEventNow (theEvent, java.awt.EventQueue.getPriority (theEvent));
61 }}, "java.awt.AWTEvent");
62 c$.getPriority = Clazz.defineMethod (c$, "getPriority", 
63  function (theEvent) {
64 if (Clazz.instanceOf (theEvent, jssun.awt.PeerEvent)) {
65 var flags = (theEvent).getFlags ();
66 if ((flags & 2) != 0) return 3;
67 if ((flags & 1) != 0) return 2;
68 if ((flags & 4) != 0) return 0;
69 }switch (theEvent.getID ()) {
70 case 1201:
71 case 800:
72 case 801:
73 return 0;
74 default:
75 return 1;
76 }
77 }, "java.awt.AWTEvent");
78 Clazz.defineMethod (c$, "postEventNow", 
79  function (theEvent, priority) {
80 if (this.coalesceEvent (theEvent, priority)) {
81 return;
82 }var newItem =  new java.awt.EventQueueItem (theEvent);
83 this.cacheEQItem (newItem);
84 if (this.queues[priority].head == null) {
85 var shouldNotify = this.noEvents ();
86 this.queues[priority].head = this.queues[priority].tail = newItem;
87 if (shouldNotify) {
88 if (theEvent.getSource () !== jssun.awt.AWTAutoShutdown.getInstance ()) {
89 jssun.awt.AWTAutoShutdown.getInstance ().notifyThreadBusy (this.dispatchThread);
90 }}} else {
91 this.queues[priority].tail.next = newItem;
92 this.queues[priority].tail = newItem;
93 }}, "java.awt.AWTEvent,~N");
94 Clazz.defineMethod (c$, "coalescePaintEvent", 
95  function (e) {
96 var sourcePeer = (e.getSource ()).peer;
97 if (sourcePeer != null) {
98 sourcePeer.coalescePaintEvent (e);
99 }var cache = (e.getSource ()).eventCache;
100 if (cache == null) {
101 return false;
102 }var index = java.awt.EventQueue.eventToCacheIndex (e);
103 if (index != -1 && cache[index] != null) {
104 var merged = this.mergePaintEvents (e, cache[index].event);
105 if (merged != null) {
106 cache[index].event = merged;
107 return true;
108 }}return false;
109 }, "java.awt.event.PaintEvent");
110 Clazz.defineMethod (c$, "mergePaintEvents", 
111  function (a, b) {
112 var aRect = a.getUpdateRect ();
113 var bRect = b.getUpdateRect ();
114 if (bRect.contains (aRect)) {
115 return b;
116 }if (aRect.contains (bRect)) {
117 return a;
118 }return null;
119 }, "java.awt.event.PaintEvent,java.awt.event.PaintEvent");
120 Clazz.defineMethod (c$, "coalesceMouseEvent", 
121  function (e) {
122 var cache = (e.getSource ()).eventCache;
123 if (cache == null) {
124 return false;
125 }var index = java.awt.EventQueue.eventToCacheIndex (e);
126 if (index != -1 && cache[index] != null) {
127 cache[index].event = e;
128 return true;
129 }return false;
130 }, "java.awt.event.MouseEvent");
131 Clazz.defineMethod (c$, "coalescePeerEvent", 
132  function (e) {
133 var cache = (e.getSource ()).eventCache;
134 if (cache == null) {
135 return false;
136 }var index = java.awt.EventQueue.eventToCacheIndex (e);
137 if (index != -1 && cache[index] != null) {
138 e = e.coalesceEvents (cache[index].event);
139 if (e != null) {
140 cache[index].event = e;
141 return true;
142 } else {
143 cache[index] = null;
144 }}return false;
145 }, "jssun.awt.PeerEvent");
146 Clazz.defineMethod (c$, "coalesceOtherEvent", 
147  function (e, priority) {
148 var id = e.getID ();
149 var source = e.getSource ();
150 for (var entry = this.queues[priority].head; entry != null; entry = entry.next) {
151 if (entry.event.getSource () === source && entry.id == id) {
152 var coalescedEvent = source.coalesceEvents (entry.event, e);
153 if (coalescedEvent != null) {
154 entry.event = coalescedEvent;
155 return true;
156 }}}
157 return false;
158 }, "java.awt.AWTEvent,~N");
159 Clazz.defineMethod (c$, "coalesceEvent", 
160  function (e, priority) {
161 if (!(Clazz.instanceOf (e.getSource (), java.awt.Component))) {
162 return false;
163 }if (Clazz.instanceOf (e, jssun.awt.PeerEvent)) {
164 return this.coalescePeerEvent (e);
165 }if ((e.getSource ()).isCoalescingEnabled () && this.coalesceOtherEvent (e, priority)) {
166 return true;
167 }if (Clazz.instanceOf (e, java.awt.event.PaintEvent)) {
168 return this.coalescePaintEvent (e);
169 }if (Clazz.instanceOf (e, java.awt.event.MouseEvent)) {
170 return this.coalesceMouseEvent (e);
171 }return false;
172 }, "java.awt.AWTEvent,~N");
173 Clazz.defineMethod (c$, "cacheEQItem", 
174  function (entry) {
175 var index = java.awt.EventQueue.eventToCacheIndex (entry.event);
176 if (index != -1 && Clazz.instanceOf (entry.event.getSource (), java.awt.Component)) {
177 var source = entry.event.getSource ();
178 if (source.eventCache == null) {
179 source.eventCache =  new Array (5);
180 }source.eventCache[index] = entry;
181 }}, "java.awt.EventQueueItem");
182 Clazz.defineMethod (c$, "uncacheEQItem", 
183  function (entry) {
184 var index = java.awt.EventQueue.eventToCacheIndex (entry.event);
185 if (index != -1 && Clazz.instanceOf (entry.event.getSource (), java.awt.Component)) {
186 var source = entry.event.getSource ();
187 if (source.eventCache == null) {
188 return;
189 }source.eventCache[index] = null;
190 }}, "java.awt.EventQueueItem");
191 c$.eventToCacheIndex = Clazz.defineMethod (c$, "eventToCacheIndex", 
192  function (e) {
193 switch (e.getID ()) {
194 case 800:
195 return 0;
196 case 801:
197 return 1;
198 case 503:
199 return 2;
200 case 506:
201 return 3;
202 default:
203 return -1;
204 }
205 }, "java.awt.AWTEvent");
206 Clazz.defineMethod (c$, "noEvents", 
207  function () {
208 for (var i = 0; i < 4; i++) {
209 if (this.queues[i].head != null) {
210 return false;
211 }}
212 return true;
213 });
214 Clazz.defineMethod (c$, "getNextEvent", 
215 function () {
216 jssun.awt.SunToolkit.flushPendingEvents ();
217 {
218 for (var i = 3; i >= 0; i--) {
219 if (this.queues[i].head != null) {
220 var entry = this.queues[i].head;
221 this.queues[i].head = entry.next;
222 if (entry.next == null) {
223 this.queues[i].tail = null;
224 }this.uncacheEQItem (entry);
225 return entry.event;
226 }}
227 jssun.awt.AWTAutoShutdown.getInstance ().notifyThreadFree (this.dispatchThread);
228 }return null;
229 });
230 Clazz.defineMethod (c$, "getNextEventForID", 
231 function (id) {
232 jssun.awt.SunToolkit.flushPendingEvents ();
233 {
234 for (var i = 0; i < 4; i++) {
235 for (var entry = this.queues[i].head, prev = null; entry != null; prev = entry, entry = entry.next) {
236 if (entry.id == id) {
237 if (prev == null) {
238 this.queues[i].head = entry.next;
239 } else {
240 prev.next = entry.next;
241 }if (this.queues[i].tail === entry) {
242 this.queues[i].tail = prev;
243 }this.uncacheEQItem (entry);
244 return entry.event;
245 }}
246 }
247 this.waitForID = id;
248 this.waitForID = 0;
249 }return null;
250 }, "~N");
251 Clazz.defineMethod (c$, "peekEvent", 
252 function () {
253 for (var i = 3; i >= 0; i--) {
254 if (this.queues[i].head != null) {
255 return this.queues[i].head.event;
256 }}
257 return null;
258 });
259 Clazz.defineMethod (c$, "peekEvent", 
260 function (id) {
261 for (var i = 3; i >= 0; i--) {
262 var q = this.queues[i].head;
263 for (; q != null; q = q.next) {
264 if (q.id == id) {
265 return q.event;
266 }}
267 }
268 return null;
269 }, "~N");
270 Clazz.defineMethod (c$, "dispatchEvent", 
271 function (event) {
272 var src = event.getSource ();
273 this.dispatchEventImpl (event, src, false);
274 }, "java.awt.AWTEvent");
275 Clazz.defineMethod (c$, "dispatchEventAndWait", 
276 function (event, src) {
277 this.dispatchEventImpl (event, src, true);
278 }, "java.awt.AWTEvent,~O");
279 Clazz.defineMethod (c$, "dispatchEventImpl", 
280  function (event, src, andWait) {
281 event.isPosted = true;
282 if (Clazz.instanceOf (event, java.awt.ActiveEvent)) {
283 this.setCurrentEventAndMostRecentTimeImpl (event);
284 swingjs.JSToolkit.dispatchEvent (event, null, andWait);
285 } else if (Clazz.instanceOf (src, java.awt.Component)) {
286 swingjs.JSToolkit.dispatchEvent (event, src, andWait);
287 event.dispatched ();
288 } else if (Clazz.instanceOf (src, jssun.awt.AWTAutoShutdown)) {
289 if (this.noEvents ()) {
290 this.dispatchThread.stopDispatching ();
291 }} else {
292 System.err.println ("unable to dispatch event: " + event);
293 }}, "java.awt.AWTEvent,~O,~B");
294 c$.getMostRecentEventTime = Clazz.defineMethod (c$, "getMostRecentEventTime", 
295 function () {
296 return 0;
297 });
298 Clazz.defineMethod (c$, "getMostRecentEventTimeEx", 
299 function () {
300 return this.mostRecentEventTime;
301 });
302 c$.getCurrentEvent = Clazz.defineMethod (c$, "getCurrentEvent", 
303 function () {
304 return java.awt.Toolkit.getEventQueue ().getCurrentEventImpl ();
305 });
306 Clazz.defineMethod (c$, "getCurrentEventImpl", 
307  function () {
308 return (swingjs.JSToolkit.isDispatchThread () ? (this.currentEvent) : null);
309 });
310 Clazz.defineMethod (c$, "push", 
311 function (newEventQueue) {
312 if (this.nextQueue != null) {
313 this.nextQueue.push (newEventQueue);
314 return;
315 }{
316 while (this.peekEvent () != null) {
317 try {
318 newEventQueue.postEventPrivate (this.getNextEvent ());
319 } catch (ie) {
320 if (Clazz.exceptionOf (ie, InterruptedException)) {
321 } else {
322 throw ie;
323 }
324 }
325 }
326 newEventQueue.previousQueue = this;
327 }if (this.dispatchThread != null) {
328 this.dispatchThread.stopDispatchingLater ();
329 }this.nextQueue = newEventQueue;
330 var appContext = jssun.awt.AppContext.getAppContext ();
331 if (appContext.get (jssun.awt.AppContext.EVENT_QUEUE_KEY) === this) {
332 appContext.put (jssun.awt.AppContext.EVENT_QUEUE_KEY, newEventQueue);
333 }}, "java.awt.EventQueue");
334 Clazz.defineMethod (c$, "pop", 
335 function () {
336 var prev = this.previousQueue;
337 {
338 {
339 if (this.nextQueue != null) {
340 this.nextQueue.pop ();
341 return;
342 }if (this.previousQueue == null) {
343 throw  new java.util.EmptyStackException ();
344 }this.previousQueue.nextQueue = null;
345 while (this.peekEvent () != null) {
346 try {
347 this.previousQueue.postEventPrivate (this.getNextEvent ());
348 } catch (ie) {
349 if (Clazz.exceptionOf (ie, InterruptedException)) {
350 } else {
351 throw ie;
352 }
353 }
354 }
355 var appContext = jssun.awt.AppContext.getAppContext ();
356 if (appContext.get (jssun.awt.AppContext.EVENT_QUEUE_KEY) === this) {
357 appContext.put (jssun.awt.AppContext.EVENT_QUEUE_KEY, this.previousQueue);
358 }this.previousQueue = null;
359 }}var dt = this.dispatchThread;
360 if (dt != null) {
361 dt.stopDispatching ();
362 }});
363 c$.isDispatchThread = Clazz.defineMethod (c$, "isDispatchThread", 
364 function () {
365 return swingjs.JSToolkit.isDispatchThread ();
366 });
367 Clazz.defineMethod (c$, "initDispatchThread", 
368 function () {
369 {
370 if (this.dispatchThread == null) {
371 var t =  new java.awt.EventDispatchThread (this.threadGroup, this.name, this);
372 jssun.awt.AWTAutoShutdown.getInstance ().notifyThreadBusy (t);
373 this.dispatchThread = t;
374 this.dispatchThread.start ();
375 }}});
376 Clazz.defineMethod (c$, "detachDispatchThread", 
377 function () {
378 this.dispatchThread = null;
379 });
380 Clazz.defineMethod (c$, "getDispatchThread", 
381 function () {
382 return this.dispatchThread;
383 });
384 Clazz.defineMethod (c$, "removeSourceEvents", 
385 function (source, removeAllEvents) {
386 jssun.awt.SunToolkit.flushPendingEvents ();
387 {
388 for (var i = 0; i < 4; i++) {
389 var entry = this.queues[i].head;
390 var prev = null;
391 while (entry != null) {
392 if ((entry.event.getSource () === source) && (removeAllEvents || !(Clazz.instanceOf (entry.event, java.awt.SequencedEvent) || Clazz.instanceOf (entry.event, java.awt.SentEvent) || Clazz.instanceOf (entry.event, java.awt.event.FocusEvent) || Clazz.instanceOf (entry.event, java.awt.event.WindowEvent) || Clazz.instanceOf (entry.event, java.awt.event.KeyEvent) || Clazz.instanceOf (entry.event, java.awt.event.InputMethodEvent)))) {
393 if (Clazz.instanceOf (entry.event, java.awt.SequencedEvent)) {
394 (entry.event).dispose ();
395 }if (Clazz.instanceOf (entry.event, java.awt.SentEvent)) {
396 (entry.event).dispose ();
397 }if (prev == null) {
398 this.queues[i].head = entry.next;
399 } else {
400 prev.next = entry.next;
401 }this.uncacheEQItem (entry);
402 } else {
403 prev = entry;
404 }entry = entry.next;
405 }
406 this.queues[i].tail = prev;
407 }
408 }}, "~O,~B");
409 c$.setCurrentEventAndMostRecentTime = Clazz.defineMethod (c$, "setCurrentEventAndMostRecentTime", 
410 function (e) {
411 java.awt.Toolkit.getEventQueue ().setCurrentEventAndMostRecentTimeImpl (e);
412 }, "java.awt.AWTEvent");
413 Clazz.defineMethod (c$, "setCurrentEventAndMostRecentTimeImpl", 
414  function (e) {
415 if (swingjs.JSToolkit.isDispatchThread ()) {
416 return;
417 }this.currentEvent = e;
418 var mostRecentEventTime2 = -9223372036854775808;
419 if (Clazz.instanceOf (e, java.awt.event.InputEvent)) {
420 var ie = e;
421 mostRecentEventTime2 = ie.getWhen ();
422 } else if (Clazz.instanceOf (e, java.awt.event.InputMethodEvent)) {
423 var ime = e;
424 mostRecentEventTime2 = ime.getWhen ();
425 } else if (Clazz.instanceOf (e, java.awt.event.ActionEvent)) {
426 var ae = e;
427 mostRecentEventTime2 = ae.getWhen ();
428 } else if (Clazz.instanceOf (e, java.awt.event.InvocationEvent)) {
429 var ie = e;
430 mostRecentEventTime2 = ie.getWhen ();
431 }this.mostRecentEventTime = Math.max (this.mostRecentEventTime, mostRecentEventTime2);
432 }, "java.awt.AWTEvent");
433 c$.invokeLater = Clazz.defineMethod (c$, "invokeLater", 
434 function (runnable) {
435 java.awt.Toolkit.getEventQueue ().postEvent ( new java.awt.event.InvocationEvent (java.awt.Toolkit.getDefaultToolkit (), runnable));
436 }, "Runnable");
437 c$.invokeAndWait = Clazz.defineMethod (c$, "invokeAndWait", 
438 function (runnable) {
439 java.awt.EventQueue.invokeAndWaitStatic (java.awt.Toolkit.getDefaultToolkit (), runnable);
440 }, "Runnable");
441 c$.invokeAndWaitStatic = Clazz.defineMethod (c$, "invokeAndWaitStatic", 
442  function (source, runnable) {
443 if (java.awt.EventQueue.isDispatchThread ()) {
444 throw  new Error ("Cannot call invokeAndWait from the event dispatcher thread");
445 }var event =  new java.awt.event.InvocationEvent (source, runnable, null, true);
446 swingjs.JSToolkit.dispatchEvent (event, null, true);
447 var eventThrowable = event.getThrowable ();
448 if (eventThrowable != null) {
449 throw  new java.lang.reflect.InvocationTargetException (eventThrowable);
450 }}, "~O,Runnable");
451 Clazz.defineMethod (c$, "wakeup", 
452 function (isShutdown) {
453 {
454 if (this.nextQueue != null) {
455 this.nextQueue.wakeup (isShutdown);
456 } else if (this.dispatchThread != null) {
457 try {
458 this.dispatchThread.start ();
459 } catch (e) {
460 if (Clazz.exceptionOf (e, IllegalThreadStateException)) {
461 this.dispatchThread.run ();
462 } else {
463 throw e;
464 }
465 }
466 } else if (!isShutdown) {
467 this.initDispatchThread ();
468 }}}, "~B");
469 Clazz.defineStatics (c$,
470 "threadInitNumber", 0,
471 "LOW_PRIORITY", 0,
472 "NORM_PRIORITY", 1,
473 "HIGH_PRIORITY", 2,
474 "ULTIMATE_PRIORITY", 3,
475 "NUM_PRIORITIES", 4,
476 "PAINT", 0,
477 "UPDATE", 1,
478 "MOVE", 2,
479 "DRAG", 3,
480 "PEER", 4,
481 "CACHE_LENGTH", 5);
482 c$ = Clazz.decorateAsClass (function () {
483 this.head = null;
484 this.tail = null;
485 Clazz.instantialize (this, arguments);
486 }, java.awt, "Queue");
487 c$ = Clazz.decorateAsClass (function () {
488 this.event = null;
489 this.id = 0;
490 this.next = null;
491 Clazz.instantialize (this, arguments);
492 }, java.awt, "EventQueueItem");
493 Clazz.makeConstructor (c$, 
494 function (evt) {
495 this.event = evt;
496 this.id = evt.getID ();
497 }, "java.awt.AWTEvent");
498 });