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.InputEvent;
  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 SequencedEventAccessor class
 483      */
 484     public interface SequencedEventAccessor {
 485         /*
 486          * Returns the nested event.
 487          */
 488         AWTEvent getNested(AWTEvent sequencedEvent);
 489 
 490         /*
 491          * Returns true if the event is an instances of SequencedEvent.
 492          */
 493         boolean isSequencedEvent(AWTEvent event);
 494     }
 495 
 496     /*
 497      * Accessor instances are initialized in the static initializers of
 498      * corresponding AWT classes by using setters defined below.
 499      */
 500     private static ComponentAccessor componentAccessor;
 501     private static ContainerAccessor containerAccessor;
 502     private static WindowAccessor windowAccessor;
 503     private static AWTEventAccessor awtEventAccessor;
 504     private static InputEventAccessor inputEventAccessor;
 505     private static FrameAccessor frameAccessor;
 506     private static KeyboardFocusManagerAccessor kfmAccessor;
 507     private static MenuComponentAccessor menuComponentAccessor;
 508     private static EventQueueAccessor eventQueueAccessor;
 509     private static PopupMenuAccessor popupMenuAccessor;
 510     private static FileDialogAccessor fileDialogAccessor;
 511     private static SequencedEventAccessor sequencedEventAccessor;
 512 
 513     /*
 514      * Set an accessor object for the java.awt.Component class.
 515      */
 516     public static void setComponentAccessor(ComponentAccessor ca) {
 517         componentAccessor = ca;
 518     }
 519 
 520     /*
 521      * Retrieve the accessor object for the java.awt.Component class.
 522      */
 523     public static ComponentAccessor getComponentAccessor() {
 524         if (componentAccessor == null) {
 525             unsafe.ensureClassInitialized(Component.class);
 526         }
 527 
 528         return componentAccessor;
 529     }
 530 
 531     /*
 532      * Set an accessor object for the java.awt.Container class.
 533      */
 534     public static void setContainerAccessor(ContainerAccessor ca) {
 535         containerAccessor = ca;
 536     }
 537 
 538     /*
 539      * Retrieve the accessor object for the java.awt.Container class.
 540      */
 541     public static ContainerAccessor getContainerAccessor() {
 542         if (containerAccessor == null) {
 543             unsafe.ensureClassInitialized(Container.class);
 544         }
 545 
 546         return containerAccessor;
 547     }
 548 
 549     /*
 550      * Set an accessor object for the java.awt.Window class.
 551      */
 552     public static void setWindowAccessor(WindowAccessor wa) {
 553         windowAccessor = wa;
 554     }
 555 
 556     /*
 557      * Retrieve the accessor object for the java.awt.Window class.
 558      */
 559     public static WindowAccessor getWindowAccessor() {
 560         if (windowAccessor == null) {
 561             unsafe.ensureClassInitialized(Window.class);
 562         }
 563         return windowAccessor;
 564     }
 565 
 566     /*
 567      * Set an accessor object for the java.awt.AWTEvent class.
 568      */
 569     public static void setAWTEventAccessor(AWTEventAccessor aea) {
 570         awtEventAccessor = aea;
 571     }
 572 
 573     /*
 574      * Retrieve the accessor object for the java.awt.AWTEvent class.
 575      */
 576     public static AWTEventAccessor getAWTEventAccessor() {
 577         if (awtEventAccessor == null) {
 578             unsafe.ensureClassInitialized(AWTEvent.class);
 579         }
 580         return awtEventAccessor;
 581     }
 582 
 583     /*
 584      * Set an accessor object for the java.awt.event.InputEvent class.
 585      */
 586     public static void setInputEventAccessor(InputEventAccessor iea) {
 587         inputEventAccessor = iea;
 588     }
 589 
 590     /*
 591      * Retrieve the accessor object for the java.awt.event.InputEvent class.
 592      */
 593     public static InputEventAccessor getInputEventAccessor() {
 594         if (inputEventAccessor == null) {
 595             unsafe.ensureClassInitialized(InputEvent.class);
 596         }
 597         return inputEventAccessor;
 598     }
 599 
 600     /*
 601      * Set an accessor object for the java.awt.Frame class.
 602      */
 603     public static void setFrameAccessor(FrameAccessor fa) {
 604         frameAccessor = fa;
 605     }
 606 
 607     /*
 608      * Retrieve the accessor object for the java.awt.Frame class.
 609      */
 610     public static FrameAccessor getFrameAccessor() {
 611         if (frameAccessor == null) {
 612             unsafe.ensureClassInitialized(Frame.class);
 613         }
 614         return frameAccessor;
 615     }
 616 
 617     /*
 618      * Set an accessor object for the java.awt.KeyboardFocusManager class.
 619      */
 620     public static void setKeyboardFocusManagerAccessor(KeyboardFocusManagerAccessor kfma) {
 621         kfmAccessor = kfma;
 622     }
 623 
 624     /*
 625      * Retrieve the accessor object for the java.awt.KeyboardFocusManager class.
 626      */
 627     public static KeyboardFocusManagerAccessor getKeyboardFocusManagerAccessor() {
 628         if (kfmAccessor == null) {
 629             unsafe.ensureClassInitialized(KeyboardFocusManager.class);
 630         }
 631         return kfmAccessor;
 632     }
 633 
 634     /*
 635      * Set an accessor object for the java.awt.MenuComponent class.
 636      */
 637     public static void setMenuComponentAccessor(MenuComponentAccessor mca) {
 638         menuComponentAccessor = mca;
 639     }
 640 
 641     /*
 642      * Retrieve the accessor object for the java.awt.MenuComponent class.
 643      */
 644     public static MenuComponentAccessor getMenuComponentAccessor() {
 645         if (menuComponentAccessor == null) {
 646             unsafe.ensureClassInitialized(MenuComponent.class);
 647         }
 648         return menuComponentAccessor;
 649     }
 650 
 651     /*
 652      * Set an accessor object for the java.awt.EventQueue class.
 653      */
 654     public static void setEventQueueAccessor(EventQueueAccessor eqa) {
 655         eventQueueAccessor = eqa;
 656     }
 657 
 658     /*
 659      * Retrieve the accessor object for the java.awt.EventQueue class.
 660      */
 661     public static EventQueueAccessor getEventQueueAccessor() {
 662         if (eventQueueAccessor == null) {
 663             unsafe.ensureClassInitialized(EventQueue.class);
 664         }
 665         return eventQueueAccessor;
 666     }
 667 
 668     /*
 669      * Set an accessor object for the java.awt.PopupMenu class.
 670      */
 671     public static void setPopupMenuAccessor(PopupMenuAccessor pma) {
 672         popupMenuAccessor = pma;
 673     }
 674 
 675     /*
 676      * Retrieve the accessor object for the java.awt.PopupMenu class.
 677      */
 678     public static PopupMenuAccessor getPopupMenuAccessor() {
 679         if (popupMenuAccessor == null) {
 680             unsafe.ensureClassInitialized(PopupMenu.class);
 681         }
 682         return popupMenuAccessor;
 683     }
 684 
 685     /*
 686      * Set an accessor object for the java.awt.FileDialog class.
 687      */
 688     public static void setFileDialogAccessor(FileDialogAccessor fda) {
 689         fileDialogAccessor = fda;
 690     }
 691 
 692     /*
 693      * Retrieve the accessor object for the java.awt.FileDialog class.
 694      */
 695     public static FileDialogAccessor getFileDialogAccessor() {
 696         if (fileDialogAccessor == null) {
 697             unsafe.ensureClassInitialized(FileDialog.class);
 698         }
 699         return fileDialogAccessor;
 700     }
 701 
 702     /*
 703      * Set an accessor object for the java.awt.SequencedEvent class.
 704      */
 705     public static void setSequencedEventAccessor(SequencedEventAccessor sea) {
 706         sequencedEventAccessor = sea;
 707     }
 708 
 709     /*
 710      * Get the accessor object for the java.awt.SequencedEvent class.
 711      */
 712     public static SequencedEventAccessor getSequencedEventAccessor() {
 713         // The class is not public. So we can't ensure it's initialized.
 714         // Null returned value means it's not initialized
 715         // (so not a single instance of the event has been created).
 716         return sequencedEventAccessor;
 717     }
 718 }