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