View Javadoc

1   /*
2    *  XNap Commons
3    *
4    *  Copyright (C) 2005  Steffen Pingel
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or (at your option) any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package org.xnap.commons.gui;
21  
22  import java.awt.event.ActionEvent;
23  import javax.swing.Action;
24  import javax.swing.JTextField;
25  import org.xnap.commons.gui.action.AbstractXNapAction;
26  import org.xnap.commons.i18n.I18nFactory;
27  
28  /***
29   * Erases the text of a {@link javax.swing.JTextField JTextField}.
30   */
31  public class EraseTextFieldAction extends AbstractXNapAction {
32  
33      private JTextField textField;
34      
35      /***
36       * Constructs an erase action, that has its text field set to
37       * <code>jtf</code>.  
38       *
39       * @param textField the text field to erase
40       */
41      public EraseTextFieldAction(JTextField textField) 
42      {
43  		this.textField = textField;
44  	
45  		putValue(Action.SHORT_DESCRIPTION, 
46  				I18nFactory.getI18n(EraseTextFieldAction.class).tr("Erases text"));
47  		putValue(ICON_FILENAME, "locationbar_erase.png");
48      }
49  
50      /***
51       * Constructs an erase action, that has no text field set. Use 
52       * {@link #setTextField(JTextField) setTextField} to set a text field.
53       */
54      public EraseTextFieldAction()
55      {
56  		this(null);
57      }
58  
59      /***
60       * Erases the text field.
61       */
62      public void actionPerformed(ActionEvent event) 
63      {
64  		if (textField != null) {
65  			textField.setText("");
66  			textField.grabFocus();
67  		}
68      }
69  
70      public JTextField getTextField()
71      {
72      	return textField;
73      }
74      
75      /***
76       * Sets the text field to <code>newValue</code>.
77       */
78      public void setTextField(JTextField newValue)
79      { 
80  		textField = newValue;
81      }
82  
83  }