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 
  34 /**
  35  * The AWTAccessor utility class.
  36  * The main purpose of this class is to enable accessing
  37  * private and package-private fields of classes from
  38  * different classes/packages. See sun.misc.SharedSecretes
  39  * for another example.
  40  */
  41 public final class AWTAccessor {
  42 
  43     private static final Unsafe unsafe = Unsafe.getUnsafe();
  44 
  45     /*
  46      * We don't need any objects of this class.
  47      * It's rather a collection of static methods
  48      * and interfaces.
  49      */
  50     private AWTAccessor() {
  51     }
  52 
  53     /*
  54      * An interface of accessor for the java.awt.Component class.
  55      */
  56     public interface ComponentAccessor {
  57         /*
  58          * Sets whether the native background erase for a component
  59          * has been disabled via SunToolkit.disableBackgroundErase().
  60          */
  61         void setBackgroundEraseDisabled(Component comp, boolean disabled);
  62         /*
  63          * Indicates whether the native background erase for a
  64          * component has been disabled via
  65          * SunToolkit.disableBackgroundErase().
  66          */
  67         boolean getBackgroundEraseDisabled(Component comp);
  68         /*
  69          *
  70          * Gets the bounds of this component in the form of a
  71          * <code>Rectangle</code> object. The bounds specify this
  72          * component's width, height, and location relative to
  73          * its parent.
  74          */
  75         Rectangle getBounds(Component comp);
  76         /*
  77          * Sets the shape of a lw component to cut out from hw components.
  78          *
  79          * See 6797587, 6776743, 6768307, and 6768332 for details
  80          */
  81         void setMixingCutoutShape(Component comp, Shape shape);
  82 
  83         /**
  84          * Sets GraphicsConfiguration value for the component.
  85          */
  86         void setGraphicsConfiguration(Component comp, GraphicsConfiguration gc);
  87         /*
  88          * Requests focus to the component.
  89          */
  90         boolean requestFocus(Component comp, CausedFocusEvent.Cause cause);
  91         /*
  92          * Determines if the component can gain focus.
  93          */
  94         boolean canBeFocusOwner(Component comp);
  95 
  96         /**
  97          * Returns whether the component is visible without invoking
  98          * any client code.
  99          */
 100         boolean isVisible_NoClientCode(Component comp);
 101     }
 102 
 103     /*
 104      * An interface of accessor for java.awt.Window class.
 105      */
 106     public interface WindowAccessor {
 107         /*
 108          * Get opacity level of the given window.
 109          */
 110         float getOpacity(Window window);
 111         /*
 112          * Set opacity level to the given window.
 113          */
 114         void setOpacity(Window window, float opacity);
 115         /*
 116          * Get a shape assigned to the given window.
 117          */
 118         Shape getShape(Window window);
 119         /*
 120          * Set a shape to the given window.
 121          */
 122         void setShape(Window window, Shape shape);
 123         /*
 124          * Identify whether the given window is opaque (true)
 125          *  or translucent (false).
 126          */
 127         boolean isOpaque(Window window);
 128         /*
 129          * Set the opaque preoperty to the given window.
 130          */
 131         void setOpaque(Window window, boolean isOpaque);
 132         /*
 133          * Update the image of a non-opaque (translucent) window.
 134          */
 135         void updateWindow(Window window);
 136 
 137         /** Get the size of the security warning.
 138          */
 139         Dimension getSecurityWarningSize(Window w);
 140 
 141         /**
 142          * Set the size of the security warning.
 143          */
 144         void setSecurityWarningSize(Window w, int width, int height);
 145 
 146         /** Set the position of the security warning.
 147          */
 148         void setSecurityWarningPosition(Window w, Point2D point,
 149                 float alignmentX, float alignmentY);
 150 
 151         /** Request to recalculate the new position of the security warning for
 152          * the given window size/location as reported by the native system.
 153          */
 154         Point2D calculateSecurityWarningPosition(Window window,
 155                 double x, double y, double w, double h);
 156         /**
 157          * Indicates whether the window has been packed via pack().
 158          */
 159         boolean isPacked(Window window);
 160     }
 161 
 162     /*
 163      * An accessor for the AWTEvent class.
 164      */
 165     public interface AWTEventAccessor {
 166         /*
 167          *
 168          * Sets the flag on this AWTEvent indicating that it was
 169          * generated by the system.
 170          */
 171         void setSystemGenerated(AWTEvent ev);
 172         /*
 173          *
 174          * Indicates whether this AWTEvent was generated by the system.
 175          */
 176         boolean isSystemGenerated(AWTEvent ev);
 177     }
 178 
 179     /*
 180      * An accessor for the java.awt.Frame class.
 181      */
 182     public interface FrameAccessor {
 183         /*
 184          * Sets the state of this frame.
 185          */
 186         void setExtendedState(Frame frame, int state);
 187         /*
 188          * Gets the state of this frame.
 189          */
 190        int getExtendedState(Frame frame);
 191     }
 192 
 193     /*
 194      * An interface of accessor for the java.awt.KeyboardFocusManager class.
 195      */
 196     public interface KeyboardFocusManagerAccessor {
 197         /*
 198          * Indicates whether the native implementation should
 199          * proceed with a pending focus request for the heavyweight.
 200          */
 201         int shouldNativelyFocusHeavyweight(Component heavyweight,
 202                                            Component descendant,
 203                                            boolean temporary,
 204                                            boolean focusedWindowChangeAllowed,
 205                                            long time,
 206                                            CausedFocusEvent.Cause cause);
 207         /*
 208          * Delivers focus for the lightweight descendant of the heavyweight
 209          * synchronously.
 210          */
 211         boolean processSynchronousLightweightTransfer(Component heavyweight,
 212                                                       Component descendant,
 213                                                       boolean temporary,
 214                                                       boolean focusedWindowChangeAllowed,
 215                                                       long time);
 216         /*
 217          * Removes the last focus request for the heavyweight from the queue.
 218          */
 219         void removeLastFocusRequest(Component heavyweight);
 220     }
 221 
 222     /*
 223      * The java.awt.Component class accessor object.
 224      */
 225     private static ComponentAccessor componentAccessor;
 226 
 227     /*
 228      * The java.awt.Window class accessor object.
 229      */
 230     private static WindowAccessor windowAccessor;
 231 
 232     /*
 233      * The java.awt.AWTEvent class accessor object.
 234      */
 235     private static AWTEventAccessor awtEventAccessor;
 236 
 237     /*
 238      * The java.awt.Frame class accessor object.
 239      */
 240     private static FrameAccessor frameAccessor;
 241 
 242     /*
 243      * The java.awt.KeyboardFocusManager class accessor object.
 244      */
 245     private static KeyboardFocusManagerAccessor kfmAccessor;
 246 
 247     /*
 248      * Set an accessor object for the java.awt.Component class.
 249      */
 250     public static void setComponentAccessor(ComponentAccessor ca) {
 251         componentAccessor = ca;
 252     }
 253 
 254     /*
 255      * Retrieve the accessor object for the java.awt.Window class.
 256      */
 257     public static ComponentAccessor getComponentAccessor() {
 258         if (componentAccessor == null) {
 259             unsafe.ensureClassInitialized(Component.class);
 260         }
 261 
 262         return componentAccessor;
 263     }
 264 
 265     /*
 266      * Set an accessor object for the java.awt.Window class.
 267      */
 268     public static void setWindowAccessor(WindowAccessor wa) {
 269         windowAccessor = wa;
 270     }
 271 
 272     /*
 273      * Retrieve the accessor object for the java.awt.Window class.
 274      */
 275     public static WindowAccessor getWindowAccessor() {
 276         if (windowAccessor == null) {
 277             unsafe.ensureClassInitialized(Window.class);
 278         }
 279         return windowAccessor;
 280     }
 281 
 282     /*
 283      * Set an accessor object for the java.awt.AWTEvent class.
 284      */
 285     public static void setAWTEventAccessor(AWTEventAccessor aea) {
 286         awtEventAccessor = aea;
 287     }
 288 
 289     /*
 290      * Retrieve the accessor object for the java.awt.AWTEvent class.
 291      */
 292     public static AWTEventAccessor getAWTEventAccessor() {
 293         return awtEventAccessor;
 294     }
 295 
 296     /*
 297      * Set an accessor object for the java.awt.Frame class.
 298      */
 299     public static void setFrameAccessor(FrameAccessor fa) {
 300         frameAccessor = fa;
 301     }
 302 
 303     /*
 304      * Retrieve the accessor object for the java.awt.Frame class.
 305      */
 306     public static FrameAccessor getFrameAccessor() {
 307         if (frameAccessor == null) {
 308             unsafe.ensureClassInitialized(Frame.class);
 309         }
 310         return frameAccessor;
 311     }
 312 
 313     /*
 314      * Set an accessor object for the java.awt.KeyboardFocusManager class.
 315      */
 316     public static void setKeyboardFocusManagerAccessor(KeyboardFocusManagerAccessor kfma) {
 317         kfmAccessor = kfma;
 318     }
 319 
 320     /*
 321      * Retrieve the accessor object for the java.awt.KeyboardFocusManager class.
 322      */
 323     public static KeyboardFocusManagerAccessor getKeyboardFocusManagerAccessor() {
 324         if (kfmAccessor == null) {
 325             unsafe.ensureClassInitialized(KeyboardFocusManager.class);
 326         }
 327         return kfmAccessor;
 328     }
 329 }