1 /*
   2  * Copyright (c) 1996, 2015, 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 package sun.awt.windows;
  26 
  27 import java.awt.*;
  28 import java.awt.event.*;
  29 import java.awt.image.*;
  30 import java.awt.peer.*;
  31 
  32 import java.beans.*;
  33 
  34 import java.util.*;
  35 import java.util.List;
  36 import sun.util.logging.PlatformLogger;
  37 
  38 import sun.awt.*;
  39 
  40 import sun.java2d.pipe.Region;
  41 
  42 public class WWindowPeer extends WPanelPeer implements WindowPeer,
  43        DisplayChangedListener
  44 {
  45 
  46     private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.windows.WWindowPeer");
  47     private static final PlatformLogger screenLog = PlatformLogger.getLogger("sun.awt.windows.screen.WWindowPeer");
  48 
  49     // we can't use WDialogPeer as blocker may be an instance of WPrintDialogPeer that
  50     // extends WWindowPeer, not WDialogPeer
  51     private WWindowPeer modalBlocker = null;
  52 
  53     private boolean isOpaque;
  54 
  55     private TranslucentWindowPainter painter;
  56 
  57     /*
  58      * A key used for storing a list of active windows in AppContext. The value
  59      * is a list of windows, sorted by the time of activation: later a window is
  60      * activated, greater its index is in the list.
  61      */
  62     private static final StringBuffer ACTIVE_WINDOWS_KEY =
  63         new StringBuffer("active_windows_list");
  64 
  65     /*
  66      * Listener for 'activeWindow' KFM property changes. It is added to each
  67      * AppContext KFM. See ActiveWindowListener inner class below.
  68      */
  69     private static PropertyChangeListener activeWindowListener =
  70         new ActiveWindowListener();
  71 
  72     /*
  73      * The object is a listener for the AppContext.GUI_DISPOSED property.
  74      */
  75     private static final PropertyChangeListener guiDisposedListener =
  76         new GuiDisposedListener();
  77 
  78     /*
  79      * Called (on the Toolkit thread) before the appropriate
  80      * WindowStateEvent is posted to the EventQueue.
  81      */
  82     private WindowListener windowListener;
  83 
  84     /**
  85      * Initialize JNI field IDs
  86      */
  87     private static native void initIDs();
  88     static {
  89         initIDs();
  90     }
  91 
  92     // WComponentPeer overrides
  93     @Override
  94     @SuppressWarnings("unchecked")
  95     protected void disposeImpl() {
  96         AppContext appContext = SunToolkit.targetToAppContext(target);
  97         synchronized (appContext) {
  98             List<WWindowPeer> l = (List<WWindowPeer>)appContext.get(ACTIVE_WINDOWS_KEY);
  99             if (l != null) {
 100                 l.remove(this);
 101             }
 102         }
 103 
 104         // Remove ourself from the Map of DisplayChangeListeners
 105         GraphicsConfiguration gc = getGraphicsConfiguration();
 106         ((Win32GraphicsDevice)gc.getDevice()).removeDisplayChangedListener(this);
 107 
 108         synchronized (getStateLock()) {
 109             TranslucentWindowPainter currentPainter = painter;
 110             if (currentPainter != null) {
 111                 currentPainter.flush();
 112                 // don't set the current one to null here; reduces the chances of
 113                 // MT issues (like NPEs)
 114             }
 115         }
 116 
 117         super.disposeImpl();
 118     }
 119 
 120     // WindowPeer implementation
 121 
 122     @Override
 123     public void toFront() {
 124         updateFocusableWindowState();
 125         _toFront();
 126     }
 127     private native void _toFront();
 128 
 129     @Override
 130     public native void toBack();
 131 
 132     private native void setAlwaysOnTopNative(boolean value);
 133 
 134     public void setAlwaysOnTop(boolean value) {
 135         if ((value && ((Window)target).isVisible()) || !value) {
 136             setAlwaysOnTopNative(value);
 137         }
 138     }
 139 
 140     @Override
 141     public void updateAlwaysOnTopState() {
 142         setAlwaysOnTop(((Window)target).isAlwaysOnTop());
 143     }
 144 
 145     @Override
 146     public void updateFocusableWindowState() {
 147         setFocusableWindow(((Window)target).isFocusableWindow());
 148     }
 149     native void setFocusableWindow(boolean value);
 150 
 151     // FramePeer & DialogPeer partial shared implementation
 152 
 153     public void setTitle(String title) {
 154         // allow a null title to pass as an empty string.
 155         if (title == null) {
 156             title = "";
 157         }
 158         _setTitle(title);
 159     }
 160     private native void _setTitle(String title);
 161 
 162     public void setResizable(boolean resizable) {
 163         _setResizable(resizable);
 164     }
 165 
 166     private native void _setResizable(boolean resizable);
 167 
 168     // Toolkit & peer internals
 169 
 170     WWindowPeer(Window target) {
 171         super(target);
 172     }
 173 
 174     @Override
 175     void initialize() {
 176         super.initialize();
 177 
 178         updateInsets(insets_);
 179 
 180         Font f = ((Window)target).getFont();
 181         if (f == null) {
 182             f = defaultFont;
 183             ((Window)target).setFont(f);
 184             setFont(f);
 185         }
 186         // Express our interest in display changes
 187         GraphicsConfiguration gc = getGraphicsConfiguration();
 188         ((Win32GraphicsDevice)gc.getDevice()).addDisplayChangedListener(this);
 189 
 190         initActiveWindowsTracking((Window)target);
 191 
 192         updateIconImages();
 193 
 194         Shape shape = ((Window)target).getShape();
 195         if (shape != null) {
 196             applyShape(Region.getInstance(shape, null));
 197         }
 198 
 199         float opacity = ((Window)target).getOpacity();
 200         if (opacity < 1.0f) {
 201             setOpacity(opacity);
 202         }
 203 
 204         synchronized (getStateLock()) {
 205             // default value of a boolean field is 'false', so set isOpaque to
 206             // true here explicitly
 207             this.isOpaque = true;
 208             setOpaque(((Window)target).isOpaque());
 209         }
 210     }
 211 
 212     native void createAwtWindow(WComponentPeer parent);
 213 
 214     private volatile Window.Type windowType = Window.Type.NORMAL;
 215 
 216     // This method must be called for Window, Dialog, and Frame before creating
 217     // the hwnd
 218     void preCreate(WComponentPeer parent) {
 219         windowType = ((Window)target).getType();
 220     }
 221 
 222     @Override
 223     void create(WComponentPeer parent) {
 224         preCreate(parent);
 225         createAwtWindow(parent);
 226     }
 227 
 228     @Override
 229     final WComponentPeer getNativeParent() {
 230         final Container owner = ((Window) target).getOwner();
 231         return (WComponentPeer) WToolkit.targetToPeer(owner);
 232     }
 233 
 234     // should be overriden in WDialogPeer
 235     protected void realShow() {
 236         super.show();
 237     }
 238 
 239     @Override
 240     public void show() {
 241         updateFocusableWindowState();
 242 
 243         boolean alwaysOnTop = ((Window)target).isAlwaysOnTop();
 244 
 245         // Fix for 4868278.
 246         // If we create a window with a specific GraphicsConfig, and then move it with
 247         // setLocation() or setBounds() to another one before its peer has been created,
 248         // then calling Window.getGraphicsConfig() returns wrong config. That may lead
 249         // to some problems like wrong-placed tooltips. It is caused by calling
 250         // super.displayChanged() in WWindowPeer.displayChanged() regardless of whether
 251         // GraphicsDevice was really changed, or not. So we need to track it here.
 252         updateGC();
 253 
 254         realShow();
 255         updateMinimumSize();
 256 
 257         if (((Window)target).isAlwaysOnTopSupported() && alwaysOnTop) {
 258             setAlwaysOnTop(alwaysOnTop);
 259         }
 260 
 261         synchronized (getStateLock()) {
 262             if (!isOpaque) {
 263                 updateWindow(true);
 264             }
 265         }
 266 
 267         // See https://javafx-jira.kenai.com/browse/RT-32570
 268         WComponentPeer owner = getNativeParent();
 269         if (owner != null && owner.isLightweightFramePeer()) {
 270             Rectangle b = getBounds();
 271             handleExpose(0, 0, b.width, b.height);
 272         }
 273     }
 274 
 275     // Synchronize the insets members (here & in helper) with actual window
 276     // state.
 277     native void updateInsets(Insets i);
 278 
 279     static native int getSysMinWidth();
 280     static native int getSysMinHeight();
 281     static native int getSysIconWidth();
 282     static native int getSysIconHeight();
 283     static native int getSysSmIconWidth();
 284     static native int getSysSmIconHeight();
 285     /**windows/classes/sun/awt/windows/
 286      * Creates native icon from specified raster data and updates
 287      * icon for window and all descendant windows that inherit icon.
 288      * Raster data should be passed in the ARGB form.
 289      * Note that raster data format was changed to provide support
 290      * for XP icons with alpha-channel
 291      */
 292     native void setIconImagesData(int[] iconRaster, int w, int h,
 293                                   int[] smallIconRaster, int smw, int smh);
 294 
 295     synchronized native void reshapeFrame(int x, int y, int width, int height);
 296 
 297     native Dimension getNativeWindowSize();
 298 
 299     public Dimension getScaledWindowSize() {
 300         return getNativeWindowSize();
 301     }
 302 
 303     public boolean requestWindowFocus(FocusEvent.Cause cause) {
 304         if (!focusAllowedFor()) {
 305             return false;
 306         }
 307         return requestWindowFocus(cause == FocusEvent.Cause.MOUSE_EVENT);
 308     }
 309     private native boolean requestWindowFocus(boolean isMouseEventCause);
 310 
 311     public boolean focusAllowedFor() {
 312         Window window = (Window)this.target;
 313         if (!window.isVisible() ||
 314             !window.isEnabled() ||
 315             !window.isFocusableWindow())
 316         {
 317             return false;
 318         }
 319         if (isModalBlocked()) {
 320             return false;
 321         }
 322         return true;
 323     }
 324 
 325     @Override
 326     void hide() {
 327         WindowListener listener = windowListener;
 328         if (listener != null) {
 329             // We're not getting WINDOW_CLOSING from the native code when hiding
 330             // the window programmatically. So, create it and notify the listener.
 331             listener.windowClosing(new WindowEvent((Window)target, WindowEvent.WINDOW_CLOSING));
 332         }
 333         super.hide();
 334     }
 335 
 336     // WARNING: it's called on the Toolkit thread!
 337     @Override
 338     void preprocessPostEvent(AWTEvent event) {
 339         if (event instanceof WindowEvent) {
 340             WindowListener listener = windowListener;
 341             if (listener != null) {
 342                 switch(event.getID()) {
 343                     case WindowEvent.WINDOW_CLOSING:
 344                         listener.windowClosing((WindowEvent)event);
 345                         break;
 346                     case WindowEvent.WINDOW_ICONIFIED:
 347                         listener.windowIconified((WindowEvent)event);
 348                         break;
 349                 }
 350             }
 351         }
 352     }
 353 
 354     synchronized void addWindowListener(WindowListener l) {
 355         windowListener = AWTEventMulticaster.add(windowListener, l);
 356     }
 357 
 358     synchronized void removeWindowListener(WindowListener l) {
 359         windowListener = AWTEventMulticaster.remove(windowListener, l);
 360     }
 361 
 362     @Override
 363     public void updateMinimumSize() {
 364         Dimension minimumSize = null;
 365         if (((Component)target).isMinimumSizeSet()) {
 366             minimumSize = ((Component)target).getMinimumSize();
 367         }
 368         if (minimumSize != null) {
 369             int msw = getSysMinWidth();
 370             int msh = getSysMinHeight();
 371             int w = (minimumSize.width >= msw) ? minimumSize.width : msw;
 372             int h = (minimumSize.height >= msh) ? minimumSize.height : msh;
 373             setMinSize(w, h);
 374         } else {
 375             setMinSize(0, 0);
 376         }
 377     }
 378 
 379     @Override
 380     public void updateIconImages() {
 381         java.util.List<Image> imageList = ((Window)target).getIconImages();
 382         if (imageList == null || imageList.size() == 0) {
 383             setIconImagesData(null, 0, 0, null, 0, 0);
 384         } else {
 385             int w = getSysIconWidth();
 386             int h = getSysIconHeight();
 387             int smw = getSysSmIconWidth();
 388             int smh = getSysSmIconHeight();
 389             DataBufferInt iconData = SunToolkit.getScaledIconData(imageList,
 390                                                                   w, h);
 391             DataBufferInt iconSmData = SunToolkit.getScaledIconData(imageList,
 392                                                                     smw, smh);
 393             if (iconData != null && iconSmData != null) {
 394                 setIconImagesData(iconData.getData(), w, h,
 395                                   iconSmData.getData(), smw, smh);
 396             } else {
 397                 setIconImagesData(null, 0, 0, null, 0, 0);
 398             }
 399         }
 400     }
 401 
 402     native void setMinSize(int width, int height);
 403 
 404 /*
 405  * ---- MODALITY SUPPORT ----
 406  */
 407 
 408     /**
 409      * Some modality-related code here because WFileDialogPeer, WPrintDialogPeer and
 410      *   WPageDialogPeer are descendants of WWindowPeer, not WDialogPeer
 411      */
 412 
 413     public boolean isModalBlocked() {
 414         return modalBlocker != null;
 415     }
 416 
 417      @Override
 418     public void setModalBlocked(Dialog dialog, boolean blocked) {
 419         synchronized (((Component)getTarget()).getTreeLock()) // State lock should always be after awtLock
 420         {
 421             // use WWindowPeer instead of WDialogPeer because of FileDialogs and PrintDialogs
 422             WWindowPeer blockerPeer = AWTAccessor.getComponentAccessor()
 423                                                  .getPeer(dialog);
 424             if (blocked)
 425             {
 426                 modalBlocker = blockerPeer;
 427                 // handle native dialogs separately, as they may have not
 428                 // got HWND yet; modalEnable/modalDisable is called from
 429                 // their setHWnd() methods
 430                 if (blockerPeer instanceof WFileDialogPeer) {
 431                     ((WFileDialogPeer)blockerPeer).blockWindow(this);
 432                 } else if (blockerPeer instanceof WPrintDialogPeer) {
 433                     ((WPrintDialogPeer)blockerPeer).blockWindow(this);
 434                 } else {
 435                     modalDisable(dialog, blockerPeer.getHWnd());
 436                 }
 437             } else {
 438                 modalBlocker = null;
 439                 if (blockerPeer instanceof WFileDialogPeer) {
 440                     ((WFileDialogPeer)blockerPeer).unblockWindow(this);
 441                 } else if (blockerPeer instanceof WPrintDialogPeer) {
 442                     ((WPrintDialogPeer)blockerPeer).unblockWindow(this);
 443                 } else {
 444                     modalEnable(dialog);
 445                 }
 446             }
 447         }
 448     }
 449 
 450     native void modalDisable(Dialog blocker, long blockerHWnd);
 451     native void modalEnable(Dialog blocker);
 452 
 453     /*
 454      * Returns all the ever active windows from the current AppContext.
 455      * The list is sorted by the time of activation, so the latest
 456      * active window is always at the end.
 457      */
 458     @SuppressWarnings("unchecked")
 459     public static long[] getActiveWindowHandles(Component target) {
 460         AppContext appContext = SunToolkit.targetToAppContext(target);
 461         if (appContext == null) return null;
 462         synchronized (appContext) {
 463             List<WWindowPeer> l = (List<WWindowPeer>)appContext.get(ACTIVE_WINDOWS_KEY);
 464             if (l == null) {
 465                 return null;
 466             }
 467             long[] result = new long[l.size()];
 468             for (int j = 0; j < l.size(); j++) {
 469                 result[j] = l.get(j).getHWnd();
 470             }
 471             return result;
 472         }
 473     }
 474 
 475 /*
 476  * ----DISPLAY CHANGE SUPPORT----
 477  */
 478 
 479     /*
 480      * Called from native code when we have been dragged onto another screen.
 481      */
 482     void draggedToNewScreen() {
 483         SunToolkit.executeOnEventHandlerThread((Component)target,new Runnable()
 484         {
 485             @Override
 486             public void run() {
 487                 displayChanged();
 488             }
 489         });
 490     }
 491 
 492     public void updateGC() {
 493         int scrn = getScreenImOn();
 494         if (screenLog.isLoggable(PlatformLogger.Level.FINER)) {
 495             log.finer("Screen number: " + scrn);
 496         }
 497 
 498         // get current GD
 499         Win32GraphicsDevice oldDev = winGraphicsConfig.getDevice();
 500 
 501         Win32GraphicsDevice newDev;
 502         GraphicsDevice devs[] = GraphicsEnvironment
 503             .getLocalGraphicsEnvironment()
 504             .getScreenDevices();
 505         // Occasionally during device addition/removal getScreenImOn can return
 506         // a non-existing screen number. Use the default device in this case.
 507         if (scrn >= devs.length) {
 508             newDev = (Win32GraphicsDevice)GraphicsEnvironment
 509                 .getLocalGraphicsEnvironment().getDefaultScreenDevice();
 510         } else {
 511             newDev = (Win32GraphicsDevice)devs[scrn];
 512         }
 513 
 514         // Set winGraphicsConfig to the default GC for the monitor this Window
 515         // is now mostly on.
 516         winGraphicsConfig = (Win32GraphicsConfig)newDev
 517                             .getDefaultConfiguration();
 518         if (screenLog.isLoggable(PlatformLogger.Level.FINE)) {
 519             if (winGraphicsConfig == null) {
 520                 screenLog.fine("Assertion (winGraphicsConfig != null) failed");
 521             }
 522         }
 523 
 524         // if on a different display, take off old GD and put on new GD
 525         if (oldDev != newDev) {
 526             oldDev.removeDisplayChangedListener(this);
 527             newDev.addDisplayChangedListener(this);
 528         }
 529 
 530         AWTAccessor.getComponentAccessor().
 531             setGraphicsConfiguration((Component)target, winGraphicsConfig);
 532     }
 533 
 534     /**
 535      * From the DisplayChangedListener interface.
 536      *
 537      * This method handles a display change - either when the display settings
 538      * are changed, or when the window has been dragged onto a different
 539      * display.
 540      * Called after a change in the display mode.  This event
 541      * triggers replacing the surfaceData object (since that object
 542      * reflects the current display depth information, which has
 543      * just changed).
 544      */
 545     @Override
 546     public void displayChanged() {
 547         updateGC();
 548     }
 549 
 550     /**
 551      * Part of the DisplayChangedListener interface: components
 552      * do not need to react to this event
 553      */
 554     @Override
 555     public void paletteChanged() {
 556     }
 557 
 558     private native int getScreenImOn();
 559 
 560     // Used in Win32GraphicsDevice.
 561     public final native void setFullScreenExclusiveModeState(boolean state);
 562 
 563 /*
 564  * ----END DISPLAY CHANGE SUPPORT----
 565  */
 566 
 567      public void grab() {
 568          nativeGrab();
 569      }
 570 
 571      public void ungrab() {
 572          nativeUngrab();
 573      }
 574      private native void nativeGrab();
 575      private native void nativeUngrab();
 576 
 577      private final boolean hasWarningWindow() {
 578          return ((Window)target).getWarningString() != null;
 579      }
 580 
 581      boolean isTargetUndecorated() {
 582          return true;
 583      }
 584 
 585      // These are the peer bounds. They get updated at:
 586      //    1. the WWindowPeer.setBounds() method.
 587      //    2. the native code (on WM_SIZE/WM_MOVE)
 588      private volatile int sysX = 0;
 589      private volatile int sysY = 0;
 590      private volatile int sysW = 0;
 591      private volatile int sysH = 0;
 592 
 593      @Override
 594      public native void repositionSecurityWarning();
 595 
 596      @Override
 597      public void setBounds(int x, int y, int width, int height, int op) {
 598          sysX = x;
 599          sysY = y;
 600          sysW = width;
 601          sysH = height;
 602 
 603          super.setBounds(x, y, width, height, op);
 604      }
 605 
 606     @Override
 607     public void print(Graphics g) {
 608         // We assume we print the whole frame,
 609         // so we expect no clip was set previously
 610         Shape shape = AWTAccessor.getWindowAccessor().getShape((Window)target);
 611         if (shape != null) {
 612             g.setClip(shape);
 613         }
 614         super.print(g);
 615     }
 616 
 617     private void replaceSurfaceDataRecursively(Component c) {
 618         if (c instanceof Container) {
 619             for (Component child : ((Container)c).getComponents()) {
 620                 replaceSurfaceDataRecursively(child);
 621             }
 622         }
 623         final Object cp = AWTAccessor.getComponentAccessor().getPeer(c);
 624         if (cp instanceof WComponentPeer) {
 625             ((WComponentPeer)cp).replaceSurfaceDataLater();
 626         }
 627     }
 628 
 629     public final Graphics getTranslucentGraphics() {
 630         synchronized (getStateLock()) {
 631             return isOpaque ? null : painter.getBackBuffer(false).getGraphics();
 632         }
 633     }
 634 
 635     @Override
 636     public void setBackground(Color c) {
 637         super.setBackground(c);
 638         synchronized (getStateLock()) {
 639             if (!isOpaque && ((Window)target).isVisible()) {
 640                 updateWindow(true);
 641             }
 642         }
 643     }
 644 
 645     private native void setOpacity(int iOpacity);
 646     private float opacity = 1.0f;
 647 
 648     @Override
 649     public void setOpacity(float opacity) {
 650         if (!((SunToolkit)((Window)target).getToolkit()).
 651             isWindowOpacitySupported())
 652         {
 653             return;
 654         }
 655 
 656         if (opacity < 0.0f || opacity > 1.0f) {
 657             throw new IllegalArgumentException(
 658                 "The value of opacity should be in the range [0.0f .. 1.0f].");
 659         }
 660 
 661         if (((this.opacity == 1.0f && opacity <  1.0f) ||
 662              (this.opacity <  1.0f && opacity == 1.0f)) &&
 663             !Win32GraphicsEnvironment.isVistaOS())
 664         {
 665             // non-Vista OS: only replace the surface data if opacity status
 666             // changed (see WComponentPeer.isAccelCapable() for more)
 667             replaceSurfaceDataRecursively((Component)getTarget());
 668         }
 669 
 670         this.opacity = opacity;
 671 
 672         final int maxOpacity = 0xff;
 673         int iOpacity = (int)(opacity * maxOpacity);
 674         if (iOpacity < 0) {
 675             iOpacity = 0;
 676         }
 677         if (iOpacity > maxOpacity) {
 678             iOpacity = maxOpacity;
 679         }
 680 
 681         setOpacity(iOpacity);
 682 
 683         synchronized (getStateLock()) {
 684             if (!isOpaque && ((Window)target).isVisible()) {
 685                 updateWindow(true);
 686             }
 687         }
 688     }
 689 
 690     private native void setOpaqueImpl(boolean isOpaque);
 691 
 692     @Override
 693     public void setOpaque(boolean isOpaque) {
 694         synchronized (getStateLock()) {
 695             if (this.isOpaque == isOpaque) {
 696                 return;
 697             }
 698         }
 699 
 700         Window target = (Window)getTarget();
 701 
 702         if (!isOpaque) {
 703             SunToolkit sunToolkit = (SunToolkit)target.getToolkit();
 704             if (!sunToolkit.isWindowTranslucencySupported() ||
 705                 !sunToolkit.isTranslucencyCapable(target.getGraphicsConfiguration()))
 706             {
 707                 return;
 708             }
 709         }
 710 
 711         boolean isVistaOS = Win32GraphicsEnvironment.isVistaOS();
 712 
 713         if (this.isOpaque != isOpaque && !isVistaOS) {
 714             // non-Vista OS: only replace the surface data if the opacity
 715             // status changed (see WComponentPeer.isAccelCapable() for more)
 716             replaceSurfaceDataRecursively(target);
 717         }
 718 
 719         synchronized (getStateLock()) {
 720             this.isOpaque = isOpaque;
 721             setOpaqueImpl(isOpaque);
 722             if (isOpaque) {
 723                 TranslucentWindowPainter currentPainter = painter;
 724                 if (currentPainter != null) {
 725                     currentPainter.flush();
 726                     painter = null;
 727                 }
 728             } else {
 729                 painter = TranslucentWindowPainter.createInstance(this);
 730             }
 731         }
 732 
 733         if (isVistaOS) {
 734             // On Vista: setting the window non-opaque makes the window look
 735             // rectangular, though still catching the mouse clicks within
 736             // its shape only. To restore the correct visual appearance
 737             // of the window (i.e. w/ the correct shape) we have to reset
 738             // the shape.
 739             Shape shape = target.getShape();
 740             if (shape != null) {
 741                 target.setShape(shape);
 742             }
 743         }
 744 
 745         if (target.isVisible()) {
 746             updateWindow(true);
 747         }
 748     }
 749 
 750     native void updateWindowImpl(int[] data, int width, int height);
 751 
 752     @Override
 753     public void updateWindow() {
 754         updateWindow(false);
 755     }
 756 
 757     private void updateWindow(boolean repaint) {
 758         Window w = (Window)target;
 759         synchronized (getStateLock()) {
 760             if (isOpaque || !w.isVisible() ||
 761                 (w.getWidth() <= 0) || (w.getHeight() <= 0))
 762             {
 763                 return;
 764             }
 765             TranslucentWindowPainter currentPainter = painter;
 766             if (currentPainter != null) {
 767                 currentPainter.updateWindow(repaint);
 768             } else if (log.isLoggable(PlatformLogger.Level.FINER)) {
 769                 log.finer("Translucent window painter is null in updateWindow");
 770             }
 771         }
 772     }
 773 
 774     /*
 775      * The method maps the list of the active windows to the window's AppContext,
 776      * then the method registers ActiveWindowListener, GuiDisposedListener listeners;
 777      * it executes the initilialization only once per AppContext.
 778      */
 779     @SuppressWarnings("unchecked")
 780     private static void initActiveWindowsTracking(Window w) {
 781         AppContext appContext = AppContext.getAppContext();
 782         synchronized (appContext) {
 783             List<WWindowPeer> l = (List<WWindowPeer>)appContext.get(ACTIVE_WINDOWS_KEY);
 784             if (l == null) {
 785                 l = new LinkedList<WWindowPeer>();
 786                 appContext.put(ACTIVE_WINDOWS_KEY, l);
 787                 appContext.addPropertyChangeListener(AppContext.GUI_DISPOSED, guiDisposedListener);
 788 
 789                 KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
 790                 kfm.addPropertyChangeListener("activeWindow", activeWindowListener);
 791             }
 792         }
 793     }
 794 
 795     /*
 796      * The GuiDisposedListener class listens for the AppContext.GUI_DISPOSED property,
 797      * it removes the list of the active windows from the disposed AppContext and
 798      * unregisters ActiveWindowListener listener.
 799      */
 800     private static class GuiDisposedListener implements PropertyChangeListener {
 801         @Override
 802         public void propertyChange(PropertyChangeEvent e) {
 803             boolean isDisposed = (Boolean)e.getNewValue();
 804             if (isDisposed != true) {
 805                 if (log.isLoggable(PlatformLogger.Level.FINE)) {
 806                     log.fine(" Assertion (newValue != true) failed for AppContext.GUI_DISPOSED ");
 807                 }
 808             }
 809             AppContext appContext = AppContext.getAppContext();
 810             synchronized (appContext) {
 811                 appContext.remove(ACTIVE_WINDOWS_KEY);
 812                 appContext.removePropertyChangeListener(AppContext.GUI_DISPOSED, this);
 813 
 814                 KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
 815                 kfm.removePropertyChangeListener("activeWindow", activeWindowListener);
 816             }
 817         }
 818     }
 819 
 820     /*
 821      * Static inner class, listens for 'activeWindow' KFM property changes and
 822      * updates the list of active windows per AppContext, so the latest active
 823      * window is always at the end of the list. The list is stored in AppContext.
 824      */
 825     @SuppressWarnings("unchecked")
 826     private static class ActiveWindowListener implements PropertyChangeListener {
 827         @Override
 828         public void propertyChange(PropertyChangeEvent e) {
 829             Window w = (Window)e.getNewValue();
 830             if (w == null) {
 831                 return;
 832             }
 833             AppContext appContext = SunToolkit.targetToAppContext(w);
 834             synchronized (appContext) {
 835                 WWindowPeer wp = AWTAccessor.getComponentAccessor().getPeer(w);
 836                 // add/move wp to the end of the list
 837                 List<WWindowPeer> l = (List<WWindowPeer>)appContext.get(ACTIVE_WINDOWS_KEY);
 838                 if (l != null) {
 839                     l.remove(wp);
 840                     l.add(wp);
 841                 }
 842             }
 843         }
 844     }
 845 }