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.JMenu;
25  import javax.swing.text.DefaultEditorKit;
26  import javax.swing.text.Document;
27  import javax.swing.text.JTextComponent;
28  import javax.swing.text.TextAction;
29  import org.xnap.commons.gui.action.AbstractXNapAction;
30  import org.xnap.commons.i18n.I18n;
31  import org.xnap.commons.i18n.I18nFactory;
32  
33  /***
34   * A menu for {@link javax.swing.JTextField} objects that provides cut, copy
35   * paste, clear and a select all action.
36   * 
37   * @author Steffen Pingel
38   */
39  public class TextFieldMenu extends JMenu
40  {
41  	private static I18n i18n = I18nFactory.getI18n(TextFieldMenu.class);
42  	
43  	public final static Action cutAction = new CutAction();
44  	public final static Action copyAction = new CopyAction();
45  	public final static Action pasteAction = new PasteAction();
46  	public final static Action selectAllAction = new SelectAllAction();
47  	public final static Action clearAction = new ClearAction();
48  
49      public TextFieldMenu()
50      {
51  		super(i18n.tr("Edit"));
52  
53  		add(Builder.createMenuItem(cutAction));
54  		add(Builder.createMenuItem(copyAction));
55  		add(Builder.createMenuItem(pasteAction));
56  		add(Builder.createMenuItem(clearAction));
57  		addSeparator();
58  		add(Builder.createMenuItem(selectAllAction));
59      }
60  
61  	/***
62  	 * Default select all action that operates on any 
63  	 * <code>JTextComponent</code>.
64  	 */
65      public static class SelectAllAction extends TextAction {
66          
67          public SelectAllAction()
68          {
69  			super("select-all");
70  			
71      		putValue(Action.NAME, i18n.tr("Select All"));
72      		putValue(Action.SHORT_DESCRIPTION, 
73      				 i18n.tr("Selects all text"));
74          }
75  
76          public void actionPerformed(ActionEvent event) 
77          {
78              JTextComponent target = getTextComponent(event);
79              if (target != null) {
80                  Document doc = target.getDocument();
81                  target.setCaretPosition(0);
82                  target.moveCaretPosition(doc.getLength());
83              }
84          }
85          
86      }
87  
88  	/***
89  	 * Default clear action that operates on any 
90  	 * <code>JTextComponent</code>.
91  	 */
92      public static class ClearAction extends TextAction {
93          
94          public ClearAction()
95          {
96  			super("clear");
97  			
98      		putValue(Action.NAME, i18n.tr("Clear"));
99      		putValue(Action.SHORT_DESCRIPTION, 
100     				 i18n.tr("Deletes all text"));
101 			putValue(AbstractXNapAction.ICON_FILENAME, "editclear.png");
102         }
103 
104         public void actionPerformed(ActionEvent event) 
105         {
106             JTextComponent target = getTextComponent(event);
107             if (target != null) {
108                 target.setText("");
109             }
110         }
111         
112     }
113 
114 	/***
115 	 * Default clipboard copy action that operates on any 
116 	 * <code>JTextComponent</code>.
117 	 */
118     public static class CopyAction extends DefaultEditorKit.CopyAction {
119         
120         public CopyAction()
121         {
122 			putValue(Action.NAME, i18n.tr("Copy"));
123 			putValue(Action.SHORT_DESCRIPTION, 
124 					i18n.tr("Copies selected text to clipboard"));
125 			putValue(AbstractXNapAction.ICON_FILENAME, "editcopy.png");
126         }
127         
128     }
129 
130 	/***
131 	 * Default clipboard cut action that operates on any 
132 	 * <code>JTextComponent</code>.
133 	 */
134     public static class CutAction extends DefaultEditorKit.CutAction {
135         
136         public CutAction()
137         {
138 			putValue(Action.NAME, i18n.tr("Cut"));
139 			putValue(Action.SHORT_DESCRIPTION, 
140 					i18n.tr("Moves selected text to clipboard"));
141 			putValue(AbstractXNapAction.ICON_FILENAME, "editcut.png");
142         }
143 		
144     }
145 
146 	/***
147 	 * Default clipboard paste action that operates on any 
148 	 * <code>JTextComponent</code>.
149 	 */
150     public static class PasteAction extends DefaultEditorKit.PasteAction {
151         
152         public PasteAction()
153         {
154 			putValue(Action.NAME, i18n.tr("Paste"));
155 			putValue(Action.SHORT_DESCRIPTION, 
156 					i18n.tr("Pastes clipboard contents"));
157 			putValue(AbstractXNapAction.ICON_FILENAME, "editpaste.png");
158         }
159 		
160     }
161 	
162 }