KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > LayoutFocusTraversalPolicy


1 /*
2  * @(#)LayoutFocusTraversalPolicy.java 1.10 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.ComponentOrientation JavaDoc;
12 import java.util.Comparator JavaDoc;
13 import java.io.*;
14
15
16 /**
17  * A SortingFocusTraversalPolicy which sorts Components based on their size,
18  * position, and orientation. Based on their size and position, Components are
19  * roughly categorized into rows and columns. For a Container with horizontal
20  * orientation, columns run left-to-right or right-to-left, and rows run top-
21  * to-bottom. For a Container with vertical orientation, columns run top-to-
22  * bottom and rows run left-to-right or right-to-left. See
23  * <code>ComponentOrientation</code> for more information. All columns in a
24  * row are fully traversed before proceeding to the next row.
25  *
26  * @version 1.10, 12/19/03
27  * @author David Mendenhall
28  *
29  * @see java.awt.ComponentOrientation
30  * @since 1.4
31  */

32 public class LayoutFocusTraversalPolicy extends SortingFocusTraversalPolicy JavaDoc
33     implements Serializable
34 {
35     // Delegate most of our fitness test to Default so that we only have to
36
// code the algorithm once.
37
private static final SwingDefaultFocusTraversalPolicy fitnessTestPolicy =
38     new SwingDefaultFocusTraversalPolicy();
39
40     /**
41      * Constructs a LayoutFocusTraversalPolicy.
42      */

43     public LayoutFocusTraversalPolicy() {
44     super(new LayoutComparator JavaDoc());
45     }
46
47     /**
48      * Constructs a LayoutFocusTraversalPolicy with the passed in
49      * <code>Comparator</code>.
50      */

51     LayoutFocusTraversalPolicy(Comparator JavaDoc c) {
52     super(c);
53     }
54
55     /**
56      * Returns the Component that should receive the focus after aComponent.
57      * aContainer must be a focus cycle root of aComponent.
58      * <p>
59      * By default, LayoutFocusTraversalPolicy implicitly transfers focus down-
60      * cycle. That is, during normal focus traversal, the Component
61      * traversed after a focus cycle root will be the focus-cycle-root's
62      * default Component to focus. This behavior can be disabled using the
63      * <code>setImplicitDownCycleTraversal</code> method.
64      * <p>
65      * If aContainer is <a HREF="doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus
66      * traversal policy provider</a>, the focus is always transferred down-cycle.
67      *
68      * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider
69      * @param aComponent a (possibly indirect) child of aContainer, or
70      * aContainer itself
71      * @return the Component that should receive the focus after aComponent, or
72      * null if no suitable Component can be found
73      * @throws IllegalArgumentException if aContainer is not a focus cycle
74      * root of aComponent or a focus traversal policy provider, or if either aContainer or
75      * aComponent is null
76      */

77     public Component JavaDoc getComponentAfter(Container JavaDoc aContainer,
78                                        Component JavaDoc aComponent) {
79         if (aContainer == null || aComponent == null) {
80             throw new IllegalArgumentException JavaDoc("aContainer and aComponent cannot be null");
81         }
82     Comparator JavaDoc comparator = getComparator();
83     if (comparator instanceof LayoutComparator JavaDoc) {
84         ((LayoutComparator JavaDoc)comparator).
85                 setComponentOrientation(aContainer.
86                     getComponentOrientation());
87     }
88         return super.getComponentAfter(aContainer, aComponent);
89     }
90
91     /**
92      * Returns the Component that should receive the focus before aComponent.
93      * aContainer must be a focus cycle root of aComponent.
94      * <p>
95      * By default, LayoutFocusTraversalPolicy implicitly transfers focus down-
96      * cycle. That is, during normal focus traversal, the Component
97      * traversed after a focus cycle root will be the focus-cycle-root's
98      * default Component to focus. This behavior can be disabled using the
99      * <code>setImplicitDownCycleTraversal</code> method.
100      * <p>
101      * If aContainer is <a HREF="doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus
102      * traversal policy provider</a>, the focus is always transferred down-cycle.
103      *
104      * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider
105      * @param aComponent a (possibly indirect) child of aContainer, or
106      * aContainer itself
107      * @return the Component that should receive the focus before aComponent,
108      * or null if no suitable Component can be found
109      * @throws IllegalArgumentException if aContainer is not a focus cycle
110      * root of aComponent or a focus traversal policy provider, or if either aContainer or
111      * aComponent is null
112      */

113     public Component JavaDoc getComponentBefore(Container JavaDoc aContainer,
114                                         Component JavaDoc aComponent) {
115         if (aContainer == null || aComponent == null) {
116             throw new IllegalArgumentException JavaDoc("aContainer and aComponent cannot be null");
117         }
118     Comparator JavaDoc comparator = getComparator();
119     if (comparator instanceof LayoutComparator JavaDoc) {
120         ((LayoutComparator JavaDoc)comparator).
121                 setComponentOrientation(aContainer.
122                     getComponentOrientation());
123     }
124         return super.getComponentBefore(aContainer, aComponent);
125     }
126
127     /**
128      * Returns the first Component in the traversal cycle. This method is used
129      * to determine the next Component to focus when traversal wraps in the
130      * forward direction.
131      *
132      * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider whose
133      * first Component is to be returned
134      * @return the first Component in the traversal cycle of aContainer,
135      * or null if no suitable Component can be found
136      * @throws IllegalArgumentException if aContainer is null
137      */

138     public Component JavaDoc getFirstComponent(Container JavaDoc aContainer) {
139         if (aContainer == null) {
140             throw new IllegalArgumentException JavaDoc("aContainer cannot be null");
141         }
142     Comparator JavaDoc comparator = getComparator();
143     if (comparator instanceof LayoutComparator JavaDoc) {
144         ((LayoutComparator JavaDoc)comparator).
145                 setComponentOrientation(aContainer.
146                     getComponentOrientation());
147     }
148         return super.getFirstComponent(aContainer);
149     }
150
151     /**
152      * Returns the last Component in the traversal cycle. This method is used
153      * to determine the next Component to focus when traversal wraps in the
154      * reverse direction.
155      *
156      * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider whose
157      * last Component is to be returned
158      * @return the last Component in the traversal cycle of aContainer,
159      * or null if no suitable Component can be found
160      * @throws IllegalArgumentException if aContainer is null
161      */

162     public Component JavaDoc getLastComponent(Container JavaDoc aContainer) {
163         if (aContainer == null) {
164             throw new IllegalArgumentException JavaDoc("aContainer cannot be null");
165         }
166     Comparator JavaDoc comparator = getComparator();
167     if (comparator instanceof LayoutComparator JavaDoc) {
168         ((LayoutComparator JavaDoc)comparator).
169                 setComponentOrientation(aContainer.
170                     getComponentOrientation());
171     }
172         return super.getLastComponent(aContainer);
173     }
174
175     /**
176      * Determines whether the specified <code>Component</code>
177      * is an acceptable choice as the new focus owner.
178      * This method performs the following sequence of operations:
179      * <ol>
180      * <li>Checks whether <code>aComponent</code> is visible, displayable,
181      * enabled, and focusable. If any of these properties is
182      * <code>false</code>, this method returns <code>false</code>.
183      * <li>If <code>aComponent</code> is an instance of <code>JTable</code>,
184      * returns <code>true</code>.
185      * <li>If <code>aComponent</code> is an instance of <code>JComboBox</code>,
186      * then returns the value of
187      * <code>aComponent.getUI().isFocusTraversable(aComponent)</code>.
188      * <li>If <code>aComponent</code> is a <code>JComponent</code>
189      * with a <code>JComponent.WHEN_FOCUSED</code>
190      * <code>InputMap</code> that is neither <code>null</code>
191      * nor empty, returns <code>true</code>.
192      * <li>Returns the value of
193      * <code>DefaultFocusTraversalPolicy.accept(aComponent)</code>.
194      * </ol>
195      *
196      * @param aComponent the <code>Component</code> whose fitness
197      * as a focus owner is to be tested
198      * @see java.awt.Component#isVisible
199      * @see java.awt.Component#isDisplayable
200      * @see java.awt.Component#isEnabled
201      * @see java.awt.Component#isFocusable
202      * @see javax.swing.plaf.ComboBoxUI#isFocusTraversable
203      * @see javax.swing.JComponent#getInputMap
204      * @see java.awt.DefaultFocusTraversalPolicy#accept
205      * @return <code>true</code> if <code>aComponent</code> is a valid choice
206      * for a focus owner;
207      * otherwise <code>false</code>
208      */

209      protected boolean accept(Component JavaDoc aComponent) {
210     if (!super.accept(aComponent)) {
211         return false;
212     } else if (aComponent instanceof JTable JavaDoc) {
213             // JTable only has ancestor focus bindings, we thus force it
214
// to be focusable by returning true here.
215
return true;
216     } else if (aComponent instanceof JComboBox JavaDoc) {
217         JComboBox JavaDoc box = (JComboBox JavaDoc)aComponent;
218         return box.getUI().isFocusTraversable(box);
219     } else if (aComponent instanceof JComponent JavaDoc) {
220         JComponent JavaDoc jComponent = (JComponent JavaDoc)aComponent;
221         InputMap JavaDoc inputMap = jComponent.getInputMap(JComponent.WHEN_FOCUSED,
222                                false);
223         while (inputMap != null && inputMap.size() == 0) {
224         inputMap = inputMap.getParent();
225         }
226             if (inputMap != null) {
227                 return true;
228             }
229             // Delegate to the fitnessTestPolicy, this will test for the
230
// case where the developer has overriden isFocusTraversable to
231
// return true.
232
}
233         return fitnessTestPolicy.accept(aComponent);
234     }
235
236     private void writeObject(ObjectOutputStream out) throws IOException {
237     out.writeObject(getComparator());
238     out.writeBoolean(getImplicitDownCycleTraversal());
239     }
240     private void readObject(ObjectInputStream in)
241     throws IOException, ClassNotFoundException JavaDoc
242     {
243     setComparator((Comparator JavaDoc)in.readObject());
244     setImplicitDownCycleTraversal(in.readBoolean());
245     }
246 }
247
248 // Create our own subclass and change accept to public so that we can call
249
// accept.
250
class SwingDefaultFocusTraversalPolicy
251     extends java.awt.DefaultFocusTraversalPolicy JavaDoc
252 {
253     public boolean accept(Component JavaDoc aComponent) {
254     return super.accept(aComponent);
255     }
256 }
257
Popular Tags