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.prefs;
21  
22  import java.awt.Component;
23  import java.awt.GridBagLayout;
24  import java.util.Locale;
25  
26  import javax.help.CSH;
27  import javax.swing.*;
28  import javax.swing.JCheckBox;
29  import javax.swing.JComboBox;
30  import javax.swing.JList;
31  import javax.swing.JPanel;
32  
33  import org.xnap.XNap;
34  import org.xnap.gui.AbstractSettingsPanel;
35  import org.xnap.gui.component.*;
36  import org.xnap.gui.util.GUIHelper;
37  import org.xnap.gui.util.GridBagHelper;
38  import org.xnap.util.StringHelper;
39  
40  /***
41   * Provides controls for personal settings.
42   */
43  public class PersonalPrefsPanel extends AbstractSettingsPanel {
44      
45      //--- Data field(s) ---
46  
47      private ValidatedTextField jteUsername;
48      private ValidatedTextField jtePassword;
49      private ValidatedTextField jteEmail;
50      private JComboBox jcbLanguage;
51  
52      private JCheckBox jcAutoVersionCheck;
53      private JCheckBox jcEmacsKeyBindings;
54      private JComboBox jcbCompletion;
55  
56  
57      //--- Constructor(s) ---
58  
59      public PersonalPrefsPanel()
60      {
61  		setLayout(new GridBagLayout());
62  
63  		// help
64  		CSH.setHelpIDString(this, "personal");
65  
66          JLabel l = GridBagHelper.addLabel(this, XNap.tr("Username"));
67          jteUsername = new ValidatedTextField
68  			(prefs.getUsername(), 20, StringHelper.EMAIL);
69  		l.setLabelFor(jteUsername);
70          GridBagHelper.add(this, jteUsername);
71  
72  		// help
73  		CSH.setHelpIDString(jteUsername, "username");
74  	
75          l = GridBagHelper.addLabel(this, XNap.tr("Password"));
76          jtePassword = new ValidatedTextField
77  			(prefs.getPassword(), 20, StringHelper.EMAIL);
78  		l.setLabelFor(jtePassword);
79          GridBagHelper.add(this, jtePassword);
80  
81  		// help
82  		CSH.setHelpIDString(jtePassword, "password");
83  
84          l = GridBagHelper.addLabel(this, XNap.tr("Email"));
85  
86  		/* this is just a crude approach, to only allow valid email addresses
87  		   someone would have to read RFC 2821 and RFC 2822.  */
88          jteEmail = new ValidatedTextField
89  			(prefs.getEmail(), 20, StringHelper.EMAIL);
90  		l.setLabelFor(jteEmail);
91  		GridBagHelper.add(this, jteEmail);
92  
93  		// help
94  		CSH.setHelpIDString(jteEmail, "email");
95  
96  		l = GridBagHelper.addLabel(this, XNap.tr("Language"));
97  		jcbLanguage = new JComboBox();
98  		l.setLabelFor(jcbLanguage);
99  		jcbLanguage.setRenderer(new LocaleCellRenderer());
100 		jcbLanguage.addItem(new Locale("", "", ""));
101 		for (int i = 0; i < XNap.LANGUAGES.length; i++) {
102 			jcbLanguage.addItem(XNap.LANGUAGES[i]);
103 			if (prefs.getLanguage().equals(XNap.LANGUAGES[i].getLanguage())) {
104 				// we have already added the "system default" locale
105 				jcbLanguage.setSelectedIndex(i + 1);
106 			}
107 		}
108 		GridBagHelper.add(this, jcbLanguage, false);
109 
110 		// help
111 		CSH.setHelpIDString(jcbLanguage, "language");
112 
113 		JPanel jpMisc = new JPanel(new GridBagLayout());
114 		jpMisc.setBorder
115 			(GUIHelper.createDefaultBorder(XNap.tr("Miscellaneous")));
116 
117 		jcAutoVersionCheck = new JCheckBox(XNap.tr("Check for New Version", 1),
118                                            prefs.getAutoVersionCheck());
119 //  		GridBagHelper.add(jpMisc, jcAutoVersionCheck);
120 	
121 		jcEmacsKeyBindings = 
122 			new JCheckBox(XNap.tr("Use Emacs Keybindings", 1),
123 						  prefs.getUseEmacsKeyBindings());
124 		GridBagHelper.add(jpMisc, jcEmacsKeyBindings);
125 
126 
127 		// help
128 		CSH.setHelpIDString(jcEmacsKeyBindings, "emacs-keybindings");
129 
130 
131 		// completion mode
132 		l = GridBagHelper.addLabel(jpMisc, XNap.tr(" Default Completion Mode"));
133 		jcbCompletion = new JComboBox
134 			(CompletionModeFactory.DEFAULT_COMPLETION_MODES);
135 		jcbCompletion.setRenderer(new CompletionModeCellRenderer());
136 		l.setLabelFor(jcbCompletion);
137 		GridBagHelper.add(jpMisc, jcbCompletion, false);
138 
139 		for (int i = 0; i < jcbCompletion.getItemCount(); i++) {
140 			if (prefs.getDefaultCompletionMode().equals
141 				(jcbCompletion.getItemAt(i))) {
142 				jcbCompletion.setSelectedIndex(i);
143 				break;
144 			}
145 		}
146 
147 		// help
148 		CSH.setHelpIDString(jcbCompletion, "default-completion");
149 
150 		GridBagHelper.add(this, jpMisc);
151 
152 		GridBagHelper.addVerticalSpacer(this);
153 		GUIHelper.setMnemonics(this);
154     }
155 
156     //--- Method(s) ---
157 
158     public void apply() 
159     {
160 		prefs.setUsername(jteUsername.getText());
161 		prefs.setPassword(jtePassword.getText());
162 		prefs.setEmail(jteEmail.getText());
163 		prefs.setLanguage(((Locale)jcbLanguage.getSelectedItem()).getLanguage());
164 
165 		prefs.setAutoVersionCheck(jcAutoVersionCheck.isSelected());
166 		prefs.setUseEmacsKeyBindings(jcEmacsKeyBindings.isSelected());
167 		prefs.setDefaultCompletionMode(jcbCompletion.getSelectedItem().toString());
168     }
169 
170     public String getTitle()
171     {
172 		return XNap.tr("Personal");
173     }
174 
175     protected class LocaleCellRenderer extends DefaultListCellRenderer {
176 
177 		public Component getListCellRendererComponent
178 			(JList list, Object val, int idx, boolean isSel, boolean hasFocus) 
179 		{
180 			super.getListCellRendererComponent
181 				(list, val, idx, isSel, hasFocus);
182 
183 			String s = ((Locale)val).getDisplayLanguage();
184 			setText((s.length() > 0) ? s : XNap.tr("System Default"));
185 
186 			return this;
187 		}
188     }
189 
190 	private class CompletionModeCellRenderer extends DefaultListCellRenderer
191 	{
192 		public Component getListCellRendererComponent
193 			(JList list, Object val, int idx, boolean isSel, boolean hasFocus) 
194 		{
195 			super.getListCellRendererComponent(list, val, idx, isSel, hasFocus);
196 			setText(XNap.tr(val.toString()));
197 			return this;
198 		}
199 	}
200 }
201 
202 
203