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 
 158     /*
 159      * An accessor for the AWTEvent class.
 160      */
 161     public interface AWTEventAccessor {
 162         /*
 163          *
 164          * Sets the flag on this AWTEvent indicating that it was
 165          * generated by the system.
 166          */
 167         void setSystemGenerated(AWTEvent ev);
 168         /*
 169          *
 170          * Indicates whether this AWTEvent was generated by the system.
 171          */
 172         boolean isSystemGenerated(AWTEvent ev);
 173     }
 174 
 175     /*
 176      * An accessor for the java.awt.Frame class.
 177      */
 178     public interface FrameAccessor {
 179         /*
 180          * Sets the state of this frame.
 181          */
 182         void setExtendedState(Frame frame, int state);
 183         /*
 184          * Gets the state of this frame.
 185          */
 186        int getExtendedState(Frame frame);
 187     }
 188 
 189     /*
 190      * An interface of accessor for the java.awt.KeyboardFocusManager class.
 191      */
 192     public interface KeyboardFocusManagerAccessor {
 193         /*
 194          * Indicates whether the native implementation should
 195          * proceed with a pending focus request for the heavyweight.
 196          */
 197         int shouldNativelyFocusHeavyweight(Component heavyweight,
 198                                            Component descendant,
 199                                            boolean temporary,
 200                                            boolean focusedWindowChangeAllowed,
 201                                            long time,
 202                                            CausedFocusEvent.Cause cause);
 203         /*
 204          * Delivers focus for the lightweight descendant of the heavyweight
 205          * synchronously.
 206          */
 207         boolean processSynchronousLightweightTransfer(Component heavyweight,
 208                                                       Component descendant,
 209                                                       boolean temporary,
 210                                                       boolean focusedWindowChangeAllowed,
 211                                                       long time);
 212         /*
 213          * Removes the last focus request for the heavyweight from the queue.
 214          */
 215         void removeLastFocusRequest(Component heavyweight);
 216     }
 217 
 218     /*
 219      * The java.awt.Component class accessor object.
 220      */
 221     private static ComponentAccessor componentAccessor;
 222 
 223     /*
 224      * The java.awt.Window class accessor object.
 225      */
 226     private static WindowAccessor windowAccessor;
 227 
 228     /*
 229      * The java.awt.AWTEvent class accessor object.
 230      */
 231     private static AWTEventAccessor awtEventAccessor;
 232 
 233     /*
 234      * The java.awt.Frame class accessor object.
 235      */
 236     private static FrameAccessor frameAccessor;
 237 
 238     /*
 239      * The java.awt.KeyboardFocusManager class accessor object.
 240      */
 241     private static KeyboardFocusManagerAccessor kfmAccessor;
 242 
 243     /*
 244      * Set an accessor object for the java.awt.Component class.
 245      */
 246     public static void setComponentAccessor(ComponentAccessor ca) {
 247         componentAccessor = ca;
 248     }
 249 
 250     /*
 251      * Retrieve the accessor object for the java.awt.Window class.
 252      */
 253     public static ComponentAccessor getComponentAccessor() {
 254         if (componentAccessor == null) {
 255             unsafe.ensureClassInitialized(Component.class);
 256         }
 257 
 258         return componentAccessor;
 259     }
 260 
 261     /*
 262      * Set an accessor object for the java.awt.Window class.
 263      */
 264     public static void setWindowAccessor(WindowAccessor wa) {
 265         windowAccessor = wa;
 266     }
 267 
 268     /*
 269      * Retrieve the accessor object for the java.awt.Window class.
 270      */
 271     public static WindowAccessor getWindowAccessor() {
 272         if (windowAccessor == null) {
 273             unsafe.ensureClassInitialized(Window.class);
 274         }
 275         return windowAccessor;
 276     }
 277 
 278     /*
 279      * Set an accessor object for the java.awt.AWTEvent class.
 280      */
 281     public static void setAWTEventAccessor(AWTEventAccessor aea) {
 282         awtEventAccessor = aea;
 283     }
 284 
 285     /*
 286      * Retrieve the accessor object for the java.awt.AWTEvent class.
 287      */
 288     public static AWTEventAccessor getAWTEventAccessor() {
 289         return awtEventAccessor;
 290     }
 291 
 292     /*
 293      * Set an accessor object for the java.awt.Frame class.
 294      */
 295     public static void setFrameAccessor(FrameAccessor fa) {
 296         frameAccessor = fa;
 297     }
 298 
 299     /*
 300      * Retrieve the accessor object for the java.awt.Frame class.
 301      */
 302     public static FrameAccessor getFrameAccessor() {
 303         if (frameAccessor == null) {
 304             unsafe.ensureClassInitialized(Frame.class);
 305         }
 306         return frameAccessor;
 307     }
 308 
 309     /*
 310      * Set an accessor object for the java.awt.KeyboardFocusManager class.
 311      */
 312     public static void setKeyboardFocusManagerAccessor(KeyboardFocusManagerAccessor kfma) {
 313         kfmAccessor = kfma;
 314     }
 315 
 316     /*
 317      * Retrieve the accessor object for the java.awt.KeyboardFocusManager class.
 318      */
 319     public static KeyboardFocusManagerAccessor getKeyboardFocusManagerAccessor() {
 320         if (kfmAccessor == null) {
 321             unsafe.ensureClassInitialized(KeyboardFocusManager.class);
 322         }
 323         return kfmAccessor;
 324     }
 325 }