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