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