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