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.event.InputEvent;
  30 import java.awt.geom.Point2D;
  31 import java.awt.image.BufferedImage;
  32 
  33 import sun.misc.Unsafe;
  34 import java.awt.peer.ComponentPeer;
  35 
  36 import java.security.AccessController;
  37 import java.security.AccessControlContext;
  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     /*
 405      * An accessor for the MenuComponent class.
 406      */
 407     public interface MenuComponentAccessor {
 408         /**
 409          * Returns the appContext of the menu component.
 410          */
 411         AppContext getAppContext(MenuComponent menuComp);
 412 
 413         /**
 414          * Sets the appContext of the menu component.
 415          */
 416         void setAppContext(MenuComponent menuComp, AppContext appContext);
 417 
 418         /**
 419          * Returns the menu container of the menu component
 420          */
 421         MenuContainer getParent(MenuComponent menuComp);
 422     }
 423 
 424     /*
 425      * An accessor for the EventQueue class
 426      */
 427     public interface EventQueueAccessor {
 428         /*
 429          * Gets the event dispatch thread.
 430          */
 431         Thread getDispatchThread(EventQueue eventQueue);
 432         /*
 433          * Checks if the current thread is EDT for the given EQ.
 434          */
 435         public boolean isDispatchThreadImpl(EventQueue eventQueue);
 436     }
 437 
 438     /*
 439      * An accessor for the PopupMenu class
 440      */
 441     public interface PopupMenuAccessor {
 442         /*
 443          * Returns whether the popup menu is attached to a tray
 444          */
 445         boolean isTrayIconPopup(PopupMenu popupMenu);
 446     }
 447 
 448     /*
 449      * An accessor for the FileDialog class
 450      */
 451     public interface FileDialogAccessor {
 452         /*
 453          * Sets the files the user selects
 454          */
 455         void setFiles(FileDialog fileDialog, String directory, String files[]);
 456 
 457         /*
 458          * Sets the file the user selects
 459          */
 460         void setFile(FileDialog fileDialog, String file);
 461 
 462         /*
 463          * Sets the directory the user selects
 464          */
 465         void setDirectory(FileDialog fileDialog, String directory);
 466 
 467         /*
 468          * Returns whether the file dialog allows the multiple file selection.
 469          */
 470         boolean isMultipleMode(FileDialog fileDialog);
 471     }
 472 
 473     /*
 474      * Accessor instances are initialized in the static initializers of
 475      * corresponding AWT classes by using setters defined below.
 476      */
 477     private static ComponentAccessor componentAccessor;
 478     private static ContainerAccessor containerAccessor;
 479     private static WindowAccessor windowAccessor;
 480     private static AWTEventAccessor awtEventAccessor;
 481     private static InputEventAccessor inputEventAccessor;
 482     private static FrameAccessor frameAccessor;
 483     private static KeyboardFocusManagerAccessor kfmAccessor;
 484     private static MenuComponentAccessor menuComponentAccessor;
 485     private static EventQueueAccessor eventQueueAccessor;
 486     private static PopupMenuAccessor popupMenuAccessor;
 487     private static FileDialogAccessor fileDialogAccessor;
 488 
 489     /*
 490      * Set an accessor object for the java.awt.Component class.
 491      */
 492     public static void setComponentAccessor(ComponentAccessor ca) {
 493         componentAccessor = ca;
 494     }
 495 
 496     /*
 497      * Retrieve the accessor object for the java.awt.Component class.
 498      */
 499     public static ComponentAccessor getComponentAccessor() {
 500         if (componentAccessor == null) {
 501             unsafe.ensureClassInitialized(Component.class);
 502         }
 503 
 504         return componentAccessor;
 505     }
 506 
 507     /*
 508      * Set an accessor object for the java.awt.Container class.
 509      */
 510     public static void setContainerAccessor(ContainerAccessor ca) {
 511         containerAccessor = ca;
 512     }
 513 
 514     /*
 515      * Retrieve the accessor object for the java.awt.Container class.
 516      */
 517     public static ContainerAccessor getContainerAccessor() {
 518         if (containerAccessor == null) {
 519             unsafe.ensureClassInitialized(Container.class);
 520         }
 521 
 522         return containerAccessor;
 523     }
 524 
 525     /*
 526      * Set an accessor object for the java.awt.Window class.
 527      */
 528     public static void setWindowAccessor(WindowAccessor wa) {
 529         windowAccessor = wa;
 530     }
 531 
 532     /*
 533      * Retrieve the accessor object for the java.awt.Window class.
 534      */
 535     public static WindowAccessor getWindowAccessor() {
 536         if (windowAccessor == null) {
 537             unsafe.ensureClassInitialized(Window.class);
 538         }
 539         return windowAccessor;
 540     }
 541 
 542     /*
 543      * Set an accessor object for the java.awt.AWTEvent class.
 544      */
 545     public static void setAWTEventAccessor(AWTEventAccessor aea) {
 546         awtEventAccessor = aea;
 547     }
 548 
 549     /*
 550      * Retrieve the accessor object for the java.awt.AWTEvent class.
 551      */
 552     public static AWTEventAccessor getAWTEventAccessor() {
 553         if (awtEventAccessor == null) {
 554             unsafe.ensureClassInitialized(AWTEvent.class);
 555         }
 556         return awtEventAccessor;
 557     }
 558 
 559     /*
 560      * Set an accessor object for the java.awt.event.InputEvent class.
 561      */
 562     public static void setInputEventAccessor(InputEventAccessor iea) {
 563         inputEventAccessor = iea;
 564     }
 565 
 566     /*
 567      * Retrieve the accessor object for the java.awt.event.InputEvent class.
 568      */
 569     public static InputEventAccessor getInputEventAccessor() {
 570         if (inputEventAccessor == null) {
 571             unsafe.ensureClassInitialized(InputEvent.class);
 572         }
 573         return inputEventAccessor;
 574     }
 575 
 576     /*
 577      * Set an accessor object for the java.awt.Frame class.
 578      */
 579     public static void setFrameAccessor(FrameAccessor fa) {
 580         frameAccessor = fa;
 581     }
 582 
 583     /*
 584      * Retrieve the accessor object for the java.awt.Frame class.
 585      */
 586     public static FrameAccessor getFrameAccessor() {
 587         if (frameAccessor == null) {
 588             unsafe.ensureClassInitialized(Frame.class);
 589         }
 590         return frameAccessor;
 591     }
 592 
 593     /*
 594      * Set an accessor object for the java.awt.KeyboardFocusManager class.
 595      */
 596     public static void setKeyboardFocusManagerAccessor(KeyboardFocusManagerAccessor kfma) {
 597         kfmAccessor = kfma;
 598     }
 599 
 600     /*
 601      * Retrieve the accessor object for the java.awt.KeyboardFocusManager class.
 602      */
 603     public static KeyboardFocusManagerAccessor getKeyboardFocusManagerAccessor() {
 604         if (kfmAccessor == null) {
 605             unsafe.ensureClassInitialized(KeyboardFocusManager.class);
 606         }
 607         return kfmAccessor;
 608     }
 609 
 610     /*
 611      * Set an accessor object for the java.awt.MenuComponent class.
 612      */
 613     public static void setMenuComponentAccessor(MenuComponentAccessor mca) {
 614         menuComponentAccessor = mca;
 615     }
 616 
 617     /*
 618      * Retrieve the accessor object for the java.awt.MenuComponent class.
 619      */
 620     public static MenuComponentAccessor getMenuComponentAccessor() {
 621         if (menuComponentAccessor == null) {
 622             unsafe.ensureClassInitialized(MenuComponent.class);
 623         }
 624         return menuComponentAccessor;
 625     }
 626 
 627     /*
 628      * Set an accessor object for the java.awt.EventQueue class.
 629      */
 630     public static void setEventQueueAccessor(EventQueueAccessor eqa) {
 631         eventQueueAccessor = eqa;
 632     }
 633 
 634     /*
 635      * Retrieve the accessor object for the java.awt.EventQueue class.
 636      */
 637     public static EventQueueAccessor getEventQueueAccessor() {
 638         if (eventQueueAccessor == null) {
 639             unsafe.ensureClassInitialized(EventQueue.class);
 640         }
 641         return eventQueueAccessor;
 642     }
 643 
 644     /*
 645      * Set an accessor object for the java.awt.PopupMenu class.
 646      */
 647     public static void setPopupMenuAccessor(PopupMenuAccessor pma) {
 648         popupMenuAccessor = pma;
 649     }
 650 
 651     /*
 652      * Retrieve the accessor object for the java.awt.PopupMenu class.
 653      */
 654     public static PopupMenuAccessor getPopupMenuAccessor() {
 655         if (popupMenuAccessor == null) {
 656             unsafe.ensureClassInitialized(PopupMenu.class);
 657         }
 658         return popupMenuAccessor;
 659     }
 660 
 661     /*
 662      * Set an accessor object for the java.awt.FileDialog class.
 663      */
 664     public static void setFileDialogAccessor(FileDialogAccessor fda) {
 665         fileDialogAccessor = fda;
 666     }
 667 
 668     /*
 669      * Retrieve the accessor object for the java.awt.FileDialog class.
 670      */
 671     public static FileDialogAccessor getFileDialogAccessor() {
 672         if (fileDialogAccessor == null) {
 673             unsafe.ensureClassInitialized(FileDialog.class);
 674         }
 675         return fileDialogAccessor;
 676     }
 677 
 678 }