1 /*
   2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.lwawt;
  27 
  28 import java.awt.*;
  29 import java.awt.event.*;
  30 import java.awt.peer.*;
  31 import java.util.List;
  32 
  33 import javax.swing.*;
  34 
  35 import sun.awt.*;
  36 import sun.java2d.*;
  37 import sun.java2d.loops.Blit;
  38 import sun.java2d.loops.CompositeType;
  39 import sun.java2d.pipe.Region;
  40 import sun.util.logging.PlatformLogger;
  41 
  42 public class LWWindowPeer
  43     extends LWContainerPeer<Window, JComponent>
  44     implements FramePeer, DialogPeer, FullScreenCapable, DisplayChangedListener, PlatformEventNotifier
  45 {
  46     public enum PeerType {
  47         SIMPLEWINDOW,
  48         FRAME,
  49         DIALOG,
  50         EMBEDDED_FRAME,
  51         VIEW_EMBEDDED_FRAME,
  52         LW_FRAME
  53     }
  54 
  55     private static final PlatformLogger focusLog = PlatformLogger.getLogger("sun.lwawt.focus.LWWindowPeer");
  56 
  57     private final PlatformWindow platformWindow;
  58 
  59     // Window bounds reported by the native system (as opposed to
  60     // regular bounds inherited from LWComponentPeer which are
  61     // requested by user and may haven't been applied yet because
  62     // of asynchronous requests to the windowing system)
  63     private int sysX;
  64     private int sysY;
  65     private int sysW;
  66     private int sysH;
  67 
  68     private static final int MINIMUM_WIDTH = 1;
  69     private static final int MINIMUM_HEIGHT = 1;
  70 
  71     private Insets insets = new Insets(0, 0, 0, 0);
  72 
  73     private GraphicsDevice graphicsDevice;
  74     private GraphicsConfiguration graphicsConfig;
  75 
  76     private SurfaceData surfaceData;
  77     private final Object surfaceDataLock = new Object();
  78 
  79     private volatile int windowState = Frame.NORMAL;
  80 
  81     // check that the mouse is over the window
  82     private volatile boolean isMouseOver = false;
  83 
  84     // A peer where the last mouse event came to. Used by cursor manager to
  85     // find the component under cursor
  86     private static volatile LWComponentPeer<?, ?> lastCommonMouseEventPeer;
  87 
  88     // A peer where the last mouse event came to. Used to generate
  89     // MOUSE_ENTERED/EXITED notifications
  90     private volatile LWComponentPeer<?, ?> lastMouseEventPeer;
  91 
  92     // Peers where all dragged/released events should come to,
  93     // depending on what mouse button is being dragged according to Cocoa
  94     private static final LWComponentPeer<?, ?>[] mouseDownTarget = new LWComponentPeer<?, ?>[3];
  95 
  96     // A bitmask that indicates what mouse buttons produce MOUSE_CLICKED events
  97     // on MOUSE_RELEASE. Click events are only generated if there were no drag
  98     // events between MOUSE_PRESSED and MOUSE_RELEASED for particular button
  99     private static int mouseClickButtons = 0;
 100 
 101     private volatile boolean isOpaque = true;
 102 
 103     private static final Font DEFAULT_FONT = new Font("Lucida Grande", Font.PLAIN, 13);
 104 
 105     private static LWWindowPeer grabbingWindow;
 106 
 107     private volatile boolean skipNextFocusChange;
 108 
 109     private static final Color nonOpaqueBackground = new Color(0, 0, 0, 0);
 110 
 111     private volatile boolean textured;
 112 
 113     private final PeerType peerType;
 114 
 115     private final SecurityWarningWindow warningWindow;
 116 
 117     /**
 118      * Current modal blocker or null.
 119      *
 120      * Synchronization: peerTreeLock.
 121      */
 122     private LWWindowPeer blocker;
 123 
 124     public LWWindowPeer(Window target, PlatformComponent platformComponent,
 125                         PlatformWindow platformWindow, PeerType peerType)
 126     {
 127         super(target, platformComponent);
 128         this.platformWindow = platformWindow;
 129         this.peerType = peerType;
 130 
 131         Window owner = target.getOwner();
 132         LWWindowPeer ownerPeer = owner == null ? null :
 133              (LWWindowPeer) AWTAccessor.getComponentAccessor().getPeer(owner);
 134         PlatformWindow ownerDelegate = (ownerPeer != null) ? ownerPeer.getPlatformWindow() : null;
 135 
 136         // The delegate.initialize() needs a non-null GC on X11.
 137         GraphicsConfiguration gc = getTarget().getGraphicsConfiguration();
 138         synchronized (getStateLock()) {
 139             // graphicsConfig should be updated according to the real window
 140             // bounds when the window is shown, see 4868278
 141             this.graphicsConfig = gc;
 142         }
 143 
 144         if (!target.isFontSet()) {
 145             target.setFont(DEFAULT_FONT);
 146         }
 147 
 148         if (!target.isBackgroundSet()) {
 149             target.setBackground(SystemColor.window);
 150         } else {
 151             // first we check if user provided alpha for background. This is
 152             // similar to what Apple's Java do.
 153             // Since JDK7 we should rely on setOpacity() only.
 154             // this.opacity = c.getAlpha();
 155         }
 156 
 157         if (!target.isForegroundSet()) {
 158             target.setForeground(SystemColor.windowText);
 159             // we should not call setForeground because it will call a repaint
 160             // which the peer may not be ready to do yet.
 161         }
 162 
 163         platformWindow.initialize(target, this, ownerDelegate);
 164 
 165         // Init warning window(for applets)
 166         SecurityWarningWindow warn = null;
 167         if (target.getWarningString() != null) {
 168             // accessSystemTray permission allows to display TrayIcon, TrayIcon tooltip
 169             // and TrayIcon balloon windows without a warning window.
 170             if (!AWTAccessor.getWindowAccessor().isTrayIconWindow(target)) {
 171                 LWToolkit toolkit = (LWToolkit)Toolkit.getDefaultToolkit();
 172                 warn = toolkit.createSecurityWarning(target, this);
 173             }
 174         }
 175 
 176         warningWindow = warn;
 177     }
 178 
 179     @Override
 180     void initializeImpl() {
 181         super.initializeImpl();
 182 
 183 
 184         if (getTarget() instanceof Frame) {
 185             setTitle(((Frame) getTarget()).getTitle());
 186             setState(((Frame) getTarget()).getExtendedState());
 187         } else if (getTarget() instanceof Dialog) {
 188             setTitle(((Dialog) getTarget()).getTitle());
 189         }
 190 
 191         updateAlwaysOnTopState();
 192         updateMinimumSize();
 193 
 194         final Shape shape = getTarget().getShape();
 195         if (shape != null) {
 196             applyShape(Region.getInstance(shape, null));
 197         }
 198 
 199         final float opacity = getTarget().getOpacity();
 200         if (opacity < 1.0f) {
 201             setOpacity(opacity);
 202         }
 203 
 204         setOpaque(getTarget().isOpaque());
 205 
 206         updateInsets(platformWindow.getInsets());
 207         if (getSurfaceData() == null) {
 208             replaceSurfaceData(false);
 209         }
 210         activateDisplayListener();
 211     }
 212 
 213     // Just a helper method
 214     @Override
 215     public PlatformWindow getPlatformWindow() {
 216         return platformWindow;
 217     }
 218 
 219     @Override
 220     protected LWWindowPeer getWindowPeerOrSelf() {
 221         return this;
 222     }
 223 
 224     // ---- PEER METHODS ---- //
 225 
 226     @Override
 227     protected void disposeImpl() {
 228         deactivateDisplayListener();
 229         SurfaceData oldData = getSurfaceData();
 230         synchronized (surfaceDataLock){
 231             surfaceData = null;
 232         }
 233         if (oldData != null) {
 234             oldData.invalidate();
 235         }
 236         if (isGrabbing()) {
 237             ungrab();
 238         }
 239         if (warningWindow != null) {
 240             warningWindow.dispose();
 241         }
 242 
 243         platformWindow.dispose();
 244         super.disposeImpl();
 245     }
 246 
 247     @Override
 248     protected void setVisibleImpl(final boolean visible) {
 249         if (!visible && warningWindow != null) {
 250             warningWindow.setVisible(false, false);
 251         }
 252 
 253         super.setVisibleImpl(visible);
 254         // TODO: update graphicsConfig, see 4868278
 255         platformWindow.setVisible(visible);
 256         if (isSimpleWindow()) {
 257             KeyboardFocusManagerPeer kfmPeer = LWKeyboardFocusManagerPeer.getInstance();
 258 
 259             if (visible) {
 260                 if (!getTarget().isAutoRequestFocus()) {
 261                     return;
 262                 } else {
 263                     requestWindowFocus(CausedFocusEvent.Cause.ACTIVATION);
 264                 }
 265             // Focus the owner in case this window is focused.
 266             } else if (kfmPeer.getCurrentFocusedWindow() == getTarget()) {
 267                 // Transfer focus to the owner.
 268                 LWWindowPeer owner = getOwnerFrameDialog(LWWindowPeer.this);
 269                 if (owner != null) {
 270                     owner.requestWindowFocus(CausedFocusEvent.Cause.ACTIVATION);
 271                 }
 272             }
 273         }
 274     }
 275 
 276     @Override
 277     public final GraphicsConfiguration getGraphicsConfiguration() {
 278         synchronized (getStateLock()) {
 279             return graphicsConfig;
 280         }
 281     }
 282 
 283     @Override
 284     public boolean updateGraphicsData(GraphicsConfiguration gc) {
 285         setGraphicsConfig(gc);
 286         return false;
 287     }
 288 
 289     protected final Graphics getOnscreenGraphics(Color fg, Color bg, Font f) {
 290         if (getSurfaceData() == null) {
 291             return null;
 292         }
 293         if (fg == null) {
 294             fg = SystemColor.windowText;
 295         }
 296         if (bg == null) {
 297             bg = SystemColor.window;
 298         }
 299         if (f == null) {
 300             f = DEFAULT_FONT;
 301         }
 302         return platformWindow.transformGraphics(new SunGraphics2D(getSurfaceData(), fg, bg, f));
 303     }
 304 
 305     @Override
 306     public void setBounds(int x, int y, int w, int h, int op) {
 307 
 308         if((op & NO_EMBEDDED_CHECK) == 0 && getPeerType() == PeerType.VIEW_EMBEDDED_FRAME) {
 309             return;
 310         }
 311 
 312         if ((op & SET_CLIENT_SIZE) != 0) {
 313             // SET_CLIENT_SIZE is only applicable to window peers, so handle it here
 314             // instead of pulling 'insets' field up to LWComponentPeer
 315             // no need to add insets since Window's notion of width and height includes insets.
 316             op &= ~SET_CLIENT_SIZE;
 317             op |= SET_SIZE;
 318         }
 319 
 320         if (w < MINIMUM_WIDTH) {
 321             w = MINIMUM_WIDTH;
 322         }
 323         if (h < MINIMUM_HEIGHT) {
 324             h = MINIMUM_HEIGHT;
 325         }
 326 
 327         final int maxW = getLWGC().getMaxTextureWidth();
 328         final int maxH = getLWGC().getMaxTextureHeight();
 329 
 330         if (w > maxW) {
 331             w = maxW;
 332         }
 333         if (h > maxH) {
 334             h = maxH;
 335         }
 336 
 337         // Don't post ComponentMoved/Resized and Paint events
 338         // until we've got a notification from the delegate
 339         setBounds(x, y, w, h, op, false, false);
 340         // Get updated bounds, so we don't have to handle 'op' here manually
 341         Rectangle r = getBounds();
 342         platformWindow.setBounds(r.x, r.y, r.width, r.height);
 343     }
 344 
 345     @Override
 346     public Point getLocationOnScreen() {
 347         return platformWindow.getLocationOnScreen();
 348     }
 349 
 350     /**
 351      * Overridden from LWContainerPeer to return the correct insets.
 352      * Insets are queried from the delegate and are kept up to date by
 353      * requiering when needed (i.e. when the window geometry is changed).
 354      */
 355     @Override
 356     public Insets getInsets() {
 357         synchronized (getStateLock()) {
 358             return insets;
 359         }
 360     }
 361 
 362     @Override
 363     public FontMetrics getFontMetrics(Font f) {
 364         // TODO: check for "use platform metrics" settings
 365         return platformWindow.getFontMetrics(f);
 366     }
 367 
 368     @Override
 369     public void toFront() {
 370         platformWindow.toFront();
 371     }
 372 
 373     @Override
 374     public void toBack() {
 375         platformWindow.toBack();
 376     }
 377 
 378     @Override
 379     public void setZOrder(ComponentPeer above) {
 380         throw new RuntimeException("not implemented");
 381     }
 382 
 383     @Override
 384     public void updateAlwaysOnTopState() {
 385         platformWindow.setAlwaysOnTop(getTarget().isAlwaysOnTop());
 386     }
 387 
 388     @Override
 389     public void updateFocusableWindowState() {
 390         platformWindow.updateFocusableWindowState();
 391     }
 392 
 393     @Override
 394     public void setModalBlocked(Dialog blocker, boolean blocked) {
 395         synchronized (getPeerTreeLock()) {
 396             ComponentPeer peer =  AWTAccessor.getComponentAccessor().getPeer(blocker);
 397             if (blocked && (peer instanceof LWWindowPeer)) {
 398                 this.blocker = (LWWindowPeer) peer;
 399             } else {
 400                 this.blocker = null;
 401             }
 402         }
 403 
 404         platformWindow.setModalBlocked(blocked);
 405     }
 406 
 407     @Override
 408     public void updateMinimumSize() {
 409         final Dimension min;
 410         if (getTarget().isMinimumSizeSet()) {
 411             min = getTarget().getMinimumSize();
 412             min.width = Math.max(min.width, MINIMUM_WIDTH);
 413             min.height = Math.max(min.height, MINIMUM_HEIGHT);
 414         } else {
 415             min = new Dimension(MINIMUM_WIDTH, MINIMUM_HEIGHT);
 416         }
 417 
 418         final Dimension max;
 419         if (getTarget().isMaximumSizeSet()) {
 420             max = getTarget().getMaximumSize();
 421             max.width = Math.min(max.width, getLWGC().getMaxTextureWidth());
 422             max.height = Math.min(max.height, getLWGC().getMaxTextureHeight());
 423         } else {
 424             max = new Dimension(getLWGC().getMaxTextureWidth(),
 425                                 getLWGC().getMaxTextureHeight());
 426         }
 427 
 428         platformWindow.setSizeConstraints(min.width, min.height, max.width, max.height);
 429     }
 430 
 431     @Override
 432     public void updateIconImages() {
 433         getPlatformWindow().updateIconImages();
 434     }
 435 
 436     @Override
 437     public void setOpacity(float opacity) {
 438         getPlatformWindow().setOpacity(opacity);
 439         repaintPeer();
 440     }
 441 
 442     @Override
 443     public final void setOpaque(final boolean isOpaque) {
 444         if (this.isOpaque != isOpaque) {
 445             this.isOpaque = isOpaque;
 446             updateOpaque();
 447         }
 448     }
 449 
 450     private void updateOpaque() {
 451         getPlatformWindow().setOpaque(!isTranslucent());
 452         replaceSurfaceData(false);
 453         repaintPeer();
 454     }
 455 
 456     @Override
 457     public void updateWindow() {
 458     }
 459 
 460     public final boolean isTextured() {
 461         return textured;
 462     }
 463 
 464     public final void setTextured(final boolean isTextured) {
 465         textured = isTextured;
 466     }
 467 
 468     @Override
 469     public final boolean isTranslucent() {
 470         synchronized (getStateLock()) {
 471             /*
 472              * Textured window is a special case of translucent window.
 473              * The difference is only in nswindow background. So when we set
 474              * texture property our peer became fully translucent. It doesn't
 475              * fill background, create non opaque backbuffers and layer etc.
 476              */
 477             return !isOpaque || isShaped() || isTextured();
 478         }
 479     }
 480 
 481     @Override
 482     final void applyShapeImpl(final Region shape) {
 483         super.applyShapeImpl(shape);
 484         updateOpaque();
 485     }
 486 
 487     @Override
 488     public void repositionSecurityWarning() {
 489         if (warningWindow != null) {
 490             AWTAccessor.ComponentAccessor compAccessor = AWTAccessor.getComponentAccessor();
 491             Window target = getTarget();
 492             int x = compAccessor.getX(target);
 493             int y = compAccessor.getY(target);
 494             int width = compAccessor.getWidth(target);
 495             int height = compAccessor.getHeight(target);
 496             warningWindow.reposition(x, y, width, height);
 497         }
 498     }
 499 
 500     // ---- FRAME PEER METHODS ---- //
 501 
 502     @Override // FramePeer and DialogPeer
 503     public void setTitle(String title) {
 504         platformWindow.setTitle(title == null ? "" : title);
 505     }
 506 
 507     @Override
 508     public void setMenuBar(MenuBar mb) {
 509          platformWindow.setMenuBar(mb);
 510     }
 511 
 512     @Override // FramePeer and DialogPeer
 513     public void setResizable(boolean resizable) {
 514         platformWindow.setResizable(resizable);
 515     }
 516 
 517     @Override
 518     public void setState(int state) {
 519         platformWindow.setWindowState(state);
 520     }
 521 
 522     @Override
 523     public int getState() {
 524         return windowState;
 525     }
 526 
 527     @Override
 528     public void setMaximizedBounds(Rectangle bounds) {
 529         // TODO: not implemented
 530     }
 531 
 532     @Override
 533     public void setBoundsPrivate(int x, int y, int width, int height) {
 534         setBounds(x, y, width, height, SET_BOUNDS | NO_EMBEDDED_CHECK);
 535     }
 536 
 537     @Override
 538     public Rectangle getBoundsPrivate() {
 539         throw new RuntimeException("not implemented");
 540     }
 541 
 542     // ---- DIALOG PEER METHODS ---- //
 543 
 544     @Override
 545     public void blockWindows(List<Window> windows) {
 546         //TODO: LWX will probably need some collectJavaToplevels to speed this up
 547         for (Window w : windows) {
 548             WindowPeer wp =
 549                     (WindowPeer) AWTAccessor.getComponentAccessor().getPeer(w);
 550             if (wp != null) {
 551                 wp.setModalBlocked((Dialog)getTarget(), true);
 552             }
 553         }
 554     }
 555 
 556     // ---- PEER NOTIFICATIONS ---- //
 557 
 558     @Override
 559     public void notifyIconify(boolean iconify) {
 560         //The toplevel target is Frame and states are applicable to it.
 561         //Otherwise, the target is Window and it don't have state property.
 562         //Hopefully, no such events are posted in the queue so consider the
 563         //target as Frame in all cases.
 564 
 565         // REMIND: should we send it anyway if the state not changed since last
 566         // time?
 567         WindowEvent iconifyEvent = new WindowEvent(getTarget(),
 568                 iconify ? WindowEvent.WINDOW_ICONIFIED
 569                         : WindowEvent.WINDOW_DEICONIFIED);
 570         postEvent(iconifyEvent);
 571 
 572         int newWindowState = iconify ? Frame.ICONIFIED : Frame.NORMAL;
 573         postWindowStateChangedEvent(newWindowState);
 574 
 575         // REMIND: RepaintManager doesn't repaint iconified windows and
 576         // hence ignores any repaint request during deiconification.
 577         // So, we need to repaint window explicitly when it becomes normal.
 578         if (!iconify) {
 579             repaintPeer();
 580         }
 581     }
 582 
 583     @Override
 584     public void notifyZoom(boolean isZoomed) {
 585         int newWindowState = isZoomed ? Frame.MAXIMIZED_BOTH : Frame.NORMAL;
 586         postWindowStateChangedEvent(newWindowState);
 587     }
 588 
 589     /**
 590      * Called by the {@code PlatformWindow} when any part of the window should
 591      * be repainted.
 592      */
 593     @Override
 594     public void notifyExpose(final Rectangle r) {
 595         repaintPeer(r);
 596     }
 597 
 598     /**
 599      * Called by the {@code PlatformWindow} when this window is moved/resized by
 600      * user or window insets are changed. There's no notifyReshape() in
 601      * LWComponentPeer as the only components which could be resized by user are
 602      * top-level windows.
 603      */
 604     @Override
 605     public void notifyReshape(int x, int y, int w, int h) {
 606         final boolean moved;
 607         final boolean resized;
 608         final boolean invalid = updateInsets(platformWindow.getInsets());
 609         synchronized (getStateLock()) {
 610             moved = (x != sysX) || (y != sysY);
 611             resized = (w != sysW) || (h != sysH);
 612             sysX = x;
 613             sysY = y;
 614             sysW = w;
 615             sysH = h;
 616         }
 617 
 618         // Check if anything changed
 619         if (!moved && !resized && !invalid) {
 620             return;
 621         }
 622         // First, update peer's bounds
 623         setBounds(x, y, w, h, SET_BOUNDS, false, false);
 624 
 625         // Second, update the graphics config and surface data
 626         final boolean isNewDevice = updateGraphicsDevice();
 627         if (resized || isNewDevice) {
 628             replaceSurfaceData();
 629         }
 630 
 631         // Third, COMPONENT_MOVED/COMPONENT_RESIZED/PAINT events
 632         if (moved || invalid) {
 633             handleMove(x, y, true);
 634         }
 635         if (resized || invalid || isNewDevice) {
 636             handleResize(w, h, true);
 637             repaintPeer();
 638         }
 639 
 640         repositionSecurityWarning();
 641     }
 642 
 643     private void clearBackground(final int w, final int h) {
 644         final Graphics g = getOnscreenGraphics(getForeground(), getBackground(),
 645                                                getFont());
 646         if (g != null) {
 647             try {
 648                 if (g instanceof Graphics2D) {
 649                     ((Graphics2D) g).setComposite(AlphaComposite.Src);
 650                 }
 651                 if (isTranslucent()) {
 652                     g.setColor(nonOpaqueBackground);
 653                     g.fillRect(0, 0, w, h);
 654                 }
 655                 if (!isTextured()) {
 656                     if (g instanceof SunGraphics2D) {
 657                         ((SunGraphics2D) g).constrain(0, 0, w, h, getRegion());
 658                     }
 659                     g.setColor(getBackground());
 660                     g.fillRect(0, 0, w, h);
 661                 }
 662             } finally {
 663                 g.dispose();
 664             }
 665         }
 666     }
 667 
 668     @Override
 669     public void notifyUpdateCursor() {
 670         getLWToolkit().getCursorManager().updateCursorLater(this);
 671     }
 672 
 673     @Override
 674     public void notifyActivation(boolean activation, LWWindowPeer opposite) {
 675         Window oppositeWindow = (opposite == null)? null : opposite.getTarget();
 676         changeFocusedWindow(activation, oppositeWindow);
 677     }
 678 
 679     // MouseDown in non-client area
 680     @Override
 681     public void notifyNCMouseDown() {
 682         // Ungrab except for a click on a Dialog with the grabbing owner
 683         if (grabbingWindow != null &&
 684             grabbingWindow != getOwnerFrameDialog(this))
 685         {
 686             grabbingWindow.ungrab();
 687         }
 688     }
 689 
 690     // ---- EVENTS ---- //
 691 
 692     /*
 693      * Called by the delegate to dispatch the event to Java. Event
 694      * coordinates are relative to non-client window are, i.e. the top-left
 695      * point of the client area is (insets.top, insets.left).
 696      */
 697     @Override
 698     public void notifyMouseEvent(int id, long when, int button,
 699                                  int x, int y, int screenX, int screenY,
 700                                  int modifiers, int clickCount, boolean popupTrigger,
 701                                  byte[] bdata)
 702     {
 703         // TODO: fill "bdata" member of AWTEvent
 704         Rectangle r = getBounds();
 705         // findPeerAt() expects parent coordinates
 706         LWComponentPeer<?, ?> targetPeer = findPeerAt(r.x + x, r.y + y);
 707 
 708         if (id == MouseEvent.MOUSE_EXITED) {
 709             isMouseOver = false;
 710             if (lastMouseEventPeer != null) {
 711                 if (lastMouseEventPeer.isEnabled()) {
 712                     Point lp = lastMouseEventPeer.windowToLocal(x, y,
 713                             this);
 714                     Component target = lastMouseEventPeer.getTarget();
 715                     postMouseExitedEvent(target, when, modifiers, lp,
 716                             screenX, screenY, clickCount, popupTrigger, button);
 717                 }
 718 
 719                 // Sometimes we may get MOUSE_EXITED after lastCommonMouseEventPeer is switched
 720                 // to a peer from another window. So we must first check if this peer is
 721                 // the same as lastWindowPeer
 722                 if (lastCommonMouseEventPeer != null && lastCommonMouseEventPeer.getWindowPeerOrSelf() == this) {
 723                     lastCommonMouseEventPeer = null;
 724                 }
 725                 lastMouseEventPeer = null;
 726             }
 727         } else if(id == MouseEvent.MOUSE_ENTERED) {
 728             isMouseOver = true;
 729             if (targetPeer != null) {
 730                 if (targetPeer.isEnabled()) {
 731                     Point lp = targetPeer.windowToLocal(x, y, this);
 732                     Component target = targetPeer.getTarget();
 733                     postMouseEnteredEvent(target, when, modifiers, lp,
 734                             screenX, screenY, clickCount, popupTrigger, button);
 735                 }
 736                 lastCommonMouseEventPeer = targetPeer;
 737                 lastMouseEventPeer = targetPeer;
 738             }
 739         } else {
 740             PlatformWindow topmostPlatforWindow =
 741                     platformWindow.getTopmostPlatformWindowUnderMouse();
 742 
 743             LWWindowPeer topmostWindowPeer =
 744                     topmostPlatforWindow != null ? topmostPlatforWindow.getPeer() : null;
 745 
 746             // topmostWindowPeer == null condition is added for the backward
 747             // compatibility with applets. It can be removed when the
 748             // getTopmostPlatformWindowUnderMouse() method will be properly
 749             // implemented in CPlatformEmbeddedFrame class
 750             if (topmostWindowPeer == this || topmostWindowPeer == null) {
 751                 generateMouseEnterExitEventsForComponents(when, button, x, y,
 752                         screenX, screenY, modifiers, clickCount, popupTrigger,
 753                         targetPeer);
 754             } else {
 755                 LWComponentPeer<?, ?> topmostTargetPeer =
 756                         topmostWindowPeer != null ? topmostWindowPeer.findPeerAt(r.x + x, r.y + y) : null;
 757                 topmostWindowPeer.generateMouseEnterExitEventsForComponents(when, button, x, y,
 758                         screenX, screenY, modifiers, clickCount, popupTrigger,
 759                         topmostTargetPeer);
 760             }
 761 
 762             // TODO: fill "bdata" member of AWTEvent
 763 
 764             int eventButtonMask = (button > 0)? MouseEvent.getMaskForButton(button) : 0;
 765             int otherButtonsPressed = modifiers & ~eventButtonMask;
 766 
 767             // For pressed/dragged/released events OS X treats other
 768             // mouse buttons as if they were BUTTON2, so we do the same
 769             int targetIdx = (button > 3) ? MouseEvent.BUTTON2 - 1 : button - 1;
 770 
 771             // MOUSE_ENTERED/EXITED are generated for the components strictly under
 772             // mouse even when dragging. That's why we first update lastMouseEventPeer
 773             // based on initial targetPeer value and only then recalculate targetPeer
 774             // for MOUSE_DRAGGED/RELEASED events
 775             if (id == MouseEvent.MOUSE_PRESSED) {
 776 
 777                 // Ungrab only if this window is not an owned window of the grabbing one.
 778                 if (!isGrabbing() && grabbingWindow != null &&
 779                     grabbingWindow != getOwnerFrameDialog(this))
 780                 {
 781                     grabbingWindow.ungrab();
 782                 }
 783                 if (otherButtonsPressed == 0) {
 784                     mouseClickButtons = eventButtonMask;
 785                 } else {
 786                     mouseClickButtons |= eventButtonMask;
 787                 }
 788 
 789                 // The window should be focused on mouse click. If it gets activated by the native platform,
 790                 // this request will be no op. It will take effect when:
 791                 // 1. A simple not focused window is clicked.
 792                 // 2. An active but not focused owner frame/dialog is clicked.
 793                 // The mouse event then will trigger a focus request "in window" to the component, so the window
 794                 // should gain focus before.
 795                 requestWindowFocus(CausedFocusEvent.Cause.MOUSE_EVENT);
 796 
 797                 mouseDownTarget[targetIdx] = targetPeer;
 798             } else if (id == MouseEvent.MOUSE_DRAGGED) {
 799                 // Cocoa dragged event has the information about which mouse
 800                 // button is being dragged. Use it to determine the peer that
 801                 // should receive the dragged event.
 802                 targetPeer = mouseDownTarget[targetIdx];
 803                 mouseClickButtons &= ~modifiers;
 804             } else if (id == MouseEvent.MOUSE_RELEASED) {
 805                 // TODO: currently, mouse released event goes to the same component
 806                 // that received corresponding mouse pressed event. For most cases,
 807                 // it's OK, however, we need to make sure that our behavior is consistent
 808                 // with 1.6 for cases where component in question have been
 809                 // hidden/removed in between of mouse pressed/released events.
 810                 targetPeer = mouseDownTarget[targetIdx];
 811 
 812                 if ((modifiers & eventButtonMask) == 0) {
 813                     mouseDownTarget[targetIdx] = null;
 814                 }
 815 
 816                 // mouseClickButtons is updated below, after MOUSE_CLICK is sent
 817             }
 818 
 819             if (targetPeer == null) {
 820                 //TODO This can happen if this window is invisible. this is correct behavior in this case?
 821                 targetPeer = this;
 822             }
 823 
 824 
 825             Point lp = targetPeer.windowToLocal(x, y, this);
 826             if (targetPeer.isEnabled()) {
 827                 MouseEvent event = new MouseEvent(targetPeer.getTarget(), id,
 828                                                   when, modifiers, lp.x, lp.y,
 829                                                   screenX, screenY, clickCount,
 830                                                   popupTrigger, button);
 831                 postEvent(event);
 832             }
 833 
 834             if (id == MouseEvent.MOUSE_RELEASED) {
 835                 if ((mouseClickButtons & eventButtonMask) != 0
 836                     && targetPeer.isEnabled()) {
 837                     postEvent(new MouseEvent(targetPeer.getTarget(),
 838                                              MouseEvent.MOUSE_CLICKED,
 839                                              when, modifiers,
 840                                              lp.x, lp.y, screenX, screenY,
 841                                              clickCount, popupTrigger, button));
 842                 }
 843                 mouseClickButtons &= ~eventButtonMask;
 844             }
 845         }
 846         notifyUpdateCursor();
 847     }
 848 
 849     private void generateMouseEnterExitEventsForComponents(long when,
 850             int button, int x, int y, int screenX, int screenY,
 851             int modifiers, int clickCount, boolean popupTrigger,
 852             final LWComponentPeer<?, ?> targetPeer) {
 853 
 854         if (!isMouseOver || targetPeer == lastMouseEventPeer) {
 855             return;
 856         }
 857 
 858         // Generate Mouse Exit for components
 859         if (lastMouseEventPeer != null && lastMouseEventPeer.isEnabled()) {
 860             Point oldp = lastMouseEventPeer.windowToLocal(x, y, this);
 861             Component target = lastMouseEventPeer.getTarget();
 862             postMouseExitedEvent(target, when, modifiers, oldp, screenX, screenY,
 863                     clickCount, popupTrigger, button);
 864         }
 865         lastCommonMouseEventPeer = targetPeer;
 866         lastMouseEventPeer = targetPeer;
 867 
 868         // Generate Mouse Enter for components
 869         if (targetPeer != null && targetPeer.isEnabled()) {
 870             Point newp = targetPeer.windowToLocal(x, y, this);
 871             Component target = targetPeer.getTarget();
 872             postMouseEnteredEvent(target, when, modifiers, newp, screenX, screenY, clickCount, popupTrigger, button);
 873         }
 874     }
 875 
 876     private void postMouseEnteredEvent(Component target, long when, int modifiers,
 877                                        Point loc, int xAbs, int yAbs,
 878                                        int clickCount, boolean popupTrigger, int button) {
 879 
 880         updateSecurityWarningVisibility();
 881 
 882         postEvent(new MouseEvent(target,
 883                 MouseEvent.MOUSE_ENTERED,
 884                 when, modifiers,
 885                 loc.x, loc.y, xAbs, yAbs,
 886                 clickCount, popupTrigger, button));
 887     }
 888 
 889     private void postMouseExitedEvent(Component target, long when, int modifiers,
 890                                       Point loc, int xAbs, int yAbs,
 891                                       int clickCount, boolean popupTrigger, int button) {
 892 
 893         updateSecurityWarningVisibility();
 894 
 895         postEvent(new MouseEvent(target,
 896                 MouseEvent.MOUSE_EXITED,
 897                 when, modifiers,
 898                 loc.x, loc.y, xAbs, yAbs,
 899                 clickCount, popupTrigger, button));
 900     }
 901 
 902     @Override
 903     public void notifyMouseWheelEvent(long when, int x, int y, int modifiers,
 904                                       int scrollType, int scrollAmount,
 905                                       int wheelRotation, double preciseWheelRotation,
 906                                       byte[] bdata)
 907     {
 908         // TODO: could we just use the last mouse event target here?
 909         Rectangle r = getBounds();
 910         // findPeerAt() expects parent coordinates
 911         final LWComponentPeer<?, ?> targetPeer = findPeerAt(r.x + x, r.y + y);
 912         if (targetPeer == null || !targetPeer.isEnabled()) {
 913             return;
 914         }
 915 
 916         Point lp = targetPeer.windowToLocal(x, y, this);
 917         // TODO: fill "bdata" member of AWTEvent
 918         // TODO: screenX/screenY
 919         postEvent(new MouseWheelEvent(targetPeer.getTarget(),
 920                                       MouseEvent.MOUSE_WHEEL,
 921                                       when, modifiers,
 922                                       lp.x, lp.y,
 923                                       0, 0, /* screenX, Y */
 924                                       0 /* clickCount */, false /* popupTrigger */,
 925                                       scrollType, scrollAmount,
 926                                       wheelRotation, preciseWheelRotation));
 927     }
 928 
 929     /*
 930      * Called by the delegate when a key is pressed.
 931      */
 932     @Override
 933     public void notifyKeyEvent(int id, long when, int modifiers,
 934                                int keyCode, char keyChar, int keyLocation)
 935     {
 936         LWKeyboardFocusManagerPeer kfmPeer = LWKeyboardFocusManagerPeer.getInstance();
 937         Component focusOwner = kfmPeer.getCurrentFocusOwner();
 938 
 939         if (focusOwner == null) {
 940             focusOwner = kfmPeer.getCurrentFocusedWindow();
 941             if (focusOwner == null) {
 942                 focusOwner = this.getTarget();
 943             }
 944         }
 945 
 946         KeyEvent keyEvent = new KeyEvent(focusOwner, id, when, modifiers,
 947             keyCode, keyChar, keyLocation);
 948         AWTAccessor.getKeyEventAccessor().setExtendedKeyCode(keyEvent,
 949             ExtendedKeyCodes.getExtendedKeyCodeForChar(keyChar));
 950         postEvent(keyEvent);
 951     }
 952 
 953     // ---- UTILITY METHODS ---- //
 954 
 955     private void activateDisplayListener() {
 956         final GraphicsEnvironment ge =
 957                 GraphicsEnvironment.getLocalGraphicsEnvironment();
 958         ((SunGraphicsEnvironment) ge).addDisplayChangedListener(this);
 959     }
 960 
 961     private void deactivateDisplayListener() {
 962         final GraphicsEnvironment ge =
 963                 GraphicsEnvironment.getLocalGraphicsEnvironment();
 964         ((SunGraphicsEnvironment) ge).removeDisplayChangedListener(this);
 965     }
 966 
 967     private void postWindowStateChangedEvent(int newWindowState) {
 968         if (getTarget() instanceof Frame) {
 969             AWTAccessor.getFrameAccessor().setExtendedState(
 970                     (Frame)getTarget(), newWindowState);
 971         }
 972 
 973         WindowEvent stateChangedEvent = new WindowEvent(getTarget(),
 974                 WindowEvent.WINDOW_STATE_CHANGED,
 975                 windowState, newWindowState);
 976         postEvent(stateChangedEvent);
 977         windowState = newWindowState;
 978 
 979         updateSecurityWarningVisibility();
 980     }
 981 
 982     private static int getGraphicsConfigScreen(GraphicsConfiguration gc) {
 983         // TODO: this method can be implemented in a more
 984         // efficient way by forwarding to the delegate
 985         GraphicsDevice gd = gc.getDevice();
 986         GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
 987         GraphicsDevice[] gds = ge.getScreenDevices();
 988         for (int i = 0; i < gds.length; i++) {
 989             if (gds[i] == gd) {
 990                 return i;
 991             }
 992         }
 993         // Should never happen if gc is a screen device config
 994         return 0;
 995     }
 996 
 997     /*
 998      * This method is called when window's graphics config is changed from
 999      * the app code (e.g. when the window is made non-opaque) or when
1000      * the window is moved to another screen by user.
1001      *
1002      * Returns true if the graphics config has been changed, false otherwise.
1003      */
1004     private boolean setGraphicsConfig(GraphicsConfiguration gc) {
1005         synchronized (getStateLock()) {
1006             if (graphicsConfig == gc) {
1007                 return false;
1008             }
1009             // If window's graphics config is changed from the app code, the
1010             // config correspond to the same device as before; when the window
1011             // is moved by user, graphicsDevice is updated in notifyReshape().
1012             // In either case, there's nothing to do with screenOn here
1013             graphicsConfig = gc;
1014         }
1015         // SurfaceData is replaced later in updateGraphicsData()
1016         return true;
1017     }
1018 
1019     /**
1020      * Returns true if the GraphicsDevice has been changed, false otherwise.
1021      */
1022     public boolean updateGraphicsDevice() {
1023         GraphicsDevice newGraphicsDevice = platformWindow.getGraphicsDevice();
1024         synchronized (getStateLock()) {
1025             if (graphicsDevice == newGraphicsDevice) {
1026                 return false;
1027             }
1028             graphicsDevice = newGraphicsDevice;
1029         }
1030 
1031         final GraphicsConfiguration newGC = newGraphicsDevice.getDefaultConfiguration();
1032 
1033         if (!setGraphicsConfig(newGC)) return false;
1034 
1035         SunToolkit.executeOnEventHandlerThread(getTarget(), new Runnable() {
1036             public void run() {
1037                 AWTAccessor.getComponentAccessor().setGraphicsConfiguration(getTarget(), newGC);
1038             }
1039         });
1040         return true;
1041     }
1042 
1043     @Override
1044     public final void displayChanged() {
1045         updateGraphicsDevice();
1046         // Replace surface unconditionally, because internal state of the
1047         // GraphicsDevice could be changed.
1048         replaceSurfaceData();
1049         repaintPeer();
1050     }
1051 
1052     @Override
1053     public final void paletteChanged() {
1054         // components do not need to react to this event.
1055     }
1056 
1057     /*
1058      * May be called by delegate to provide SD to Java2D code.
1059      */
1060     public SurfaceData getSurfaceData() {
1061         synchronized (surfaceDataLock) {
1062             return surfaceData;
1063         }
1064     }
1065 
1066     private void replaceSurfaceData() {
1067         replaceSurfaceData(true);
1068     }
1069 
1070     private void replaceSurfaceData(final boolean blit) {
1071         synchronized (surfaceDataLock) {
1072             final SurfaceData oldData = getSurfaceData();
1073             surfaceData = platformWindow.replaceSurfaceData();
1074             final Rectangle size = getSize();
1075             if (getSurfaceData() != null && oldData != getSurfaceData()) {
1076                 clearBackground(size.width, size.height);
1077             }
1078 
1079             if (blit) {
1080                 blitSurfaceData(oldData, getSurfaceData());
1081             }
1082 
1083             if (oldData != null && oldData != getSurfaceData()) {
1084                 // TODO: drop oldData for D3D/WGL pipelines
1085                 // This can only happen when this peer is being created
1086                 oldData.flush();
1087             }
1088         }
1089         flushOnscreenGraphics();
1090     }
1091 
1092     private void blitSurfaceData(final SurfaceData src, final SurfaceData dst) {
1093         //TODO blit. proof-of-concept
1094         if (src != dst && src != null && dst != null
1095             && !(dst instanceof NullSurfaceData)
1096             && !(src instanceof NullSurfaceData)
1097             && src.getSurfaceType().equals(dst.getSurfaceType())
1098             && src.getDefaultScale() == dst.getDefaultScale()) {
1099             final Rectangle size = src.getBounds();
1100             final Blit blit = Blit.locate(src.getSurfaceType(),
1101                                           CompositeType.Src,
1102                                           dst.getSurfaceType());
1103             if (blit != null) {
1104                 blit.Blit(src, dst, AlphaComposite.Src, null, 0, 0, 0, 0,
1105                           size.width, size.height);
1106             }
1107         }
1108     }
1109 
1110     /**
1111      * Request the window insets from the delegate and compares it with the
1112      * current one. This method is mostly called by the delegate, e.g. when the
1113      * window state is changed and insets should be recalculated.
1114      * <p/>
1115      * This method may be called on the toolkit thread.
1116      */
1117     public final boolean updateInsets(final Insets newInsets) {
1118         synchronized (getStateLock()) {
1119             if (insets.equals(newInsets)) {
1120                 return false;
1121             }
1122             insets = newInsets;
1123         }
1124         return true;
1125     }
1126 
1127     public static LWWindowPeer getWindowUnderCursor() {
1128         return lastCommonMouseEventPeer != null ? lastCommonMouseEventPeer.getWindowPeerOrSelf() : null;
1129     }
1130 
1131     public static LWComponentPeer<?, ?> getPeerUnderCursor() {
1132         return lastCommonMouseEventPeer;
1133     }
1134 
1135     /*
1136      * Requests platform to set native focus on a frame/dialog.
1137      * In case of a simple window, triggers appropriate java focus change.
1138      */
1139     public boolean requestWindowFocus(CausedFocusEvent.Cause cause) {
1140         if (focusLog.isLoggable(PlatformLogger.Level.FINE)) {
1141             focusLog.fine("requesting native focus to " + this);
1142         }
1143 
1144         if (!focusAllowedFor()) {
1145             focusLog.fine("focus is not allowed");
1146             return false;
1147         }
1148 
1149         if (platformWindow.rejectFocusRequest(cause)) {
1150             return false;
1151         }
1152 
1153         Window currentActive = KeyboardFocusManager.
1154             getCurrentKeyboardFocusManager().getActiveWindow();
1155 
1156         Window opposite = LWKeyboardFocusManagerPeer.getInstance().
1157             getCurrentFocusedWindow();
1158 
1159         // Make the owner active window.
1160         if (isSimpleWindow()) {
1161             LWWindowPeer owner = getOwnerFrameDialog(this);
1162 
1163             // If owner is not natively active, request native
1164             // activation on it w/o sending events up to java.
1165             if (owner != null && !owner.platformWindow.isActive()) {
1166                 if (focusLog.isLoggable(PlatformLogger.Level.FINE)) {
1167                     focusLog.fine("requesting native focus to the owner " + owner);
1168                 }
1169                 LWWindowPeer currentActivePeer = currentActive == null ? null :
1170                 (LWWindowPeer) AWTAccessor.getComponentAccessor().getPeer(
1171                         currentActive);
1172 
1173                 // Ensure the opposite is natively active and suppress sending events.
1174                 if (currentActivePeer != null && currentActivePeer.platformWindow.isActive()) {
1175                     if (focusLog.isLoggable(PlatformLogger.Level.FINE)) {
1176                         focusLog.fine("the opposite is " + currentActivePeer);
1177                     }
1178                     currentActivePeer.skipNextFocusChange = true;
1179                 }
1180                 owner.skipNextFocusChange = true;
1181 
1182                 owner.platformWindow.requestWindowFocus();
1183             }
1184 
1185             // DKFM will synthesize all the focus/activation events correctly.
1186             changeFocusedWindow(true, opposite);
1187             return true;
1188 
1189         // In case the toplevel is active but not focused, change focus directly,
1190         // as requesting native focus on it will not have effect.
1191         } else if (getTarget() == currentActive && !getTarget().hasFocus()) {
1192 
1193             changeFocusedWindow(true, opposite);
1194             return true;
1195         }
1196 
1197         return platformWindow.requestWindowFocus();
1198     }
1199 
1200     protected boolean focusAllowedFor() {
1201         Window window = getTarget();
1202         // TODO: check if modal blocked
1203         return window.isVisible() && window.isEnabled() && isFocusableWindow();
1204     }
1205 
1206     private boolean isFocusableWindow() {
1207         boolean focusable = getTarget().isFocusableWindow();
1208         if (isSimpleWindow()) {
1209             LWWindowPeer ownerPeer = getOwnerFrameDialog(this);
1210             if (ownerPeer == null) {
1211                 return false;
1212             }
1213             return focusable && ownerPeer.getTarget().isFocusableWindow();
1214         }
1215         return focusable;
1216     }
1217 
1218     public boolean isSimpleWindow() {
1219         Window window = getTarget();
1220         return !(window instanceof Dialog || window instanceof Frame);
1221     }
1222 
1223     @Override
1224     public void emulateActivation(boolean activate) {
1225         changeFocusedWindow(activate, null);
1226     }
1227 
1228     /*
1229      * Changes focused window on java level.
1230      */
1231     protected void changeFocusedWindow(boolean becomesFocused, Window opposite) {
1232         if (focusLog.isLoggable(PlatformLogger.Level.FINE)) {
1233             focusLog.fine((becomesFocused?"gaining":"loosing") + " focus window: " + this);
1234         }
1235         if (skipNextFocusChange) {
1236             focusLog.fine("skipping focus change");
1237             skipNextFocusChange = false;
1238             return;
1239         }
1240         if (!isFocusableWindow() && becomesFocused) {
1241             focusLog.fine("the window is not focusable");
1242             return;
1243         }
1244         if (becomesFocused) {
1245             synchronized (getPeerTreeLock()) {
1246                 if (blocker != null) {
1247                     if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
1248                         focusLog.finest("the window is blocked by " + blocker);
1249                     }
1250                     return;
1251                 }
1252             }
1253         }
1254 
1255         // Note, the method is not called:
1256         // - when the opposite (gaining focus) window is an owned/owner window.
1257         // - for a simple window in any case.
1258         if (!becomesFocused &&
1259             (isGrabbing() || getOwnerFrameDialog(grabbingWindow) == this))
1260         {
1261             if (focusLog.isLoggable(PlatformLogger.Level.FINE)) {
1262                 focusLog.fine("ungrabbing on " + grabbingWindow);
1263             }
1264             // ungrab a simple window if its owner looses activation.
1265             grabbingWindow.ungrab();
1266         }
1267 
1268         KeyboardFocusManagerPeer kfmPeer = LWKeyboardFocusManagerPeer.getInstance();
1269         kfmPeer.setCurrentFocusedWindow(becomesFocused ? getTarget() : null);
1270 
1271         int eventID = becomesFocused ? WindowEvent.WINDOW_GAINED_FOCUS : WindowEvent.WINDOW_LOST_FOCUS;
1272         WindowEvent windowEvent = new TimedWindowEvent(getTarget(), eventID, opposite, System.currentTimeMillis());
1273 
1274         // TODO: wrap in SequencedEvent
1275         postEvent(windowEvent);
1276     }
1277 
1278     static LWWindowPeer getOwnerFrameDialog(LWWindowPeer peer) {
1279         Window owner = (peer != null ? peer.getTarget().getOwner() : null);
1280         while (owner != null && !(owner instanceof Frame || owner instanceof Dialog)) {
1281             owner = owner.getOwner();
1282         }
1283         return owner == null ? null :
1284                (LWWindowPeer) AWTAccessor.getComponentAccessor().getPeer(owner);
1285     }
1286 
1287     /**
1288      * Returns the foremost modal blocker of this window, or null.
1289      */
1290     public LWWindowPeer getBlocker() {
1291         synchronized (getPeerTreeLock()) {
1292             LWWindowPeer blocker = this.blocker;
1293             if (blocker == null) {
1294                 return null;
1295             }
1296             while (blocker.blocker != null) {
1297                 blocker = blocker.blocker;
1298             }
1299             return blocker;
1300         }
1301     }
1302 
1303     @Override
1304     public void enterFullScreenMode() {
1305         platformWindow.enterFullScreenMode();
1306         updateSecurityWarningVisibility();
1307     }
1308 
1309     @Override
1310     public void exitFullScreenMode() {
1311         platformWindow.exitFullScreenMode();
1312         updateSecurityWarningVisibility();
1313     }
1314 
1315     public long getLayerPtr() {
1316         return getPlatformWindow().getLayerPtr();
1317     }
1318 
1319     void grab() {
1320         if (grabbingWindow != null && !isGrabbing()) {
1321             grabbingWindow.ungrab();
1322         }
1323         grabbingWindow = this;
1324     }
1325 
1326     final void ungrab(boolean doPost) {
1327         if (isGrabbing()) {
1328             grabbingWindow = null;
1329             if (doPost) {
1330                 postEvent(new UngrabEvent(getTarget()));
1331             }
1332         }
1333     }
1334 
1335     void ungrab() {
1336         ungrab(true);
1337     }
1338 
1339     private boolean isGrabbing() {
1340         return this == grabbingWindow;
1341     }
1342 
1343     public PeerType getPeerType() {
1344         return peerType;
1345     }
1346 
1347     public void updateSecurityWarningVisibility() {
1348         if (warningWindow == null) {
1349             return;
1350         }
1351 
1352         if (!isVisible()) {
1353             return; // The warning window should already be hidden.
1354         }
1355 
1356         boolean show = false;
1357 
1358         if (!platformWindow.isFullScreenMode()) {
1359             if (isVisible()) {
1360                 if (LWKeyboardFocusManagerPeer.getInstance().getCurrentFocusedWindow() ==
1361                         getTarget()) {
1362                     show = true;
1363                 }
1364 
1365                 if (platformWindow.isUnderMouse() || warningWindow.isUnderMouse()) {
1366                     show = true;
1367                 }
1368             }
1369         }
1370 
1371         warningWindow.setVisible(show, true);
1372     }
1373 
1374     @Override
1375     public String toString() {
1376         return super.toString() + " [target is " + getTarget() + "]";
1377     }
1378 }