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.6 2007/11/02 17:50:36 taqua 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: import javax.swing.text.JTextComponent; 48: 49: /** 50: * An observer that selects all the text when a field gains the focus. 51: * 52: * @author Thomas Morgner 53: */ 54: public final class JTextObserver implements FocusListener { 55: 56: /** The singleton instance. */ 57: private static JTextObserver singleton; 58: 59: /** 60: * Creates a new instance. 61: */ 62: private JTextObserver() { 63: // nothing required 64: } 65: 66: /** 67: * Returns the single instance. 68: * 69: * @return The single instance. 70: */ 71: public static JTextObserver getInstance() { 72: if (singleton == null) { 73: singleton = new JTextObserver(); 74: } 75: return singleton; 76: } 77: 78: /** 79: * Selects all the text when a field gains the focus. 80: * 81: * @param e the focus event. 82: */ 83: public void focusGained(final FocusEvent e) { 84: if (e.getSource() instanceof JTextComponent) { 85: final JTextComponent tex = (JTextComponent) e.getSource(); 86: tex.selectAll(); 87: } 88: } 89: 90: /** 91: * Deselects the text when a field loses the focus. 92: * 93: * @param e the event. 94: */ 95: public void focusLost(final FocusEvent e) { 96: if (e.getSource() instanceof JTextComponent) { 97: final JTextComponent tex = (JTextComponent) e.getSource(); 98: tex.select(0, 0); 99: } 100: } 101: 102: /** 103: * Adds this instance as a listener for the specified text component. 104: * 105: * @param t the text component. 106: */ 107: public static void addTextComponent(final JTextComponent t) { 108: if (singleton == null) { 109: singleton = new JTextObserver(); 110: } 111: t.addFocusListener(singleton); 112: } 113: 114: /** 115: * Removes this instance as a listener for the specified text component. 116: * 117: * @param t the text component. 118: */ 119: public static void removeTextComponent(final JTextComponent t) { 120: if (singleton == null) { 121: singleton = new JTextObserver(); 122: } 123: t.removeFocusListener(singleton); 124: } 125: 126: }