Path: blob/master/src/java.desktop/share/classes/javax/swing/FocusManager.java
41153 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package javax.swing;2526import java.awt.*;272829/**30* This class has been obsoleted by the 1.4 focus APIs. While client code may31* still use this class, developers are strongly encouraged to use32* <code>java.awt.KeyboardFocusManager</code> and33* <code>java.awt.DefaultKeyboardFocusManager</code> instead.34* <p>35* Please see36* <a href="https://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html">37* How to Use the Focus Subsystem</a>,38* a section in <em>The Java Tutorial</em>, and the39* <a href="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a>40* for more information.41*42* @see <a href="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a>43*44* @author Arnaud Weber45* @author David Mendenhall46* @since 1.247*/48public abstract class FocusManager extends DefaultKeyboardFocusManager {4950/**51* This field is obsolete, and its use is discouraged since its52* specification is incompatible with the 1.4 focus APIs.53* The current FocusManager is no longer a property of the UI.54* Client code must query for the current FocusManager using55* <code>KeyboardFocusManager.getCurrentKeyboardFocusManager()</code>.56* See the Focus Specification for more information.57*58* @see java.awt.KeyboardFocusManager#getCurrentKeyboardFocusManager59* @see <a href="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a>60*/61public static final String FOCUS_MANAGER_CLASS_PROPERTY =62"FocusManagerClassName";6364private static boolean enabled = true;6566/**67* Constructor for subclasses to call.68*/69protected FocusManager() {}7071/**72* Returns the current <code>KeyboardFocusManager</code> instance73* for the calling thread's context.74*75* @return this thread's context's <code>KeyboardFocusManager</code>76* @see #setCurrentManager77*/78public static FocusManager getCurrentManager() {79KeyboardFocusManager manager =80KeyboardFocusManager.getCurrentKeyboardFocusManager();81if (manager instanceof FocusManager) {82return (FocusManager)manager;83} else {84return new DelegatingDefaultFocusManager(manager);85}86}8788/**89* Sets the current <code>KeyboardFocusManager</code> instance90* for the calling thread's context. If <code>null</code> is91* specified, then the current <code>KeyboardFocusManager</code>92* is replaced with a new instance of93* <code>DefaultKeyboardFocusManager</code>.94* <p>95* If a <code>SecurityManager</code> is installed,96* the calling thread must be granted the <code>AWTPermission</code>97* "replaceKeyboardFocusManager" in order to replace the98* the current <code>KeyboardFocusManager</code>.99* If this permission is not granted,100* this method will throw a <code>SecurityException</code>,101* and the current <code>KeyboardFocusManager</code> will be unchanged.102*103* @param aFocusManager the new <code>KeyboardFocusManager</code>104* for this thread's context105* @see #getCurrentManager106* @see java.awt.DefaultKeyboardFocusManager107* @throws SecurityException if the calling thread does not have permission108* to replace the current <code>KeyboardFocusManager</code>109*/110public static void setCurrentManager(FocusManager aFocusManager)111throws SecurityException112{113// Note: This method is not backward-compatible with 1.3 and earlier114// releases. It now throws a SecurityException in an applet, whereas115// in previous releases, it did not. This issue was discussed at116// length, and ultimately approved by Hans.117KeyboardFocusManager toSet =118(aFocusManager instanceof DelegatingDefaultFocusManager)119? ((DelegatingDefaultFocusManager)aFocusManager).getDelegate()120: aFocusManager;121KeyboardFocusManager.setCurrentKeyboardFocusManager(toSet);122}123124/**125* Changes the current <code>KeyboardFocusManager</code>'s default126* <code>FocusTraversalPolicy</code> to127* <code>DefaultFocusTraversalPolicy</code>.128*129* @see java.awt.DefaultFocusTraversalPolicy130* @see java.awt.KeyboardFocusManager#setDefaultFocusTraversalPolicy131* @deprecated as of 1.4, replaced by132* <code>KeyboardFocusManager.setDefaultFocusTraversalPolicy(FocusTraversalPolicy)</code>133*/134@Deprecated135public static void disableSwingFocusManager() {136if (enabled) {137enabled = false;138KeyboardFocusManager.getCurrentKeyboardFocusManager().139setDefaultFocusTraversalPolicy(140new DefaultFocusTraversalPolicy());141}142}143144/**145* Returns whether the application has invoked146* <code>disableSwingFocusManager()</code>.147*148* @return {@code true} if focus manager is enabled.149* @see #disableSwingFocusManager150* @deprecated As of 1.4, replaced by151* <code>KeyboardFocusManager.getDefaultFocusTraversalPolicy()</code>152*/153@Deprecated154public static boolean isFocusManagerEnabled() {155return enabled;156}157}158159160