Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jgift/ui/preferences/SharingPreferencePanel.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.DefaultListModel;
32  import javax.swing.JButton;
33  import javax.swing.JCheckBox;
34  import javax.swing.JFileChooser;
35  import javax.swing.JLabel;
36  import javax.swing.JList;
37  import javax.swing.JPanel;
38  import javax.swing.JRadioButton;
39  import javax.swing.JScrollPane;
40  import javax.swing.JTextField;
41  import javax.swing.border.TitledBorder;
42  import jgift.GiftUtil;
43  
44  /**
45   * User interface for modifying the giFT sharing configuration settings.
46   *
47   * @author  Jason Shobe
48   * @version $Revision: 1.3 $
49   */
50  public class SharingPreferencePanel extends AbstractPreferencePanel {
51     /**
52      * Create a new instance of SharingPreferencePanel.
53      */
54     public SharingPreferencePanel() {
55        bundle = ResourceBundle.getBundle("jgift/Bundle");
56        
57        setLayout(new GridBagLayout());
58        initComponents();
59     }
60     
61     /**
62      * Populate the fields of this form using the specified property set.
63      *
64      * @param properties the property set.
65      */
66     public void populate(Properties properties) {
67        String prop = properties.getProperty("sharing.max_peruser_uploads", "1");
68        maxPeruserField.setText(prop);
69        
70        prop = properties.getProperty("sharing.hide_dot_files", "1");
71        hideDotFilesCheck.setSelected(!prop.equals("0"));
72        
73        prop = properties.getProperty("sharing.root", "");
74        StringTokenizer tokens = new StringTokenizer(prop, ":", false);
75        DefaultListModel model = (DefaultListModel) shareDirList.getModel();
76        
77        while(tokens.hasMoreTokens()) {
78           String token = tokens.nextToken().trim();
79           model.addElement(token);
80        }
81        
82        prop = properties.getProperty("sharing.max_uploads", "-1");
83        maxUploadsField.setText(prop);
84        
85        prop = properties.getProperty("sharing.auto_resync_interval", "86400");
86        int interval = Integer.parseInt(prop);
87        
88        if(interval == 0) {
89           noResyncRadio.setSelected(true);
90        }
91        else if(interval == 3600) {
92           hourlyResyncRadio.setSelected(true);
93        }
94        else if(interval == 86400) {
95           dailyResyncRadio.setSelected(true);
96        }
97        else if(interval == 604800) {
98           weeklyResyncRadio.setSelected(true);
99        }
100       else {
101          otherResyncRadio.setSelected(true);
102          otherResyncField.setEnabled(true);
103          otherResyncField.setText(prop);
104       }
105       
106       prop = properties.getProperty("sharing.share_completed", "1");
107       shareCompletedCheck.setSelected(!prop.equals("0"));
108    }
109    
110    /**
111     * Update the specified property set with the values of the fields of this
112     * form.
113     *
114     * @param properties the property set.
115     *
116     * @throws IllegalStateException if one of the fields has an invalid value.
117     */
118    public void update(Properties properties) throws IllegalStateException {
119       try {
120          Integer.parseInt(maxPeruserField.getText());
121       }
122       catch(NumberFormatException nfe) {
123          throw new IllegalStateException("ERR_INVALID_PERUSER");
124       }
125       
126       properties.setProperty("sharing.max_peruser_uploads",
127                              maxPeruserField.getText());
128       
129       properties.setProperty("sharing.hide_dot_files",
130                              hideDotFilesCheck.isSelected() ? "1" : "0");
131                              
132       DefaultListModel model = (DefaultListModel) shareDirList.getModel();
133       StringBuffer buff = new StringBuffer();
134       
135       for(int i = 0; i < model.getSize(); i++) {
136          if(i > 0) {
137             buff.append(':');
138          }
139          
140          buff.append((String) model.get(i));
141       }
142       
143       properties.setProperty("sharing.root", buff.toString());
144       
145       try {
146          Integer.parseInt(maxUploadsField.getText());
147       }
148       catch(NumberFormatException nfe) {
149          throw new IllegalStateException("ERR_INVALID_UPLOADS");
150       }
151       
152       properties.setProperty("sharing.max_uploads",
153                              maxUploadsField.getText());
154       
155       if(noResyncRadio.isSelected()) {
156          System.setProperty("sharing.auto_resync_interval", "0");
157       }
158       
159       else if(hourlyResyncRadio.isSelected()) {
160          System.setProperty("sharing.auto_resync_interval", "3600");
161       }
162       else if(dailyResyncRadio.isSelected()) {
163          System.setProperty("sharing.auto_resync_interval", "86400");
164       }
165       else if(weeklyResyncRadio.isSelected()) {
166          System.setProperty("sharing.auto_resync_interval", "604800");
167       }
168       else {
169          try {
170             Integer.parseInt(otherResyncField.getText());
171          }
172          catch(NumberFormatException nfe) {
173             throw new IllegalStateException("ERR_INVALID_RESYNC");
174          }
175          
176          System.setProperty("sharing.auto_resync_interval",
177                             otherResyncField.getText());
178       }
179       
180       properties.setProperty("sharing.share_completed",
181                              shareCompletedCheck.isSelected() ? "1" : "0");
182    }
183    
184    /**
185     * This method is called from within the constructor to initialize the form.
186     */
187    private void initComponents() {
188       GridBagConstraints gridBagConstraints;
189       Font font = new Font("Dialog", 0, 10);
190 
191       maxUploadsLabel.setFont(font);
192       maxUploadsLabel.setText(bundle.getString("LBL_MAX_UPLOADS"));
193       gridBagConstraints = new GridBagConstraints();
194       gridBagConstraints.gridx = 0;
195       gridBagConstraints.gridy = 0;
196       gridBagConstraints.insets = new Insets(5, 5, 5, 5);
197       gridBagConstraints.anchor = GridBagConstraints.EAST;
198       add(maxUploadsLabel, gridBagConstraints);
199 
200       maxUploadsField.setFont(font);
201       gridBagConstraints = new GridBagConstraints();
202       gridBagConstraints.gridx = 1;
203       gridBagConstraints.gridy = 0;
204       gridBagConstraints.gridwidth = 2;
205       gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
206       gridBagConstraints.insets = new Insets(5, 5, 5, 5);
207       gridBagConstraints.weightx = 1.0;
208       add(maxUploadsField, gridBagConstraints);
209       
210       maxPeruserLabel.setFont(font);
211       maxPeruserLabel.setText(bundle.getString("LBL_MAX_PERUSER_UPLOADS"));
212       gridBagConstraints = new GridBagConstraints();
213       gridBagConstraints.gridx = 0;
214       gridBagConstraints.gridy = 1;
215       gridBagConstraints.insets = new Insets(0, 5, 5, 5);
216       gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
217       add(maxPeruserLabel, gridBagConstraints);
218 
219       maxPeruserField.setFont(font);
220       gridBagConstraints = new GridBagConstraints();
221       gridBagConstraints.gridx = 1;
222       gridBagConstraints.gridy = 1;
223       gridBagConstraints.gridwidth = 2;
224       gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
225       gridBagConstraints.insets = new Insets(0, 5, 5, 5);
226       gridBagConstraints.weightx = 1.0;
227       add(maxPeruserField, gridBagConstraints);
228 
229       shareDirLabel.setFont(font);
230       shareDirLabel.setText(bundle.getString("LBL_SHARE_DIR"));
231       gridBagConstraints = new GridBagConstraints();
232       gridBagConstraints.gridx = 0;
233       gridBagConstraints.gridy = 2;
234       gridBagConstraints.gridheight = 2;
235       gridBagConstraints.insets = new Insets(0, 5, 5, 5);
236       gridBagConstraints.anchor = GridBagConstraints.NORTHEAST;
237       add(shareDirLabel, gridBagConstraints);
238 
239       shareDirList.setFont(font);
240       shareDirList.setModel(new DefaultListModel());
241       shareDirScroll.setViewportView(shareDirList);
242 
243       gridBagConstraints = new GridBagConstraints();
244       gridBagConstraints.gridx = 1;
245       gridBagConstraints.gridy = 2;
246       gridBagConstraints.gridheight = 2;
247       gridBagConstraints.fill = GridBagConstraints.BOTH;
248       gridBagConstraints.insets = new Insets(0, 5, 5, 5);
249       gridBagConstraints.weightx = 1.0;
250       gridBagConstraints.weighty = 1.0;
251       add(shareDirScroll, gridBagConstraints);
252 
253       addShareButton.setFont(font);
254       addShareButton.setText(bundle.getString("LBL_ADD"));
255       addShareButton.addActionListener(new ActionListener() {
256          public void actionPerformed(ActionEvent evt) {
257             addSharedDir();
258          }
259       });
260       gridBagConstraints = new GridBagConstraints();
261       gridBagConstraints.gridx = 2;
262       gridBagConstraints.gridy = 2;
263       gridBagConstraints.insets = new Insets(0, 5, 2, 5);
264       gridBagConstraints.anchor = GridBagConstraints.SOUTHWEST;
265       add(addShareButton, gridBagConstraints);
266 
267       removeShareButton.setFont(font);
268       removeShareButton.setText(bundle.getString("LBL_REMOVE"));
269       removeShareButton.addActionListener(new ActionListener() {
270          public void actionPerformed(ActionEvent evt) {
271             removeSharedDir();
272          }
273       });
274       gridBagConstraints = new GridBagConstraints();
275       gridBagConstraints.gridx = 2;
276       gridBagConstraints.gridy = 3;
277       gridBagConstraints.insets = new Insets(2, 5, 5, 5);
278       gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
279       add(removeShareButton, gridBagConstraints);
280 
281       resyncPanel.setLayout(new GridBagLayout());
282       resyncPanel.setBorder(new TitledBorder(null,
283                             bundle.getString("LBL_RESYNC_INTERVAL"),
284                             TitledBorder.DEFAULT_JUSTIFICATION,
285                             TitledBorder.DEFAULT_POSITION, font));
286       gridBagConstraints = new GridBagConstraints();
287       gridBagConstraints.gridwidth = 3;
288       gridBagConstraints.gridx = 0;
289       gridBagConstraints.gridy = 5;
290       gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
291       gridBagConstraints.weightx = 1.0;
292       gridBagConstraints.insets = new Insets(0, 5, 5, 5);
293       gridBagConstraints.anchor = GridBagConstraints.EAST;
294       add(resyncPanel, gridBagConstraints);
295       
296       noResyncRadio.setText(bundle.getString("LBL_SYNC_NONE"));
297       noResyncRadio.setFont(font);
298       noResyncRadio.addActionListener(new ActionListener() {
299          public void actionPerformed(ActionEvent evt) {
300             otherResyncField.setEnabled(false);
301          }
302       });
303       gridBagConstraints = new GridBagConstraints();
304       gridBagConstraints.gridx = 0;
305       gridBagConstraints.gridy = 0;
306       gridBagConstraints.insets = new Insets(5, 5, 5, 5);
307       gridBagConstraints.anchor = GridBagConstraints.WEST;
308       resyncPanel.add(noResyncRadio, gridBagConstraints);
309       
310       hourlyResyncRadio.setText(bundle.getString("LBL_SYNC_HOURLY"));
311       hourlyResyncRadio.setFont(font);
312       hourlyResyncRadio.addActionListener(new ActionListener() {
313          public void actionPerformed(ActionEvent evt) {
314             otherResyncField.setEnabled(false);
315          }
316       });
317       gridBagConstraints = new GridBagConstraints();
318       gridBagConstraints.gridx = 1;
319       gridBagConstraints.gridy = 0;
320       gridBagConstraints.insets = new Insets(5, 5, 5, 5);
321       gridBagConstraints.anchor = GridBagConstraints.WEST;
322       resyncPanel.add(hourlyResyncRadio, gridBagConstraints);
323       
324       dailyResyncRadio.setText(bundle.getString("LBL_SYNC_DAILY"));
325       dailyResyncRadio.setFont(font);
326       dailyResyncRadio.addActionListener(new ActionListener() {
327          public void actionPerformed(ActionEvent evt) {
328             otherResyncField.setEnabled(false);
329          }
330       });
331       gridBagConstraints = new GridBagConstraints();
332       gridBagConstraints.gridx = 2;
333       gridBagConstraints.gridy = 0;
334       gridBagConstraints.insets = new Insets(5, 5, 5, 5);
335       gridBagConstraints.anchor = GridBagConstraints.WEST;
336       resyncPanel.add(dailyResyncRadio, gridBagConstraints);
337       
338       weeklyResyncRadio.setText(bundle.getString("LBL_SYNC_WEEKLY"));
339       weeklyResyncRadio.setFont(font);
340       weeklyResyncRadio.addActionListener(new ActionListener() {
341          public void actionPerformed(ActionEvent evt) {
342             otherResyncField.setEnabled(false);
343          }
344       });
345       gridBagConstraints = new GridBagConstraints();
346       gridBagConstraints.gridx = 3;
347       gridBagConstraints.gridy = 0;
348       gridBagConstraints.insets = new Insets(5, 5, 5, 5);
349       gridBagConstraints.anchor = GridBagConstraints.WEST;
350       resyncPanel.add(weeklyResyncRadio, gridBagConstraints);
351       
352       otherResyncRadio.setText(bundle.getString("LBL_OTHER"));
353       otherResyncRadio.setFont(font);
354       otherResyncRadio.addActionListener(new ActionListener() {
355          public void actionPerformed(ActionEvent evt) {
356             otherResyncField.setEnabled(true);
357          }
358       });
359       gridBagConstraints = new GridBagConstraints();
360       gridBagConstraints.gridx = 0;
361       gridBagConstraints.gridy = 1;
362       gridBagConstraints.insets = new Insets(0, 5, 5, 5);
363       gridBagConstraints.anchor = GridBagConstraints.WEST;
364       resyncPanel.add(otherResyncRadio, gridBagConstraints);
365 
366       otherResyncField.setFont(font);
367       otherResyncField.setEnabled(false);
368       gridBagConstraints = new GridBagConstraints();
369       gridBagConstraints.gridwidth = 3;
370       gridBagConstraints.gridx = 1;
371       gridBagConstraints.gridy = 1;
372       gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
373       gridBagConstraints.weightx = 1.0;
374       gridBagConstraints.insets = new Insets(0, 5, 5, 5);
375       gridBagConstraints.anchor = GridBagConstraints.CENTER;
376       resyncPanel.add(otherResyncField, gridBagConstraints);
377       
378       resyncGroup.add(noResyncRadio);
379       resyncGroup.add(hourlyResyncRadio);
380       resyncGroup.add(dailyResyncRadio);
381       resyncGroup.add(weeklyResyncRadio);
382       resyncGroup.add(otherResyncRadio);
383 
384       hideDotFilesCheck.setFont(font);
385       hideDotFilesCheck.setText(bundle.getString("LBL_HIDE_DOT_FILES"));
386       gridBagConstraints = new GridBagConstraints();
387       gridBagConstraints.gridx = 0;
388       gridBagConstraints.gridy = 6;
389       gridBagConstraints.gridwidth = 3;
390       gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
391       gridBagConstraints.insets = new Insets(0, 5, 5, 5);
392       gridBagConstraints.anchor = GridBagConstraints.EAST;
393       gridBagConstraints.weightx = 1.0;
394       add(hideDotFilesCheck, gridBagConstraints);
395 
396       shareCompletedCheck.setFont(font);
397       shareCompletedCheck.setText(bundle.getString("LBL_SHARE_COMPLETED"));
398       gridBagConstraints = new GridBagConstraints();
399       gridBagConstraints.gridx = 0;
400       gridBagConstraints.gridy = 7;
401       gridBagConstraints.gridwidth = 3;
402       gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
403       gridBagConstraints.insets = new Insets(0, 5, 5, 5);
404       gridBagConstraints.anchor = GridBagConstraints.WEST;
405       gridBagConstraints.weightx = 1.0;
406       add(shareCompletedCheck, gridBagConstraints);
407    }
408    
409    /**
410     * Add a directory to the shared directory list.
411     */
412    private void addSharedDir() {
413       JFileChooser chooser = new JFileChooser(System.getProperty("user.home"));
414       chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
415       chooser.setMultiSelectionEnabled(false);
416       chooser.setFileHidingEnabled(false);
417       
418       if(chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
419          ((DefaultListModel) shareDirList.getModel()).addElement(GiftUtil.
420             normalizePath(chooser.getSelectedFile().getAbsolutePath()));
421       }
422    }
423    
424    /**
425     * Remove the selected directories from the shared directory list.
426     */
427    private void removeSharedDir() {
428       Object[] selected = shareDirList.getSelectedValues();
429       DefaultListModel model = (DefaultListModel) shareDirList.getModel();
430       
431       for(int i = 0; i < selected.length; i++) {
432          model.removeElement(selected[i]);
433       }
434    }
435    
436    private JLabel maxPeruserLabel = new JLabel();
437    private JTextField maxPeruserField = new JTextField();
438    private JCheckBox hideDotFilesCheck = new JCheckBox();
439    private JLabel shareDirLabel = new JLabel();
440    private JScrollPane shareDirScroll = new JScrollPane();
441    private JList shareDirList = new JList();
442    private JButton addShareButton = new JButton();
443    private JButton removeShareButton = new JButton();
444    private JLabel maxUploadsLabel = new JLabel();
445    private JTextField maxUploadsField = new JTextField();
446    private JPanel resyncPanel = new JPanel();
447    private JRadioButton noResyncRadio = new JRadioButton();
448    private JRadioButton hourlyResyncRadio = new JRadioButton();
449    private JRadioButton dailyResyncRadio = new JRadioButton();
450    private JRadioButton weeklyResyncRadio = new JRadioButton();
451    private JRadioButton otherResyncRadio = new JRadioButton();
452    private ButtonGroup resyncGroup = new ButtonGroup();
453    private JTextField otherResyncField = new JTextField();
454    private JCheckBox shareCompletedCheck = new JCheckBox();
455       
456    private ResourceBundle bundle = null;
457 }
458