527 PostEventQueue postEventQueue = 528 (PostEventQueue)appContext.get(POST_EVENT_QUEUE_KEY); 529 if (postEventQueue != null) { 530 postEventQueue.postEvent(event); 531 } 532 } 533 534 /* 535 * Post AWTEvent of high priority. 536 */ 537 public static void postPriorityEvent(final AWTEvent e) { 538 PeerEvent pe = new PeerEvent(Toolkit.getDefaultToolkit(), new Runnable() { 539 public void run() { 540 AWTAccessor.getAWTEventAccessor().setPosted(e); 541 ((Component)e.getSource()).dispatchEvent(e); 542 } 543 }, PeerEvent.ULTIMATE_PRIORITY_EVENT); 544 postEvent(targetToAppContext(e.getSource()), pe); 545 } 546 547 protected static final Lock flushLock = new ReentrantLock(); 548 private static boolean isFlushingPendingEvents = false; 549 550 /* 551 * Flush any pending events which haven't been posted to the AWT 552 * EventQueue yet. 553 */ 554 public static void flushPendingEvents() { 555 flushLock.lock(); 556 try { 557 // Don't call flushPendingEvents() recursively 558 if (!isFlushingPendingEvents) { 559 isFlushingPendingEvents = true; 560 AppContext appContext = AppContext.getAppContext(); 561 PostEventQueue postEventQueue = 562 (PostEventQueue)appContext.get(POST_EVENT_QUEUE_KEY); 563 if (postEventQueue != null) { 564 postEventQueue.flush(); 565 } 566 } 567 } finally { 568 isFlushingPendingEvents = false; 569 flushLock.unlock(); 570 } 571 } 572 573 public static boolean isPostEventQueueEmpty() { 574 AppContext appContext = AppContext.getAppContext(); 575 PostEventQueue postEventQueue = 576 (PostEventQueue)appContext.get(POST_EVENT_QUEUE_KEY); 577 if (postEventQueue != null) { 578 return postEventQueue.noEvents(); 579 } else { 580 return true; 581 } 582 } 583 584 /* 585 * Execute a chunk of code on the Java event handler thread for the 586 * given target. Does not wait for the execution to occur before 587 * returning to the caller. 588 */ 589 public static void executeOnEventHandlerThread(Object target, 590 Runnable runnable) { 591 executeOnEventHandlerThread(new PeerEvent(target, runnable, PeerEvent.PRIORITY_EVENT)); 592 } 593 594 /* 595 * Fixed 5064013: the InvocationEvent time should be equals 596 * the time of the ActionEvent 597 */ 598 @SuppressWarnings("serial") 599 public static void executeOnEventHandlerThread(Object target, 600 Runnable runnable, 2095 return AWTAccessor.getAWTEventAccessor().isSystemGenerated(e); 2096 } 2097 2098 } // class SunToolkit 2099 2100 2101 /* 2102 * PostEventQueue is a Thread that runs in the same AppContext as the 2103 * Java EventQueue. It is a queue of AWTEvents to be posted to the 2104 * Java EventQueue. The toolkit Thread (AWT-Windows/AWT-Motif) posts 2105 * events to this queue, which then calls EventQueue.postEvent(). 2106 * 2107 * We do this because EventQueue.postEvent() may be overridden by client 2108 * code, and we mustn't ever call client code from the toolkit thread. 2109 */ 2110 class PostEventQueue { 2111 private EventQueueItem queueHead = null; 2112 private EventQueueItem queueTail = null; 2113 private final EventQueue eventQueue; 2114 2115 // For the case when queue is cleared but events are not posted 2116 private volatile boolean isFlushing = false; 2117 2118 PostEventQueue(EventQueue eq) { 2119 eventQueue = eq; 2120 } 2121 2122 public synchronized boolean noEvents() { 2123 return queueHead == null && !isFlushing; 2124 } 2125 2126 /* 2127 * Continually post pending AWTEvents to the Java EventQueue. The method 2128 * is synchronized to ensure the flush is completed before a new event 2129 * can be posted to this queue. 2130 * 2131 * 7177040: The method couldn't be wholly synchronized because of calls 2132 * of EventQueue.postEvent() that uses pushPopLock, otherwise it could 2133 * potentially lead to deadlock 2134 */ 2135 public void flush() { 2136 EventQueueItem tempQueue; 2137 synchronized (this) { 2138 tempQueue = queueHead; 2139 queueHead = queueTail = null; 2140 isFlushing = true; 2141 } 2142 try { 2143 while (tempQueue != null) { 2144 eventQueue.postEvent(tempQueue.event); 2145 tempQueue = tempQueue.next; 2146 } 2147 } 2148 finally { 2149 isFlushing = false; 2150 } 2151 } 2152 2153 /* 2154 * Enqueue an AWTEvent to be posted to the Java EventQueue. 2155 */ 2156 void postEvent(AWTEvent event) { 2157 EventQueueItem item = new EventQueueItem(event); 2158 2159 synchronized (this) { 2160 if (queueHead == null) { 2161 queueHead = queueTail = item; 2162 } else { 2163 queueTail.next = item; 2164 queueTail = item; 2165 } 2166 } 2167 SunToolkit.wakeupEventQueue(eventQueue, event.getSource() == AWTAutoShutdown.getInstance()); 2168 } 2169 } // class PostEventQueue | 527 PostEventQueue postEventQueue = 528 (PostEventQueue)appContext.get(POST_EVENT_QUEUE_KEY); 529 if (postEventQueue != null) { 530 postEventQueue.postEvent(event); 531 } 532 } 533 534 /* 535 * Post AWTEvent of high priority. 536 */ 537 public static void postPriorityEvent(final AWTEvent e) { 538 PeerEvent pe = new PeerEvent(Toolkit.getDefaultToolkit(), new Runnable() { 539 public void run() { 540 AWTAccessor.getAWTEventAccessor().setPosted(e); 541 ((Component)e.getSource()).dispatchEvent(e); 542 } 543 }, PeerEvent.ULTIMATE_PRIORITY_EVENT); 544 postEvent(targetToAppContext(e.getSource()), pe); 545 } 546 547 private static boolean isFlushingPendingEvents = false; 548 549 /* 550 * Flush any pending events which haven't been posted to the AWT 551 * EventQueue yet. 552 */ 553 public static void flushPendingEvents() { 554 try { 555 // Don't call flushPendingEvents() recursively 556 if (!isFlushingPendingEvents) { 557 isFlushingPendingEvents = true; 558 AppContext appContext = AppContext.getAppContext(); 559 PostEventQueue postEventQueue = 560 (PostEventQueue)appContext.get(POST_EVENT_QUEUE_KEY); 561 if (postEventQueue != null) { 562 postEventQueue.flush(); 563 } 564 } 565 } finally { 566 isFlushingPendingEvents = false; 567 } 568 } 569 570 /* 571 * Execute a chunk of code on the Java event handler thread for the 572 * given target. Does not wait for the execution to occur before 573 * returning to the caller. 574 */ 575 public static void executeOnEventHandlerThread(Object target, 576 Runnable runnable) { 577 executeOnEventHandlerThread(new PeerEvent(target, runnable, PeerEvent.PRIORITY_EVENT)); 578 } 579 580 /* 581 * Fixed 5064013: the InvocationEvent time should be equals 582 * the time of the ActionEvent 583 */ 584 @SuppressWarnings("serial") 585 public static void executeOnEventHandlerThread(Object target, 586 Runnable runnable, 2081 return AWTAccessor.getAWTEventAccessor().isSystemGenerated(e); 2082 } 2083 2084 } // class SunToolkit 2085 2086 2087 /* 2088 * PostEventQueue is a Thread that runs in the same AppContext as the 2089 * Java EventQueue. It is a queue of AWTEvents to be posted to the 2090 * Java EventQueue. The toolkit Thread (AWT-Windows/AWT-Motif) posts 2091 * events to this queue, which then calls EventQueue.postEvent(). 2092 * 2093 * We do this because EventQueue.postEvent() may be overridden by client 2094 * code, and we mustn't ever call client code from the toolkit thread. 2095 */ 2096 class PostEventQueue { 2097 private EventQueueItem queueHead = null; 2098 private EventQueueItem queueTail = null; 2099 private final EventQueue eventQueue; 2100 2101 PostEventQueue(EventQueue eq) { 2102 eventQueue = eq; 2103 } 2104 2105 /* 2106 * Continually post pending AWTEvents to the Java EventQueue. The method 2107 * is synchronized to ensure the flush is completed before a new event 2108 * can be posted to this queue. 2109 * 2110 * 7177040: The method couldn't be wholly synchronized because of calls 2111 * of EventQueue.postEvent() that uses pushPopLock, otherwise it could 2112 * potentially lead to deadlock 2113 */ 2114 public synchronized void flush() { 2115 EventQueueItem tempQueue = queueHead; 2116 queueHead = queueTail = null; 2117 while (tempQueue != null) { 2118 eventQueue.postEvent(tempQueue.event); 2119 tempQueue = tempQueue.next; 2120 } 2121 } 2122 2123 /* 2124 * Enqueue an AWTEvent to be posted to the Java EventQueue. 2125 */ 2126 void postEvent(AWTEvent event) { 2127 EventQueueItem item = new EventQueueItem(event); 2128 2129 synchronized (this) { 2130 if (queueHead == null) { 2131 queueHead = queueTail = item; 2132 } else { 2133 queueTail.next = item; 2134 queueTail = item; 2135 } 2136 } 2137 SunToolkit.wakeupEventQueue(eventQueue, event.getSource() == AWTAutoShutdown.getInstance()); 2138 } 2139 } // class PostEventQueue |