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