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      * An accessor for the DirectoryDialog class
 420      */
 421     public interface DirectoryDialogAccessor {
 422         /*
 423          * Sets the directory the user selects
 424          */
 425         void setDirectory(DirectoryDialog fileDialog, String directory);
 426     }
 427     
 428     /*
 429      * The java.awt.Component class accessor object.
 430      */
 431     private static ComponentAccessor componentAccessor;
 432 
 433     /*
 434      * The java.awt.Window class accessor object.
 435      */
 436     private static WindowAccessor windowAccessor;
 437 
 438     /*
 439      * The java.awt.AWTEvent class accessor object.
 440      */
 441     private static AWTEventAccessor awtEventAccessor;
 442 
 443     /*
 444      * The java.awt.Frame class accessor object.
 445      */
 446     private static FrameAccessor frameAccessor;
 447 
 448     /*
 449      * The java.awt.KeyboardFocusManager class accessor object.
 450      */
 451     private static KeyboardFocusManagerAccessor kfmAccessor;
 452 
 453     /*
 454      * The java.awt.MenuComponent class accessor object.
 455      */
 456     private static MenuComponentAccessor menuComponentAccessor;
 457 
 458     /*
 459      * The java.awt.EventQueue class accessor object.
 460      */
 461     private static EventQueueAccessor eventQueueAccessor;
 462 
 463     /*
 464      * The java.awt.PopupMenu class accessor object.
 465      */
 466     private static PopupMenuAccessor popupMenuAccessor;
 467 
 468     /*
 469      * The java.awt.FileDialog class accessor object.
 470      */
 471     private static FileDialogAccessor fileDialogAccessor;
 472     
 473     /*
 474      * The java.awt.DirectoryDialog class accessor object.
 475      */
 476     private static DirectoryDialogAccessor directoryDialogAccessor;
 477 
 478     /*
 479      * Set an accessor object for the java.awt.Component class.
 480      */
 481     public static void setComponentAccessor(ComponentAccessor ca) {
 482         componentAccessor = ca;
 483     }
 484 
 485     /*
 486      * Retrieve the accessor object for the java.awt.Window class.
 487      */
 488     public static ComponentAccessor getComponentAccessor() {
 489         if (componentAccessor == null) {
 490             unsafe.ensureClassInitialized(Component.class);
 491         }
 492 
 493         return componentAccessor;
 494     }
 495 
 496     /*
 497      * Set an accessor object for the java.awt.Window class.
 498      */
 499     public static void setWindowAccessor(WindowAccessor wa) {
 500         windowAccessor = wa;
 501     }
 502 
 503     /*
 504      * Retrieve the accessor object for the java.awt.Window class.
 505      */
 506     public static WindowAccessor getWindowAccessor() {
 507         if (windowAccessor == null) {
 508             unsafe.ensureClassInitialized(Window.class);
 509         }
 510         return windowAccessor;
 511     }
 512 
 513     /*
 514      * Set an accessor object for the java.awt.AWTEvent class.
 515      */
 516     public static void setAWTEventAccessor(AWTEventAccessor aea) {
 517         awtEventAccessor = aea;
 518     }
 519 
 520     /*
 521      * Retrieve the accessor object for the java.awt.AWTEvent class.
 522      */
 523     public static AWTEventAccessor getAWTEventAccessor() {
 524         if (awtEventAccessor == null) {
 525             unsafe.ensureClassInitialized(AWTEvent.class);
 526         }
 527         return awtEventAccessor;
 528     }
 529 
 530     /*
 531      * Set an accessor object for the java.awt.Frame class.
 532      */
 533     public static void setFrameAccessor(FrameAccessor fa) {
 534         frameAccessor = fa;
 535     }
 536 
 537     /*
 538      * Retrieve the accessor object for the java.awt.Frame class.
 539      */
 540     public static FrameAccessor getFrameAccessor() {
 541         if (frameAccessor == null) {
 542             unsafe.ensureClassInitialized(Frame.class);
 543         }
 544         return frameAccessor;
 545     }
 546 
 547     /*
 548      * Set an accessor object for the java.awt.KeyboardFocusManager class.
 549      */
 550     public static void setKeyboardFocusManagerAccessor(KeyboardFocusManagerAccessor kfma) {
 551         kfmAccessor = kfma;
 552     }
 553 
 554     /*
 555      * Retrieve the accessor object for the java.awt.KeyboardFocusManager class.
 556      */
 557     public static KeyboardFocusManagerAccessor getKeyboardFocusManagerAccessor() {
 558         if (kfmAccessor == null) {
 559             unsafe.ensureClassInitialized(KeyboardFocusManager.class);
 560         }
 561         return kfmAccessor;
 562     }
 563 
 564     /*
 565      * Set an accessor object for the java.awt.MenuComponent class.
 566      */
 567     public static void setMenuComponentAccessor(MenuComponentAccessor mca) {
 568         menuComponentAccessor = mca;
 569     }
 570 
 571     /*
 572      * Retrieve the accessor object for the java.awt.MenuComponent class.
 573      */
 574     public static MenuComponentAccessor getMenuComponentAccessor() {
 575         if (menuComponentAccessor == null) {
 576             unsafe.ensureClassInitialized(MenuComponent.class);
 577         }
 578         return menuComponentAccessor;
 579     }
 580 
 581     /*
 582      * Set an accessor object for the java.awt.EventQueue class.
 583      */
 584     public static void setEventQueueAccessor(EventQueueAccessor eqa) {
 585         eventQueueAccessor = eqa;
 586     }
 587 
 588     /*
 589      * Retrieve the accessor object for the java.awt.EventQueue class.
 590      */
 591     public static EventQueueAccessor getEventQueueAccessor() {
 592         if (eventQueueAccessor == null) {
 593             unsafe.ensureClassInitialized(EventQueue.class);
 594         }
 595         return eventQueueAccessor;
 596     }
 597 
 598     /*
 599      * Set an accessor object for the java.awt.PopupMenu class.
 600      */
 601     public static void setPopupMenuAccessor(PopupMenuAccessor pma) {
 602         popupMenuAccessor = pma;
 603     }
 604 
 605     /*
 606      * Retrieve the accessor object for the java.awt.PopupMenu class.
 607      */
 608     public static PopupMenuAccessor getPopupMenuAccessor() {
 609         if (popupMenuAccessor == null) {
 610             unsafe.ensureClassInitialized(PopupMenu.class);
 611         }
 612         return popupMenuAccessor;
 613     }
 614 
 615     /*
 616      * Set an accessor object for the java.awt.FileDialog class.
 617      */
 618     public static void setFileDialogAccessor(FileDialogAccessor fda) {
 619         fileDialogAccessor = fda;
 620     }
 621 
 622     /*
 623      * Retrieve the accessor object for the java.awt.FileDialog class.
 624      */
 625     public static FileDialogAccessor getFileDialogAccessor() {
 626         if (fileDialogAccessor == null) {
 627             unsafe.ensureClassInitialized(FileDialog.class);
 628         }
 629         return fileDialogAccessor;
 630     }
 631 
 632     /*
 633      * Set an accessor object for the java.awt.DirectoryDialog class.
 634      */
 635     public static void setDirectoryDialogAccessor(DirectoryDialogAccessor dda) {
 636         directoryDialogAccessor = dda;
 637     }
 638 
 639     /*
 640      * Retrieve the accessor object for the java.awt.DirectoryDialog class.
 641      */
 642     public static DirectoryDialogAccessor getDirectoryDialogAccessor() {
 643         if (directoryDialogAccessor == null) {
 644             unsafe.ensureClassInitialized(DirectoryDialog.class);
 645         }
 646         return directoryDialogAccessor;
 647     }
 648 }