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  
25  import javax.swing.JLabel;
26  import javax.swing.JPanel;
27  
28  import org.xnap.XNap;
29  import org.xnap.gui.AbstractSettingsPanel;
30  import org.xnap.gui.action.EnableAction;
31  import org.xnap.gui.component.ValidatedTextField;
32  import org.xnap.gui.component.XNapCheckBox;
33  import org.xnap.gui.util.GUIHelper;
34  import org.xnap.gui.util.GridBagHelper;
35  
36  public class TransferPrefsPanel extends AbstractSettingsPanel {
37      
38      //--- Data field(s) ---
39  
40      private EnableAction acMaxDl;
41      private ValidatedTextField jtMaxDl;
42  
43      private EnableAction acMaxUl;
44      private ValidatedTextField jtMaxUl;
45  
46      private EnableAction acMaxDlPerUser;
47      private ValidatedTextField jtMaxDlPerUser;
48  
49      private EnableAction acMaxUlPerUser;
50      private ValidatedTextField jtMaxUlPerUser;
51  
52      private EnableAction acDlThrottle;
53      private ValidatedTextField jtDlThrottle;
54  
55      private EnableAction acUlThrottle;
56      private ValidatedTextField jtUlThrottle;
57  
58      //--- Constructor(s) ---
59  
60      public TransferPrefsPanel()
61      {
62  		setLayout(new GridBagLayout());
63  
64  		// limits
65  		JPanel jpLimits = new JPanel(new GridBagLayout());
66  		jpLimits.setBorder(GUIHelper.createDefaultBorder(XNap.tr("Limits")));
67  		GridBagHelper.add(this, jpLimits);
68  
69          jtMaxDl = new ValidatedTextField
70  			(prefs.getMaxDownloads() + "", 5, ValidatedTextField.NUMBERS_INT);
71  		acMaxDl = new EnableAction(XNap.tr("Maximum Downloads"), 
72  								   new Component[] { jtMaxDl },
73  								   prefs.getLimitDownloads());
74          GridBagHelper.addComponent(jpLimits, new XNapCheckBox(acMaxDl));
75          GridBagHelper.add(jpLimits, jtMaxDl, false);
76  
77  		jtMaxUl = new ValidatedTextField
78  			(prefs.getMaxUploads() + "", 5, ValidatedTextField.NUMBERS_INT);
79  		acMaxUl = new EnableAction(XNap.tr("Maximum Uploads"), 
80  								   new Component[] { jtMaxUl },
81  								   prefs.getLimitUploads());
82  		GridBagHelper.addComponent(jpLimits, new XNapCheckBox(acMaxUl));
83  		GridBagHelper.add(jpLimits, jtMaxUl, false);
84  
85  		// user limits
86  		JPanel jpUserLimits = new JPanel(new GridBagLayout());
87  		jpUserLimits.setBorder(GUIHelper.createDefaultBorder(XNap.tr("User Limits")));
88  		GridBagHelper.add(this, jpUserLimits);
89  
90          jtMaxDlPerUser = new ValidatedTextField
91  			(prefs.getMaxDownloadsPerUser() + "", 5, 
92  			 ValidatedTextField.NUMBERS_INT);
93  		acMaxDlPerUser = new EnableAction(XNap.tr("Maximum Downloads Per User"), 
94  										  new Component[] { jtMaxDlPerUser },
95  										  prefs.getLimitDownloadsPerUser());
96          GridBagHelper.addComponent(jpUserLimits, 
97  								   new XNapCheckBox(acMaxDlPerUser));
98          GridBagHelper.add(jpUserLimits, jtMaxDlPerUser, false);
99  
100 		jtMaxUlPerUser = new ValidatedTextField
101 			(prefs.getMaxUploadsPerUser() + "", 5, 
102 			 ValidatedTextField.NUMBERS_INT);
103 		acMaxUlPerUser = new EnableAction(XNap.tr("Maximum Uploads Per User"), 
104 										  new Component[] { jtMaxUlPerUser },
105 										  prefs.getLimitUploadsPerUser());
106 		GridBagHelper.addComponent(jpUserLimits, 
107 								   new XNapCheckBox(acMaxUlPerUser));
108 		GridBagHelper.add(jpUserLimits, jtMaxUlPerUser, false);
109 
110 		// throttle
111 		JPanel jpThrottle = new JPanel(new GridBagLayout());
112 		jpThrottle.setBorder(GUIHelper.createDefaultBorder(XNap.tr("Throttle")));
113 
114 		jtDlThrottle = new ValidatedTextField
115 			(prefs.getDownloadThrottle() + "", 5, 
116 			 ValidatedTextField.NUMBERS_INT);
117 		acDlThrottle = new EnableAction(XNap.tr("Maximum Download Rate"), 
118 										new Component[] {jtDlThrottle},
119 										prefs.getThrottleDownloads());
120 		GridBagHelper.addComponent(jpThrottle, new XNapCheckBox(acDlThrottle));
121 		GridBagHelper.addComponent(jpThrottle, jtDlThrottle);
122 		GridBagHelper.addLabel(jpThrottle, XNap.tr("KB/s"));
123 		// XXX: workaroung to ensure left alignment
124 		GridBagHelper.add(jpThrottle, new JLabel());
125 
126 		jtUlThrottle 
127 			= new ValidatedTextField(prefs.getUploadThrottle() + "", 5, 
128 									 ValidatedTextField.NUMBERS_INT);
129 		acUlThrottle = new EnableAction(XNap.tr("Maximum Upload Rate"), 
130 										new Component[] {jtUlThrottle},
131 										prefs.getThrottleUploads());
132 		GridBagHelper.addComponent(jpThrottle, new XNapCheckBox(acUlThrottle));
133 		GridBagHelper.addComponent(jpThrottle, jtUlThrottle);
134 		GridBagHelper.addLabel(jpThrottle, XNap.tr("KB/s"));
135 		// XXX: workaroung to ensure left alignment
136 		GridBagHelper.add(jpThrottle, new JLabel());
137 
138 		GridBagHelper.add(this, jpThrottle);
139 
140 		GridBagHelper.addVerticalSpacer(this);
141 		GUIHelper.setMnemonics(this);
142     }
143 
144     public void apply() 
145     {
146 		prefs.setLimitDownloads(acMaxDl.isSelected());
147 		prefs.setMaxDownloads(jtMaxDl.getIntValue());
148 
149 		prefs.setLimitUploads(acMaxUl.isSelected());
150 		prefs.setMaxUploads(jtMaxUl.getIntValue());
151 
152 		prefs.setLimitDownloadsPerUser(acMaxDlPerUser.isSelected());
153 		prefs.setMaxDownloadsPerUser(jtMaxDlPerUser.getIntValue());
154 
155 		prefs.setLimitUploadsPerUser(acMaxUlPerUser.isSelected());
156 		prefs.setMaxUploadsPerUser(jtMaxUlPerUser.getIntValue());
157 
158 		prefs.setThrottleDownloads(acDlThrottle.isSelected());
159 		prefs.setDownloadThrottle(jtDlThrottle.getIntValue());
160 
161 		prefs.setThrottleUploads(acUlThrottle.isSelected());
162 		prefs.setUploadThrottle(jtUlThrottle.getIntValue());
163     }
164 
165     public String getTitle()
166     {
167 		return XNap.tr("Transfer");
168     }
169 
170 }