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