KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > LegacyGlueFocusTraversalPolicy


1 /*
2  * @(#)LegacyGlueFocusTraversalPolicy.java 1.6 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.FocusTraversalPolicy JavaDoc;
10 import java.awt.Component JavaDoc;
11 import java.awt.Container JavaDoc;
12 import java.awt.Window JavaDoc;
13 import java.util.HashMap JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.io.*;
16
17
18 /**
19  * A FocusTraversalPolicy which provides support for legacy applications which
20  * handle focus traversal via JComponent.setNextFocusableComponent or by
21  * installing a custom DefaultFocusManager. If a specific traversal has not
22  * been hard coded, then that traversal is provided either by the custom
23  * DefaultFocusManager, or by a wrapped FocusTraversalPolicy instance.
24  *
25  * @version 1.6, 12/19/03
26  * @author David Mendenhall
27  */

28 final class LegacyGlueFocusTraversalPolicy extends FocusTraversalPolicy JavaDoc
29     implements Serializable
30 {
31     private transient FocusTraversalPolicy JavaDoc delegatePolicy;
32     private transient DefaultFocusManager JavaDoc delegateManager;
33
34     private HashMap JavaDoc forwardMap = new HashMap JavaDoc(),
35         backwardMap = new HashMap JavaDoc();
36
37     LegacyGlueFocusTraversalPolicy(FocusTraversalPolicy JavaDoc delegatePolicy) {
38         this.delegatePolicy = delegatePolicy;
39     }
40     LegacyGlueFocusTraversalPolicy(DefaultFocusManager JavaDoc delegateManager) {
41         this.delegateManager = delegateManager;
42     }
43
44     void setNextFocusableComponent(Component JavaDoc left, Component JavaDoc right) {
45         forwardMap.put(left, right);
46         backwardMap.put(right, left);
47     }
48     void unsetNextFocusableComponent(Component JavaDoc left, Component JavaDoc right) {
49         forwardMap.remove(left);
50         backwardMap.remove(right);
51     }
52
53     public Component JavaDoc getComponentAfter(Container JavaDoc focusCycleRoot,
54                                        Component JavaDoc aComponent) {
55         Component JavaDoc hardCoded = aComponent, prevHardCoded;
56         HashSet JavaDoc sanity = new HashSet JavaDoc();
57
58         do {
59             prevHardCoded = hardCoded;
60             hardCoded = (Component JavaDoc)forwardMap.get(hardCoded);
61             if (hardCoded == null) {
62                 if (delegatePolicy != null &&
63             prevHardCoded.isFocusCycleRoot(focusCycleRoot)) {
64                     return delegatePolicy.getComponentAfter(focusCycleRoot,
65                                                             prevHardCoded);
66                 } else if (delegateManager != null) {
67                     return delegateManager.
68                         getComponentAfter(focusCycleRoot, aComponent);
69                 } else {
70                     return null;
71                 }
72             }
73             if (sanity.contains(hardCoded)) {
74                 // cycle detected; bail
75
return null;
76             }
77             sanity.add(hardCoded);
78         } while (!accept(hardCoded));
79
80         return hardCoded;
81     }
82     public Component JavaDoc getComponentBefore(Container JavaDoc focusCycleRoot,
83                                         Component JavaDoc aComponent) {
84         Component JavaDoc hardCoded = aComponent, prevHardCoded;
85         HashSet JavaDoc sanity = new HashSet JavaDoc();
86
87         do {
88             prevHardCoded = hardCoded;
89             hardCoded = (Component JavaDoc)backwardMap.get(hardCoded);
90             if (hardCoded == null) {
91                 if (delegatePolicy != null &&
92             prevHardCoded.isFocusCycleRoot(focusCycleRoot)) {
93                     return delegatePolicy.getComponentBefore(focusCycleRoot,
94                                                        prevHardCoded);
95                 } else if (delegateManager != null) {
96                     return delegateManager.
97                         getComponentBefore(focusCycleRoot, aComponent);
98                 } else {
99                     return null;
100                 }
101             }
102             if (sanity.contains(hardCoded)) {
103                 // cycle detected; bail
104
return null;
105             }
106             sanity.add(hardCoded);
107         } while (!accept(hardCoded));
108
109         return hardCoded;
110     }
111     public Component JavaDoc getFirstComponent(Container JavaDoc focusCycleRoot) {
112         if (delegatePolicy != null) {
113             return delegatePolicy.getFirstComponent(focusCycleRoot);
114         } else if (delegateManager != null) {
115             return delegateManager.getFirstComponent(focusCycleRoot);
116         } else {
117             return null;
118         }
119     }
120     public Component JavaDoc getLastComponent(Container JavaDoc focusCycleRoot) {
121         if (delegatePolicy != null) {
122             return delegatePolicy.getLastComponent(focusCycleRoot);
123         } else if (delegateManager != null) {
124             return delegateManager.getLastComponent(focusCycleRoot);
125         } else {
126             return null;
127         }
128     }
129     public Component JavaDoc getDefaultComponent(Container JavaDoc focusCycleRoot) {
130         if (delegatePolicy != null) {
131             return delegatePolicy.getDefaultComponent(focusCycleRoot);
132         } else {
133             return getFirstComponent(focusCycleRoot);
134         }
135     }
136     private boolean accept(Component JavaDoc aComponent) {
137         if (!(aComponent.isVisible() && aComponent.isDisplayable() &&
138               aComponent.isFocusable() && aComponent.isEnabled())) {
139             return false;
140         }
141
142         // Verify that the Component is recursively enabled. Disabling a
143
// heavyweight Container disables its children, whereas disabling
144
// a lightweight Container does not.
145
if (!(aComponent instanceof Window JavaDoc)) {
146             for (Container JavaDoc enableTest = aComponent.getParent();
147                  enableTest != null;
148                  enableTest = enableTest.getParent())
149             {
150                 if (!(enableTest.isEnabled() || enableTest.isLightweight())) {
151                     return false;
152                 }
153                 if (enableTest instanceof Window JavaDoc) {
154                     break;
155                 }
156             }
157         }
158
159         return true;
160     }
161     private void writeObject(ObjectOutputStream out) throws IOException {
162         out.defaultWriteObject();
163
164         if (delegatePolicy instanceof Serializable) {
165             out.writeObject(delegatePolicy);
166         } else {
167             out.writeObject(null);
168         }
169
170         if (delegateManager instanceof Serializable) {
171             out.writeObject(delegateManager);
172         } else {
173             out.writeObject(null);
174         }
175     }
176     private void readObject(ObjectInputStream in)
177         throws IOException, ClassNotFoundException JavaDoc
178     {
179         in.defaultReadObject();
180         delegatePolicy = (FocusTraversalPolicy JavaDoc)in.readObject();
181         delegateManager = (DefaultFocusManager JavaDoc)in.readObject();
182     }
183 }
184
Popular Tags