public static void main(String[] args) throws Exception { SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); Frame frame = new Frame(); frame.setSize(300, 300); frame.setVisible(true); toolkit.realSync(); Robot robot = new Robot(); robot.keyPress(KeyEvent.VK_DELETE); robot.keyRelease(KeyEvent.VK_DELETE); toolkit.realSync(); frame.dispose(); if (eventsCount != 3) { throw new RuntimeException("Wrong number of key events: " + eventsCount); } }
boolean isWindowPerpixelTranslucencySupported() { /* * Per-pixel alpha is supported if all the conditions are TRUE: * 1. The toolkit is a sort of SunToolkit * 2. The toolkit supports translucency in general * (isWindowTranslucencySupported()) * 3. There's at least one translucency-capable * GraphicsConfiguration */ Toolkit curToolkit = Toolkit.getDefaultToolkit(); if (!(curToolkit instanceof SunToolkit)) { return false; } if (!((SunToolkit)curToolkit).isWindowTranslucencySupported()) { return false; } // TODO: cache translucency capable GC return getTranslucencyCapableGC() != null; }
public void Blit(SurfaceData src, SurfaceData dst, Composite comp, Region clip, int sx, int sy, int dx, int dy, int w, int h) { try { SunToolkit.awtLock(); XRSurfaceData x11sdDst = (XRSurfaceData) dst; x11sdDst.validateAsDestination(null, clip); XRSurfaceData x11sdSrc = (XRSurfaceData) src; x11sdSrc.validateAsSource(null, XRUtils.RepeatNone, XRUtils.FAST); x11sdDst.maskBuffer.validateCompositeState(comp, null, null, null); x11sdDst.maskBuffer.compositeBlit(x11sdSrc, x11sdDst, sx, sy, dx, dy, w, h); } finally { SunToolkit.awtUnlock(); } }
@Override public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) { if (isRVObserver()) { isRVObserverCalled = true; SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); Image resolutionVariant = getResolutionVariant(img); int rvFlags = toolkit.checkImage(resolutionVariant, width, height, new IdleImageObserver()); if (rvFlags < infoflags) { throw new RuntimeException("Info flags are greater than" + " resolution varint info flags"); } } else if ((infoflags & ALLBITS) != 0) { isImageLoaded = true; } return (infoflags & ALLBITS) == 0; }
/** * Locks the queue for read/write access. */ public final void lock() { /* * Implementation note: In theory we should have two separate locks: * one lock to synchronize access to the RenderQueue, and then a * separate lock (the AWT lock) that only needs to be acquired when * we are about to flush the queue (using native windowing system * operations). In practice it has been difficult to enforce the * correct lock ordering; sometimes AWT will have already acquired * the AWT lock before grabbing the RQ lock (see 6253009), while the * expected order should be RQ lock and then AWT lock. Due to this * issue, using two separate locks is prone to deadlocks. Therefore, * to solve this issue we have decided to eliminate the separate RQ * lock and instead just acquire the AWT lock here. (Someday it might * be nice to go back to the old two-lock system, but that would * require potentially risky changes to AWT to ensure that it never * acquires the AWT lock before calling into 2D code that wants to * acquire the RQ lock.) */ SunToolkit.awtLock(); }
private void validateOptimizedDrawing() { boolean layeredComponentFound = false; synchronized(getTreeLock()) { Integer layer; for (Component c : getComponents()) { layer = null; if(SunToolkit.isInstanceOf(c, "javax.swing.JInternalFrame") || (c instanceof JComponent && (layer = (Integer)((JComponent)c). getClientProperty(LAYER_PROPERTY)) != null)) { if(layer != null && layer.equals(FRAME_CONTENT_LAYER)) continue; layeredComponentFound = true; break; } } } if(layeredComponentFound) optimizedDrawingPossible = false; else optimizedDrawingPossible = true; }
void test() throws Exception { ((SunToolkit) Toolkit.getDefaultToolkit()).realSync(); focusLatch.await(5, TimeUnit.SECONDS); Robot robot = new Robot(); robot.setAutoDelay(100); robot.keyPress(KeyEvent.VK_ALT); robot.keyPress(KeyEvent.VK_T); robot.keyRelease(KeyEvent.VK_T); robot.keyRelease(KeyEvent.VK_ALT); robot.keyPress(KeyEvent.VK_ALT); robot.keyPress(KeyEvent.VK_F); robot.keyRelease(KeyEvent.VK_F); robot.keyRelease(KeyEvent.VK_ALT); actionLatch.await(5, TimeUnit.SECONDS); if (!action1 || !action2) { throw new RuntimeException("Actions not performed"); } System.out.println("Passed."); }
void dispatch(final _NativeEvent event) { // grab a local ref to the handler final H localHandler; final AppContext localHandlerContext; synchronized (_AppEventDispatcher.this) { localHandler = _handler; localHandlerContext = handlerContext; } if (localHandler == null) { performDefaultAction(event); } else { SunToolkit.invokeLaterOnAppContext(localHandlerContext, new Runnable() { public void run() { performUsing(localHandler, event); } }); } }
/** * Waits until all events currently on the event queue have been processed. * @throws IllegalThreadStateException if called on the AWT event dispatching thread */ public synchronized void waitForIdle() { checkNotDispatchThread(); // post a dummy event to the queue so we know when // all the events before it have been processed try { SunToolkit.flushPendingEvents(); EventQueue.invokeAndWait( new Runnable() { public void run() { // dummy implementation } } ); } catch(InterruptedException ite) { System.err.println("Robot.waitForIdle, non-fatal exception caught:"); ite.printStackTrace(); } catch(InvocationTargetException ine) { System.err.println("Robot.waitForIdle, non-fatal exception caught:"); ine.printStackTrace(); } }
private void stopLWModal() { synchronized (getTreeLock()) { if (modalAppContext != null) { Container nativeContainer = getHeavyweightContainer(); if(nativeContainer != null) { if (this.modalComp != null) { nativeContainer.modalComp = this.modalComp; this.modalComp = null; return; } else { nativeContainer.modalComp = null; } } // Wake up event dispatch thread on which the dialog was // initially shown SunToolkit.postEvent(modalAppContext, new PeerEvent(this, new WakingRunnable(), PeerEvent.PRIORITY_EVENT)); } EventQueue.invokeLater(new WakingRunnable()); getTreeLock().notifyAll(); } }
private static MouseEvent newMouseEvent( Component source, Point point, MouseEvent template ) { MouseEvent e = template; MouseEvent nme = new MouseEvent( source, e.getID(), e.getWhen(), e.getModifiersEx() | e.getModifiers(), point.x, point.y, e.getXOnScreen(), e.getYOnScreen(), e.getClickCount(), e.isPopupTrigger(), e.getButton() ); // Because these MouseEvents are dispatched directly to // their target, we need to mark them as being // system-generated here SunToolkit.setSystemGenerated(nme); return nme; }
/** * Adds the specified component to this scroll pane container. * If the scroll pane has an existing child component, that * component is removed and the new one is added. * @param comp the component to be added * @param constraints not applicable * @param index position of child component (must be <= 0) */ protected final void addImpl(Component comp, Object constraints, int index) { synchronized (getTreeLock()) { if (getComponentCount() > 0) { remove(0); } if (index > 0) { throw new IllegalArgumentException("position greater than 0"); } if (!SunToolkit.isLightweightOrUnknown(comp)) { super.addImpl(comp, constraints, index); } else { addToPanel(comp, constraints, index); } } }
/** * upcall from native code via implemented class (do) */ protected final void dragDropFinished(final boolean success, final int operations, final int x, final int y) { DragSourceEvent event = new DragSourceDropEvent(getDragSourceContext(), operations & sourceActions, success, x, y); EventDispatcher dispatcher = new EventDispatcher(DISPATCH_FINISH, event); SunToolkit.invokeLaterOnAppContext( SunToolkit.targetToAppContext(getComponent()), dispatcher); startSecondaryEventLoop(); setNativeContext(0); dragImage = null; dragImageOffset = null; }
public void drawLine(SunGraphics2D sg2d, int x1, int y1, int x2, int y2) { Region compClip = sg2d.getCompClip(); int transX1 = Region.clipAdd(x1, sg2d.transX); int transY1 = Region.clipAdd(y1, sg2d.transY); int transX2 = Region.clipAdd(x2, sg2d.transX); int transY2 = Region.clipAdd(y2, sg2d.transY); SunToolkit.awtLock(); try { validateSurface(sg2d); lineGen.rasterizeLine(rectBuffer, transX1, transY1, transX2, transY2, compClip.getLoX(), compClip.getLoY(), compClip.getHiX(), compClip.getHiY(), true, true); tileManager.fillMask((XRSurfaceData) sg2d.surfaceData); } finally { SunToolkit.awtUnlock(); } }
public void Blit(SurfaceData src, SurfaceData dst, Composite comp, Region clip, int sx, int sy, int dx, int dy, int w, int h) { SunToolkit.awtLock(); try { X11SurfaceData x11sd = (X11SurfaceData)dst; // pass null clip region here since we clip manually in native code // also use false for needExposures since we clip to the pixmap long xgc = x11sd.getBlitGC(null, false); nativeBlit(src.getNativeOps(), dst.getNativeOps(), xgc, clip, sx, sy, dx, dy, w, h); } finally { SunToolkit.awtUnlock(); } }
static void testToolkitMultiResolutionImageLoad() throws Exception { File imageFile = new File(IMAGE_NAME_1X); String fileName = imageFile.getAbsolutePath(); Image image = Toolkit.getDefaultToolkit().getImage(fileName); SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); toolkit.prepareImage(image, -1, -1, new LoadImageObserver()); final long time = WAIT_TIME + System.currentTimeMillis(); while ((!isImageLoaded || !isRVObserverCalled) && System.currentTimeMillis() < time) { Thread.sleep(50); } if(!isImageLoaded){ throw new RuntimeException("Image is not loaded!"); } if(!isRVObserverCalled){ throw new RuntimeException("Resolution Variant observer is not called!"); } }
@Override public void BlitBg(SurfaceData src, SurfaceData dst, Composite comp, Region clip, int bgColor, int sx, int sy, int dx, int dy, int w, int h) { SunToolkit.awtLock(); try { int pixel = dst.pixelFor(bgColor); X11SurfaceData x11sd = (X11SurfaceData)dst; // use false for needExposures since we clip to the pixmap long xgc = x11sd.getBlitGC(clip, false); nativeBlitBg(src.getNativeOps(), dst.getNativeOps(), xgc, pixel, sx, sy, dx, dy, w, h); } finally { SunToolkit.awtUnlock(); } }
@Override public synchronized void showPopupMenu(final int x, final int y) { if (isDisposed()) return; SunToolkit.executeOnEventHandlerThread(target, new Runnable() { @Override public void run() { PopupMenu newPopup = ((TrayIcon)target).getPopupMenu(); if (popup != newPopup) { if (popup != null) { popupParent.remove(popup); } if (newPopup != null) { popupParent.add(newPopup); } popup = newPopup; } if (popup != null) { ((WPopupMenuPeer)popup.getPeer()).show(popupParent, new Point(x, y)); } } }); }
@Override protected void disposeImpl() { // TODO: we should somehow reset the listener when the choice // is moved to another toplevel without destroying its peer. Window parentWindow = SunToolkit.getContainingWindow((Component)target); if (parentWindow != null) { final WWindowPeer wpeer = AWTAccessor.getComponentAccessor() .getPeer(parentWindow); if (wpeer != null) { wpeer.removeWindowListener(windowListener); } } super.disposeImpl(); }
public static void main(String[] args) throws Exception { SwingUtilities.invokeAndWait(new Runnable() { public void run() { editor = new JEditorPane("text/html", "Hello world!"); } }); Thread thread = new Thread(new ThreadGroup("Some ThreadGroup"), new Runnable() { public void run() { SunToolkit.createNewAppContext(); try { SwingUtilities.invokeAndWait(new Runnable() { public void run() { editor.setText("Hello world!"); } }); } catch (Exception e) { throw new RuntimeException(e); } } }); thread.start(); thread.join(); }
public void drawRect(SunGraphics2D sg2d, int x, int y, int width, int height) { SunToolkit.awtLock(); try { long xgc = validate(sg2d); XDrawRect(sg2d.surfaceData.getNativeOps(), xgc, x+sg2d.transX, y+sg2d.transY, width, height); } finally { SunToolkit.awtUnlock(); } }
synchronized void addNotify() { if (peer == null) { Toolkit toolkit = Toolkit.getDefaultToolkit(); if (toolkit instanceof SunToolkit) { peer = ((SunToolkit)Toolkit.getDefaultToolkit()).createSystemTray(this); } else if (toolkit instanceof HeadlessToolkit) { peer = ((HeadlessToolkit)Toolkit.getDefaultToolkit()).createSystemTray(this); } } }
public void drawLine(SunGraphics2D sg2d, int x1, int y1, int x2, int y2) { SunToolkit.awtLock(); try { long xgc = validate(sg2d); int transx = sg2d.transX; int transy = sg2d.transY; XDrawLine(sg2d.surfaceData.getNativeOps(), xgc, x1+transx, y1+transy, x2+transx, y2+transy); } finally { SunToolkit.awtUnlock(); } }
private static void setLayersOpaque(Component component, boolean isOpaque) { // Shouldn't use instanceof to avoid loading Swing classes // if it's a pure AWT application. if (SunToolkit.isInstanceOf(component, "javax.swing.RootPaneContainer")) { javax.swing.RootPaneContainer rpc = (javax.swing.RootPaneContainer)component; javax.swing.JRootPane root = rpc.getRootPane(); javax.swing.JLayeredPane lp = root.getLayeredPane(); Container c = root.getContentPane(); javax.swing.JComponent content = (c instanceof javax.swing.JComponent) ? (javax.swing.JComponent)c : null; lp.setOpaque(isOpaque); root.setOpaque(isOpaque); if (content != null) { content.setOpaque(isOpaque); // Iterate down one level to see whether we have a JApplet // (which is also a RootPaneContainer) which requires processing int numChildren = content.getComponentCount(); if (numChildren > 0) { Component child = content.getComponent(0); // It's OK to use instanceof here because we've // already loaded the RootPaneContainer class by now if (child instanceof javax.swing.RootPaneContainer) { setLayersOpaque(child, isOpaque); } } } } }
public void dispatch() { try { if (nested != null) { Toolkit.getEventQueue().dispatchEvent(nested); } } finally { dispatched = true; if (toNotify != null) { SunToolkit.postEvent(toNotify, new SentEvent()); } synchronized (this) { notifyAll(); } } }
public void fillRect(SunGraphics2D sg2d, int x, int y, int width, int height) { SunToolkit.awtLock(); try { long xgc = validate(sg2d); XFillRect(sg2d.surfaceData.getNativeOps(), xgc, x+sg2d.transX, y+sg2d.transY, width, height); } finally { SunToolkit.awtUnlock(); } }
private void invokeOnEachAppContext(Runnable runnable) { for (AppContext appContext : AppContext.getAppContexts()) { SunToolkit.invokeLaterOnAppContext(appContext, runnable); } }
private static boolean focusedWindowChanged(Component to, Component from) { Window wto = SunToolkit.getContainingWindow(to); Window wfrom = SunToolkit.getContainingWindow(from); if (wto == null && wfrom == null) { return true; } if (wto == null) { return true; } if (wfrom == null) { return true; } return (wto != wfrom); }
public static void main(String args[]) throws Exception { SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { createAndShowGUI(); } }); toolkit.realSync(); SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { try { page = new URL(page, "#linkname"); jep.setPage(page); } catch (Exception e) { throw new RuntimeException(e); } } }); toolkit.realSync(); if (getPageAnchor() == null) { throw new RuntimeException("JEditorPane.getPage() returns null anchor reference"); } }
private void showInputMethodMenuOnRequesterEDT(Component requester) throws InterruptedException, InvocationTargetException { if (requester == null){ return; } class AWTInvocationLock {} Object lock = new AWTInvocationLock(); InvocationEvent event = new InvocationEvent(requester, new Runnable() { public void run() { showInputMethodMenu(); } }, lock, true); AppContext requesterAppContext = SunToolkit.targetToAppContext(requester); synchronized (lock) { SunToolkit.postEvent(requesterAppContext, event); while (!event.isDispatched()) { lock.wait(); } } Throwable eventThrowable = event.getThrowable(); if (eventThrowable != null) { throw new InvocationTargetException(eventThrowable); } }
public boolean queueSurfaceDataReplacing(Component c, Runnable r) { if (c instanceof RootPaneContainer) { AppContext appContext = SunToolkit.targetToAppContext(c); RepaintManager.currentManager(appContext). nativeQueueSurfaceDataRunnable(appContext, c, r); return true; } return super.queueSurfaceDataReplacing(c, r); }
public static void main(String[] args) throws Exception { SwingUtilities.invokeAndWait(new Runnable() { public void run() { jtf = new JTextField("WW"); JFrame frame = new JFrame(); frame.getContentPane().add(jtf); frame.pack(); frame.setVisible(true); } }); ((SunToolkit) SunToolkit.getDefaultToolkit()).realSync(); SwingUtilities.invokeAndWait(new Runnable() { public void run() { try { Rectangle r = jtf.modelToView(1); int delta = 2; for (int x = r.x - delta; x < r.x + delta; x++) { assertEquals(jtf.viewToModel(new Point(x, r.y)), 1); } System.out.println("Passed."); } catch (BadLocationException e) { throw new RuntimeException("Test failed", e); } } }); }
public void drawPolyline(SunGraphics2D sg2d, int xpoints[], int ypoints[], int npoints) { SunToolkit.awtLock(); try { long xgc = validate(sg2d); XDrawPoly(sg2d.surfaceData.getNativeOps(), xgc, sg2d.transX, sg2d.transY, xpoints, ypoints, npoints, false); } finally { SunToolkit.awtUnlock(); } }
/** * Returns whether or not this window peer has native X window * configured as non-focusable window. It might happen if: * - Java window is non-focusable * - Java window is simple Window(not Frame or Dialog) */ boolean isNativelyNonFocusableWindow() { if (XToolkit.isToolkitThread() || SunToolkit.isAWTLockHeldByCurrentThread()) { return isSimpleWindow() || !cachedFocusableWindow; } else { return isSimpleWindow() || !(((Window)target).isFocusableWindow()); } }
public void Transform(SurfaceData src, SurfaceData dst, Composite comp, Region clip, AffineTransform xform, int hint, int sx, int sy, int dstx, int dsty, int w, int h) { try { SunToolkit.awtLock(); XRSurfaceData vImgSurface = XRPMBlitLoops.cacheToTmpSurface(src, (XRSurfaceData) dst, w, h, sx, sy); pmToSurfaceBlit.Transform(vImgSurface, dst, comp, clip, xform, hint, 0, 0, dstx, dsty, w, h); } finally { SunToolkit.awtUnlock(); } }