1 /*
2 * Copyright (c) 2000, 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 */
25 package java.awt;
26
27 import java.util.List;
28 import java.util.ArrayList;
29 import sun.util.logging.PlatformLogger;
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 * By default, ContainerOrderFocusTraversalPolicy implicitly transfers focus
40 * down-cycle. That is, during normal forward focus traversal, the Component
41 * traversed after a focus cycle root will be the focus-cycle-root's default
42 * Component to focus. This behavior can be disabled using the
43 * {@code setImplicitDownCycleTraversal} method.
44 * <p>
45 * By default, methods of this class will return a Component only if it is
46 * visible, displayable, enabled, and focusable. Subclasses can modify this
47 * behavior by overriding the {@code accept} method.
48 * <p>
49 * This policy takes into account <a
50 * href="doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus traversal
568 */
569 public boolean getImplicitDownCycleTraversal() {
570 return implicitDownCycleTraversal;
571 }
572
573 /**
574 * Determines whether a Component is an acceptable choice as the new
575 * focus owner. By default, this method will accept a Component if and
576 * only if it is visible, displayable, enabled, and focusable.
577 *
578 * @param aComponent the Component whose fitness as a focus owner is to
579 * be tested
580 * @return {@code true} if aComponent is visible, displayable,
581 * enabled, and focusable; {@code false} otherwise
582 */
583 protected boolean accept(Component aComponent) {
584 if (!aComponent.canBeFocusOwner()) {
585 return false;
586 }
587
588 // Verify that the Component is recursively enabled. Disabling a
589 // heavyweight Container disables its children, whereas disabling
590 // a lightweight Container does not.
591 if (!(aComponent instanceof Window)) {
592 for (Container enableTest = aComponent.getParent();
593 enableTest != null;
594 enableTest = enableTest.getParent())
595 {
596 if (!(enableTest.isEnabled() || enableTest.isLightweight())) {
597 return false;
598 }
599 if (enableTest instanceof Window) {
600 break;
601 }
602 }
603 }
604
605 return true;
606 }
607 }
|
1 /*
2 * Copyright (c) 2000, 2019, 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 package java.awt;
26
27 import java.util.List;
28 import java.util.ArrayList;
29 import sun.util.logging.PlatformLogger;
30
31 import java.util.Enumeration;
32 import sun.awt.SunToolkit;
33 import javax.swing.AbstractButton;
34 import javax.swing.ButtonGroup;
35 import javax.swing.ButtonModel;
36 import javax.swing.JToggleButton;
37
38
39 /**
40 * A FocusTraversalPolicy that determines traversal order based on the order
41 * of child Components in a Container. From a particular focus cycle root, the
42 * policy makes a pre-order traversal of the Component hierarchy, and traverses
43 * a Container's children according to the ordering of the array returned by
44 * {@code Container.getComponents()}. Portions of the hierarchy that are
45 * not visible and displayable will not be searched.
46 * <p>
47 * By default, ContainerOrderFocusTraversalPolicy implicitly transfers focus
48 * down-cycle. That is, during normal forward focus traversal, the Component
49 * traversed after a focus cycle root will be the focus-cycle-root's default
50 * Component to focus. This behavior can be disabled using the
51 * {@code setImplicitDownCycleTraversal} method.
52 * <p>
53 * By default, methods of this class will return a Component only if it is
54 * visible, displayable, enabled, and focusable. Subclasses can modify this
55 * behavior by overriding the {@code accept} method.
56 * <p>
57 * This policy takes into account <a
58 * href="doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus traversal
576 */
577 public boolean getImplicitDownCycleTraversal() {
578 return implicitDownCycleTraversal;
579 }
580
581 /**
582 * Determines whether a Component is an acceptable choice as the new
583 * focus owner. By default, this method will accept a Component if and
584 * only if it is visible, displayable, enabled, and focusable.
585 *
586 * @param aComponent the Component whose fitness as a focus owner is to
587 * be tested
588 * @return {@code true} if aComponent is visible, displayable,
589 * enabled, and focusable; {@code false} otherwise
590 */
591 protected boolean accept(Component aComponent) {
592 if (!aComponent.canBeFocusOwner()) {
593 return false;
594 }
595
596 if (SunToolkit.isInstanceOf(aComponent,
597 "javax.swing.JToggleButton")) {
598 ButtonModel model = ((JToggleButton)aComponent).getModel();
599 if (model != null) {
600 ButtonGroup group = model.getGroup();
601 if (group != null) {
602 Enumeration<AbstractButton> elements =
603 group.getElements();
604 int idx = 0;
605 while (group.getElements().hasMoreElements()) {
606 AbstractButton member = elements.nextElement();
607 if (member instanceof JToggleButton &&
608 member.isVisible() && member.isDisplayable() &&
609 member.isEnabled() && member.isFocusable()) {
610 if (member == aComponent) {
611 return idx == 0;
612 }
613 idx++;
614 }
615 }
616 }
617 }
618 }
619
620 // Verify that the Component is recursively enabled. Disabling a
621 // heavyweight Container disables its children, whereas disabling
622 // a lightweight Container does not.
623 if (!(aComponent instanceof Window)) {
624 for (Container enableTest = aComponent.getParent();
625 enableTest != null;
626 enableTest = enableTest.getParent())
627 {
628 if (!(enableTest.isEnabled() || enableTest.isLightweight())) {
629 return false;
630 }
631 if (enableTest instanceof Window) {
632 break;
633 }
634 }
635 }
636
637 return true;
638 }
639 }
|