1 /*
   2  * Copyright (c) 2009, 2014, 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 JTextComponent class.
  55      * Note that we intentionally introduce the JTextComponentAccessor,
  56      * and not the JComponentAccessor because the needed methods
  57      * aren't override methods.
  58      */
  59     public interface JTextComponentAccessor {
  60 
  61         /**
  62          * Calculates a custom drop location for the text component,
  63          * representing where a drop at the given point should insert data.
  64          */
  65         TransferHandler.DropLocation dropLocationForPoint(JTextComponent textComp, Point p);
  66 
  67         /**
  68          * Called to set or clear the drop location during a DnD operation.
  69          */
  70         Object setDropLocation(JTextComponent textComp, TransferHandler.DropLocation location,
  71                                Object state, boolean forDrop);
  72     }
  73 
  74     /**
  75      * An accessor for the JLightweightFrame class.
  76      */
  77     public interface JLightweightFrameAccessor {
  78         /**
  79          * Notifies the JLightweight frame that it needs to update a cursor
  80          */
  81         void updateCursor(JLightweightFrame frame);
  82     }
  83 
  84     /**
  85      * An accessor for the RepaintManager class.
  86      */
  87     public interface RepaintManagerAccessor {
  88         void addRepaintListener(RepaintManager rm, SwingUtilities2.RepaintListener l);
  89         void removeRepaintListener(RepaintManager rm, SwingUtilities2.RepaintListener l);
  90     }
  91 
  92     /**
  93      * An accessor for PopupFactory class.
  94      */
  95     public interface PopupFactoryAccessor {
  96         Popup getHeavyWeightPopup(PopupFactory factory, Component owner, Component contents,
  97                                   int ownerX, int ownerY);
  98     }
  99 
 100     /*
 101      * An accessor for the KeyStroke class
 102      */
 103     public interface KeyStrokeAccessor {
 104 
 105         KeyStroke create();
 106     }
 107 
 108     /**
 109      * The javax.swing.text.JTextComponent class accessor object.
 110      */
 111     private static JTextComponentAccessor jtextComponentAccessor;
 112 
 113     /**
 114      * Set an accessor object for the javax.swing.text.JTextComponent class.
 115      */
 116     public static void setJTextComponentAccessor(JTextComponentAccessor jtca) {
 117          jtextComponentAccessor = jtca;
 118     }
 119 
 120     /**
 121      * Retrieve the accessor object for the javax.swing.text.JTextComponent class.
 122      */
 123     public static JTextComponentAccessor getJTextComponentAccessor() {
 124         if (jtextComponentAccessor == null) {
 125             unsafe.ensureClassInitialized(JTextComponent.class);
 126         }
 127 
 128         return jtextComponentAccessor;
 129     }
 130 
 131     /**
 132      * The JLightweightFrame class accessor object
 133      */
 134     private static JLightweightFrameAccessor jLightweightFrameAccessor;
 135 
 136     /**
 137      * Set an accessor object for the JLightweightFrame class.
 138      */
 139     public static void setJLightweightFrameAccessor(JLightweightFrameAccessor accessor) {
 140         jLightweightFrameAccessor = accessor;
 141     }
 142 
 143     /**
 144      * Retrieve the accessor object for the JLightweightFrame class
 145      */
 146     public static JLightweightFrameAccessor getJLightweightFrameAccessor() {
 147         if (jLightweightFrameAccessor == null) {
 148             unsafe.ensureClassInitialized(JLightweightFrame.class);
 149         }
 150         return jLightweightFrameAccessor;
 151     }
 152 
 153     /**
 154      * The RepaintManager class accessor object.
 155      */
 156     private static RepaintManagerAccessor repaintManagerAccessor;
 157 
 158     /**
 159      * Set an accessor object for the RepaintManager class.
 160      */
 161     public static void setRepaintManagerAccessor(RepaintManagerAccessor accessor) {
 162         repaintManagerAccessor = accessor;
 163     }
 164 
 165     /**
 166      * Retrieve the accessor object for the RepaintManager class.
 167      */
 168     public static RepaintManagerAccessor getRepaintManagerAccessor() {
 169         if (repaintManagerAccessor == null) {
 170             unsafe.ensureClassInitialized(RepaintManager.class);
 171         }
 172         return repaintManagerAccessor;
 173     }
 174 
 175     /**
 176      * The PopupFactory class accessor object.
 177      */
 178     private static PopupFactoryAccessor popupFactoryAccessor;
 179 
 180     /**
 181      * Retrieve the accessor object for the PopupFactory class.
 182      */
 183     public static PopupFactoryAccessor getPopupFactoryAccessor() {
 184         if (popupFactoryAccessor == null) {
 185             unsafe.ensureClassInitialized(PopupFactory.class);
 186         }
 187         return popupFactoryAccessor;
 188     }
 189 
 190     /**
 191      * Set an Accessor object for the PopupFactory class.
 192      */
 193     public static void setPopupFactoryAccessor(PopupFactoryAccessor popupFactoryAccessor) {
 194         SwingAccessor.popupFactoryAccessor = popupFactoryAccessor;
 195     }
 196 
 197     /**
 198      * The KeyStroke class accessor object.
 199      */
 200     private static KeyStrokeAccessor keyStrokeAccessor;
 201 
 202     /**
 203      * Retrieve the accessor object for the KeyStroke class.
 204      */
 205     public static KeyStrokeAccessor getKeyStrokeAccessor() {
 206         if (keyStrokeAccessor == null) {
 207             unsafe.ensureClassInitialized(KeyStroke.class);
 208         }
 209         return keyStrokeAccessor;
 210     }
 211 
 212     /*
 213      * Set the accessor object for the KeyStroke class.
 214      */
 215     public static void setKeyStrokeAccessor(KeyStrokeAccessor accessor) {
 216         SwingAccessor.keyStrokeAccessor = accessor;
 217     }
 218 }