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