1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
42
43
44
45 private JTextField jtf;
46
47
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
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
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 }