KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > DefaultFocusManager


1 /*
2  * @(#)DefaultFocusManager.java 1.29 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing;
8
9 import java.awt.Component JavaDoc;
10 import java.awt.Container JavaDoc;
11 import java.awt.FocusTraversalPolicy JavaDoc;
12 import java.util.Comparator JavaDoc;
13
14
15 /**
16  * This class has been obsoleted by the 1.4 focus APIs. While client code may
17  * still use this class, developers are strongly encouraged to use
18  * <code>java.awt.KeyboardFocusManager</code> and
19  * <code>java.awt.DefaultKeyboardFocusManager</code> instead.
20  * <p>
21  * Please see
22  * <a HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html">
23  * How to Use the Focus Subsystem</a>,
24  * a section in <em>The Java Tutorial</em>, and the
25  * <a HREF="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a>
26  * for more information.
27  *
28  * @version 1.29, 12/19/03
29  * @author Arnaud Weber
30  * @author David Mendenhall
31  */

32 public class DefaultFocusManager extends FocusManager JavaDoc {
33
34     final FocusTraversalPolicy JavaDoc gluePolicy =
35     new LegacyGlueFocusTraversalPolicy JavaDoc(this);
36     private final FocusTraversalPolicy JavaDoc layoutPolicy =
37     new LegacyLayoutFocusTraversalPolicy(this);
38     private final LayoutComparator JavaDoc comparator =
39     new LayoutComparator JavaDoc();
40
41     public DefaultFocusManager() {
42     setDefaultFocusTraversalPolicy(gluePolicy);
43     }
44
45     public Component JavaDoc getComponentAfter(Container JavaDoc aContainer,
46                        Component JavaDoc aComponent)
47     {
48     Container JavaDoc root = (aContainer.isFocusCycleRoot())
49         ? aContainer
50         : aContainer.getFocusCycleRootAncestor();
51
52     // Support for mixed 1.4/pre-1.4 focus APIs. If a particular root's
53
// traversal policy is non-legacy, then honor it.
54
if (root != null) {
55         FocusTraversalPolicy JavaDoc policy = root.getFocusTraversalPolicy();
56         if (policy != gluePolicy) {
57         return policy.getComponentAfter(root, aComponent);
58         }
59
60         comparator.setComponentOrientation(root.getComponentOrientation());
61         return layoutPolicy.getComponentAfter(root, aComponent);
62     }
63
64     return null;
65     }
66
67     public Component JavaDoc getComponentBefore(Container JavaDoc aContainer,
68                     Component JavaDoc aComponent)
69     {
70     Container JavaDoc root = (aContainer.isFocusCycleRoot())
71         ? aContainer
72         : aContainer.getFocusCycleRootAncestor();
73
74     // Support for mixed 1.4/pre-1.4 focus APIs. If a particular root's
75
// traversal policy is non-legacy, then honor it.
76
if (root != null) {
77         FocusTraversalPolicy JavaDoc policy = root.getFocusTraversalPolicy();
78         if (policy != gluePolicy) {
79         return policy.getComponentBefore(root, aComponent);
80         }
81
82         comparator.setComponentOrientation(root.getComponentOrientation());
83         return layoutPolicy.getComponentBefore(root, aComponent);
84     }
85
86     return null;
87     }
88
89     public Component JavaDoc getFirstComponent(Container JavaDoc aContainer) {
90     Container JavaDoc root = (aContainer.isFocusCycleRoot())
91         ? aContainer
92         : aContainer.getFocusCycleRootAncestor();
93
94     // Support for mixed 1.4/pre-1.4 focus APIs. If a particular root's
95
// traversal policy is non-legacy, then honor it.
96
if (root != null) {
97         FocusTraversalPolicy JavaDoc policy = root.getFocusTraversalPolicy();
98         if (policy != gluePolicy) {
99         return policy.getFirstComponent(root);
100         }
101
102         comparator.setComponentOrientation(root.getComponentOrientation());
103         return layoutPolicy.getFirstComponent(root);
104     }
105
106     return null;
107     }
108
109     public Component JavaDoc getLastComponent(Container JavaDoc aContainer) {
110     Container JavaDoc root = (aContainer.isFocusCycleRoot())
111         ? aContainer
112         : aContainer.getFocusCycleRootAncestor();
113
114     // Support for mixed 1.4/pre-1.4 focus APIs. If a particular root's
115
// traversal policy is non-legacy, then honor it.
116
if (root != null) {
117         FocusTraversalPolicy JavaDoc policy = root.getFocusTraversalPolicy();
118         if (policy != gluePolicy) {
119         return policy.getLastComponent(root);
120         }
121
122         comparator.setComponentOrientation(root.getComponentOrientation());
123         return layoutPolicy.getLastComponent(root);
124     }
125
126     return null;
127     }
128
129     public boolean compareTabOrder(Component JavaDoc a, Component JavaDoc b) {
130     return (comparator.compare(a, b) < 0);
131     }
132 }
133
134 final class LegacyLayoutFocusTraversalPolicy
135     extends LayoutFocusTraversalPolicy JavaDoc
136 {
137     LegacyLayoutFocusTraversalPolicy(DefaultFocusManager JavaDoc defaultFocusManager) {
138     super(new CompareTabOrderComparator(defaultFocusManager));
139     }
140 }
141
142 final class CompareTabOrderComparator implements Comparator JavaDoc {
143     private final DefaultFocusManager JavaDoc defaultFocusManager;
144
145     CompareTabOrderComparator(DefaultFocusManager JavaDoc defaultFocusManager) {
146     this.defaultFocusManager = defaultFocusManager;
147     }
148
149     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
150         if (o1 == o2) {
151             return 0;
152         }
153     return (defaultFocusManager.compareTabOrder((Component JavaDoc)o1,
154                             (Component JavaDoc)o2)) ? -1 : 1;
155     }
156 }
157
Popular Tags