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
26package sun.swing;
27
28import jdk.internal.misc.Unsafe;
29
30import java.awt.*;
31import javax.swing.*;
32
33import 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 */
42public 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    /**
95     * An accessor for the UIDefaults class.
96     */
97    public interface UIDefaultsAccessor {
98        /**
99         * Adds a resource bundle to the list of resource bundles.
100         */
101        void addInternalBundle(UIDefaults uiDefaults, String bundleName);
102    }
103
104    /**
105     * An accessor for the RepaintManager class.
106     */
107    public interface RepaintManagerAccessor {
108        void addRepaintListener(RepaintManager rm, SwingUtilities2.RepaintListener l);
109        void removeRepaintListener(RepaintManager rm, SwingUtilities2.RepaintListener l);
110    }
111
112    /**
113     * An accessor for PopupFactory class.
114     */
115    public interface PopupFactoryAccessor {
116        Popup getHeavyWeightPopup(PopupFactory factory, Component owner, Component contents,
117                                  int ownerX, int ownerY);
118    }
119
120    /*
121     * An accessor for the KeyStroke class
122     */
123    public interface KeyStrokeAccessor {
124
125        KeyStroke create();
126    }
127
128    /**
129     * The javax.swing.JComponent class accessor object.
130     */
131    private static JComponentAccessor jComponentAccessor;
132
133    /**
134     * Set an accessor object for the javax.swing.JComponent class.
135     */
136    public static void setJComponentAccessor(JComponentAccessor jCompAccessor) {
137        jComponentAccessor = jCompAccessor;
138    }
139
140    /**
141     * Retrieve the accessor object for the javax.swing.JComponent class.
142     */
143    public static JComponentAccessor getJComponentAccessor() {
144        if (jComponentAccessor == null) {
145            unsafe.ensureClassInitialized(JComponent.class);
146        }
147
148        return jComponentAccessor;
149    }
150
151    /**
152     * The javax.swing.text.JTextComponent class accessor object.
153     */
154    private static JTextComponentAccessor jtextComponentAccessor;
155
156    /**
157     * Set an accessor object for the javax.swing.text.JTextComponent class.
158     */
159    public static void setJTextComponentAccessor(JTextComponentAccessor jtca) {
160         jtextComponentAccessor = jtca;
161    }
162
163    /**
164     * Retrieve the accessor object for the javax.swing.text.JTextComponent class.
165     */
166    public static JTextComponentAccessor getJTextComponentAccessor() {
167        if (jtextComponentAccessor == null) {
168            unsafe.ensureClassInitialized(JTextComponent.class);
169        }
170
171        return jtextComponentAccessor;
172    }
173
174    /**
175     * The JLightweightFrame class accessor object
176     */
177    private static JLightweightFrameAccessor jLightweightFrameAccessor;
178
179    /**
180     * Set an accessor object for the JLightweightFrame class.
181     */
182    public static void setJLightweightFrameAccessor(JLightweightFrameAccessor accessor) {
183        jLightweightFrameAccessor = accessor;
184    }
185
186    /**
187     * Retrieve the accessor object for the JLightweightFrame class
188     */
189    public static JLightweightFrameAccessor getJLightweightFrameAccessor() {
190        if (jLightweightFrameAccessor == null) {
191            unsafe.ensureClassInitialized(JLightweightFrame.class);
192        }
193        return jLightweightFrameAccessor;
194    }
195
196    /**
197     * The UIDefaults class accessor object
198     */
199    private static UIDefaultsAccessor uiDefaultsAccessor;
200
201    /**
202     * Set an accessor object for the UIDefaults class.
203     */
204    public static void setUIDefaultsAccessor(UIDefaultsAccessor accessor) {
205        uiDefaultsAccessor = accessor;
206    }
207
208    /**
209     * Retrieve the accessor object for the JLightweightFrame class
210     */
211    public static UIDefaultsAccessor getUIDefaultsAccessor() {
212        if (uiDefaultsAccessor == null) {
213            unsafe.ensureClassInitialized(UIDefaults.class);
214        }
215        return uiDefaultsAccessor;
216    }
217
218    /**
219     * The RepaintManager class accessor object.
220     */
221    private static RepaintManagerAccessor repaintManagerAccessor;
222
223    /**
224     * Set an accessor object for the RepaintManager class.
225     */
226    public static void setRepaintManagerAccessor(RepaintManagerAccessor accessor) {
227        repaintManagerAccessor = accessor;
228    }
229
230    /**
231     * Retrieve the accessor object for the RepaintManager class.
232     */
233    public static RepaintManagerAccessor getRepaintManagerAccessor() {
234        if (repaintManagerAccessor == null) {
235            unsafe.ensureClassInitialized(RepaintManager.class);
236        }
237        return repaintManagerAccessor;
238    }
239
240    /**
241     * The PopupFactory class accessor object.
242     */
243    private static PopupFactoryAccessor popupFactoryAccessor;
244
245    /**
246     * Retrieve the accessor object for the PopupFactory class.
247     */
248    public static PopupFactoryAccessor getPopupFactoryAccessor() {
249        if (popupFactoryAccessor == null) {
250            unsafe.ensureClassInitialized(PopupFactory.class);
251        }
252        return popupFactoryAccessor;
253    }
254
255    /**
256     * Set an Accessor object for the PopupFactory class.
257     */
258    public static void setPopupFactoryAccessor(PopupFactoryAccessor popupFactoryAccessor) {
259        SwingAccessor.popupFactoryAccessor = popupFactoryAccessor;
260    }
261
262    /**
263     * The KeyStroke class accessor object.
264     */
265    private static KeyStrokeAccessor keyStrokeAccessor;
266
267    /**
268     * Retrieve the accessor object for the KeyStroke class.
269     */
270    public static KeyStrokeAccessor getKeyStrokeAccessor() {
271        if (keyStrokeAccessor == null) {
272            unsafe.ensureClassInitialized(KeyStroke.class);
273        }
274        return keyStrokeAccessor;
275    }
276
277    /*
278     * Set the accessor object for the KeyStroke class.
279     */
280    public static void setKeyStrokeAccessor(KeyStrokeAccessor accessor) {
281        SwingAccessor.keyStrokeAccessor = accessor;
282    }
283}
284