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      * 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 
 497     /*
 498      * Set an accessor object for the java.awt.Component class.
 499      */
 500     public static void setComponentAccessor(ComponentAccessor ca) {
 501         componentAccessor = ca;
 502     }
 503 
 504     /*
 505      * Retrieve the accessor object for the java.awt.Component class.
 506      */
 507     public static ComponentAccessor getComponentAccessor() {
 508         if (componentAccessor == null) {
 509             unsafe.ensureClassInitialized(Component.class);
 510         }
 511 
 512         return componentAccessor;
 513     }
 514 
 515     /*
 516      * Set an accessor object for the java.awt.Container class.
 517      */
 518     public static void setContainerAccessor(ContainerAccessor ca) {
 519         containerAccessor = ca;
 520     }
 521 
 522     /*
 523      * Retrieve the accessor object for the java.awt.Container class.
 524      */
 525     public static ContainerAccessor getContainerAccessor() {
 526         if (containerAccessor == null) {
 527             unsafe.ensureClassInitialized(Container.class);
 528         }
 529 
 530         return containerAccessor;
 531     }
 532 
 533     /*
 534      * Set an accessor object for the java.awt.Window class.
 535      */
 536     public static void setWindowAccessor(WindowAccessor wa) {
 537         windowAccessor = wa;
 538     }
 539 
 540     /*
 541      * Retrieve the accessor object for the java.awt.Window class.
 542      */
 543     public static WindowAccessor getWindowAccessor() {
 544         if (windowAccessor == null) {
 545             unsafe.ensureClassInitialized(Window.class);
 546         }
 547         return windowAccessor;
 548     }
 549 
 550     /*
 551      * Set an accessor object for the java.awt.AWTEvent class.
 552      */
 553     public static void setAWTEventAccessor(AWTEventAccessor aea) {
 554         awtEventAccessor = aea;
 555     }
 556 
 557     /*
 558      * Retrieve the accessor object for the java.awt.AWTEvent class.
 559      */
 560     public static AWTEventAccessor getAWTEventAccessor() {
 561         if (awtEventAccessor == null) {
 562             unsafe.ensureClassInitialized(AWTEvent.class);
 563         }
 564         return awtEventAccessor;
 565     }
 566 
 567     /*
 568      * Set an accessor object for the java.awt.event.InputEvent class.
 569      */
 570     public static void setInputEventAccessor(InputEventAccessor iea) {
 571         inputEventAccessor = iea;
 572     }
 573 
 574     /*
 575      * Retrieve the accessor object for the java.awt.event.InputEvent class.
 576      */
 577     public static InputEventAccessor getInputEventAccessor() {
 578         if (inputEventAccessor == null) {
 579             unsafe.ensureClassInitialized(InputEvent.class);
 580         }
 581         return inputEventAccessor;
 582     }
 583 
 584     /*
 585      * Set an accessor object for the java.awt.Frame class.
 586      */
 587     public static void setFrameAccessor(FrameAccessor fa) {
 588         frameAccessor = fa;
 589     }
 590 
 591     /*
 592      * Retrieve the accessor object for the java.awt.Frame class.
 593      */
 594     public static FrameAccessor getFrameAccessor() {
 595         if (frameAccessor == null) {
 596             unsafe.ensureClassInitialized(Frame.class);
 597         }
 598         return frameAccessor;
 599     }
 600 
 601     /*
 602      * Set an accessor object for the java.awt.KeyboardFocusManager class.
 603      */
 604     public static void setKeyboardFocusManagerAccessor(KeyboardFocusManagerAccessor kfma) {
 605         kfmAccessor = kfma;
 606     }
 607 
 608     /*
 609      * Retrieve the accessor object for the java.awt.KeyboardFocusManager class.
 610      */
 611     public static KeyboardFocusManagerAccessor getKeyboardFocusManagerAccessor() {
 612         if (kfmAccessor == null) {
 613             unsafe.ensureClassInitialized(KeyboardFocusManager.class);
 614         }
 615         return kfmAccessor;
 616     }
 617 
 618     /*
 619      * Set an accessor object for the java.awt.MenuComponent class.
 620      */
 621     public static void setMenuComponentAccessor(MenuComponentAccessor mca) {
 622         menuComponentAccessor = mca;
 623     }
 624 
 625     /*
 626      * Retrieve the accessor object for the java.awt.MenuComponent class.
 627      */
 628     public static MenuComponentAccessor getMenuComponentAccessor() {
 629         if (menuComponentAccessor == null) {
 630             unsafe.ensureClassInitialized(MenuComponent.class);
 631         }
 632         return menuComponentAccessor;
 633     }
 634 
 635     /*
 636      * Set an accessor object for the java.awt.EventQueue class.
 637      */
 638     public static void setEventQueueAccessor(EventQueueAccessor eqa) {
 639         eventQueueAccessor = eqa;
 640     }
 641 
 642     /*
 643      * Retrieve the accessor object for the java.awt.EventQueue class.
 644      */
 645     public static EventQueueAccessor getEventQueueAccessor() {
 646         if (eventQueueAccessor == null) {
 647             unsafe.ensureClassInitialized(EventQueue.class);
 648         }
 649         return eventQueueAccessor;
 650     }
 651 
 652     /*
 653      * Set an accessor object for the java.awt.PopupMenu class.
 654      */
 655     public static void setPopupMenuAccessor(PopupMenuAccessor pma) {
 656         popupMenuAccessor = pma;
 657     }
 658 
 659     /*
 660      * Retrieve the accessor object for the java.awt.PopupMenu class.
 661      */
 662     public static PopupMenuAccessor getPopupMenuAccessor() {
 663         if (popupMenuAccessor == null) {
 664             unsafe.ensureClassInitialized(PopupMenu.class);
 665         }
 666         return popupMenuAccessor;
 667     }
 668 
 669     /*
 670      * Set an accessor object for the java.awt.FileDialog class.
 671      */
 672     public static void setFileDialogAccessor(FileDialogAccessor fda) {
 673         fileDialogAccessor = fda;
 674     }
 675 
 676     /*
 677      * Retrieve the accessor object for the java.awt.FileDialog class.
 678      */
 679     public static FileDialogAccessor getFileDialogAccessor() {
 680         if (fileDialogAccessor == null) {
 681             unsafe.ensureClassInitialized(FileDialog.class);
 682         }
 683         return fileDialogAccessor;
 684     }
 685 
 686 }