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