1/*
2 * Copyright (c) 1997, 2013, 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 */
25package javax.swing;
26
27import java.awt.*;
28
29
30/**
31 * This class has been obsoleted by the 1.4 focus APIs. While client code may
32 * still use this class, developers are strongly encouraged to use
33 * <code>java.awt.KeyboardFocusManager</code> and
34 * <code>java.awt.DefaultKeyboardFocusManager</code> instead.
35 * <p>
36 * Please see
37 * <a href="http://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html">
38 * How to Use the Focus Subsystem</a>,
39 * a section in <em>The Java Tutorial</em>, and the
40 * <a href="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a>
41 * for more information.
42 *
43 * @see <a href="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a>
44 *
45 * @author Arnaud Weber
46 * @author David Mendenhall
47 * @since 1.2
48 */
49public abstract class FocusManager extends DefaultKeyboardFocusManager {
50
51    /**
52     * This field is obsolete, and its use is discouraged since its
53     * specification is incompatible with the 1.4 focus APIs.
54     * The current FocusManager is no longer a property of the UI.
55     * Client code must query for the current FocusManager using
56     * <code>KeyboardFocusManager.getCurrentKeyboardFocusManager()</code>.
57     * See the Focus Specification for more information.
58     *
59     * @see java.awt.KeyboardFocusManager#getCurrentKeyboardFocusManager
60     * @see <a href="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a>
61     */
62    public static final String FOCUS_MANAGER_CLASS_PROPERTY =
63        "FocusManagerClassName";
64
65    private static boolean enabled = true;
66
67    /**
68     * Returns the current <code>KeyboardFocusManager</code> instance
69     * for the calling thread's context.
70     *
71     * @return this thread's context's <code>KeyboardFocusManager</code>
72     * @see #setCurrentManager
73     */
74    public static FocusManager getCurrentManager() {
75        KeyboardFocusManager manager =
76            KeyboardFocusManager.getCurrentKeyboardFocusManager();
77        if (manager instanceof FocusManager) {
78            return (FocusManager)manager;
79        } else {
80            return new DelegatingDefaultFocusManager(manager);
81        }
82    }
83
84    /**
85     * Sets the current <code>KeyboardFocusManager</code> instance
86     * for the calling thread's context. If <code>null</code> is
87     * specified, then the current <code>KeyboardFocusManager</code>
88     * is replaced with a new instance of
89     * <code>DefaultKeyboardFocusManager</code>.
90     * <p>
91     * If a <code>SecurityManager</code> is installed,
92     * the calling thread must be granted the <code>AWTPermission</code>
93     * "replaceKeyboardFocusManager" in order to replace the
94     * the current <code>KeyboardFocusManager</code>.
95     * If this permission is not granted,
96     * this method will throw a <code>SecurityException</code>,
97     * and the current <code>KeyboardFocusManager</code> will be unchanged.
98     *
99     * @param aFocusManager the new <code>KeyboardFocusManager</code>
100     *     for this thread's context
101     * @see #getCurrentManager
102     * @see java.awt.DefaultKeyboardFocusManager
103     * @throws SecurityException if the calling thread does not have permission
104     *         to replace the current <code>KeyboardFocusManager</code>
105     */
106    public static void setCurrentManager(FocusManager aFocusManager)
107        throws SecurityException
108    {
109        // Note: This method is not backward-compatible with 1.3 and earlier
110        // releases. It now throws a SecurityException in an applet, whereas
111        // in previous releases, it did not. This issue was discussed at
112        // length, and ultimately approved by Hans.
113        KeyboardFocusManager toSet =
114            (aFocusManager instanceof DelegatingDefaultFocusManager)
115                ? ((DelegatingDefaultFocusManager)aFocusManager).getDelegate()
116                : aFocusManager;
117        KeyboardFocusManager.setCurrentKeyboardFocusManager(toSet);
118    }
119
120    /**
121     * Changes the current <code>KeyboardFocusManager</code>'s default
122     * <code>FocusTraversalPolicy</code> to
123     * <code>DefaultFocusTraversalPolicy</code>.
124     *
125     * @see java.awt.DefaultFocusTraversalPolicy
126     * @see java.awt.KeyboardFocusManager#setDefaultFocusTraversalPolicy
127     * @deprecated as of 1.4, replaced by
128     * <code>KeyboardFocusManager.setDefaultFocusTraversalPolicy(FocusTraversalPolicy)</code>
129     */
130    @Deprecated
131    public static void disableSwingFocusManager() {
132        if (enabled) {
133            enabled = false;
134            KeyboardFocusManager.getCurrentKeyboardFocusManager().
135                setDefaultFocusTraversalPolicy(
136                    new DefaultFocusTraversalPolicy());
137        }
138    }
139
140    /**
141     * Returns whether the application has invoked
142     * <code>disableSwingFocusManager()</code>.
143     *
144     * @return {@code true} if focus manager is enabled.
145     * @see #disableSwingFocusManager
146     * @deprecated As of 1.4, replaced by
147     *   <code>KeyboardFocusManager.getDefaultFocusTraversalPolicy()</code>
148     */
149    @Deprecated
150    public static boolean isFocusManagerEnabled() {
151        return enabled;
152    }
153}
154