View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.gui.action;
21  
22  import java.awt.event.ActionEvent;
23  import java.awt.event.InputEvent;
24  import java.awt.event.KeyEvent;
25  
26  import javax.swing.AbstractAction;
27  import javax.swing.Action;
28  import javax.swing.Icon;
29  import javax.swing.JTextField;
30  import javax.swing.KeyStroke;
31  
32  import org.xnap.XNap;
33  import org.xnap.action.AbstractXNapAction;
34  import org.xnap.gui.util.IconHelper;
35  
36  /***
37   * Erases the text of a {@link javax.swing.JTextField JTextField}.
38   */
39  public class EraseAction extends AbstractAction {
40  
41      //--- Constant(s) ---
42      
43      //--- Data field(s) ---
44  
45      private JTextField jtf;
46      
47      //--- Constructor(s) ---
48      
49      /***
50       * Constructs an erase action, that has its text field set to
51       * <code>jtf</code>.  
52       *
53       * @param jtf the text field to erase
54       */
55      public EraseAction(JTextField jtf) 
56      {
57  		this.jtf = jtf;
58  	
59  		// do not scale this icon
60  		Icon icon = IconHelper.getImage("16/locationbar_erase.png");
61  		if (icon != null) {
62  			putValue(Action.SMALL_ICON, icon);
63  		}
64  		else {
65  			putValue(Action.NAME, ">");
66  		}
67  
68  		putValue(Action.SHORT_DESCRIPTION, XNap.tr("Erase Text"));
69  		putValue(Action.ACTION_COMMAND_KEY, "eraseAction");
70  		putValue(AbstractXNapAction.DEFAULT_KEYSTROKE,
71  				 KeyStroke.getKeyStroke(KeyEvent.VK_E, InputEvent.CTRL_MASK));
72      }
73  
74      /***
75       * Constructs an erase action, that has no text field set. Use 
76       * {@link #setTextField(JTextField) setTextField} to set a text field.
77       */
78      public EraseAction()
79      {
80  		this(null);
81      }
82      
83      //--- Method(s) ---
84  
85      /***
86       * Erases the text field.
87       */
88      public void actionPerformed(ActionEvent event) 
89      {
90  		if (jtf != null) {
91  			jtf.setText("");
92  			jtf.grabFocus();
93  		}
94      }
95  
96      /***
97       * Sets the text field to <code>newValue</code>.
98       */
99      public void setTextField(JTextField newValue)
100     { 
101 		jtf = newValue;
102     }
103 
104 }