/** * Similar to {@link EventQueue#invokeAndWait} but posts the event at the same * priority as paint requests, to avoid bad visual artifacts. */ static void invokeAndWaitLowPriority(RWLock m, Runnable r) throws InterruptedException, InvocationTargetException { Toolkit t = Toolkit.getDefaultToolkit(); EventQueue q = t.getSystemEventQueue(); Object lock = new PaintPriorityEventLock(); InvocationEvent ev = new PaintPriorityEvent(m, t, r, lock, true); synchronized (lock) { q.postEvent(ev); lock.wait(); } Exception e = ev.getException(); if (e != null) { throw new InvocationTargetException(e); } }
public static void invokeLater(Runnable event, Component component) throws InvocationTargetException { final InvocationEvent invocationEvent = new InvocationEvent(component != null ? component : Toolkit.getDefaultToolkit(), event); if (component != null) { final AppContext appContext = SunToolkit.targetToAppContext(component); SunToolkit.postEvent(appContext, invocationEvent); // 3746956 - flush events from PostEventQueue to prevent them from getting stuck and causing a deadlock SunToolkit.flushPendingEvents(appContext); } else { // This should be the equivalent to EventQueue.invokeAndWait ((LWCToolkit)Toolkit.getDefaultToolkit()).getSystemEventQueueForInvokeAndWait().postEvent(invocationEvent); } final Throwable eventException = invocationEvent.getException(); if (eventException == null) return; if (eventException instanceof UndeclaredThrowableException) { throw new InvocationTargetException(((UndeclaredThrowableException)eventException).getUndeclaredThrowable()); } throw new InvocationTargetException(eventException); }
@SuppressWarnings("deprecation") public void synthesizeWindowActivation(final boolean activate) { if (!activate || EventQueue.isDispatchThread()) { ((WFramePeer)getPeer()).emulateActivation(activate); } else { // To avoid focus concurrence b/w IE and EmbeddedFrame // activation is postponed by means of posting it to EDT. Runnable r = new Runnable() { public void run() { ((WFramePeer)getPeer()).emulateActivation(true); } }; WToolkit.postEvent(WToolkit.targetToAppContext(this), new InvocationEvent(this, r)); } }
public void replaceSurfaceDataLater() { Runnable r = new Runnable() { @Override public void run() { // Shouldn't do anything if object is disposed in meanwhile // No need for sync as disposeAction in Window is performed // on EDT if (!isDisposed()) { try { replaceSurfaceData(); } catch (InvalidPipeException e) { // REMIND : what do we do if our surface creation failed? } } } }; Component c = (Component)target; // Fix 6255371. if (!PaintEventDispatcher.getPaintEventDispatcher().queueSurfaceDataReplacing(c, r)) { postEvent(new InvocationEvent(c, r)); } }
/** * Kicks an event over to the appropriate event queue and waits for it to * finish To avoid deadlocking, we manually run the NSRunLoop while waiting * Any selector invoked using ThreadUtilities performOnMainThread will be * processed in doAWTRunLoop The InvocationEvent will call * LWCToolkit.stopAWTRunLoop() when finished, which will stop our manual * run loop. Does not dispatch native events while in the loop */ public static void invokeAndWait(Runnable runnable, Component component) throws InvocationTargetException { Objects.requireNonNull(component, "Null component provided to invokeAndWait"); long mediator = createAWTRunLoopMediator(); InvocationEvent invocationEvent = new InvocationEvent(component, runnable, () -> { if (mediator != 0) { stopAWTRunLoop(mediator); } }, true); AppContext appContext = SunToolkit.targetToAppContext(component); SunToolkit.postEvent(appContext, invocationEvent); // 3746956 - flush events from PostEventQueue to prevent them from getting stuck and causing a deadlock SunToolkit.flushPendingEvents(appContext); doAWTRunLoop(mediator, false); checkException(invocationEvent); }
public void synthesizeWindowActivation(final boolean activate) { final FramePeer peer = AWTAccessor.getComponentAccessor().getPeer(this); if (!activate || EventQueue.isDispatchThread()) { peer.emulateActivation(activate); } else { // To avoid focus concurrence b/w IE and EmbeddedFrame // activation is postponed by means of posting it to EDT. Runnable r = new Runnable() { public void run() { peer.emulateActivation(true); } }; WToolkit.postEvent(WToolkit.targetToAppContext(this), new InvocationEvent(this, r)); } }
/** * Checks if there's an active SelectorPerformer corresponding to the invocation's AppContext, * adds the invocation to the SelectorPerformer's queue and returns true. * Otherwise does nothing and returns false. */ public static boolean offer(InvocationEvent invocation) { Object source = invocation.getSource(); SelectorPerformer performer = (source instanceof Component) ? getInstance((Component)source) : getInstance(Toolkit.getDefaultToolkit().getSystemEventQueue()); if (performer == null) return false; synchronized (performer.invocations) { if (!performer.invocations.isEmpty()) { performer.invocations.peek().add(invocation); return true; } } return false; }
/** * Causes runnable to have its run method called in the dispatch thread of the * EventQueue. This will happen after all pending events are processed. The * call blocks until this has happened. This method will throw an Error if * called from the event dispatcher thread. * * @exception InterruptedException If another thread has interrupted * this thread. * @exception InvocationTargetException If an exception is thrown when running * runnable. * * @since 1.2 */ public static void invokeAndWait(Runnable runnable) throws InterruptedException, InvocationTargetException { if (isDispatchThread ()) throw new Error("Can't call invokeAndWait from event dispatch thread"); EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue(); Object notifyObject = new Object(); InvocationEvent ie = new InvocationEvent(eq, runnable, notifyObject, true); synchronized (notifyObject) { eq.postEvent(ie); notifyObject.wait(); } Exception exception; if ((exception = ie.getException()) != null) throw new InvocationTargetException(exception); }
/** * Causes <code>runnable</code> to have its <code>run</code> * method called in the dispatch thread of * {@link Toolkit#getSystemEventQueue the system EventQueue}. * This will happen after all pending events are processed. * The call blocks until this has happened. This method * will throw an Error if called from the event dispatcher thread. * * @param runnable the <code>Runnable</code> whose <code>run</code> * method should be executed * synchronously on the <code>EventQueue</code> * @exception InterruptedException if any thread has * interrupted this thread * @exception InvocationTargetException if an throwable is thrown * when running <code>runnable</code> * @see #invokeLater * @since 1.2 */ public static void invokeAndWait(Runnable runnable) throws InterruptedException, InvocationTargetException { if (EventQueue.isDispatchThread()) { throw new Error("Cannot call invokeAndWait from the event dispatcher thread"); } class AWTInvocationLock {} Object lock = new AWTInvocationLock(); InvocationEvent event = new InvocationEvent(Toolkit.getDefaultToolkit(), runnable, lock, true); synchronized (lock) { Toolkit.getEventQueue().postEvent(event); lock.wait(); } Throwable eventThrowable = event.getThrowable(); if (eventThrowable != null) { throw new InvocationTargetException(eventThrowable); } }
public void replaceSurfaceDataLater() { Runnable r = new Runnable() { public void run() { // Shouldn't do anything if object is disposed in meanwhile // No need for sync as disposeAction in Window is performed // on EDT if (!isDisposed()) { try { replaceSurfaceData(); } catch (InvalidPipeException e) { // REMIND : what do we do if our surface creation failed? } } } }; // Fix 6255371. if (!PaintEventDispatcher.getPaintEventDispatcher().queueSurfaceDataReplacing((Component)target, r)) { postEvent(new InvocationEvent(Toolkit.getDefaultToolkit(), r)); } }
public void handleButtonPressRelease(XEvent xev) { /* * Fix for 6385277. * We request focus on simple Window by click in order * to make it behave like Frame/Dialog in this case and also to unify * the behaviour with what we have on MS Windows. * handleJavaMouseEvent() would be more suitable place to do this * but we want Swing to have this functionality also. */ if (xev.get_type() == XConstants.ButtonPress) { final XWindowPeer parentXWindow = getParentTopLevel(); Window parentWindow = (Window)parentXWindow.getTarget(); if (parentXWindow.isFocusableWindow() && parentXWindow.isSimpleWindow() && XKeyboardFocusManagerPeer.getCurrentNativeFocusedWindow() != parentWindow) { postEvent(new InvocationEvent(parentWindow, new Runnable() { public void run() { // Request focus on the EDT of 'parentWindow' because // XDecoratedPeer.requestWindowFocus() calls client code. parentXWindow.requestXFocus(); } })); } } super.handleButtonPressRelease(xev); }
public void replaceSurfaceDataLater() { Runnable r = new Runnable() { public void run() { // Shouldn't do anything if object is disposed in meanwhile // No need for sync as disposeAction in Window is performed // on EDT if (!isDisposed()) { try { replaceSurfaceData(); } catch (InvalidPipeException e) { // REMIND : what do we do if our surface creation failed? } } } }; Component c = (Component)target; // Fix 6255371. if (!PaintEventDispatcher.getPaintEventDispatcher().queueSurfaceDataReplacing(c, r)) { postEvent(new InvocationEvent(c, r)); } }
private void updateCurrentEventAndTime(AWTEvent event) { currentEvent = event; long when = 0; if (event instanceof ActionEvent) { when = ((ActionEvent) event).getWhen(); } else if (event instanceof InputEvent) { when = ((InputEvent) event).getWhen(); } else if (event instanceof InputMethodEvent) { when = ((InputMethodEvent) event).getWhen(); } else if (event instanceof InvocationEvent) { when = ((InvocationEvent) event).getWhen(); } if (when != 0) { mostRecentEventTime = when; } }
/** * Set native caret at the given position <br> * Note: this method takes AWT lock inside because it walks through the * component hierarchy */ void setCaretPos(final int x, final int y) { Runnable r = new Runnable() { public void run() { toolkit.lockAWT(); try { setCaretPosImpl(x, y); } finally { toolkit.unlockAWT(); } } }; if (Thread.currentThread() instanceof EventDispatchThread) { r.run(); } else { toolkit.getSystemEventQueueImpl().postEvent(new InvocationEvent(this, r)); } }
public static void invokeAndWait(Runnable runnable) throws InterruptedException, InvocationTargetException { if (isDispatchThread()) { throw new Error(); } final Toolkit toolkit = Toolkit.getDefaultToolkit(); final Object notifier = new Object(); //$NON-LOCK-1$ InvocationEvent event = new InvocationEvent( toolkit, runnable, notifier, true); synchronized (notifier) { toolkit.getSystemEventQueueImpl().postEvent(event); notifier.wait(); } Exception exception = event.getException(); if (exception != null) { throw new InvocationTargetException(exception); } }
private void postAdjustmentEvent(final int type) { // create and post invocation event here: comp.postEvent(new InvocationEvent(ScrollPaneAdjustable.this, new Runnable() { public void run() { AdjustmentEvent event = new AdjustmentEvent(ScrollPaneAdjustable.this, AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED, type, value, getValueIsAdjusting()); if (callAWTListener) { comp.toolkit.lockAWT(); try { processAdjustmentEvent(event, adjustmentListeners.getSystemListeners()); } finally { comp.toolkit.unlockAWT(); } } processAdjustmentEvent(event, adjustmentListeners.getUserListeners()); } })); }