001 /*
002 * Copyright 2000-2002 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 package javax.swing;
026
027 import java.awt.FocusTraversalPolicy;
028 import java.awt.Component;
029 import java.awt.Container;
030 import java.awt.Window;
031 import java.util.HashMap;
032 import java.util.HashSet;
033 import java.io.*;
034
035 /**
036 * A FocusTraversalPolicy which provides support for legacy applications which
037 * handle focus traversal via JComponent.setNextFocusableComponent or by
038 * installing a custom DefaultFocusManager. If a specific traversal has not
039 * been hard coded, then that traversal is provided either by the custom
040 * DefaultFocusManager, or by a wrapped FocusTraversalPolicy instance.
041 *
042 * @version 1.13, 05/05/07
043 * @author David Mendenhall
044 */
045 final class LegacyGlueFocusTraversalPolicy extends FocusTraversalPolicy
046 implements Serializable {
047 private transient FocusTraversalPolicy delegatePolicy;
048 private transient DefaultFocusManager delegateManager;
049
050 private HashMap forwardMap = new HashMap(),
051 backwardMap = new HashMap();
052
053 LegacyGlueFocusTraversalPolicy(FocusTraversalPolicy delegatePolicy) {
054 this .delegatePolicy = delegatePolicy;
055 }
056
057 LegacyGlueFocusTraversalPolicy(DefaultFocusManager delegateManager) {
058 this .delegateManager = delegateManager;
059 }
060
061 void setNextFocusableComponent(Component left, Component right) {
062 forwardMap.put(left, right);
063 backwardMap.put(right, left);
064 }
065
066 void unsetNextFocusableComponent(Component left, Component right) {
067 forwardMap.remove(left);
068 backwardMap.remove(right);
069 }
070
071 public Component getComponentAfter(Container focusCycleRoot,
072 Component aComponent) {
073 Component hardCoded = aComponent, prevHardCoded;
074 HashSet sanity = new HashSet();
075
076 do {
077 prevHardCoded = hardCoded;
078 hardCoded = (Component) forwardMap.get(hardCoded);
079 if (hardCoded == null) {
080 if (delegatePolicy != null
081 && prevHardCoded
082 .isFocusCycleRoot(focusCycleRoot)) {
083 return delegatePolicy.getComponentAfter(
084 focusCycleRoot, prevHardCoded);
085 } else if (delegateManager != null) {
086 return delegateManager.getComponentAfter(
087 focusCycleRoot, aComponent);
088 } else {
089 return null;
090 }
091 }
092 if (sanity.contains(hardCoded)) {
093 // cycle detected; bail
094 return null;
095 }
096 sanity.add(hardCoded);
097 } while (!accept(hardCoded));
098
099 return hardCoded;
100 }
101
102 public Component getComponentBefore(Container focusCycleRoot,
103 Component aComponent) {
104 Component hardCoded = aComponent, prevHardCoded;
105 HashSet sanity = new HashSet();
106
107 do {
108 prevHardCoded = hardCoded;
109 hardCoded = (Component) backwardMap.get(hardCoded);
110 if (hardCoded == null) {
111 if (delegatePolicy != null
112 && prevHardCoded
113 .isFocusCycleRoot(focusCycleRoot)) {
114 return delegatePolicy.getComponentBefore(
115 focusCycleRoot, prevHardCoded);
116 } else if (delegateManager != null) {
117 return delegateManager.getComponentBefore(
118 focusCycleRoot, aComponent);
119 } else {
120 return null;
121 }
122 }
123 if (sanity.contains(hardCoded)) {
124 // cycle detected; bail
125 return null;
126 }
127 sanity.add(hardCoded);
128 } while (!accept(hardCoded));
129
130 return hardCoded;
131 }
132
133 public Component getFirstComponent(Container focusCycleRoot) {
134 if (delegatePolicy != null) {
135 return delegatePolicy.getFirstComponent(focusCycleRoot);
136 } else if (delegateManager != null) {
137 return delegateManager.getFirstComponent(focusCycleRoot);
138 } else {
139 return null;
140 }
141 }
142
143 public Component getLastComponent(Container focusCycleRoot) {
144 if (delegatePolicy != null) {
145 return delegatePolicy.getLastComponent(focusCycleRoot);
146 } else if (delegateManager != null) {
147 return delegateManager.getLastComponent(focusCycleRoot);
148 } else {
149 return null;
150 }
151 }
152
153 public Component getDefaultComponent(Container focusCycleRoot) {
154 if (delegatePolicy != null) {
155 return delegatePolicy.getDefaultComponent(focusCycleRoot);
156 } else {
157 return getFirstComponent(focusCycleRoot);
158 }
159 }
160
161 private boolean accept(Component aComponent) {
162 if (!(aComponent.isVisible() && aComponent.isDisplayable()
163 && aComponent.isFocusable() && aComponent.isEnabled())) {
164 return false;
165 }
166
167 // Verify that the Component is recursively enabled. Disabling a
168 // heavyweight Container disables its children, whereas disabling
169 // a lightweight Container does not.
170 if (!(aComponent instanceof Window)) {
171 for (Container enableTest = aComponent.getParent(); enableTest != null; enableTest = enableTest
172 .getParent()) {
173 if (!(enableTest.isEnabled() || enableTest
174 .isLightweight())) {
175 return false;
176 }
177 if (enableTest instanceof Window) {
178 break;
179 }
180 }
181 }
182
183 return true;
184 }
185
186 private void writeObject(ObjectOutputStream out) throws IOException {
187 out.defaultWriteObject();
188
189 if (delegatePolicy instanceof Serializable) {
190 out.writeObject(delegatePolicy);
191 } else {
192 out.writeObject(null);
193 }
194
195 if (delegateManager instanceof Serializable) {
196 out.writeObject(delegateManager);
197 } else {
198 out.writeObject(null);
199 }
200 }
201
202 private void readObject(ObjectInputStream in) throws IOException,
203 ClassNotFoundException {
204 in.defaultReadObject();
205 delegatePolicy = (FocusTraversalPolicy) in.readObject();
206 delegateManager = (DefaultFocusManager) in.readObject();
207 }
208 }
|