Source code: jgift/ui/preferences/MainPreferencePanel.java
1 /*
2 * This file is part of jgiFT.
3 * Copyright (C) 2003, Jason Shobe
4 *
5 * jgiFT is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * jgiFT 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 jgiFT; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 package jgift.ui.preferences;
20
21 import java.awt.Font;
22 import java.awt.GridBagConstraints;
23 import java.awt.GridBagLayout;
24 import java.awt.Insets;
25 import java.awt.event.ActionEvent;
26 import java.awt.event.ActionListener;
27 import java.util.Properties;
28 import java.util.ResourceBundle;
29 import java.util.StringTokenizer;
30 import javax.swing.ButtonGroup;
31 import javax.swing.JCheckBox;
32 import javax.swing.JLabel;
33 import javax.swing.JPanel;
34 import javax.swing.JRadioButton;
35 import javax.swing.JTextField;
36 import javax.swing.border.TitledBorder;
37
38 /**
39 * User interface for modifying the main giFT configuration settings.
40 *
41 * @author Jason Shobe
42 * @version $Revision: 1.2 $
43 */
44 public class MainPreferencePanel extends AbstractPreferencePanel {
45 /**
46 * Creates a new instance of MainPreferencePanel.
47 */
48 public MainPreferencePanel() {
49 bundle = ResourceBundle.getBundle("jgift/Bundle");
50
51 setLayout(new GridBagLayout());
52 initComponents();
53 }
54
55 /**
56 * Populate the fields of this form using the specified property set.
57 *
58 * @param properties the property set.
59 */
60 public void populate(Properties properties) {
61 String prop = properties.getProperty("main.hosts_allow", "LOCAL");
62
63 if(prop.equals("LOCAL")) {
64 localHostsRadio.setSelected(true);
65 }
66 else if(prop.equals("ALL")) {
67 allHostsRadio.setSelected(true);
68 }
69 else {
70 otherHostsRadio.setSelected(true);
71 otherHostsField.setText(prop);
72 otherHostsField.setEnabled(true);
73 }
74
75 prop = properties.getProperty("main.client_port", "1213");
76 clientPortField.setText(prop);
77
78 prop = properties.getProperty("main.follow_symlinks", "1");
79 symlinksCheck.setSelected(prop.equals("1"));
80
81 prop = properties.getProperty("main.plugins", "OpenFT");
82 StringTokenizer tokens = new StringTokenizer(prop, ":", false);
83
84 while(tokens.hasMoreTokens()) {
85 String plugin = tokens.nextToken().trim();
86
87 if(plugin.equals("OpenFT")) {
88 openFTCheck.setSelected(true);
89 }
90 else if(plugin.equals("Gnutella")) {
91 gnutellaCheck.setSelected(true);
92 }
93 else if(plugin.equals("FastTrack")) {
94 fastTrackCheck.setSelected(true);
95 }
96 }
97 }
98
99 /**
100 * Update the specified property set with the values of the fields of this
101 * form.
102 *
103 * @param properties the property set.
104 *
105 * @throws IllegalStateException if one of the fields has an invalid value.
106 */
107 public void update(Properties properties) throws IllegalStateException {
108 if(localHostsRadio.isSelected()) {
109 properties.setProperty("main.hosts_allow", "LOCAL");
110 }
111 else if(allHostsRadio.isSelected()) {
112 properties.setProperty("main.hosts_allow", "ALL");
113 }
114 else {
115 //TODO: validate hosts value
116 properties.setProperty("main.hosts_allow", otherHostsField.getText());
117 }
118
119 try {
120 Integer.parseInt(clientPortField.getText());
121 }
122 catch(NumberFormatException nfe) {
123 throw new IllegalStateException("ERR_INVALID_PORT");
124 }
125
126 properties.setProperty("main.client_port", clientPortField.getText());
127
128 properties.setProperty("main.follow_symlinks",
129 symlinksCheck.isSelected() ? "1" : "0");
130
131 StringBuffer prop = new StringBuffer();
132
133 if(openFTCheck.isSelected()) {
134 prop.append("OpenFT");
135 }
136
137 if(gnutellaCheck.isSelected()) {
138 if(prop.length() > 0) {
139 prop.append(':');
140 }
141
142 prop.append("Gnutella");
143 }
144
145 if(fastTrackCheck.isSelected()) {
146 if(prop.length() > 0) {
147 prop.append(':');
148 }
149
150 prop.append("FastTrack");
151 }
152
153 properties.setProperty("main.plugins", prop.toString());
154 }
155
156 /**
157 * This method is called from within the constructor to initialize the form.
158 */
159 private void initComponents() {
160 GridBagConstraints gridBagConstraints;
161 Font font = new Font("Dialog", 0, 10);
162
163 hostsPanel.setLayout(new GridBagLayout());
164 hostsPanel.setBorder(new TitledBorder(null,
165 bundle.getString("LBL_HOSTS_ALLOW"),
166 TitledBorder.DEFAULT_JUSTIFICATION,
167 TitledBorder.DEFAULT_POSITION, font));
168 gridBagConstraints = new GridBagConstraints();
169 gridBagConstraints.gridwidth = 2;
170 gridBagConstraints.gridx = 0;
171 gridBagConstraints.gridy = 0;
172 gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
173 gridBagConstraints.weightx = 1.0;
174 gridBagConstraints.insets = new Insets(5, 5, 5, 5);
175 gridBagConstraints.anchor = GridBagConstraints.EAST;
176 add(hostsPanel, gridBagConstraints);
177
178 localHostsRadio.setText(bundle.getString("LBL_LOCAL_HOSTS"));
179 localHostsRadio.setFont(font);
180 localHostsRadio.addActionListener(new ActionListener() {
181 public void actionPerformed(ActionEvent evt) {
182 otherHostsField.setEnabled(false);
183 }
184 });
185 gridBagConstraints = new GridBagConstraints();
186 gridBagConstraints.gridx = 0;
187 gridBagConstraints.gridy = 0;
188 gridBagConstraints.insets = new Insets(5, 5, 5, 5);
189 gridBagConstraints.anchor = GridBagConstraints.WEST;
190 hostsPanel.add(localHostsRadio, gridBagConstraints);
191
192 allHostsRadio.setText(bundle.getString("LBL_ALL_HOSTS"));
193 allHostsRadio.setFont(font);
194 allHostsRadio.addActionListener(new ActionListener() {
195 public void actionPerformed(ActionEvent evt) {
196 otherHostsField.setEnabled(false);
197 }
198 });
199 gridBagConstraints = new GridBagConstraints();
200 gridBagConstraints.gridx = 1;
201 gridBagConstraints.gridy = 0;
202 gridBagConstraints.insets = new Insets(5, 5, 5, 5);
203 gridBagConstraints.anchor = GridBagConstraints.WEST;
204 hostsPanel.add(allHostsRadio, gridBagConstraints);
205
206 otherHostsRadio.setText(bundle.getString("LBL_OTHER"));
207 otherHostsRadio.setFont(font);
208 otherHostsRadio.addActionListener(new ActionListener() {
209 public void actionPerformed(ActionEvent evt) {
210 otherHostsField.setEnabled(true);
211 }
212 });
213 gridBagConstraints = new GridBagConstraints();
214 gridBagConstraints.gridx = 2;
215 gridBagConstraints.gridy = 0;
216 gridBagConstraints.insets = new Insets(5, 5, 5, 5);
217 gridBagConstraints.anchor = GridBagConstraints.WEST;
218 hostsPanel.add(otherHostsRadio, gridBagConstraints);
219
220 otherHostsField.setFont(font);
221 otherHostsField.setEnabled(false);
222 gridBagConstraints = new GridBagConstraints();
223 gridBagConstraints.gridx = 3;
224 gridBagConstraints.gridy = 0;
225 gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
226 gridBagConstraints.weightx = 1.0;
227 gridBagConstraints.insets = new Insets(5, 5, 5, 5);
228 gridBagConstraints.anchor = GridBagConstraints.CENTER;
229 hostsPanel.add(otherHostsField, gridBagConstraints);
230
231 hostsGroup.add(localHostsRadio);
232 hostsGroup.add(allHostsRadio);
233 hostsGroup.add(otherHostsRadio);
234
235 clientPortLabel.setFont(font);
236 clientPortLabel.setText(bundle.getString("LBL_CLIENT_PORT"));
237 gridBagConstraints = new GridBagConstraints();
238 gridBagConstraints.gridx = 0;
239 gridBagConstraints.gridy = 1;
240 gridBagConstraints.insets = new Insets(0, 5, 5, 5);
241 gridBagConstraints.anchor = GridBagConstraints.NORTHEAST;
242 add(clientPortLabel, gridBagConstraints);
243
244 clientPortField.setFont(font);
245 gridBagConstraints = new GridBagConstraints();
246 gridBagConstraints.gridx = 1;
247 gridBagConstraints.gridy = 1;
248 gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
249 gridBagConstraints.insets = new Insets(0, 5, 5, 5);
250 gridBagConstraints.weightx = 1.0;
251 add(clientPortField, gridBagConstraints);
252
253 symlinksCheck.setFont(font);
254 symlinksCheck.setText(bundle.getString("LBL_FOLLOW_SYMLINKS"));
255 gridBagConstraints = new GridBagConstraints();
256 gridBagConstraints.gridx = 0;
257 gridBagConstraints.gridy = 2;
258 gridBagConstraints.gridwidth = 2;
259 gridBagConstraints.insets = new Insets(0, 5, 5, 5);
260 gridBagConstraints.anchor = GridBagConstraints.WEST;
261 add(symlinksCheck, gridBagConstraints);
262
263 pluginPanel.setLayout(new GridBagLayout());
264
265 pluginPanel.setBorder(new TitledBorder(null,
266 bundle.getString("LBL_PLUGINS"),
267 TitledBorder.DEFAULT_JUSTIFICATION,
268 TitledBorder.DEFAULT_POSITION, font));
269 openFTCheck.setFont(font);
270 openFTCheck.setText("OpenFT");
271 gridBagConstraints = new GridBagConstraints();
272 gridBagConstraints.gridx = 0;
273 gridBagConstraints.gridy = 0;
274 gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
275 gridBagConstraints.insets = new Insets(5, 5, 5, 5);
276 gridBagConstraints.anchor = GridBagConstraints.WEST;
277 gridBagConstraints.weightx = 1.0;
278 pluginPanel.add(openFTCheck, gridBagConstraints);
279
280 gnutellaCheck.setFont(font);
281 gnutellaCheck.setText("Gnutella");
282 gridBagConstraints = new GridBagConstraints();
283 gridBagConstraints.gridx = 0;
284 gridBagConstraints.gridy = 1;
285 gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
286 gridBagConstraints.insets = new Insets(0, 5, 5, 5);
287 gridBagConstraints.anchor = GridBagConstraints.WEST;
288 gridBagConstraints.weightx = 1.0;
289 pluginPanel.add(gnutellaCheck, gridBagConstraints);
290
291 fastTrackCheck.setFont(font);
292 fastTrackCheck.setText("FastTrack");
293 gridBagConstraints = new GridBagConstraints();
294 gridBagConstraints.gridx = 0;
295 gridBagConstraints.gridy = 2;
296 gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
297 gridBagConstraints.insets = new Insets(0, 5, 5, 5);
298 gridBagConstraints.anchor = GridBagConstraints.WEST;
299 gridBagConstraints.weightx = 1.0;
300 pluginPanel.add(fastTrackCheck, gridBagConstraints);
301
302 gridBagConstraints = new GridBagConstraints();
303 gridBagConstraints.gridx = 0;
304 gridBagConstraints.gridy = 3;
305 gridBagConstraints.gridwidth = 2;
306 gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
307 gridBagConstraints.insets = new Insets(0, 5, 5, 5);
308 gridBagConstraints.weightx = 1.0;
309 add(pluginPanel, gridBagConstraints);
310 }
311
312 private JPanel hostsPanel = new JPanel();
313 private JRadioButton localHostsRadio = new JRadioButton();
314 private JRadioButton allHostsRadio = new JRadioButton();
315 private JRadioButton otherHostsRadio = new JRadioButton();
316 private JTextField otherHostsField = new JTextField();
317 private ButtonGroup hostsGroup = new ButtonGroup();
318 private JLabel clientPortLabel = new JLabel();
319 private JTextField clientPortField = new JTextField();
320 private JCheckBox symlinksCheck = new JCheckBox();
321 private JPanel pluginPanel = new JPanel();
322 private JCheckBox openFTCheck = new JCheckBox();
323 private JCheckBox gnutellaCheck = new JCheckBox();
324 private JCheckBox fastTrackCheck = new JCheckBox();
325
326 private ResourceBundle bundle = null;
327 }
328