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.menu;
21  
22  import java.awt.event.ActionEvent;
23  
24  import javax.swing.AbstractAction;
25  import javax.swing.Action;
26  import javax.swing.JMenu;
27  import javax.swing.text.JTextComponent;
28  
29  import org.xnap.XNap;
30  import org.xnap.gui.action.ActionHelper;
31  import org.xnap.gui.component.Completable;
32  import org.xnap.gui.component.XNapMenuItem;
33  import org.xnap.util.Preferences;
34  import org.xnap.util.PreferencesProvider;
35  
36  public class TextFieldMenu extends JMenu
37  {
38      
39  	//--- Constant(s) ---
40      
41      //--- Data field(s) ---
42      
43  	private JTextComponent field;
44  
45  	private CompletionModeMenu menu = null;
46  
47      //--- Constructor(s) ---
48  
49      public TextFieldMenu(JTextComponent field)
50      {
51  		super(XNap.tr("Edit"));
52  		
53  		this.field = field;
54  		
55  		add(new XNapMenuItem(ActionHelper.cutAction));
56  		add(new XNapMenuItem(ActionHelper.copyAction));
57  		add(new XNapMenuItem(ActionHelper.pasteAction));
58  		addSeparator();
59  		add(new SelectAllAction());
60      }
61  
62  	public TextFieldMenu(Completable comp)
63  	{
64  		this(comp.getTextComponent());
65  		addSeparator();
66  		menu = new CompletionModeMenu(comp);
67  		add(menu);
68  	}
69  
70      public TextFieldMenu(Completable comp, String prefsKey, 
71  						 PreferencesProvider prefs)
72      {
73      	this(comp.getTextComponent());
74  		addSeparator();
75  		menu = new CompletionModeMenu(comp, prefsKey, prefs);
76  		add(menu);
77      }
78      
79      public TextFieldMenu(Completable comp, String prefsKey)
80      {
81      	this(comp, prefsKey, Preferences.getInstance());
82      }
83  
84      //--- Method(s) ---
85  
86  	public void setPreferences(String prefsKey, PreferencesProvider prefs)
87  	{
88  		if (menu != null) {
89  			menu.setPreferences(prefsKey, prefs);
90  		}
91  	}
92  
93      
94      //--- Inner Class(es) ---
95      
96      /***
97       *
98  	 */
99      private class SelectAllAction extends AbstractAction 
100     {
101         
102         public SelectAllAction()
103         {
104     		putValue(Action.NAME, XNap.tr("Select All"));
105     		putValue(Action.SHORT_DESCRIPTION, 
106     				 XNap.tr("Selects all text."));
107         }
108 
109         //--- Method(s) ---
110 
111         public void actionPerformed(ActionEvent event) 
112         {
113     		field.selectAll();
114         }
115         
116     }
117 }