1 /*
   2  * Copyright (c) 2009, 2017, 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.swing;
  27 
  28 import jdk.internal.misc.Unsafe;
  29 
  30 import java.awt.*;
  31 import javax.swing.*;
  32 
  33 import javax.swing.text.JTextComponent;
  34 
  35 /**
  36  * The SwingAccessor 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 SwingAccessor {
  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 SwingAccessor() {
  51     }
  52 
  53     /**
  54      * An accessor for the JComponent class.
  55      */
  56     public interface JComponentAccessor {
  57 
  58         boolean getFlag(JComponent comp, int aFlag);
  59 
  60         void compWriteObjectNotify(JComponent comp);
  61     }
  62 
  63     /**
  64      * An accessor for the JTextComponent class.
  65      * Note that we intentionally introduce the JTextComponentAccessor,
  66      * and not the JComponentAccessor because the needed methods
  67      * aren't override methods.
  68      */
  69     public interface JTextComponentAccessor {
  70 
  71         /**
  72          * Calculates a custom drop location for the text component,
  73          * representing where a drop at the given point should insert data.
  74          */
  75         TransferHandler.DropLocation dropLocationForPoint(JTextComponent textComp, Point p);
  76 
  77         /**
  78          * Called to set or clear the drop location during a DnD operation.
  79          */
  80         Object setDropLocation(JTextComponent textComp, TransferHandler.DropLocation location,
  81                                Object state, boolean forDrop);
  82     }
  83 
  84     /**
  85      * An accessor for the JLightweightFrame class.
  86      */
  87     public interface JLightweightFrameAccessor {
  88         /**
  89          * Notifies the JLightweight frame that it needs to update a cursor
  90          */
  91         void updateCursor(JLightweightFrame frame);
  92 
  93         /**
  94          * Notifies the JLightweight frame embed to JavaFX stage about
  95          * its native toplevel window change
  96          */
  97 
  98         void overrideNativeWindowHandle(JLightweightFrame frame, long ptr);
  99     }
 100 
 101     /**
 102      * An accessor for the UIDefaults class.
 103      */
 104     public interface UIDefaultsAccessor {
 105         /**
 106          * Adds a resource bundle to the list of resource bundles.
 107          */
 108         void addInternalBundle(UIDefaults uiDefaults, String bundleName);
 109     }
 110 
 111     /**
 112      * An accessor for the RepaintManager class.
 113      */
 114     public interface RepaintManagerAccessor {
 115         void addRepaintListener(RepaintManager rm, SwingUtilities2.RepaintListener l);
 116         void removeRepaintListener(RepaintManager rm, SwingUtilities2.RepaintListener l);
 117     }
 118 
 119     /**
 120      * An accessor for PopupFactory class.
 121      */
 122     public interface PopupFactoryAccessor {
 123         Popup getHeavyWeightPopup(PopupFactory factory, Component owner, Component contents,
 124                                   int ownerX, int ownerY);
 125     }
 126 
 127     /*
 128      * An accessor for the KeyStroke class
 129      */
 130     public interface KeyStrokeAccessor {
 131 
 132         KeyStroke create();
 133     }
 134 
 135     /**
 136      * The javax.swing.JComponent class accessor object.
 137      */
 138     private static JComponentAccessor jComponentAccessor;
 139 
 140     /**
 141      * Set an accessor object for the javax.swing.JComponent class.
 142      */
 143     public static void setJComponentAccessor(JComponentAccessor jCompAccessor) {
 144         jComponentAccessor = jCompAccessor;
 145     }
 146 
 147     /**
 148      * Retrieve the accessor object for the javax.swing.JComponent class.
 149      */
 150     public static JComponentAccessor getJComponentAccessor() {
 151         if (jComponentAccessor == null) {
 152             unsafe.ensureClassInitialized(JComponent.class);
 153         }
 154 
 155         return jComponentAccessor;
 156     }
 157 
 158     /**
 159      * The javax.swing.text.JTextComponent class accessor object.
 160      */
 161     private static JTextComponentAccessor jtextComponentAccessor;
 162 
 163     /**
 164      * Set an accessor object for the javax.swing.text.JTextComponent class.
 165      */
 166     public static void setJTextComponentAccessor(JTextComponentAccessor jtca) {
 167          jtextComponentAccessor = jtca;
 168     }
 169 
 170     /**
 171      * Retrieve the accessor object for the javax.swing.text.JTextComponent class.
 172      */
 173     public static JTextComponentAccessor getJTextComponentAccessor() {
 174         if (jtextComponentAccessor == null) {
 175             unsafe.ensureClassInitialized(JTextComponent.class);
 176         }
 177 
 178         return jtextComponentAccessor;
 179     }
 180 
 181     /**
 182      * The JLightweightFrame class accessor object
 183      */
 184     private static JLightweightFrameAccessor jLightweightFrameAccessor;
 185 
 186     /**
 187      * Set an accessor object for the JLightweightFrame class.
 188      */
 189     public static void setJLightweightFrameAccessor(JLightweightFrameAccessor accessor) {
 190         jLightweightFrameAccessor = accessor;
 191     }
 192 
 193     /**
 194      * Retrieve the accessor object for the JLightweightFrame class
 195      */
 196     public static JLightweightFrameAccessor getJLightweightFrameAccessor() {
 197         if (jLightweightFrameAccessor == null) {
 198             unsafe.ensureClassInitialized(JLightweightFrame.class);
 199         }
 200         return jLightweightFrameAccessor;
 201     }
 202 
 203     /**
 204      * The UIDefaults class accessor object
 205      */
 206     private static UIDefaultsAccessor uiDefaultsAccessor;
 207 
 208     /**
 209      * Set an accessor object for the UIDefaults class.
 210      */
 211     public static void setUIDefaultsAccessor(UIDefaultsAccessor accessor) {
 212         uiDefaultsAccessor = accessor;
 213     }
 214 
 215     /**
 216      * Retrieve the accessor object for the JLightweightFrame class
 217      */
 218     public static UIDefaultsAccessor getUIDefaultsAccessor() {
 219         if (uiDefaultsAccessor == null) {
 220             unsafe.ensureClassInitialized(UIDefaults.class);
 221         }
 222         return uiDefaultsAccessor;
 223     }
 224 
 225     /**
 226      * The RepaintManager class accessor object.
 227      */
 228     private static RepaintManagerAccessor repaintManagerAccessor;
 229 
 230     /**
 231      * Set an accessor object for the RepaintManager class.
 232      */
 233     public static void setRepaintManagerAccessor(RepaintManagerAccessor accessor) {
 234         repaintManagerAccessor = accessor;
 235     }
 236 
 237     /**
 238      * Retrieve the accessor object for the RepaintManager class.
 239      */
 240     public static RepaintManagerAccessor getRepaintManagerAccessor() {
 241         if (repaintManagerAccessor == null) {
 242             unsafe.ensureClassInitialized(RepaintManager.class);
 243         }
 244         return repaintManagerAccessor;
 245     }
 246 
 247     /**
 248      * The PopupFactory class accessor object.
 249      */
 250     private static PopupFactoryAccessor popupFactoryAccessor;
 251 
 252     /**
 253      * Retrieve the accessor object for the PopupFactory class.
 254      */
 255     public static PopupFactoryAccessor getPopupFactoryAccessor() {
 256         if (popupFactoryAccessor == null) {
 257             unsafe.ensureClassInitialized(PopupFactory.class);
 258         }
 259         return popupFactoryAccessor;
 260     }
 261 
 262     /**
 263      * Set an Accessor object for the PopupFactory class.
 264      */
 265     public static void setPopupFactoryAccessor(PopupFactoryAccessor popupFactoryAccessor) {
 266         SwingAccessor.popupFactoryAccessor = popupFactoryAccessor;
 267     }
 268 
 269     /**
 270      * The KeyStroke class accessor object.
 271      */
 272     private static KeyStrokeAccessor keyStrokeAccessor;
 273 
 274     /**
 275      * Retrieve the accessor object for the KeyStroke class.
 276      */
 277     public static KeyStrokeAccessor getKeyStrokeAccessor() {
 278         if (keyStrokeAccessor == null) {
 279             unsafe.ensureClassInitialized(KeyStroke.class);
 280         }
 281         return keyStrokeAccessor;
 282     }
 283 
 284     /*
 285      * Set the accessor object for the KeyStroke class.
 286      */
 287     public static void setKeyStrokeAccessor(KeyStrokeAccessor accessor) {
 288         SwingAccessor.keyStrokeAccessor = accessor;
 289     }
 290 }