1 /*
   2  * Copyright (c) 1996, 2010, 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 final static 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 final static 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     @SuppressWarnings("unchecked")
  94     protected void disposeImpl() {
  95         AppContext appContext = SunToolkit.targetToAppContext(target);
  96         synchronized (appContext) {
  97             List<WWindowPeer> l = (List<WWindowPeer>)appContext.get(ACTIVE_WINDOWS_KEY);
  98             if (l != null) {
  99                 l.remove(this);
 100             }
 101         }
 102 
 103         // Remove ourself from the Map of DisplayChangeListeners
 104         GraphicsConfiguration gc = getGraphicsConfiguration();
 105         ((Win32GraphicsDevice)gc.getDevice()).removeDisplayChangedListener(this);
 106 
 107         synchronized (getStateLock()) {
 108             TranslucentWindowPainter currentPainter = painter;
 109             if (currentPainter != null) {
 110                 currentPainter.flush();
 111                 // don't set the current one to null here; reduces the chances of
 112                 // MT issues (like NPEs)
 113             }
 114         }
 115 
 116         super.disposeImpl();
 117     }
 118 
 119     // WindowPeer implementation
 120 
 121     public void toFront() {
 122         updateFocusableWindowState();
 123         _toFront();
 124     }
 125     native void _toFront();
 126     public native void toBack();
 127 
 128     public native void setAlwaysOnTopNative(boolean value);
 129     public void setAlwaysOnTop(boolean value) {
 130         if ((value && ((Window)target).isVisible()) || !value) {
 131             setAlwaysOnTopNative(value);
 132         }
 133     }
 134 
 135     public void updateFocusableWindowState() {
 136         setFocusableWindow(((Window)target).isFocusableWindow());
 137     }
 138     native void setFocusableWindow(boolean value);
 139 
 140     // FramePeer & DialogPeer partial shared implementation
 141 
 142     public void setTitle(String title) {
 143         // allow a null title to pass as an empty string.
 144         if (title == null) {
 145             title = "";
 146         }
 147         _setTitle(title);
 148     }
 149     native void _setTitle(String title);
 150 
 151     public void setResizable(boolean resizable) {
 152         _setResizable(resizable);
 153     }
 154     public native void _setResizable(boolean resizable);
 155 
 156     // Toolkit & peer internals
 157 
 158     WWindowPeer(Window target) {
 159         super(target);
 160     }
 161 
 162     void initialize() {
 163         super.initialize();
 164 
 165         updateInsets(insets_);
 166 
 167         Font f = ((Window)target).getFont();
 168         if (f == null) {
 169             f = defaultFont;
 170             ((Window)target).setFont(f);
 171             setFont(f);
 172         }
 173         // Express our interest in display changes
 174         GraphicsConfiguration gc = getGraphicsConfiguration();
 175         ((Win32GraphicsDevice)gc.getDevice()).addDisplayChangedListener(this);
 176 
 177         initActiveWindowsTracking((Window)target);
 178 
 179         updateIconImages();
 180 
 181         Shape shape = ((Window)target).getShape();
 182         if (shape != null) {
 183             applyShape(Region.getInstance(shape, null));
 184         }
 185 
 186         float opacity = ((Window)target).getOpacity();
 187         if (opacity < 1.0f) {
 188             setOpacity(opacity);
 189         }
 190 
 191         synchronized (getStateLock()) {
 192             // default value of a boolean field is 'false', so set isOpaque to
 193             // true here explicitly
 194             this.isOpaque = true;
 195             setOpaque(((Window)target).isOpaque());
 196         }
 197     }
 198 
 199     native void createAwtWindow(WComponentPeer parent);
 200 
 201     private volatile Window.Type windowType = Window.Type.NORMAL;
 202 
 203     // This method must be called for Window, Dialog, and Frame before creating
 204     // the hwnd
 205     void preCreate(WComponentPeer parent) {
 206         windowType = ((Window)target).getType();
 207     }
 208 
 209     void create(WComponentPeer parent) {
 210         preCreate(parent);
 211         createAwtWindow(parent);
 212     }
 213 
 214     // should be overriden in WDialogPeer
 215     protected void realShow() {
 216         super.show();
 217     }
 218 
 219     public void show() {
 220         updateFocusableWindowState();
 221 
 222         boolean alwaysOnTop = ((Window)target).isAlwaysOnTop();
 223 
 224         // Fix for 4868278.
 225         // If we create a window with a specific GraphicsConfig, and then move it with
 226         // setLocation() or setBounds() to another one before its peer has been created,
 227         // then calling Window.getGraphicsConfig() returns wrong config. That may lead
 228         // to some problems like wrong-placed tooltips. It is caused by calling
 229         // super.displayChanged() in WWindowPeer.displayChanged() regardless of whether
 230         // GraphicsDevice was really changed, or not. So we need to track it here.
 231         updateGC();
 232 
 233         realShow();
 234         updateMinimumSize();
 235 
 236         if (((Window)target).isAlwaysOnTopSupported() && alwaysOnTop) {
 237             setAlwaysOnTop(alwaysOnTop);
 238         }
 239 
 240         synchronized (getStateLock()) {
 241             if (!isOpaque) {
 242                 updateWindow(true);
 243             }
 244         }
 245     }
 246 
 247     // Synchronize the insets members (here & in helper) with actual window
 248     // state.
 249     native void updateInsets(Insets i);
 250 
 251     static native int getSysMinWidth();
 252     static native int getSysMinHeight();
 253     static native int getSysIconWidth();
 254     static native int getSysIconHeight();
 255     static native int getSysSmIconWidth();
 256     static native int getSysSmIconHeight();
 257     /**windows/classes/sun/awt/windows/
 258      * Creates native icon from specified raster data and updates
 259      * icon for window and all descendant windows that inherit icon.
 260      * Raster data should be passed in the ARGB form.
 261      * Note that raster data format was changed to provide support
 262      * for XP icons with alpha-channel
 263      */
 264     native void setIconImagesData(int[] iconRaster, int w, int h,
 265                                   int[] smallIconRaster, int smw, int smh);
 266 
 267     synchronized native void reshapeFrame(int x, int y, int width, int height);
 268 
 269     public boolean requestWindowFocus(CausedFocusEvent.Cause cause) {
 270         if (!focusAllowedFor()) {
 271             return false;
 272         }
 273         return requestWindowFocus(cause == CausedFocusEvent.Cause.MOUSE_EVENT);
 274     }
 275     public native boolean requestWindowFocus(boolean isMouseEventCause);
 276 
 277     public boolean focusAllowedFor() {
 278         Window window = (Window)this.target;
 279         if (!window.isVisible() ||
 280             !window.isEnabled() ||
 281             !window.isFocusableWindow())
 282         {
 283             return false;
 284         }
 285         if (isModalBlocked()) {
 286             return false;
 287         }
 288         return true;
 289     }
 290 
 291     public void hide() {
 292         WindowListener listener = windowListener;
 293         if (listener != null) {
 294             // We're not getting WINDOW_CLOSING from the native code when hiding
 295             // the window programmatically. So, create it and notify the listener.
 296             listener.windowClosing(new WindowEvent((Window)target, WindowEvent.WINDOW_CLOSING));
 297         }
 298         super.hide();
 299     }
 300 
 301     // WARNING: it's called on the Toolkit thread!
 302     void preprocessPostEvent(AWTEvent event) {
 303         if (event instanceof WindowEvent) {
 304             WindowListener listener = windowListener;
 305             if (listener != null) {
 306                 switch(event.getID()) {
 307                     case WindowEvent.WINDOW_CLOSING:
 308                         listener.windowClosing((WindowEvent)event);
 309                         break;
 310                     case WindowEvent.WINDOW_ICONIFIED:
 311                         listener.windowIconified((WindowEvent)event);
 312                         break;
 313                 }
 314             }
 315         }
 316     }
 317 
 318     synchronized void addWindowListener(WindowListener l) {
 319         windowListener = AWTEventMulticaster.add(windowListener, l);
 320     }
 321 
 322     synchronized void removeWindowListener(WindowListener l) {
 323         windowListener = AWTEventMulticaster.remove(windowListener, l);
 324     }
 325 
 326     public void updateMinimumSize() {
 327         Dimension minimumSize = null;
 328         if (((Component)target).isMinimumSizeSet()) {
 329             minimumSize = ((Component)target).getMinimumSize();
 330         }
 331         if (minimumSize != null) {
 332             int msw = getSysMinWidth();
 333             int msh = getSysMinHeight();
 334             int w = (minimumSize.width >= msw) ? minimumSize.width : msw;
 335             int h = (minimumSize.height >= msh) ? minimumSize.height : msh;
 336             setMinSize(w, h);
 337         } else {
 338             setMinSize(0, 0);
 339         }
 340     }
 341 
 342     public void updateIconImages() {
 343         java.util.List<Image> imageList = ((Window)target).getIconImages();
 344         if (imageList == null || imageList.size() == 0) {
 345             setIconImagesData(null, 0, 0, null, 0, 0);
 346         } else {
 347             int w = getSysIconWidth();
 348             int h = getSysIconHeight();
 349             int smw = getSysSmIconWidth();
 350             int smh = getSysSmIconHeight();
 351             DataBufferInt iconData = SunToolkit.getScaledIconData(imageList,
 352                                                                   w, h);
 353             DataBufferInt iconSmData = SunToolkit.getScaledIconData(imageList,
 354                                                                     smw, smh);
 355             if (iconData != null && iconSmData != null) {
 356                 setIconImagesData(iconData.getData(), w, h,
 357                                   iconSmData.getData(), smw, smh);
 358             } else {
 359                 setIconImagesData(null, 0, 0, null, 0, 0);
 360             }
 361         }
 362     }
 363 
 364     native void setMinSize(int width, int height);
 365 
 366 /*
 367  * ---- MODALITY SUPPORT ----
 368  */
 369 
 370     /**
 371      * Some modality-related code here because WFileDialogPeer, WPrintDialogPeer and
 372      *   WPageDialogPeer are descendants of WWindowPeer, not WDialogPeer
 373      */
 374 
 375     public boolean isModalBlocked() {
 376         return modalBlocker != null;
 377     }
 378 
 379      @SuppressWarnings("deprecation")
 380     public void setModalBlocked(Dialog dialog, boolean blocked) {
 381         synchronized (((Component)getTarget()).getTreeLock()) // State lock should always be after awtLock
 382         {
 383             // use WWindowPeer instead of WDialogPeer because of FileDialogs and PrintDialogs
 384             WWindowPeer blockerPeer = (WWindowPeer)dialog.getPeer();
 385             if (blocked)
 386             {
 387                 modalBlocker = blockerPeer;
 388                 // handle native dialogs separately, as they may have not
 389                 // got HWND yet; modalEnable/modalDisable is called from
 390                 // their setHWnd() methods
 391                 if (blockerPeer instanceof WFileDialogPeer) {
 392                     ((WFileDialogPeer)blockerPeer).blockWindow(this);
 393                 } else if (blockerPeer instanceof WPrintDialogPeer) {
 394                     ((WPrintDialogPeer)blockerPeer).blockWindow(this);
 395                 } else {
 396                     modalDisable(dialog, blockerPeer.getHWnd());
 397                 }
 398             } else {
 399                 modalBlocker = null;
 400                 if (blockerPeer instanceof WFileDialogPeer) {
 401                     ((WFileDialogPeer)blockerPeer).unblockWindow(this);
 402                 } else if (blockerPeer instanceof WPrintDialogPeer) {
 403                     ((WPrintDialogPeer)blockerPeer).unblockWindow(this);
 404                 } else {
 405                     modalEnable(dialog);
 406                 }
 407             }
 408         }
 409     }
 410 
 411     native void modalDisable(Dialog blocker, long blockerHWnd);
 412     native void modalEnable(Dialog blocker);
 413 
 414     /*
 415      * Returns all the ever active windows from the current AppContext.
 416      * The list is sorted by the time of activation, so the latest
 417      * active window is always at the end.
 418      */
 419     @SuppressWarnings("unchecked")
 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(boolean doPost) {
 530          nativeUngrab(doPost);
 531      }
 532      private native void nativeGrab();
 533      private native void nativeUngrab(boolean doPost);
 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     @SuppressWarnings("deprecation")
 575     private void replaceSurfaceDataRecursively(Component c) {
 576         if (c instanceof Container) {
 577             for (Component child : ((Container)c).getComponents()) {
 578                 replaceSurfaceDataRecursively(child);
 579             }
 580         }
 581         ComponentPeer cp = c.getPeer();
 582         if (cp instanceof WComponentPeer) {
 583             ((WComponentPeer)cp).replaceSurfaceDataLater();
 584         }
 585     }
 586 
 587     public final Graphics getTranslucentGraphics() {
 588         synchronized (getStateLock()) {
 589             return isOpaque ? null : painter.getBackBuffer(false).getGraphics();
 590         }
 591     }
 592 
 593     @Override
 594     public void setBackground(Color c) {
 595         super.setBackground(c);
 596         synchronized (getStateLock()) {
 597             if (!isOpaque && ((Window)target).isVisible()) {
 598                 updateWindow(true);
 599             }
 600         }
 601     }
 602 
 603     private native void setOpacity(int iOpacity);
 604     private float opacity = 1.0f;
 605 
 606     public void setOpacity(float opacity) {
 607         if (!((SunToolkit)((Window)target).getToolkit()).
 608             isWindowOpacitySupported())
 609         {
 610             return;
 611         }
 612 
 613         if (opacity < 0.0f || opacity > 1.0f) {
 614             throw new IllegalArgumentException(
 615                 "The value of opacity should be in the range [0.0f .. 1.0f].");
 616         }
 617 
 618         if (((this.opacity == 1.0f && opacity <  1.0f) ||
 619              (this.opacity <  1.0f && opacity == 1.0f)) &&
 620             !Win32GraphicsEnvironment.isVistaOS())
 621         {
 622             // non-Vista OS: only replace the surface data if opacity status
 623             // changed (see WComponentPeer.isAccelCapable() for more)
 624             replaceSurfaceDataRecursively((Component)getTarget());
 625         }
 626 
 627         this.opacity = opacity;
 628 
 629         final int maxOpacity = 0xff;
 630         int iOpacity = (int)(opacity * maxOpacity);
 631         if (iOpacity < 0) {
 632             iOpacity = 0;
 633         }
 634         if (iOpacity > maxOpacity) {
 635             iOpacity = maxOpacity;
 636         }
 637 
 638         setOpacity(iOpacity);
 639 
 640         synchronized (getStateLock()) {
 641             if (!isOpaque && ((Window)target).isVisible()) {
 642                 updateWindow(true);
 643             }
 644         }
 645     }
 646 
 647     private native void setOpaqueImpl(boolean isOpaque);
 648 
 649     public void setOpaque(boolean isOpaque) {
 650         synchronized (getStateLock()) {
 651             if (this.isOpaque == isOpaque) {
 652                 return;
 653             }
 654         }
 655 
 656         Window target = (Window)getTarget();
 657 
 658         if (!isOpaque) {
 659             SunToolkit sunToolkit = (SunToolkit)target.getToolkit();
 660             if (!sunToolkit.isWindowTranslucencySupported() ||
 661                 !sunToolkit.isTranslucencyCapable(target.getGraphicsConfiguration()))
 662             {
 663                 return;
 664             }
 665         }
 666 
 667         boolean isVistaOS = Win32GraphicsEnvironment.isVistaOS();
 668 
 669         if (this.isOpaque != isOpaque && !isVistaOS) {
 670             // non-Vista OS: only replace the surface data if the opacity
 671             // status changed (see WComponentPeer.isAccelCapable() for more)
 672             replaceSurfaceDataRecursively(target);
 673         }
 674 
 675         synchronized (getStateLock()) {
 676             this.isOpaque = isOpaque;
 677             setOpaqueImpl(isOpaque);
 678             if (isOpaque) {
 679                 TranslucentWindowPainter currentPainter = painter;
 680                 if (currentPainter != null) {
 681                     currentPainter.flush();
 682                     painter = null;
 683                 }
 684             } else {
 685                 painter = TranslucentWindowPainter.createInstance(this);
 686             }
 687         }
 688 
 689         if (isVistaOS) {
 690             // On Vista: setting the window non-opaque makes the window look
 691             // rectangular, though still catching the mouse clicks within
 692             // its shape only. To restore the correct visual appearance
 693             // of the window (i.e. w/ the correct shape) we have to reset
 694             // the shape.
 695             Shape shape = target.getShape();
 696             if (shape != null) {
 697                 target.setShape(shape);
 698             }
 699         }
 700 
 701         if (target.isVisible()) {
 702             updateWindow(true);
 703         }
 704     }
 705 
 706     public native void updateWindowImpl(int[] data, int width, int height);
 707 
 708     public void updateWindow() {
 709         updateWindow(false);
 710     }
 711 
 712     private void updateWindow(boolean repaint) {
 713         Window w = (Window)target;
 714         synchronized (getStateLock()) {
 715             if (isOpaque || !w.isVisible() ||
 716                 (w.getWidth() <= 0) || (w.getHeight() <= 0))
 717             {
 718                 return;
 719             }
 720             TranslucentWindowPainter currentPainter = painter;
 721             if (currentPainter != null) {
 722                 currentPainter.updateWindow(repaint);
 723             } else if (log.isLoggable(PlatformLogger.FINER)) {
 724                 log.finer("Translucent window painter is null in updateWindow");
 725             }
 726         }
 727     }
 728 
 729     /*
 730      * The method maps the list of the active windows to the window's AppContext,
 731      * then the method registers ActiveWindowListener, GuiDisposedListener listeners;
 732      * it executes the initilialization only once per AppContext.
 733      */
 734     @SuppressWarnings("unchecked")
 735     private static void initActiveWindowsTracking(Window w) {
 736         AppContext appContext = AppContext.getAppContext();
 737         synchronized (appContext) {
 738             List<WWindowPeer> l = (List<WWindowPeer>)appContext.get(ACTIVE_WINDOWS_KEY);
 739             if (l == null) {
 740                 l = new LinkedList<WWindowPeer>();
 741                 appContext.put(ACTIVE_WINDOWS_KEY, l);
 742                 appContext.addPropertyChangeListener(AppContext.GUI_DISPOSED, guiDisposedListener);
 743 
 744                 KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
 745                 kfm.addPropertyChangeListener("activeWindow", activeWindowListener);
 746             }
 747         }
 748     }
 749 
 750     /*
 751      * The GuiDisposedListener class listens for the AppContext.GUI_DISPOSED property,
 752      * it removes the list of the active windows from the disposed AppContext and
 753      * unregisters ActiveWindowListener listener.
 754      */
 755     private static class GuiDisposedListener implements PropertyChangeListener {
 756         public void propertyChange(PropertyChangeEvent e) {
 757             boolean isDisposed = (Boolean)e.getNewValue();
 758             if (isDisposed != true) {
 759                 if (log.isLoggable(PlatformLogger.FINE)) {
 760                     log.fine(" Assertion (newValue != true) failed for AppContext.GUI_DISPOSED ");
 761                 }
 762             }
 763             AppContext appContext = AppContext.getAppContext();
 764             synchronized (appContext) {
 765                 appContext.remove(ACTIVE_WINDOWS_KEY);
 766                 appContext.removePropertyChangeListener(AppContext.GUI_DISPOSED, this);
 767 
 768                 KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
 769                 kfm.removePropertyChangeListener("activeWindow", activeWindowListener);
 770             }
 771         }
 772     }
 773 
 774     /*
 775      * Static inner class, listens for 'activeWindow' KFM property changes and
 776      * updates the list of active windows per AppContext, so the latest active
 777      * window is always at the end of the list. The list is stored in AppContext.
 778      */
 779     @SuppressWarnings( value = {"deprecation", "unchecked"})
 780     private static class ActiveWindowListener implements PropertyChangeListener {
 781         public void propertyChange(PropertyChangeEvent e) {
 782             Window w = (Window)e.getNewValue();
 783             if (w == null) {
 784                 return;
 785             }
 786             AppContext appContext = SunToolkit.targetToAppContext(w);
 787             synchronized (appContext) {
 788                 WWindowPeer wp = (WWindowPeer)w.getPeer();
 789                 // add/move wp to the end of the list
 790                 List<WWindowPeer> l = (List<WWindowPeer>)appContext.get(ACTIVE_WINDOWS_KEY);
 791                 if (l != null) {
 792                     l.remove(wp);
 793                     l.add(wp);
 794                 }
 795             }
 796         }
 797     }
 798     
 799     @Override    
 800     public void grabFocus() {
 801         grab();
 802     }
 803     
 804     @Override
 805     public void ungrabFocus(boolean postEvent) {
 806         ungrab(postEvent);
 807     }
 808 }