Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/classes/java/awt/DefaultFocusTraversalPolicy.java
41152 views
1
/*
2
* Copyright (c) 2000, 2021, 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 java.awt;
27
28
import java.awt.peer.ComponentPeer;
29
import java.io.Serial;
30
31
/**
32
* A FocusTraversalPolicy that determines traversal order based on the order
33
* of child Components in a Container. From a particular focus cycle root, the
34
* policy makes a pre-order traversal of the Component hierarchy, and traverses
35
* a Container's children according to the ordering of the array returned by
36
* {@code Container.getComponents()}. Portions of the hierarchy that are
37
* not visible and displayable will not be searched.
38
* <p>
39
* If client code has explicitly set the focusability of a Component by either
40
* overriding {@code Component.isFocusTraversable()} or
41
* {@code Component.isFocusable()}, or by calling
42
* {@code Component.setFocusable()}, then a DefaultFocusTraversalPolicy
43
* behaves exactly like a ContainerOrderFocusTraversalPolicy. If, however, the
44
* Component is relying on default focusability, then a
45
* DefaultFocusTraversalPolicy will reject all Components with non-focusable
46
* peers. This is the default FocusTraversalPolicy for all AWT Containers.
47
* <p>
48
* The focusability of a peer is implementation-dependent. Sun recommends that
49
* all implementations for a particular native platform construct peers with
50
* the same focusability. The recommendations for Windows and Unix are that
51
* Canvases, Labels, Panels, Scrollbars, ScrollPanes, Windows, and lightweight
52
* Components have non-focusable peers, and all other Components have focusable
53
* peers. These recommendations are used in the Sun AWT implementations. Note
54
* that the focusability of a Component's peer is different from, and does not
55
* impact, the focusability of the Component itself.
56
* <p>
57
* Please see
58
* <a href="https://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html">
59
* How to Use the Focus Subsystem</a>,
60
* a section in <em>The Java Tutorial</em>, and the
61
* <a href="doc-files/FocusSpec.html">Focus Specification</a>
62
* for more information.
63
*
64
* @author David Mendenhall
65
*
66
* @see Container#getComponents
67
* @see Component#isFocusable
68
* @see Component#setFocusable
69
* @since 1.4
70
*/
71
public class DefaultFocusTraversalPolicy
72
extends ContainerOrderFocusTraversalPolicy
73
{
74
/**
75
* Use serialVersionUID from JDK 1.6 for interoperability.
76
*/
77
@Serial
78
private static final long serialVersionUID = 8876966522510157497L;
79
80
/**
81
* Constructs a {@code DefaultFocusTraversalPolicy}.
82
*/
83
public DefaultFocusTraversalPolicy() {}
84
85
/**
86
* Determines whether a Component is an acceptable choice as the new
87
* focus owner. The Component must be visible, displayable, and enabled
88
* to be accepted. If client code has explicitly set the focusability
89
* of the Component by either overriding
90
* {@code Component.isFocusTraversable()} or
91
* {@code Component.isFocusable()}, or by calling
92
* {@code Component.setFocusable()}, then the Component will be
93
* accepted if and only if it is focusable. If, however, the Component is
94
* relying on default focusability, then all Canvases, Labels, Panels,
95
* Scrollbars, ScrollPanes, Windows, and lightweight Components will be
96
* rejected.
97
*
98
* @param aComponent the Component whose fitness as a focus owner is to
99
* be tested
100
* @return {@code true} if aComponent meets the above requirements;
101
* {@code false} otherwise
102
*/
103
protected boolean accept(Component aComponent) {
104
if (!(aComponent.isVisible() && aComponent.isDisplayable() &&
105
aComponent.isEnabled()))
106
{
107
return false;
108
}
109
110
// Verify that the Component is recursively enabled. Disabling a
111
// heavyweight Container disables its children, whereas disabling
112
// a lightweight Container does not.
113
if (!(aComponent instanceof Window)) {
114
for (Container enableTest = aComponent.getParent();
115
enableTest != null;
116
enableTest = enableTest.getParent())
117
{
118
if (!(enableTest.isEnabled() || enableTest.isLightweight())) {
119
return false;
120
}
121
if (enableTest instanceof Window) {
122
break;
123
}
124
}
125
}
126
127
boolean focusable = aComponent.isFocusable();
128
if (aComponent.isFocusTraversableOverridden()) {
129
return focusable;
130
}
131
132
ComponentPeer peer = aComponent.peer;
133
return (peer != null && peer.isFocusable());
134
}
135
}
136
137