001: /*
002: * CustomKeyboardFocusManager.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing;
023:
024: import java.awt.Component;
025: import java.awt.Container;
026: import java.awt.DefaultKeyboardFocusManager;
027: import java.awt.FocusTraversalPolicy;
028:
029: import java.beans.PropertyChangeEvent;
030: import java.beans.PropertyChangeListener;
031: import java.beans.PropertyVetoException;
032: import java.beans.VetoableChangeListener;
033:
034: import javax.swing.JComponent;
035: import javax.swing.JTextField;
036:
037: /* ----------------------------------------------------------
038: * CVS NOTE: Changes to the CVS repository prior to the
039: * release of version 3.0.0beta1 has meant a
040: * resetting of CVS revision numbers.
041: * ----------------------------------------------------------
042: */
043:
044: /** <p>Custom KeyboardFocusManager to mainly control the focus of
045: * <code>JTextField<code> objects within a container. This
046: * provides the functionaliaty to select (highlight) any
047: * text already contained within the field when focus is
048: * gained through keyboard traversal (ie. TAB key).
049: *
050: * @author Takis Diakoumis
051: * @version $Revision: 1.4 $
052: * @date $Date: 2006/05/14 06:56:07 $
053: */
054: public class CustomKeyboardFocusManager extends
055: DefaultKeyboardFocusManager {
056:
057: /** <p>Focuses the Component before aComponent, typically based on a
058: * FocusTraversalPolicy.
059: *
060: * @param aComponent the Component that is the basis for the focus
061: * traversal operation
062: */
063: public void focusPreviousComponent(Component aComponent) {
064:
065: if (aComponent == null)
066: return;
067:
068: if (aComponent.getParent() instanceof TextFieldFocusController) {
069:
070: Container rootAncestor = aComponent
071: .getFocusCycleRootAncestor();
072: Component comp = aComponent;
073:
074: if (comp instanceof JTextField) {
075: ((JTextField) comp).select(0, 0);
076: }
077:
078: while (rootAncestor != null
079: && !(rootAncestor.isShowing()
080: && rootAncestor.isFocusable() && rootAncestor
081: .isEnabled())) {
082:
083: comp = rootAncestor;
084: rootAncestor = comp.getFocusCycleRootAncestor();
085:
086: }
087:
088: if (rootAncestor != null) {
089:
090: FocusTraversalPolicy policy = rootAncestor
091: .getFocusTraversalPolicy();
092: Component _component = policy.getComponentBefore(
093: rootAncestor, comp);
094:
095: if (_component == null)
096: _component = policy
097: .getDefaultComponent(rootAncestor);
098:
099: if (_component != null) {
100:
101: if (_component instanceof JTextField) {
102: ((JTextField) _component).selectAll();
103: }
104:
105: ((JComponent) _component).grabFocus();
106:
107: } else {
108: aComponent.transferFocusBackward();
109: }
110:
111: }
112:
113: } else {
114: aComponent.transferFocusBackward();
115: }
116:
117: }
118:
119: /** <p>Focuses the Component after aComponent, typically based on a
120: * FocusTraversalPolicy.
121: *
122: * @param the Component that is the basis for the focus
123: * traversal operation
124: */
125: public void focusNextComponent(Component aComponent) {
126:
127: if (aComponent == null)
128: return;
129:
130: if (aComponent.getParent() instanceof TextFieldFocusController) {
131:
132: Container rootAncestor = aComponent
133: .getFocusCycleRootAncestor();
134: Component comp = aComponent;
135:
136: if (comp instanceof JTextField) {
137: ((JTextField) comp).select(0, 0);
138: }
139:
140: while (rootAncestor != null
141: && !(rootAncestor.isShowing()
142: && rootAncestor.isFocusable() && rootAncestor
143: .isEnabled())) {
144: comp = rootAncestor;
145: rootAncestor = comp.getFocusCycleRootAncestor();
146: }
147:
148: if (rootAncestor != null) {
149:
150: FocusTraversalPolicy policy = rootAncestor
151: .getFocusTraversalPolicy();
152: Component _component = policy.getComponentAfter(
153: rootAncestor, comp);
154:
155: if (_component == null)
156: _component = policy
157: .getDefaultComponent(rootAncestor);
158:
159: if (_component != null) {
160:
161: if (_component instanceof JTextField) {
162: ((JTextField) _component).selectAll();
163: }
164:
165: ((JComponent) _component).grabFocus();
166:
167: } else {
168: aComponent.transferFocus();
169: }
170:
171: }
172:
173: } else {
174: aComponent.transferFocus();
175: }
176:
177: }
178:
179: // --- some fiddling with the focus bits ---
180:
181: /*
182: public CustomKeyboardFocusManager() {
183: super();
184: // addVetoableChangeListener(new FocusVetoableChangeListener());
185: addPropertyChangeListener(new FocusChangeListener());
186: }
187:
188: class FocusChangeListener implements PropertyChangeListener {
189: public void propertyChange(PropertyChangeEvent evt) {
190:
191: Component oldComp = null;
192: Component newComp = null;
193:
194: Object oldObject = evt.getOldValue();
195: Object newObject = evt.getNewValue();
196:
197: if (oldObject != null && oldObject instanceof Component) {
198: oldComp = (Component)oldObject;
199: }
200:
201: if (newObject != null && newObject instanceof Component) {
202: newComp = (Component)evt.getNewValue();
203: }
204:
205: if ("focusOwner".equals(evt.getPropertyName())) {
206: if (oldComp != null) {
207: System.out.println("oldComp: " + oldComp.getClass().getName());
208: }
209: else {
210: System.out.println("oldComp is null");
211: }
212: if (newComp != null) {
213: System.out.println("newComp: " + newComp.getClass().getName());
214: }
215: else {
216: System.out.println("newComp is null");
217: }
218:
219: }
220:
221: }
222:
223: }
224:
225: class FocusVetoableChangeListener implements VetoableChangeListener {
226: public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException {
227: Component oldComp = (Component)evt.getOldValue();
228: Component newComp = (Component)evt.getNewValue();
229:
230: boolean vetoFocusChange = false;
231: if ("focusOwner".equals(evt.getPropertyName())) {
232: if (newComp == null) {
233: vetoFocusChange = true;
234: // the newComp component will gain the focus
235: }
236: }
237:
238: if (vetoFocusChange) {
239: throw new PropertyVetoException("message", evt);
240: }
241: }
242: }
243: */
244: }
|