1 /*
   2  * Copyright (c) 2002, 2014, 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.X11;
  26 
  27 import java.awt.AWTEvent;
  28 import java.awt.AWTException;
  29 import java.awt.BufferCapabilities;
  30 import java.awt.Color;
  31 import java.awt.Component;
  32 import java.awt.Container;
  33 import java.awt.Cursor;
  34 import java.awt.Dimension;
  35 import java.awt.Font;
  36 import java.awt.FontMetrics;
  37 import java.awt.Graphics;
  38 import java.awt.GraphicsConfiguration;
  39 import java.awt.Image;
  40 import java.awt.Insets;
  41 import java.awt.Rectangle;
  42 import java.awt.SystemColor;
  43 import java.awt.Toolkit;
  44 import java.awt.Window;
  45 import java.awt.dnd.DropTarget;
  46 import java.awt.dnd.peer.DropTargetPeer;
  47 import java.awt.event.FocusEvent;
  48 import java.awt.event.InputEvent;
  49 import java.awt.event.InputMethodEvent;
  50 import java.awt.event.KeyEvent;
  51 import java.awt.event.MouseEvent;
  52 import java.awt.event.MouseWheelEvent;
  53 import java.awt.event.PaintEvent;
  54 import java.awt.event.WindowEvent;
  55 import java.awt.event.InvocationEvent;
  56 import java.awt.image.ImageObserver;
  57 import java.awt.image.ImageProducer;
  58 import java.awt.image.VolatileImage;
  59 import java.awt.peer.ComponentPeer;
  60 import java.awt.peer.ContainerPeer;
  61 import java.lang.reflect.*;
  62 import java.security.*;
  63 import java.util.Collection;
  64 import java.util.Objects;
  65 import java.util.Set;
  66 import sun.util.logging.PlatformLogger;
  67 import sun.awt.*;
  68 import sun.awt.event.IgnorePaintEvent;
  69 import sun.awt.image.SunVolatileImage;
  70 import sun.awt.image.ToolkitImage;
  71 import sun.java2d.BackBufferCapsProvider;
  72 import sun.java2d.pipe.Region;
  73 
  74 
  75 public class XComponentPeer extends XWindow implements ComponentPeer, DropTargetPeer,
  76     BackBufferCapsProvider
  77 {
  78     private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.XComponentPeer");
  79     private static final PlatformLogger buffersLog = PlatformLogger.getLogger("sun.awt.X11.XComponentPeer.multibuffer");
  80     private static final PlatformLogger focusLog = PlatformLogger.getLogger("sun.awt.X11.focus.XComponentPeer");
  81     private static final PlatformLogger fontLog = PlatformLogger.getLogger("sun.awt.X11.font.XComponentPeer");
  82     private static final PlatformLogger enableLog = PlatformLogger.getLogger("sun.awt.X11.enable.XComponentPeer");
  83     private static final PlatformLogger shapeLog = PlatformLogger.getLogger("sun.awt.X11.shape.XComponentPeer");
  84 
  85     boolean paintPending = false;
  86     boolean isLayouting = false;
  87     private boolean enabled;
  88 
  89     // Actually used only by XDecoratedPeer
  90     protected int boundsOperation;
  91 
  92     Color foreground;
  93     Color background;
  94 
  95     // Colors calculated as on Motif using MotifColorUtilties.
  96     // If you use these, call updateMotifColors() in the peer's Constructor and
  97     // setBackground().  Examples are XCheckboxPeer and XButtonPeer.
  98     Color darkShadow;
  99     Color lightShadow;
 100     Color selectColor;
 101 
 102     Font font;
 103     private long backBuffer = 0;
 104     private VolatileImage xBackBuffer = null;
 105 
 106     static Color[] systemColors;
 107 
 108     XComponentPeer() {
 109     }
 110 
 111     XComponentPeer (XCreateWindowParams params) {
 112         super(params);
 113     }
 114 
 115     XComponentPeer(Component target, long parentWindow, Rectangle bounds) {
 116         super(target, parentWindow, bounds);
 117     }
 118 
 119     /**
 120      * Standard peer constructor, with corresponding Component
 121      */
 122     XComponentPeer(Component target) {
 123         super(target);
 124     }
 125 
 126 
 127     void preInit(XCreateWindowParams params) {
 128         super.preInit(params);
 129         boundsOperation = DEFAULT_OPERATION;
 130     }
 131     void postInit(XCreateWindowParams params) {
 132         super.postInit(params);
 133 
 134         pSetCursor(target.getCursor());
 135 
 136         foreground = target.getForeground();
 137         background = target.getBackground();
 138         font = target.getFont();
 139 
 140         if (isInitialReshape()) {
 141             Rectangle r = target.getBounds();
 142             reshape(r.x, r.y, r.width, r.height);
 143         }
 144 
 145         setEnabled(target.isEnabled());
 146 
 147         if (target.isVisible()) {
 148             setVisible(true);
 149         }
 150     }
 151 
 152     protected boolean isInitialReshape() {
 153         return true;
 154     }
 155 
 156     public void reparent(ContainerPeer newNativeParent) {
 157         XComponentPeer newPeer = (XComponentPeer)newNativeParent;
 158         XToolkit.awtLock();
 159         try {
 160             XlibWrapper.XReparentWindow(XToolkit.getDisplay(), getWindow(), newPeer.getContentWindow(), x, y);
 161             parentWindow = newPeer;
 162         } finally {
 163             XToolkit.awtUnlock();
 164         }
 165     }
 166     public boolean isReparentSupported() {
 167         return System.getProperty("sun.awt.X11.XComponentPeer.reparentNotSupported", "false").equals("false");
 168     }
 169 
 170     public boolean isObscured() {
 171         Container container  = (target instanceof Container) ?
 172             (Container)target : target.getParent();
 173 
 174         if (container == null) {
 175             return true;
 176         }
 177 
 178         Container parent;
 179         while ((parent = container.getParent()) != null) {
 180             container = parent;
 181         }
 182 
 183         if (container instanceof Window) {
 184             XWindowPeer wpeer = (XWindowPeer)(container.getPeer());
 185             if (wpeer != null) {
 186                 return (wpeer.winAttr.visibilityState !=
 187                         XWindowAttributesData.AWT_UNOBSCURED);
 188             }
 189         }
 190         return true;
 191     }
 192 
 193     public boolean canDetermineObscurity() {
 194         return true;
 195     }
 196 
 197     /*************************************************
 198      * FOCUS STUFF
 199      *************************************************/
 200 
 201     /**
 202      * Keeps the track of focused state of the _NATIVE_ window
 203      */
 204     boolean bHasFocus = false;
 205 
 206     /**
 207      * Descendants should use this method to determine whether or not native window
 208      * has focus.
 209      */
 210     final public boolean hasFocus() {
 211         return bHasFocus;
 212     }
 213 
 214     /**
 215      * Called when component receives focus
 216      */
 217     public void focusGained(FocusEvent e) {
 218         if (focusLog.isLoggable(PlatformLogger.Level.FINE)) {
 219             focusLog.fine("{0}", e);
 220         }
 221         bHasFocus = true;
 222     }
 223 
 224     /**
 225      * Called when component loses focus
 226      */
 227     public void focusLost(FocusEvent e) {
 228         if (focusLog.isLoggable(PlatformLogger.Level.FINE)) {
 229             focusLog.fine("{0}", e);
 230         }
 231         bHasFocus = false;
 232     }
 233 
 234     public boolean isFocusable() {
 235         /* should be implemented by other sub-classes */
 236         return false;
 237     }
 238 
 239     private static Class seClass;
 240     private static Constructor seCtor;
 241 
 242     final static AWTEvent wrapInSequenced(AWTEvent event) {
 243         try {
 244             if (seClass == null) {
 245                 seClass = Class.forName("java.awt.SequencedEvent");
 246             }
 247 
 248             if (seCtor == null) {
 249                 seCtor = (Constructor) AccessController.doPrivileged(new PrivilegedExceptionAction() {
 250                         public Object run() throws Exception {
 251                             Constructor ctor = seClass.getConstructor(new Class[] { AWTEvent.class });
 252                             ctor.setAccessible(true);
 253                             return ctor;
 254                         }
 255                     });
 256             }
 257 
 258             return (AWTEvent) seCtor.newInstance(new Object[] { event });
 259         }
 260         catch (ClassNotFoundException e) {
 261             throw new NoClassDefFoundError("java.awt.SequencedEvent.");
 262         }
 263         catch (PrivilegedActionException ex) {
 264             throw new NoClassDefFoundError("java.awt.SequencedEvent.");
 265         }
 266         catch (InstantiationException e) {
 267             assert false;
 268         }
 269         catch (IllegalAccessException e) {
 270             assert false;
 271         }
 272         catch (InvocationTargetException e) {
 273             assert false;
 274         }
 275 
 276         return null;
 277     }
 278 
 279     // TODO: consider moving it to KeyboardFocusManagerPeerImpl
 280     final public boolean requestFocus(Component lightweightChild, boolean temporary,
 281                                       boolean focusedWindowChangeAllowed, long time,
 282                                       CausedFocusEvent.Cause cause)
 283     {
 284         if (XKeyboardFocusManagerPeer.
 285             processSynchronousLightweightTransfer(target, lightweightChild, temporary,
 286                                                   focusedWindowChangeAllowed, time))
 287         {
 288             return true;
 289         }
 290 
 291         int result = XKeyboardFocusManagerPeer.
 292             shouldNativelyFocusHeavyweight(target, lightweightChild,
 293                                            temporary, focusedWindowChangeAllowed,
 294                                            time, cause);
 295 
 296         switch (result) {
 297           case XKeyboardFocusManagerPeer.SNFH_FAILURE:
 298               return false;
 299           case XKeyboardFocusManagerPeer.SNFH_SUCCESS_PROCEED:
 300               // Currently we just generate focus events like we deal with lightweight instead of calling
 301               // XSetInputFocus on native window
 302               if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
 303                   focusLog.finer("Proceeding with request to " +
 304                                  lightweightChild + " in " + target);
 305               }
 306               /**
 307                * The problems with requests in non-focused window arise because shouldNativelyFocusHeavyweight
 308                * checks that native window is focused while appropriate WINDOW_GAINED_FOCUS has not yet
 309                * been processed - it is in EventQueue. Thus, SNFH allows native request and stores request record
 310                * in requests list - and it breaks our requests sequence as first record on WGF should be the last
 311                * focus owner which had focus before WLF. So, we should not add request record for such requests
 312                * but store this component in mostRecent - and return true as before for compatibility.
 313                */
 314               Window parentWindow = SunToolkit.getContainingWindow(target);
 315               if (parentWindow == null) {
 316                   return rejectFocusRequestHelper("WARNING: Parent window is null");
 317               }
 318               XWindowPeer wpeer = (XWindowPeer)parentWindow.getPeer();
 319               if (wpeer == null) {
 320                   return rejectFocusRequestHelper("WARNING: Parent window's peer is null");
 321               }
 322               /*
 323                * Passing null 'actualFocusedWindow' as we don't want to restore focus on it
 324                * when a component inside a Frame is requesting focus.
 325                * See 6314575 for details.
 326                */
 327               boolean res = wpeer.requestWindowFocus(null);
 328 
 329               if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
 330                   focusLog.finer("Requested window focus: " + res);
 331               }
 332               // If parent window can be made focused and has been made focused(synchronously)
 333               // then we can proceed with children, otherwise we retreat.
 334               if (!(res && parentWindow.isFocused())) {
 335                   return rejectFocusRequestHelper("Waiting for asynchronous processing of the request");
 336               }
 337               return XKeyboardFocusManagerPeer.deliverFocus(lightweightChild,
 338                                                             target,
 339                                                             temporary,
 340                                                             focusedWindowChangeAllowed,
 341                                                             time, cause);
 342               // Motif compatibility code
 343           case XKeyboardFocusManagerPeer.SNFH_SUCCESS_HANDLED:
 344               // Either lightweight or excessive request - all events are generated.
 345               return true;
 346         }
 347         return false;
 348     }
 349 
 350     private boolean rejectFocusRequestHelper(String logMsg) {
 351         if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
 352             focusLog.finer(logMsg);
 353         }
 354         XKeyboardFocusManagerPeer.removeLastFocusRequest(target);
 355         return false;
 356     }
 357 
 358     void handleJavaFocusEvent(AWTEvent e) {
 359         if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
 360             focusLog.finer(e.toString());
 361         }
 362         if (e.getID() == FocusEvent.FOCUS_GAINED) {
 363             focusGained((FocusEvent)e);
 364         } else {
 365             focusLost((FocusEvent)e);
 366         }
 367     }
 368 
 369     void handleJavaWindowFocusEvent(AWTEvent e) {
 370     }
 371 
 372     /*************************************************
 373      * END OF FOCUS STUFF
 374      *************************************************/
 375 
 376 
 377 
 378     public void setVisible(boolean b) {
 379         xSetVisible(b);
 380     }
 381 
 382     public void hide() {
 383         setVisible(false);
 384     }
 385 
 386     /**
 387      * @see java.awt.peer.ComponentPeer
 388      */
 389     public void setEnabled(final boolean value) {
 390         if (enableLog.isLoggable(PlatformLogger.Level.FINE)) {
 391             enableLog.fine("{0}ing {1}", (value ? "Enabl" : "Disabl"), this);
 392         }
 393         boolean status = value;
 394         // If any of our heavyweight ancestors are disable, we should be too
 395         // See 6176875 for more information
 396         final Container cp = SunToolkit.getNativeContainer(target);
 397         if (cp != null) {
 398             status &= ((XComponentPeer) cp.getPeer()).isEnabled();
 399         }
 400         synchronized (getStateLock()) {
 401             if (enabled == status) {
 402                 return;
 403             }
 404             enabled = status;
 405         }
 406 
 407         if (target instanceof Container) {
 408             final Component[] list = ((Container) target).getComponents();
 409             for (final Component child : list) {
 410                 final ComponentPeer p = child.getPeer();
 411                 if (p != null) {
 412                     p.setEnabled(status && child.isEnabled());
 413                 }
 414             }
 415         }
 416         repaint();
 417     }
 418 
 419     //
 420     // public so aw/Window can call it
 421     //
 422     public final boolean isEnabled() {
 423         synchronized (getStateLock()) {
 424             return enabled;
 425         }
 426     }
 427 
 428     @Override
 429     public void paint(final Graphics g) {
 430         super.paint(g);
 431         // allow target to change the picture
 432         target.paint(g);
 433     }
 434 
 435     public Graphics getGraphics() {
 436         return getGraphics(surfaceData, getPeerForeground(), getPeerBackground(), getPeerFont());
 437     }
 438     public void print(Graphics g) {
 439         // clear rect here to emulate X clears rect before Expose
 440         g.setColor(target.getBackground());
 441         g.fillRect(0, 0, target.getWidth(), target.getHeight());
 442         g.setColor(target.getForeground());
 443         // paint peer
 444         paintPeer(g);
 445         // allow target to change the picture
 446         target.print(g);
 447     }
 448 
 449     public void setBounds(int x, int y, int width, int height, int op) {
 450         this.x = x;
 451         this.y = y;
 452         this.width = width;
 453         this.height = height;
 454         xSetBounds(x,y,width,height);
 455         validateSurface();
 456         layout();
 457     }
 458 
 459     public void reshape(int x, int y, int width, int height) {
 460         setBounds(x, y, width, height, SET_BOUNDS);
 461     }
 462 
 463     public void coalescePaintEvent(PaintEvent e) {
 464         Rectangle r = e.getUpdateRect();
 465         if (!(e instanceof IgnorePaintEvent)) {
 466             paintArea.add(r, e.getID());
 467         }
 468         if (true) {
 469             switch(e.getID()) {
 470               case PaintEvent.UPDATE:
 471                   if (log.isLoggable(PlatformLogger.Level.FINER)) {
 472                       log.finer("XCP coalescePaintEvent : UPDATE : add : x = " +
 473                             r.x + ", y = " + r.y + ", width = " + r.width + ",height = " + r.height);
 474                   }
 475                   return;
 476               case PaintEvent.PAINT:
 477                   if (log.isLoggable(PlatformLogger.Level.FINER)) {
 478                       log.finer("XCP coalescePaintEvent : PAINT : add : x = " +
 479                             r.x + ", y = " + r.y + ", width = " + r.width + ",height = " + r.height);
 480                   }
 481                   return;
 482             }
 483         }
 484     }
 485 
 486     XWindowPeer getParentTopLevel() {
 487         AWTAccessor.ComponentAccessor compAccessor = AWTAccessor.getComponentAccessor();
 488         Container parent = (target instanceof Container) ? ((Container)target) : (compAccessor.getParent(target));
 489         // Search for parent window
 490         while (parent != null && !(parent instanceof Window)) {
 491             parent = compAccessor.getParent(parent);
 492         }
 493         if (parent != null) {
 494             return (XWindowPeer)compAccessor.getPeer(parent);
 495         } else {
 496             return null;
 497         }
 498     }
 499 
 500     /* This method is intended to be over-ridden by peers to perform user interaction */
 501     void handleJavaMouseEvent(MouseEvent e) {
 502         switch (e.getID()) {
 503           case MouseEvent.MOUSE_PRESSED:
 504               if (target == e.getSource() &&
 505                   !target.isFocusOwner() &&
 506                   XKeyboardFocusManagerPeer.shouldFocusOnClick(target))
 507               {
 508                   XWindowPeer parentXWindow = getParentTopLevel();
 509                   Window parentWindow = ((Window)parentXWindow.getTarget());
 510                   // Simple windows are non-focusable in X terms but focusable in Java terms.
 511                   // As X-non-focusable they don't receive any focus events - we should generate them
 512                   // by ourselfves.
 513 //                   if (parentXWindow.isFocusableWindow() /*&& parentXWindow.isSimpleWindow()*/ &&
 514 //                       !(getCurrentNativeFocusedWindow() == parentWindow))
 515 //                   {
 516 //                       setCurrentNativeFocusedWindow(parentWindow);
 517 //                       WindowEvent wfg = new WindowEvent(parentWindow, WindowEvent.WINDOW_GAINED_FOCUS);
 518 //                       parentWindow.dispatchEvent(wfg);
 519 //                   }
 520                   XKeyboardFocusManagerPeer.requestFocusFor(target, CausedFocusEvent.Cause.MOUSE_EVENT);
 521               }
 522               break;
 523         }
 524     }
 525 
 526     /* This method is intended to be over-ridden by peers to perform user interaction */
 527     void handleJavaKeyEvent(KeyEvent e) {
 528     }
 529 
 530     /* This method is intended to be over-ridden by peers to perform user interaction */
 531     void handleJavaMouseWheelEvent(MouseWheelEvent e) {
 532     }
 533 
 534 
 535     /* This method is intended to be over-ridden by peers to perform user interaction */
 536     void handleJavaInputMethodEvent(InputMethodEvent e) {
 537     }
 538 
 539     void handleF10JavaKeyEvent(KeyEvent e) {
 540         if (e.getID() == KeyEvent.KEY_PRESSED && e.getKeyCode() == KeyEvent.VK_F10) {
 541             XWindowPeer winPeer = this.getToplevelXWindow();
 542             if (winPeer instanceof XFramePeer) {
 543                 XMenuBarPeer mPeer = ((XFramePeer)winPeer).getMenubarPeer();
 544                 if (mPeer != null) {
 545                     mPeer.handleF10KeyPress(e);
 546                 }
 547             }
 548         }
 549     }
 550 
 551     @SuppressWarnings("fallthrough")
 552     public void handleEvent(java.awt.AWTEvent e) {
 553         if ((e instanceof InputEvent) && !((InputEvent)e).isConsumed() && target.isEnabled())  {
 554             if (e instanceof MouseEvent) {
 555                 if (e instanceof MouseWheelEvent) {
 556                     handleJavaMouseWheelEvent((MouseWheelEvent) e);
 557                 }
 558                 else
 559                     handleJavaMouseEvent((MouseEvent) e);
 560             }
 561             else if (e instanceof KeyEvent) {
 562                 handleF10JavaKeyEvent((KeyEvent)e);
 563                 handleJavaKeyEvent((KeyEvent)e);
 564             }
 565         }
 566         else if (e instanceof KeyEvent && !((InputEvent)e).isConsumed()) {
 567             // even if target is disabled.
 568             handleF10JavaKeyEvent((KeyEvent)e);
 569         }
 570         else if (e instanceof InputMethodEvent) {
 571             handleJavaInputMethodEvent((InputMethodEvent) e);
 572         }
 573 
 574         int id = e.getID();
 575 
 576         switch(id) {
 577           case PaintEvent.PAINT:
 578               // Got native painting
 579               paintPending = false;
 580               // Fallthrough to next statement
 581           case PaintEvent.UPDATE:
 582               // Skip all painting while layouting and all UPDATEs
 583               // while waiting for native paint
 584               if (!isLayouting && !paintPending) {
 585                   paintArea.paint(target,false);
 586               }
 587               return;
 588           case FocusEvent.FOCUS_LOST:
 589           case FocusEvent.FOCUS_GAINED:
 590               handleJavaFocusEvent(e);
 591               break;
 592           case WindowEvent.WINDOW_LOST_FOCUS:
 593           case WindowEvent.WINDOW_GAINED_FOCUS:
 594               handleJavaWindowFocusEvent(e);
 595               break;
 596           default:
 597               break;
 598         }
 599 
 600     }
 601 
 602     public Dimension getMinimumSize() {
 603         return target.getSize();
 604     }
 605 
 606     public Dimension getPreferredSize() {
 607         return getMinimumSize();
 608     }
 609 
 610     public void layout() {}
 611 
 612     void updateMotifColors(Color bg) {
 613         int red = bg.getRed();
 614         int green = bg.getGreen();
 615         int blue = bg.getBlue();
 616 
 617         darkShadow = new Color(MotifColorUtilities.calculateBottomShadowFromBackground(red,green,blue));
 618         lightShadow = new Color(MotifColorUtilities.calculateTopShadowFromBackground(red,green,blue));
 619         selectColor= new Color(MotifColorUtilities.calculateSelectFromBackground(red,green,blue));
 620     }
 621 
 622     /*
 623      * Draw a 3D rectangle using the Motif colors.
 624      * "Normal" rectangles have shadows on the bottom.
 625      * "Depressed" rectangles (such as pressed buttons) have shadows on the top,
 626      * in which case true should be passed for topShadow.
 627      */
 628     public void drawMotif3DRect(Graphics g,
 629                                           int x, int y, int width, int height,
 630                                           boolean topShadow) {
 631         g.setColor(topShadow ? darkShadow : lightShadow);
 632         g.drawLine(x, y, x+width, y);       // top
 633         g.drawLine(x, y+height, x, y);      // left
 634 
 635         g.setColor(topShadow ? lightShadow : darkShadow );
 636         g.drawLine(x+1, y+height, x+width, y+height); // bottom
 637         g.drawLine(x+width, y+height, x+width, y+1);  // right
 638     }
 639 
 640     @Override
 641     public void setBackground(Color c) {
 642         if (log.isLoggable(PlatformLogger.Level.FINE)) {
 643             log.fine("Set background to " + c);
 644         }
 645         synchronized (getStateLock()) {
 646             if (Objects.equals(background, c)) {
 647                 return;
 648             }
 649             background = c;
 650         }
 651         super.setBackground(c);
 652         repaint();
 653     }
 654 
 655     @Override
 656     public void setForeground(Color c) {
 657         if (log.isLoggable(PlatformLogger.Level.FINE)) {
 658             log.fine("Set foreground to " + c);
 659         }
 660         synchronized (getStateLock()) {
 661             if (Objects.equals(foreground, c)) {
 662                 return;
 663             }
 664             foreground = c;
 665         }
 666         repaint();
 667     }
 668 
 669     /**
 670      * Gets the font metrics for the specified font.
 671      * @param font the font for which font metrics is to be
 672      *      obtained
 673      * @return the font metrics for <code>font</code>
 674      * @see       #getFont
 675      * @see       #getPeer
 676      * @see       java.awt.peer.ComponentPeer#getFontMetrics(Font)
 677      * @see       Toolkit#getFontMetrics(Font)
 678      * @since     JDK1.0
 679      */
 680     public FontMetrics getFontMetrics(Font font) {
 681         if (fontLog.isLoggable(PlatformLogger.Level.FINE)) {
 682             fontLog.fine("Getting font metrics for " + font);
 683         }
 684         return sun.font.FontDesignMetrics.getMetrics(font);
 685     }
 686 
 687     @Override
 688     public void setFont(Font f) {
 689         if (f == null) {
 690             f = XWindow.getDefaultFont();
 691         }
 692         synchronized (getStateLock()) {
 693             if (f.equals(font)) {
 694                 return;
 695             }
 696             font = f;
 697         }
 698         // as it stands currently we don't need to do layout since
 699         // layout is done in the Component upon setFont.
 700         //layout();
 701         repaint();
 702     }
 703 
 704     public Font getFont() {
 705         return font;
 706     }
 707 
 708     public void updateCursorImmediately() {
 709         XGlobalCursorManager.getCursorManager().updateCursorImmediately();
 710     }
 711 
 712     public final void pSetCursor(Cursor cursor) {
 713         this.pSetCursor(cursor, true);
 714     }
 715 
 716     /*
 717      * The method changes the cursor.
 718      * @param cursor - a new cursor to change to.
 719      * @param ignoreSubComponents - if {@code true} is passed then
 720      *                              the new cursor will be installed on window.
 721      *                              if {@code false} is passed then
 722      *                              subsequent components will try to handle
 723      *                              this request and install their cursor.
 724      */
 725     //ignoreSubComponents not used here
 726     public void pSetCursor(Cursor cursor, boolean ignoreSubComponents) {
 727         XToolkit.awtLock();
 728         try {
 729             long xcursor = XGlobalCursorManager.getCursor(cursor);
 730 
 731             XSetWindowAttributes xwa = new XSetWindowAttributes();
 732             xwa.set_cursor(xcursor);
 733 
 734             long valuemask = XConstants.CWCursor;
 735 
 736             XlibWrapper.XChangeWindowAttributes(XToolkit.getDisplay(),getWindow(),valuemask,xwa.pData);
 737             XlibWrapper.XFlush(XToolkit.getDisplay());
 738             xwa.dispose();
 739         } finally {
 740             XToolkit.awtUnlock();
 741         }
 742     }
 743 
 744     public Image createImage(ImageProducer producer) {
 745         return new ToolkitImage(producer);
 746     }
 747 
 748     public Image createImage(int width, int height) {
 749         return graphicsConfig.createAcceleratedImage(target, width, height);
 750     }
 751 
 752     public VolatileImage createVolatileImage(int width, int height) {
 753         return new SunVolatileImage(target, width, height);
 754     }
 755 
 756     public boolean prepareImage(Image img, int w, int h, ImageObserver o) {
 757         return Toolkit.getDefaultToolkit().prepareImage(img, w, h, o);
 758     }
 759 
 760     public int checkImage(Image img, int w, int h, ImageObserver o) {
 761         return Toolkit.getDefaultToolkit().checkImage(img, w, h, o);
 762     }
 763 
 764     public Dimension preferredSize() {
 765         return getPreferredSize();
 766     }
 767 
 768     public Dimension minimumSize() {
 769         return getMinimumSize();
 770     }
 771 
 772     public Insets getInsets() {
 773         return new Insets(0, 0, 0, 0);
 774     }
 775 
 776     public void beginValidate() {
 777     }
 778 
 779     public void endValidate() {
 780     }
 781 
 782 
 783     /**
 784      * DEPRECATED:  Replaced by getInsets().
 785      */
 786 
 787     public Insets insets() {
 788         return getInsets();
 789     }
 790 
 791     // Returns true if we are inside begin/endLayout and
 792     // are waiting for native painting
 793     public boolean isPaintPending() {
 794         return paintPending && isLayouting;
 795     }
 796 
 797     public boolean handlesWheelScrolling() {
 798         return false;
 799     }
 800 
 801     public void beginLayout() {
 802         // Skip all painting till endLayout
 803         isLayouting = true;
 804 
 805     }
 806 
 807     public void endLayout() {
 808         if (!paintPending && !paintArea.isEmpty()
 809             && !AWTAccessor.getComponentAccessor().getIgnoreRepaint(target))
 810         {
 811             // if not waiting for native painting repaint damaged area
 812             postEvent(new PaintEvent(target, PaintEvent.PAINT,
 813                                      new Rectangle()));
 814         }
 815         isLayouting = false;
 816     }
 817 
 818     public Color getWinBackground() {
 819         return getPeerBackground();
 820     }
 821 
 822     static int[] getRGBvals(Color c) {
 823 
 824         int rgbvals[] = new int[3];
 825 
 826         rgbvals[0] = c.getRed();
 827         rgbvals[1] = c.getGreen();
 828         rgbvals[2] = c.getBlue();
 829 
 830         return rgbvals;
 831     }
 832 
 833     static final int BACKGROUND_COLOR = 0;
 834     static final int HIGHLIGHT_COLOR = 1;
 835     static final int SHADOW_COLOR = 2;
 836     static final int FOREGROUND_COLOR = 3;
 837 
 838     public Color[] getGUIcolors() {
 839         Color c[] = new Color[4];
 840         float backb, highb, shadowb, hue, saturation;
 841         c[BACKGROUND_COLOR] = getWinBackground();
 842         if (c[BACKGROUND_COLOR] == null) {
 843             c[BACKGROUND_COLOR] = super.getWinBackground();
 844         }
 845         if (c[BACKGROUND_COLOR] == null) {
 846             c[BACKGROUND_COLOR] = Color.lightGray;
 847         }
 848 
 849         int[] rgb = getRGBvals(c[BACKGROUND_COLOR]);
 850 
 851         float[] hsb = Color.RGBtoHSB(rgb[0],rgb[1],rgb[2],null);
 852 
 853         hue = hsb[0];
 854         saturation = hsb[1];
 855         backb = hsb[2];
 856 
 857 
 858 /*      Calculate Highlight Brightness  */
 859 
 860         highb = backb + 0.2f;
 861         shadowb = backb - 0.4f;
 862         if ((highb > 1.0) ) {
 863             if  ((1.0 - backb) < 0.05) {
 864                 highb = shadowb + 0.25f;
 865             } else {
 866                 highb = 1.0f;
 867             }
 868         } else {
 869             if (shadowb < 0.0) {
 870                 if ((backb - 0.0) < 0.25) {
 871                     highb = backb + 0.75f;
 872                     shadowb = highb - 0.2f;
 873                 } else {
 874                     shadowb = 0.0f;
 875                 }
 876             }
 877         }
 878         c[HIGHLIGHT_COLOR] = Color.getHSBColor(hue,saturation,highb);
 879         c[SHADOW_COLOR] = Color.getHSBColor(hue,saturation,shadowb);
 880 
 881 
 882 /*
 883   c[SHADOW_COLOR] = c[BACKGROUND_COLOR].darker();
 884   int r2 = c[SHADOW_COLOR].getRed();
 885   int g2 = c[SHADOW_COLOR].getGreen();
 886   int b2 = c[SHADOW_COLOR].getBlue();
 887 */
 888 
 889         c[FOREGROUND_COLOR] = getPeerForeground();
 890         if (c[FOREGROUND_COLOR] == null) {
 891             c[FOREGROUND_COLOR] = Color.black;
 892         }
 893 /*
 894   if ((c[BACKGROUND_COLOR].equals(c[HIGHLIGHT_COLOR]))
 895   && (c[BACKGROUND_COLOR].equals(c[SHADOW_COLOR]))) {
 896   c[SHADOW_COLOR] = new Color(c[BACKGROUND_COLOR].getRed() + 75,
 897   c[BACKGROUND_COLOR].getGreen() + 75,
 898   c[BACKGROUND_COLOR].getBlue() + 75);
 899   c[HIGHLIGHT_COLOR] = c[SHADOW_COLOR].brighter();
 900   } else if (c[BACKGROUND_COLOR].equals(c[HIGHLIGHT_COLOR])) {
 901   c[HIGHLIGHT_COLOR] = c[SHADOW_COLOR];
 902   c[SHADOW_COLOR] = c[SHADOW_COLOR].darker();
 903   }
 904 */
 905         if (! isEnabled()) {
 906             c[BACKGROUND_COLOR] = c[BACKGROUND_COLOR].darker();
 907             // Reduce the contrast
 908             // Calculate the NTSC gray (NB: REC709 L* might be better!)
 909             // for foreground and background; then multiply the foreground
 910             // by the average lightness
 911 
 912 
 913             Color tc = c[BACKGROUND_COLOR];
 914             int bg = tc.getRed() * 30 + tc.getGreen() * 59 + tc.getBlue() * 11;
 915 
 916             tc = c[FOREGROUND_COLOR];
 917             int fg = tc.getRed() * 30 + tc.getGreen() * 59 + tc.getBlue() * 11;
 918 
 919             float ave = (float) ((fg + bg) / 51000.0);
 920             // 255 * 100 * 2
 921 
 922             Color newForeground = new Color((int) (tc.getRed() * ave),
 923                                             (int) (tc.getGreen() * ave),
 924                                             (int) (tc.getBlue() * ave));
 925 
 926             if (newForeground.equals(c[FOREGROUND_COLOR])) {
 927                 // This probably means the foreground color is black or white
 928                 newForeground = new Color(ave, ave, ave);
 929             }
 930             c[FOREGROUND_COLOR] = newForeground;
 931 
 932         }
 933 
 934 
 935         return c;
 936     }
 937 
 938     /**
 939      * Returns an array of Colors similar to getGUIcolors(), but using the
 940      * System colors.  This is useful if pieces of a Component (such as
 941      * the integrated scrollbars of a List) should retain the System color
 942      * instead of the background color set by Component.setBackground().
 943      */
 944     static Color[] getSystemColors() {
 945         if (systemColors == null) {
 946             systemColors = new Color[4];
 947             systemColors[BACKGROUND_COLOR] = SystemColor.window;
 948             systemColors[HIGHLIGHT_COLOR] = SystemColor.controlLtHighlight;
 949             systemColors[SHADOW_COLOR] = SystemColor.controlShadow;
 950             systemColors[FOREGROUND_COLOR] = SystemColor.windowText;
 951         }
 952         return systemColors;
 953     }
 954 
 955     /**
 956      * Draw a 3D oval.
 957      */
 958     public void draw3DOval(Graphics g, Color colors[],
 959                            int x, int y, int w, int h, boolean raised)
 960         {
 961         Color c = g.getColor();
 962         g.setColor(raised ? colors[HIGHLIGHT_COLOR] : colors[SHADOW_COLOR]);
 963         g.drawArc(x, y, w, h, 45, 180);
 964         g.setColor(raised ? colors[SHADOW_COLOR] : colors[HIGHLIGHT_COLOR]);
 965         g.drawArc(x, y, w, h, 225, 180);
 966         g.setColor(c);
 967     }
 968 
 969     public void draw3DRect(Graphics g, Color colors[],
 970                            int x, int y, int width, int height, boolean raised)
 971         {
 972             Color c = g.getColor();
 973             g.setColor(raised ? colors[HIGHLIGHT_COLOR] : colors[SHADOW_COLOR]);
 974             g.drawLine(x, y, x, y + height);
 975             g.drawLine(x + 1, y, x + width - 1, y);
 976             g.setColor(raised ? colors[SHADOW_COLOR] : colors[HIGHLIGHT_COLOR]);
 977             g.drawLine(x + 1, y + height, x + width, y + height);
 978             g.drawLine(x + width, y, x + width, y + height - 1);
 979             g.setColor(c);
 980         }
 981 
 982     /*
 983      * drawXXX() methods are used to print the native components by
 984      * rendering the Motif look ourselves.
 985      * ToDo(aim): needs to query native motif for more accurate color
 986      * information.
 987      */
 988     void draw3DOval(Graphics g, Color bg,
 989                     int x, int y, int w, int h, boolean raised)
 990         {
 991             Color c = g.getColor();
 992             Color shadow = bg.darker();
 993             Color highlight = bg.brighter();
 994 
 995             g.setColor(raised ? highlight : shadow);
 996             g.drawArc(x, y, w, h, 45, 180);
 997             g.setColor(raised ? shadow : highlight);
 998             g.drawArc(x, y, w, h, 225, 180);
 999             g.setColor(c);
1000         }
1001 
1002     void draw3DRect(Graphics g, Color bg,
1003                     int x, int y, int width, int height,
1004                     boolean raised) {
1005         Color c = g.getColor();
1006         Color shadow = bg.darker();
1007         Color highlight = bg.brighter();
1008 
1009         g.setColor(raised ? highlight : shadow);
1010         g.drawLine(x, y, x, y + height);
1011         g.drawLine(x + 1, y, x + width - 1, y);
1012         g.setColor(raised ? shadow : highlight);
1013         g.drawLine(x + 1, y + height, x + width, y + height);
1014         g.drawLine(x + width, y, x + width, y + height - 1);
1015         g.setColor(c);
1016     }
1017 
1018     void drawScrollbar(Graphics g, Color bg, int thickness, int length,
1019                int min, int max, int val, int vis, boolean horizontal) {
1020         Color c = g.getColor();
1021         double f = (double)(length - 2*(thickness-1)) / Math.max(1, ((max - min) + vis));
1022         int v1 = thickness + (int)(f * (val - min));
1023         int v2 = (int)(f * vis);
1024         int w2 = thickness-4;
1025         int tpts_x[] = new int[3];
1026         int tpts_y[] = new int[3];
1027 
1028         if (length < 3*w2 ) {
1029             v1 = v2 = 0;
1030             if (length < 2*w2 + 2) {
1031                 w2 = (length-2)/2;
1032             }
1033         } else  if (v2 < 7) {
1034             // enforce a minimum handle size
1035             v1 = Math.max(0, v1 - ((7 - v2)>>1));
1036             v2 = 7;
1037         }
1038 
1039         int ctr   = thickness/2;
1040         int sbmin = ctr - w2/2;
1041         int sbmax = ctr + w2/2;
1042 
1043         // paint the background slightly darker
1044         {
1045             Color d = new Color((int) (bg.getRed()   * 0.85),
1046                                 (int) (bg.getGreen() * 0.85),
1047                                 (int) (bg.getBlue()  * 0.85));
1048 
1049             g.setColor(d);
1050             if (horizontal) {
1051                 g.fillRect(0, 0, length, thickness);
1052             } else {
1053                 g.fillRect(0, 0, thickness, length);
1054             }
1055         }
1056 
1057         // paint the thumb and arrows in the normal background color
1058         g.setColor(bg);
1059         if (v1 > 0) {
1060             if (horizontal) {
1061                 g.fillRect(v1, 3, v2, thickness-3);
1062             } else {
1063                 g.fillRect(3, v1, thickness-3, v2);
1064             }
1065         }
1066 
1067         tpts_x[0] = ctr;    tpts_y[0] = 2;
1068         tpts_x[1] = sbmin;  tpts_y[1] = w2;
1069         tpts_x[2] = sbmax;  tpts_y[2] = w2;
1070         if (horizontal) {
1071             g.fillPolygon(tpts_y, tpts_x, 3);
1072         } else {
1073             g.fillPolygon(tpts_x, tpts_y, 3);
1074         }
1075 
1076         tpts_y[0] = length-2;
1077         tpts_y[1] = length-w2;
1078         tpts_y[2] = length-w2;
1079         if (horizontal) {
1080             g.fillPolygon(tpts_y, tpts_x, 3);
1081         } else {
1082             g.fillPolygon(tpts_x, tpts_y, 3);
1083         }
1084 
1085         Color highlight = bg.brighter();
1086 
1087         // // // // draw the "highlighted" edges
1088         g.setColor(highlight);
1089 
1090         // outline & arrows
1091         if (horizontal) {
1092             g.drawLine(1, thickness, length - 1, thickness);
1093             g.drawLine(length - 1, 1, length - 1, thickness);
1094 
1095             // arrows
1096             g.drawLine(1, ctr, w2, sbmin);
1097             g.drawLine(length - w2, sbmin, length - w2, sbmax);
1098             g.drawLine(length - w2, sbmin, length - 2, ctr);
1099 
1100         } else {
1101             g.drawLine(thickness, 1, thickness, length - 1);
1102             g.drawLine(1, length - 1, thickness, length - 1);
1103 
1104             // arrows
1105             g.drawLine(ctr, 1, sbmin, w2);
1106             g.drawLine(sbmin, length - w2, sbmax, length - w2);
1107             g.drawLine(sbmin, length - w2, ctr, length - 2);
1108         }
1109 
1110         // thumb
1111         if (v1 > 0) {
1112             if (horizontal) {
1113                 g.drawLine(v1, 2, v1 + v2, 2);
1114                 g.drawLine(v1, 2, v1, thickness-3);
1115             } else {
1116                 g.drawLine(2, v1, 2, v1 + v2);
1117                 g.drawLine(2, v1, thickness-3, v1);
1118             }
1119         }
1120 
1121         Color shadow = bg.darker();
1122 
1123         // // // // draw the "shadowed" edges
1124         g.setColor(shadow);
1125 
1126         // outline && arrows
1127         if (horizontal) {
1128             g.drawLine(0, 0, 0, thickness);
1129             g.drawLine(0, 0, length - 1, 0);
1130 
1131             // arrows
1132             g.drawLine(w2, sbmin, w2, sbmax);
1133             g.drawLine(w2, sbmax, 1, ctr);
1134             g.drawLine(length-2, ctr, length-w2, sbmax);
1135 
1136         } else {
1137             g.drawLine(0, 0, thickness, 0);
1138             g.drawLine(0, 0, 0, length - 1);
1139 
1140             // arrows
1141             g.drawLine(sbmin, w2, sbmax, w2);
1142             g.drawLine(sbmax, w2, ctr, 1);
1143             g.drawLine(ctr, length-2, sbmax, length-w2);
1144         }
1145 
1146         // thumb
1147         if (v1 > 0) {
1148             if (horizontal) {
1149                 g.drawLine(v1 + v2, 2, v1 + v2, thickness-2);
1150                 g.drawLine(v1, thickness-2, v1 + v2, thickness-2);
1151             } else {
1152                 g.drawLine(2, v1 + v2, thickness-2, v1 + v2);
1153                 g.drawLine(thickness-2, v1, thickness-2, v1 + v2);
1154             }
1155         }
1156         g.setColor(c);
1157     }
1158 
1159     /**
1160      * The following multibuffering-related methods delegate to our
1161      * associated GraphicsConfig (X11 or GLX) to handle the appropriate
1162      * native windowing system specific actions.
1163      */
1164 
1165     private BufferCapabilities backBufferCaps;
1166 
1167     public void createBuffers(int numBuffers, BufferCapabilities caps)
1168       throws AWTException
1169     {
1170         if (buffersLog.isLoggable(PlatformLogger.Level.FINE)) {
1171             buffersLog.fine("createBuffers(" + numBuffers + ", " + caps + ")");
1172         }
1173         // set the caps first, they're used when creating the bb
1174         backBufferCaps = caps;
1175         backBuffer = graphicsConfig.createBackBuffer(this, numBuffers, caps);
1176         xBackBuffer = graphicsConfig.createBackBufferImage(target,
1177                                                            backBuffer);
1178     }
1179 
1180     @Override
1181     public BufferCapabilities getBackBufferCaps() {
1182         return backBufferCaps;
1183     }
1184 
1185     public void flip(int x1, int y1, int x2, int y2,
1186                      BufferCapabilities.FlipContents flipAction)
1187     {
1188         if (buffersLog.isLoggable(PlatformLogger.Level.FINE)) {
1189             buffersLog.fine("flip(" + flipAction + ")");
1190         }
1191         if (backBuffer == 0) {
1192             throw new IllegalStateException("Buffers have not been created");
1193         }
1194         graphicsConfig.flip(this, target, xBackBuffer,
1195                             x1, y1, x2, y2, flipAction);
1196     }
1197 
1198     public Image getBackBuffer() {
1199         if (buffersLog.isLoggable(PlatformLogger.Level.FINE)) {
1200             buffersLog.fine("getBackBuffer()");
1201         }
1202         if (backBuffer == 0) {
1203             throw new IllegalStateException("Buffers have not been created");
1204         }
1205         return xBackBuffer;
1206     }
1207 
1208     public void destroyBuffers() {
1209         if (buffersLog.isLoggable(PlatformLogger.Level.FINE)) {
1210             buffersLog.fine("destroyBuffers()");
1211         }
1212         graphicsConfig.destroyBackBuffer(backBuffer);
1213         backBuffer = 0;
1214         xBackBuffer = null;
1215     }
1216 
1217     // End of multi-buffering
1218 
1219     public void notifyTextComponentChange(boolean add){
1220         Container parent = AWTAccessor.getComponentAccessor().getParent(target);
1221         while(!(parent == null ||
1222                 parent instanceof java.awt.Frame ||
1223                 parent instanceof java.awt.Dialog)) {
1224             parent = AWTAccessor.getComponentAccessor().getParent(parent);
1225         }
1226 
1227 /*      FIX ME - FIX ME need to implement InputMethods
1228     if (parent instanceof java.awt.Frame ||
1229         parent instanceof java.awt.Dialog) {
1230         if (add)
1231         ((MInputMethodControl)parent.getPeer()).addTextComponent((MComponentPeer)this);
1232         else
1233         ((MInputMethodControl)parent.getPeer()).removeTextComponent((MComponentPeer)this);
1234     }
1235 */
1236     }
1237 
1238     /**
1239      * Returns true if this event is disabled and shouldn't be processed by window
1240      * Currently if target component is disabled the following event will be disabled on window:
1241      * ButtonPress, ButtonRelease, KeyPress, KeyRelease, EnterNotify, LeaveNotify, MotionNotify
1242      */
1243     protected boolean isEventDisabled(XEvent e) {
1244         if (enableLog.isLoggable(PlatformLogger.Level.FINEST)) {
1245             enableLog.finest("Component is {1}, checking for disabled event {0}", e, (isEnabled()?"enabled":"disable"));
1246         }
1247         if (!isEnabled()) {
1248             switch (e.get_type()) {
1249               case XConstants.ButtonPress:
1250               case XConstants.ButtonRelease:
1251               case XConstants.KeyPress:
1252               case XConstants.KeyRelease:
1253               case XConstants.EnterNotify:
1254               case XConstants.LeaveNotify:
1255               case XConstants.MotionNotify:
1256                   if (enableLog.isLoggable(PlatformLogger.Level.FINER)) {
1257                       enableLog.finer("Event {0} is disable", e);
1258                   }
1259                   return true;
1260             }
1261         }
1262         switch(e.get_type()) {
1263           case XConstants.MapNotify:
1264           case XConstants.UnmapNotify:
1265               return true;
1266         }
1267         return super.isEventDisabled(e);
1268     }
1269 
1270     Color getPeerBackground() {
1271         return background;
1272     }
1273 
1274     Color getPeerForeground() {
1275         return foreground;
1276     }
1277 
1278     Font getPeerFont() {
1279         return font;
1280     }
1281 
1282     Dimension getPeerSize() {
1283         return new Dimension(width,height);
1284     }
1285 
1286     public void setBoundsOperation(int operation) {
1287         synchronized(getStateLock()) {
1288             if (boundsOperation == DEFAULT_OPERATION) {
1289                 boundsOperation = operation;
1290             } else if (operation == RESET_OPERATION) {
1291                 boundsOperation = DEFAULT_OPERATION;
1292             }
1293         }
1294     }
1295 
1296     static String operationToString(int operation) {
1297         switch (operation) {
1298           case SET_LOCATION:
1299               return "SET_LOCATION";
1300           case SET_SIZE:
1301               return "SET_SIZE";
1302           case SET_CLIENT_SIZE:
1303               return "SET_CLIENT_SIZE";
1304           default:
1305           case SET_BOUNDS:
1306               return "SET_BOUNDS";
1307         }
1308     }
1309 
1310     /**
1311      * Lowers this component at the bottom of the above HW peer. If the above parameter
1312      * is null then the method places this component at the top of the Z-order.
1313      */
1314     public void setZOrder(ComponentPeer above) {
1315         long aboveWindow = (above != null) ? ((XComponentPeer)above).getWindow() : 0;
1316 
1317         XToolkit.awtLock();
1318         try{
1319             XlibWrapper.SetZOrder(XToolkit.getDisplay(), getWindow(), aboveWindow);
1320         }finally{
1321             XToolkit.awtUnlock();
1322         }
1323     }
1324 
1325     private void addTree(Collection order, Set set, Container cont) {
1326         for (int i = 0; i < cont.getComponentCount(); i++) {
1327             Component comp = cont.getComponent(i);
1328             ComponentPeer peer = comp.getPeer();
1329             if (peer instanceof XComponentPeer) {
1330                 Long window = Long.valueOf(((XComponentPeer)peer).getWindow());
1331                 if (!set.contains(window)) {
1332                     set.add(window);
1333                     order.add(window);
1334                 }
1335             } else if (comp instanceof Container) {
1336                 // It is lightweight container, it might contain heavyweight components attached to this
1337                 // peer
1338                 addTree(order, set, (Container)comp);
1339             }
1340         }
1341     }
1342 
1343     /****** DropTargetPeer implementation ********************/
1344 
1345     public void addDropTarget(DropTarget dt) {
1346         Component comp = target;
1347         while(!(comp == null || comp instanceof Window)) {
1348             comp = comp.getParent();
1349         }
1350 
1351         if (comp instanceof Window) {
1352             XWindowPeer wpeer = (XWindowPeer)(comp.getPeer());
1353             if (wpeer != null) {
1354                 wpeer.addDropTarget();
1355             }
1356         }
1357     }
1358 
1359     public void removeDropTarget(DropTarget dt) {
1360         Component comp = target;
1361         while(!(comp == null || comp instanceof Window)) {
1362             comp = comp.getParent();
1363         }
1364 
1365         if (comp instanceof Window) {
1366             XWindowPeer wpeer = (XWindowPeer)(comp.getPeer());
1367             if (wpeer != null) {
1368                 wpeer.removeDropTarget();
1369             }
1370         }
1371     }
1372 
1373     /**
1374      * Applies the shape to the X-window.
1375      * @since 1.7
1376      */
1377     public void applyShape(Region shape) {
1378         if (XlibUtil.isShapingSupported()) {
1379             if (shapeLog.isLoggable(PlatformLogger.Level.FINER)) {
1380                 shapeLog.finer(
1381                         "*** INFO: Setting shape: PEER: " + this
1382                         + "; WINDOW: " + getWindow()
1383                         + "; TARGET: " + target
1384                         + "; SHAPE: " + shape);
1385             }
1386             XToolkit.awtLock();
1387             try {
1388                 if (shape != null) {
1389                     XlibWrapper.SetRectangularShape(
1390                             XToolkit.getDisplay(),
1391                             getWindow(),
1392                             shape.getLoX(), shape.getLoY(),
1393                             shape.getHiX(), shape.getHiY(),
1394                             (shape.isRectangular() ? null : shape)
1395                             );
1396                 } else {
1397                     XlibWrapper.SetRectangularShape(
1398                             XToolkit.getDisplay(),
1399                             getWindow(),
1400                             0, 0,
1401                             0, 0,
1402                             null
1403                             );
1404                 }
1405             } finally {
1406                 XToolkit.awtUnlock();
1407             }
1408         } else {
1409             if (shapeLog.isLoggable(PlatformLogger.Level.FINER)) {
1410                 shapeLog.finer("*** WARNING: Shaping is NOT supported!");
1411             }
1412         }
1413     }
1414 
1415     public boolean updateGraphicsData(GraphicsConfiguration gc) {
1416         int oldVisual = -1, newVisual = -1;
1417 
1418         if (graphicsConfig != null) {
1419             oldVisual = graphicsConfig.getVisual();
1420         }
1421         if (gc != null && gc instanceof X11GraphicsConfig) {
1422             newVisual = ((X11GraphicsConfig)gc).getVisual();
1423         }
1424 
1425         // If the new visual differs from the old one, the peer must be
1426         // recreated because X11 does not allow changing the visual on the fly.
1427         // So we even skip the initGraphicsConfiguration() call.
1428         // The initial assignment should happen though, hence the != -1 thing.
1429         if (oldVisual != -1 && oldVisual != newVisual) {
1430             return true;
1431         }
1432 
1433         initGraphicsConfiguration();
1434         doValidateSurface();
1435         return false;
1436     }
1437 }