Frames | No Frames |
1: /* ======================================================================== 2: * JCommon : a free general purpose class library for the Java(tm) platform 3: * ======================================================================== 4: * 5: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors. 6: * 7: * Project Info: http://www.jfree.org/jcommon/index.html 8: * 9: * This library is free software; you can redistribute it and/or modify it 10: * under the terms of the GNU Lesser General Public License as published by 11: * the Free Software Foundation; either version 2.1 of the License, or 12: * (at your option) any later version. 13: * 14: * This library is distributed in the hope that it will be useful, but 15: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 17: * License for more details. 18: * 19: * You should have received a copy of the GNU Lesser General Public 20: * License along with this library; if not, write to the Free Software 21: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 22: * USA. 23: * 24: * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 25: * in the United States and other countries.] 26: * 27: * ------------------ 28: * JTextObserver.java 29: * ------------------ 30: * (C) Copyright 2004, by Thomas Morgner and Contributors. 31: * 32: * Original Author: Thomas Morgner; 33: * Contributor(s): David Gilbert (for Object Refinery Limited); 34: * 35: * $Id: JTextObserver.java,v 1.5 2005/10/18 13:18:34 mungady Exp $ 36: * 37: * Changes 38: * ------- 39: * 07-Jun-2004 : Added JCommon header (DG); 40: * 41: */ 42: 43: package org.jfree.ui; 44: 45: import java.awt.event.FocusEvent; 46: import java.awt.event.FocusListener; 47: 48: import javax.swing.text.JTextComponent; 49: 50: /** 51: * An observer that selects all the text when a field gains the focus. 52: * 53: * @author Thomas Morgner 54: */ 55: public final class JTextObserver implements FocusListener { 56: 57: /** The singleton instance. */ 58: private static JTextObserver singleton; 59: 60: /** 61: * Creates a new instance. 62: */ 63: private JTextObserver() { 64: // nothing required 65: } 66: 67: /** 68: * Returns the single instance. 69: * 70: * @return The single instance. 71: */ 72: public static JTextObserver getInstance() { 73: if (singleton == null) { 74: singleton = new JTextObserver(); 75: } 76: return singleton; 77: } 78: 79: /** 80: * Selects all the text when a field gains the focus. 81: * 82: * @param e the focus event. 83: */ 84: public void focusGained(final FocusEvent e) { 85: if (e.getSource() instanceof JTextComponent) { 86: final JTextComponent tex = (JTextComponent) e.getSource(); 87: tex.selectAll(); 88: } 89: } 90: 91: /** 92: * Deselects the text when a field loses the focus. 93: * 94: * @param e the event. 95: */ 96: public void focusLost(final FocusEvent e) { 97: if (e.getSource() instanceof JTextComponent) { 98: final JTextComponent tex = (JTextComponent) e.getSource(); 99: tex.select(0, 0); 100: } 101: } 102: 103: /** 104: * Adds this instance as a listener for the specified text component. 105: * 106: * @param t the text component. 107: */ 108: public static void addTextComponent(final JTextComponent t) { 109: if (singleton == null) { 110: singleton = new JTextObserver(); 111: } 112: t.addFocusListener(singleton); 113: } 114: 115: /** 116: * Removes this instance as a listener for the specified text component. 117: * 118: * @param t the text component. 119: */ 120: public static void removeTextComponent(final JTextComponent t) { 121: if (singleton == null) { 122: singleton = new JTextObserver(); 123: } 124: t.removeFocusListener(singleton); 125: } 126: 127: }