1 /*
   2  * Copyright (c) 2008, 2011, 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 
  26 package sun.awt;
  27 
  28 import java.awt.*;
  29 import java.awt.KeyboardFocusManager;
  30 import java.awt.event.*;
  31 import java.awt.geom.Point2D;
  32 import java.awt.image.BufferedImage;
  33 
  34 import sun.misc.Unsafe;
  35 import java.awt.peer.ComponentPeer;
  36 
  37 import java.security.AccessController;
  38 import java.security.AccessControlContext;
  39 
  40 import java.io.File;
  41 
  42 /**
  43  * The AWTAccessor utility class.
  44  * The main purpose of this class is to enable accessing
  45  * private and package-private fields of classes from
  46  * different classes/packages. See sun.misc.SharedSecretes
  47  * for another example.
  48  */
  49 public final class AWTAccessor {
  50 
  51     private static final Unsafe unsafe = Unsafe.getUnsafe();
  52 
  53     /*
  54      * We don't need any objects of this class.
  55      * It's rather a collection of static methods
  56      * and interfaces.
  57      */
  58     private AWTAccessor() {
  59     }
  60 
  61     /*
  62      * An interface of accessor for the java.awt.Component class.
  63      */
  64     public interface ComponentAccessor {
  65         /*
  66          * Sets whether the native background erase for a component
  67          * has been disabled via SunToolkit.disableBackgroundErase().
  68          */
  69         void setBackgroundEraseDisabled(Component comp, boolean disabled);
  70         /*
  71          * Indicates whether the native background erase for a
  72          * component has been disabled via
  73          * SunToolkit.disableBackgroundErase().
  74          */
  75         boolean getBackgroundEraseDisabled(Component comp);
  76         /*
  77          *
  78          * Gets the bounds of this component in the form of a
  79          * <code>Rectangle</code> object. The bounds specify this
  80          * component's width, height, and location relative to
  81          * its parent.
  82          */
  83         Rectangle getBounds(Component comp);
  84         /*
  85          * Sets the shape of a lw component to cut out from hw components.
  86          *
  87          * See 6797587, 6776743, 6768307, and 6768332 for details
  88          */
  89         void setMixingCutoutShape(Component comp, Shape shape);
  90 
  91         /**
  92          * Sets GraphicsConfiguration value for the component.
  93          */
  94         void setGraphicsConfiguration(Component comp, GraphicsConfiguration gc);
  95         /*
  96          * Requests focus to the component.
  97          */
  98         boolean requestFocus(Component comp, CausedFocusEvent.Cause cause);
  99         /*
 100          * Determines if the component can gain focus.
 101          */
 102         boolean canBeFocusOwner(Component comp);
 103 
 104         /**
 105          * Returns whether the component is visible without invoking
 106          * any client code.
 107          */
 108         boolean isVisible(Component comp);
 109 
 110         /**
 111          * Sets the RequestFocusController.
 112          */
 113         void setRequestFocusController(RequestFocusController requestController);
 114 
 115         /**
 116          * Returns the appContext of the component.
 117          */
 118         AppContext getAppContext(Component comp);
 119 
 120         /**
 121          * Sets the appContext of the component.
 122          */
 123         void setAppContext(Component comp, AppContext appContext);
 124 
 125         /**
 126          * Returns the parent of the component.
 127          */
 128         Container getParent(Component comp);
 129 
 130         /**
 131          * Sets the parent of the component to the specified parent.
 132          */
 133         void setParent(Component comp, Container parent);
 134 
 135         /**
 136          * Resizes the component to the specified width and height.
 137          */
 138         void setSize(Component comp, int width, int height);
 139 
 140         /**
 141          * Returns the location of the component.
 142          */
 143         Point getLocation(Component comp);
 144 
 145         /**
 146          * Moves the component to the new location.
 147          */
 148         void setLocation(Component comp, int x, int y);
 149 
 150         /**
 151          * Determines whether this component is enabled.
 152          */
 153         boolean isEnabled(Component comp);
 154 
 155         /**
 156          * Determines whether this component is displayable.
 157          */
 158         boolean isDisplayable(Component comp);
 159 
 160         /**
 161          * Gets the cursor set in the component.
 162          */
 163         Cursor getCursor(Component comp);
 164 
 165         /**
 166          * Returns the peer of the component.
 167          */
 168         ComponentPeer getPeer(Component comp);
 169 
 170         /**
 171          * Sets the peer of the component to the specified peer.
 172          */
 173         void setPeer(Component comp, ComponentPeer peer);
 174 
 175         /**
 176          * Determines whether this component is lightweight.
 177          */
 178         boolean isLightweight(Component comp);
 179 
 180         /**
 181          * Returns whether or not paint messages received from
 182          * the operating system should be ignored.
 183          */
 184         boolean getIgnoreRepaint(Component comp);
 185 
 186         /**
 187          * Returns the width of the component.
 188          */
 189         int getWidth(Component comp);
 190 
 191         /**
 192          * Returns the height of the component.
 193          */
 194         int getHeight(Component comp);
 195 
 196         /**
 197          * Returns the x coordinate of the component.
 198          */
 199         int getX(Component comp);
 200 
 201         /**
 202          * Returns the y coordinate of the component.
 203          */
 204         int getY(Component comp);
 205 
 206         /**
 207          * Gets the foreground color of this component.
 208          */
 209         Color getForeground(Component comp);
 210 
 211         /**
 212          * Gets the background color of this component.
 213          */
 214         Color getBackground(Component comp);
 215 
 216         /**
 217          * Sets the background of this component to the specified color.
 218          */
 219         void setBackground(Component comp, Color background);
 220 
 221         /**
 222          * Gets the font of the component.
 223          */
 224         Font getFont(Component comp);
 225 
 226         /**
 227          * Processes events occurring on this component.
 228          */
 229         void processEvent(Component comp, AWTEvent e);
 230 
 231 
 232         /*
 233          * Returns the acc this component was constructed with.
 234          */
 235         AccessControlContext getAccessControlContext(Component comp);
 236 
 237     }
 238 
 239     /*
 240      * An interface of accessor for the java.awt.Container class.
 241      */
 242     public interface ContainerAccessor {
 243         /**
 244          * Validates the container unconditionally.
 245          */
 246         void validateUnconditionally(Container cont);
 247     }
 248 
 249     /*
 250      * An interface of accessor for java.awt.Window class.
 251      */
 252     public interface WindowAccessor {
 253         /*
 254          * Get opacity level of the given window.
 255          */
 256         float getOpacity(Window window);
 257         /*
 258          * Set opacity level to the given window.
 259          */
 260         void setOpacity(Window window, float opacity);
 261         /*
 262          * Get a shape assigned to the given window.
 263          */
 264         Shape getShape(Window window);
 265         /*
 266          * Set a shape to the given window.
 267          */
 268         void setShape(Window window, Shape shape);
 269         /*
 270          * Set the opaque preoperty to the given window.
 271          */
 272         void setOpaque(Window window, boolean isOpaque);
 273         /*
 274          * Update the image of a non-opaque (translucent) window.
 275          */
 276         void updateWindow(Window window);
 277 
 278         /** Get the size of the security warning.
 279          */
 280         Dimension getSecurityWarningSize(Window w);
 281 
 282         /**
 283          * Set the size of the security warning.
 284          */
 285         void setSecurityWarningSize(Window w, int width, int height);
 286 
 287         /** Set the position of the security warning.
 288          */
 289         void setSecurityWarningPosition(Window w, Point2D point,
 290                 float alignmentX, float alignmentY);
 291 
 292         /** Request to recalculate the new position of the security warning for
 293          * the given window size/location as reported by the native system.
 294          */
 295         Point2D calculateSecurityWarningPosition(Window window,
 296                 double x, double y, double w, double h);
 297 
 298         /** Sets the synchronous status of focus requests on lightweight
 299          * components in the specified window to the specified value.
 300          */
 301         void setLWRequestStatus(Window changed, boolean status);
 302 
 303         /**
 304          * Indicates whether this window should receive focus on subsequently
 305          * being shown, or being moved to the front.
 306          */
 307         boolean isAutoRequestFocus(Window w);
 308 
 309         /**
 310          * Indicates whether the specified window is an utility window for TrayIcon.
 311          */
 312         boolean isTrayIconWindow(Window w);
 313 
 314         /**
 315          * Marks the specified window as an utility window for TrayIcon.
 316          */
 317         void setTrayIconWindow(Window w, boolean isTrayIconWindow);
 318     }
 319 
 320     /*
 321      * An accessor for the AWTEvent class.
 322      */
 323     public interface AWTEventAccessor {
 324         /**
 325          * Marks the event as posted.
 326          */
 327         void setPosted(AWTEvent ev);
 328 
 329         /**
 330          * Sets the flag on this AWTEvent indicating that it was
 331          * generated by the system.
 332          */
 333         void setSystemGenerated(AWTEvent ev);
 334 
 335         /**
 336          * Indicates whether this AWTEvent was generated by the system.
 337          */
 338         boolean isSystemGenerated(AWTEvent ev);
 339 
 340 
 341         /*
 342          * Returns the acc this event was constructed with.
 343          */
 344         AccessControlContext getAccessControlContext(AWTEvent ev);
 345 
 346     }
 347 
 348     public interface InputEventAccessor {
 349         /*
 350          * Accessor for InputEvent.getButtonDownMasks()
 351          */
 352         int[] getButtonDownMasks();
 353     }
 354 
 355     /*
 356      * An accessor for the java.awt.Frame class.
 357      */
 358     public interface FrameAccessor {
 359         /*
 360          * Sets the state of this frame.
 361          */
 362         void setExtendedState(Frame frame, int state);
 363         /*
 364          * Gets the state of this frame.
 365          */
 366        int getExtendedState(Frame frame);
 367         /*
 368          * Gets the maximized bounds of this frame.
 369          */
 370        Rectangle getMaximizedBounds(Frame frame);
 371     }
 372 
 373     /*
 374      * An interface of accessor for the java.awt.KeyboardFocusManager class.
 375      */
 376     public interface KeyboardFocusManagerAccessor {
 377         /*
 378          * Indicates whether the native implementation should
 379          * proceed with a pending focus request for the heavyweight.
 380          */
 381         int shouldNativelyFocusHeavyweight(Component heavyweight,
 382                                            Component descendant,
 383                                            boolean temporary,
 384                                            boolean focusedWindowChangeAllowed,
 385                                            long time,
 386                                            CausedFocusEvent.Cause cause);
 387         /*
 388          * Delivers focus for the lightweight descendant of the heavyweight
 389          * synchronously.
 390          */
 391         boolean processSynchronousLightweightTransfer(Component heavyweight,
 392                                                       Component descendant,
 393                                                       boolean temporary,
 394                                                       boolean focusedWindowChangeAllowed,
 395                                                       long time);
 396         /*
 397          * Removes the last focus request for the heavyweight from the queue.
 398          */
 399         void removeLastFocusRequest(Component heavyweight);
 400 
 401         /*
 402          * Sets the most recent focus owner in the window.
 403          */
 404         void setMostRecentFocusOwner(Window window, Component component);
 405 
 406         /*
 407          * Returns current KFM of the specified AppContext.
 408          */
 409         KeyboardFocusManager getCurrentKeyboardFocusManager(AppContext ctx);
 410     }
 411 
 412     /*
 413      * An accessor for the MenuComponent class.
 414      */
 415     public interface MenuComponentAccessor {
 416         /**
 417          * Returns the appContext of the menu component.
 418          */
 419         AppContext getAppContext(MenuComponent menuComp);
 420 
 421         /**
 422          * Sets the appContext of the menu component.
 423          */
 424         void setAppContext(MenuComponent menuComp, AppContext appContext);
 425 
 426         /**
 427          * Returns the menu container of the menu component
 428          */
 429         MenuContainer getParent(MenuComponent menuComp);
 430     }
 431 
 432     /*
 433      * An accessor for the EventQueue class
 434      */
 435     public interface EventQueueAccessor {
 436         /*
 437          * Gets the event dispatch thread.
 438          */
 439         Thread getDispatchThread(EventQueue eventQueue);
 440         /*
 441          * Checks if the current thread is EDT for the given EQ.
 442          */
 443         public boolean isDispatchThreadImpl(EventQueue eventQueue);
 444     }
 445 
 446     /*
 447      * An accessor for the PopupMenu class
 448      */
 449     public interface PopupMenuAccessor {
 450         /*
 451          * Returns whether the popup menu is attached to a tray
 452          */
 453         boolean isTrayIconPopup(PopupMenu popupMenu);
 454     }
 455 
 456     /*
 457      * An accessor for the FileDialog class
 458      */
 459     public interface FileDialogAccessor {
 460         /*
 461          * Sets the files the user selects
 462          */
 463         void setFiles(FileDialog fileDialog, File files[]);
 464 
 465         /*
 466          * Sets the file the user selects
 467          */
 468         void setFile(FileDialog fileDialog, String file);
 469 
 470         /*
 471          * Sets the directory the user selects
 472          */
 473         void setDirectory(FileDialog fileDialog, String directory);
 474 
 475         /*
 476          * Returns whether the file dialog allows the multiple file selection.
 477          */
 478         boolean isMultipleMode(FileDialog fileDialog);
 479     }
 480 
 481     /**
 482      * An accessor for the InvocationEvent class
 483      */
 484     public interface InvocationEventAccessor {
 485         /**
 486          * Dispose an InvocationEvent (cancel it).
 487          */
 488         void dispose(InvocationEvent ie);
 489     }
 490 
 491     /*
 492      * Accessor instances are initialized in the static initializers of
 493      * corresponding AWT classes by using setters defined below.
 494      */
 495     private static ComponentAccessor componentAccessor;
 496     private static ContainerAccessor containerAccessor;
 497     private static WindowAccessor windowAccessor;
 498     private static AWTEventAccessor awtEventAccessor;
 499     private static InputEventAccessor inputEventAccessor;
 500     private static FrameAccessor frameAccessor;
 501     private static KeyboardFocusManagerAccessor kfmAccessor;
 502     private static MenuComponentAccessor menuComponentAccessor;
 503     private static EventQueueAccessor eventQueueAccessor;
 504     private static PopupMenuAccessor popupMenuAccessor;
 505     private static FileDialogAccessor fileDialogAccessor;
 506     private static InvocationEventAccessor invocationEventAccessor;
 507 
 508     /*
 509      * Set an accessor object for the java.awt.Component class.
 510      */
 511     public static void setComponentAccessor(ComponentAccessor ca) {
 512         componentAccessor = ca;
 513     }
 514 
 515     /*
 516      * Retrieve the accessor object for the java.awt.Component class.
 517      */
 518     public static ComponentAccessor getComponentAccessor() {
 519         if (componentAccessor == null) {
 520             unsafe.ensureClassInitialized(Component.class);
 521         }
 522 
 523         return componentAccessor;
 524     }
 525 
 526     /*
 527      * Set an accessor object for the java.awt.Container class.
 528      */
 529     public static void setContainerAccessor(ContainerAccessor ca) {
 530         containerAccessor = ca;
 531     }
 532 
 533     /*
 534      * Retrieve the accessor object for the java.awt.Container class.
 535      */
 536     public static ContainerAccessor getContainerAccessor() {
 537         if (containerAccessor == null) {
 538             unsafe.ensureClassInitialized(Container.class);
 539         }
 540 
 541         return containerAccessor;
 542     }
 543 
 544     /*
 545      * Set an accessor object for the java.awt.Window class.
 546      */
 547     public static void setWindowAccessor(WindowAccessor wa) {
 548         windowAccessor = wa;
 549     }
 550 
 551     /*
 552      * Retrieve the accessor object for the java.awt.Window class.
 553      */
 554     public static WindowAccessor getWindowAccessor() {
 555         if (windowAccessor == null) {
 556             unsafe.ensureClassInitialized(Window.class);
 557         }
 558         return windowAccessor;
 559     }
 560 
 561     /*
 562      * Set an accessor object for the java.awt.AWTEvent class.
 563      */
 564     public static void setAWTEventAccessor(AWTEventAccessor aea) {
 565         awtEventAccessor = aea;
 566     }
 567 
 568     /*
 569      * Retrieve the accessor object for the java.awt.AWTEvent class.
 570      */
 571     public static AWTEventAccessor getAWTEventAccessor() {
 572         if (awtEventAccessor == null) {
 573             unsafe.ensureClassInitialized(AWTEvent.class);
 574         }
 575         return awtEventAccessor;
 576     }
 577 
 578     /*
 579      * Set an accessor object for the java.awt.event.InputEvent class.
 580      */
 581     public static void setInputEventAccessor(InputEventAccessor iea) {
 582         inputEventAccessor = iea;
 583     }
 584 
 585     /*
 586      * Retrieve the accessor object for the java.awt.event.InputEvent class.
 587      */
 588     public static InputEventAccessor getInputEventAccessor() {
 589         if (inputEventAccessor == null) {
 590             unsafe.ensureClassInitialized(InputEvent.class);
 591         }
 592         return inputEventAccessor;
 593     }
 594 
 595     /*
 596      * Set an accessor object for the java.awt.Frame class.
 597      */
 598     public static void setFrameAccessor(FrameAccessor fa) {
 599         frameAccessor = fa;
 600     }
 601 
 602     /*
 603      * Retrieve the accessor object for the java.awt.Frame class.
 604      */
 605     public static FrameAccessor getFrameAccessor() {
 606         if (frameAccessor == null) {
 607             unsafe.ensureClassInitialized(Frame.class);
 608         }
 609         return frameAccessor;
 610     }
 611 
 612     /*
 613      * Set an accessor object for the java.awt.KeyboardFocusManager class.
 614      */
 615     public static void setKeyboardFocusManagerAccessor(KeyboardFocusManagerAccessor kfma) {
 616         kfmAccessor = kfma;
 617     }
 618 
 619     /*
 620      * Retrieve the accessor object for the java.awt.KeyboardFocusManager class.
 621      */
 622     public static KeyboardFocusManagerAccessor getKeyboardFocusManagerAccessor() {
 623         if (kfmAccessor == null) {
 624             unsafe.ensureClassInitialized(KeyboardFocusManager.class);
 625         }
 626         return kfmAccessor;
 627     }
 628 
 629     /*
 630      * Set an accessor object for the java.awt.MenuComponent class.
 631      */
 632     public static void setMenuComponentAccessor(MenuComponentAccessor mca) {
 633         menuComponentAccessor = mca;
 634     }
 635 
 636     /*
 637      * Retrieve the accessor object for the java.awt.MenuComponent class.
 638      */
 639     public static MenuComponentAccessor getMenuComponentAccessor() {
 640         if (menuComponentAccessor == null) {
 641             unsafe.ensureClassInitialized(MenuComponent.class);
 642         }
 643         return menuComponentAccessor;
 644     }
 645 
 646     /*
 647      * Set an accessor object for the java.awt.EventQueue class.
 648      */
 649     public static void setEventQueueAccessor(EventQueueAccessor eqa) {
 650         eventQueueAccessor = eqa;
 651     }
 652 
 653     /*
 654      * Retrieve the accessor object for the java.awt.EventQueue class.
 655      */
 656     public static EventQueueAccessor getEventQueueAccessor() {
 657         if (eventQueueAccessor == null) {
 658             unsafe.ensureClassInitialized(EventQueue.class);
 659         }
 660         return eventQueueAccessor;
 661     }
 662 
 663     /*
 664      * Set an accessor object for the java.awt.PopupMenu class.
 665      */
 666     public static void setPopupMenuAccessor(PopupMenuAccessor pma) {
 667         popupMenuAccessor = pma;
 668     }
 669 
 670     /*
 671      * Retrieve the accessor object for the java.awt.PopupMenu class.
 672      */
 673     public static PopupMenuAccessor getPopupMenuAccessor() {
 674         if (popupMenuAccessor == null) {
 675             unsafe.ensureClassInitialized(PopupMenu.class);
 676         }
 677         return popupMenuAccessor;
 678     }
 679 
 680     /*
 681      * Set an accessor object for the java.awt.FileDialog class.
 682      */
 683     public static void setFileDialogAccessor(FileDialogAccessor fda) {
 684         fileDialogAccessor = fda;
 685     }
 686 
 687     /*
 688      * Retrieve the accessor object for the java.awt.FileDialog class.
 689      */
 690     public static FileDialogAccessor getFileDialogAccessor() {
 691         if (fileDialogAccessor == null) {
 692             unsafe.ensureClassInitialized(FileDialog.class);
 693         }
 694         return fileDialogAccessor;
 695     }
 696 
 697     /**
 698      * Set an accessor object for the java.awt.event.InvocationEvent class.
 699      */
 700     public static void setInvocationEventAccessor(InvocationEventAccessor iea) {
 701         invocationEventAccessor = iea;
 702     }
 703 
 704     /**
 705      * Retrieve the accessor object for the java.awt.event.InvocationEvent class.
 706      */
 707     public static InvocationEventAccessor getInvocationEventAccessor() {
 708         if (invocationEventAccessor == null) {
 709             unsafe.ensureClassInitialized(InvocationEvent.class);
 710         }
 711         return invocationEventAccessor;
 712     }
 713 
 714 }