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 sun.misc.Unsafe;
  29 
  30 import java.awt.Point;
  31 import javax.swing.RepaintManager;
  32 import javax.swing.Popup;
  33 import javax.swing.PopupFactory;
  34 
  35 import javax.swing.text.JTextComponent;
  36 import javax.swing.TransferHandler;
  37 
  38 /**
  39  * The SwingAccessor utility class.
  40  * The main purpose of this class is to enable accessing
  41  * private and package-private fields of classes from
  42  * different classes/packages. See sun.misc.SharedSecretes
  43  * for another example.
  44  */
  45 public final class SwingAccessor {
  46     private static final Unsafe unsafe = Unsafe.getUnsafe();
  47 
  48     /**
  49      * We don't need any objects of this class.
  50      * It's rather a collection of static methods
  51      * and interfaces.
  52      */
  53     private SwingAccessor() {
  54     }
  55 
  56     /**
  57      * An accessor for the JTextComponent class.
  58      * Note that we intentionally introduce the JTextComponentAccessor,
  59      * and not the JComponentAccessor because the needed methods
  60      * aren't override methods.
  61      */
  62     public interface JTextComponentAccessor {
  63 
  64         /**
  65          * Calculates a custom drop location for the text component,
  66          * representing where a drop at the given point should insert data.
  67          */
  68         TransferHandler.DropLocation dropLocationForPoint(JTextComponent textComp, Point p);
  69 
  70         /**
  71          * Called to set or clear the drop location during a DnD operation.
  72          */
  73         Object setDropLocation(JTextComponent textComp, TransferHandler.DropLocation location,
  74                                Object state, boolean forDrop);
  75     }
  76 
  77     /**
  78      * An accessor for the JLightweightFrame class.
  79      */
  80     public interface JLightweightFrameAccessor {
  81         /**
  82          * Notifies the JLightweight frame that it needs to update a cursor
  83          */
  84         void updateCursor(JLightweightFrame frame);
  85     }
  86 
  87     /**
  88      * An accessor for the RepaintManager class.
  89      */
  90     public interface RepaintManagerAccessor {
  91         void addRepaintListener(RepaintManager rm, SwingUtilities2.RepaintListener l);
  92         void removeRepaintListener(RepaintManager rm, SwingUtilities2.RepaintListener l);
  93     }
  94 
  95     /**
  96      * An accessor for the PopupFctory class.
  97      */
  98     public interface PopupFactoryAccessor {
  99         /**
 100          * Enables or disables HeavyWeightPopup cache. If the popup is not
 101          * HeavyWeightPopup, the method does nothing.
 102          */
 103         void setHeavyWeightPopupCacheEnabled(Popup popup, boolean enable);
 104     }
 105 
 106     /**
 107      * The javax.swing.text.JTextComponent class accessor object.
 108      */
 109     private static JTextComponentAccessor jtextComponentAccessor;
 110 
 111     /**
 112      * Set an accessor object for the javax.swing.text.JTextComponent class.
 113      */
 114     public static void setJTextComponentAccessor(JTextComponentAccessor jtca) {
 115          jtextComponentAccessor = jtca;
 116     }
 117 
 118     /**
 119      * Retrieve the accessor object for the javax.swing.text.JTextComponent class.
 120      */
 121     public static JTextComponentAccessor getJTextComponentAccessor() {
 122         if (jtextComponentAccessor == null) {
 123             unsafe.ensureClassInitialized(JTextComponent.class);
 124         }
 125 
 126         return jtextComponentAccessor;
 127     }
 128 
 129     /**
 130      * The JLightweightFrame class accessor object
 131      */
 132     private static JLightweightFrameAccessor jLightweightFrameAccessor;
 133 
 134     /**
 135      * Set an accessor object for the JLightweightFrame class.
 136      */
 137     public static void setJLightweightFrameAccessor(JLightweightFrameAccessor accessor) {
 138         jLightweightFrameAccessor = accessor;
 139     }
 140 
 141     /**
 142      * Retrieve the accessor object for the JLightweightFrame class
 143      */
 144     public static JLightweightFrameAccessor getJLightweightFrameAccessor() {
 145         if (jLightweightFrameAccessor == null) {
 146             unsafe.ensureClassInitialized(JLightweightFrame.class);
 147         }
 148         return jLightweightFrameAccessor;
 149     }
 150 
 151     /**
 152      * The RepaintManager class accessor object.
 153      */
 154     private static RepaintManagerAccessor repaintManagerAccessor;
 155 
 156     /**
 157      * Set an accessor object for the RepaintManager class.
 158      */
 159     public static void setRepaintManagerAccessor(RepaintManagerAccessor accessor) {
 160         repaintManagerAccessor = accessor;
 161     }
 162 
 163     /**
 164      * Retrieve the accessor object for the RepaintManager class.
 165      */
 166     public static RepaintManagerAccessor getRepaintManagerAccessor() {
 167         if (repaintManagerAccessor == null) {
 168             unsafe.ensureClassInitialized(RepaintManager.class);
 169         }
 170         return repaintManagerAccessor;
 171     }
 172 
 173     /**
 174      * The PopupFactory class accessor object.
 175      */
 176     private static PopupFactoryAccessor popupFactoryAccessor;
 177 
 178     /**
 179      * Set an accessor object for the PopupFactory class.
 180      */
 181     public static void setPopupFactoryAccessor(PopupFactoryAccessor accessor) { popupFactoryAccessor = accessor; }
 182 
 183     /**
 184      * Retrive the accessor object for the PopupFactory class.
 185      */
 186     public static PopupFactoryAccessor getPopupFactoryAccessor() {
 187         if (popupFactoryAccessor == null) {
 188             unsafe.ensureClassInitialized(PopupFactory.class);
 189         }
 190         return popupFactoryAccessor;
 191     }
 192 }