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;
21  
22  import java.awt.Component;
23  import java.awt.GridBagLayout;
24  import java.awt.event.ActionEvent;
25  import java.awt.event.KeyEvent;
26  import java.beans.PropertyChangeEvent;
27  import java.beans.PropertyChangeListener;
28  
29  import javax.swing.AbstractAction;
30  import javax.swing.Action;
31  import javax.swing.JPanel;
32  
33  import org.xnap.XNap;
34  import org.xnap.gui.action.EnableAction;
35  import org.xnap.gui.component.ValidatedTextField;
36  import org.xnap.gui.component.XNapCheckBox;
37  import org.xnap.gui.util.GUIHelper;
38  import org.xnap.gui.util.GridBagHelper;
39  import org.xnap.util.Preferences;
40  
41  /***
42   * Provides a simple panel with transfer limit and throttle settings.
43   */
44  public class TransferSettingsPanel extends JPanel
45      implements PropertyChangeListener
46  {
47  
48      //--- Data field(s) ---
49  
50      private static Preferences prefs = Preferences.getInstance();
51  
52      private ValidatedTextField jtDlThrottle;
53      private ValidatedTextField jtUlThrottle;
54      private ValidatedTextField jtMaxDl;
55      private ValidatedTextField jtMaxUl;
56  
57      private MyEnableAction acMaxDl;
58      private MyEnableAction acMaxUl;
59      private MyEnableAction acDlThrottle;
60      private MyEnableAction acUlThrottle;
61  
62      private ApplyAction acApply = new ApplyAction();
63  
64      //--- Constructor(s) ---
65  
66      public TransferSettingsPanel() 
67      {
68          initialize();
69  
70  		prefs.addPropertyChangeListener(this);
71      }
72  
73      //--- Method(s) ---
74  
75      private void initialize() 
76      {
77  		setLayout(new GridBagLayout());
78  
79  		// limits
80  		JPanel jpLimits = new JPanel();
81  		jpLimits.setLayout(new GridBagLayout());
82  		jpLimits.setBorder(GUIHelper.createDefaultBorder(XNap.tr("Transfer Limit")));
83  		GridBagHelper.add(this, jpLimits);
84  
85  		jtMaxDl = new ValidatedTextField
86  			(prefs.getMaxDownloads() + "", 3, ValidatedTextField.NUMBERS_INT);
87  		jtMaxDl.setMinimumSize(jtMaxDl.getPreferredSize());
88  		GUIHelper.bindEnterKeyLocally(jtMaxDl, acApply);
89  		acMaxDl = new MyEnableAction(XNap.tr("Downloads"), 
90  									 new Component[] { jtMaxDl },
91  									 prefs.getLimitDownloads());
92  		GridBagHelper.addComponent(jpLimits, new XNapCheckBox(acMaxDl));
93  		GridBagHelper.add(jpLimits, jtMaxDl, false);
94  
95  		jtMaxUl = new ValidatedTextField
96  			(prefs.getMaxUploads() + "", 3, ValidatedTextField.NUMBERS_INT);
97  		jtMaxUl.setMinimumSize(jtMaxUl.getPreferredSize());
98  		GUIHelper.bindEnterKeyLocally(jtMaxUl, acApply);
99  		acMaxUl = new MyEnableAction(XNap.tr("Uploads"), 
100 									 new Component[] { jtMaxUl },
101 									 prefs.getLimitUploads());
102 		GridBagHelper.addComponent(jpLimits, new XNapCheckBox(acMaxUl));
103 		GridBagHelper.add(jpLimits, jtMaxUl, false);
104 
105 		// throttle
106 		JPanel jpThrottle = new JPanel(new GridBagLayout());
107 		jpThrottle.setBorder(GUIHelper.createDefaultBorder(XNap.tr("Throttle (KB/s)")));
108 		GridBagHelper.add(this, jpThrottle);
109 
110 		jtDlThrottle = new ValidatedTextField
111 			(prefs.getDownloadThrottle() + "", 3, 
112 			 ValidatedTextField.NUMBERS_INT);
113 		jtDlThrottle.setMinimumSize(jtDlThrottle.getPreferredSize());
114 		GUIHelper.bindEnterKeyLocally(jtDlThrottle, acApply);
115 		acDlThrottle = new MyEnableAction(XNap.tr("Downloads"), 
116 										  new Component[] {jtDlThrottle},
117 										  prefs.getThrottleDownloads());
118 		GridBagHelper.addComponent(jpThrottle, new XNapCheckBox(acDlThrottle));
119 		GridBagHelper.add(jpThrottle, jtDlThrottle, false);
120 		//GridBagHelper.add(jpThrottle, new JLabel(XNap.tr("kb/s", 1)), false);
121 
122 		jtUlThrottle 
123 			= new ValidatedTextField(prefs.getUploadThrottle() + "", 3, 
124 									 ValidatedTextField.NUMBERS_INT);
125 		jtUlThrottle.setMinimumSize(jtUlThrottle.getPreferredSize());
126 		GUIHelper.bindEnterKeyLocally(jtUlThrottle, acApply);
127 		acUlThrottle = new MyEnableAction(XNap.tr("Uploads"), 
128 										  new Component[] {jtUlThrottle},
129 										  prefs.getThrottleUploads());
130 		GridBagHelper.addComponent(jpThrottle, new XNapCheckBox(acUlThrottle));
131 		GridBagHelper.add(jpThrottle, jtUlThrottle, false);
132 		//GridBagHelper.add(jpThrottle, new JLabel(XNap.tr("kb/s")), false);
133 
134 		// button
135 		//boxOne.add(new JButton(acApply));
136 
137 		//  	JPanel jpTop = new JPanel(new BorderLayout());
138     	//jpTop.setBorder(GUIHelper.createDefaultBorder(XNap.tr("Settings")));
139 		//  	jpTop.add(boxOne, BorderLayout.CENTER);
140     }
141 
142     public void propertyChange(PropertyChangeEvent e)
143     {
144 		String p = e.getPropertyName();
145 
146 		if (p.equals("downloadThrottle")) {
147 			jtDlThrottle.setText(prefs.getDownloadThrottle() + "");
148 		}
149 		else if (p.equals("limitDownloads")) {
150 			acMaxDl.setSelected(prefs.getLimitDownloads());
151 		}
152 		else if (p.equals("limitUploads")) {
153 			acMaxUl.setSelected(prefs.getLimitUploads());
154 		}
155 		else if (p.equals("maxDownloads")) {
156 			jtMaxDl.setText(prefs.getMaxDownloads() + "");
157 		}	    
158 		else if (p.equals("maxUploads")) {
159 			jtMaxUl.setText(prefs.getMaxUploads() + "");
160 		}	    
161 		else if (p.equals("throttleDownloads")) {
162 			acDlThrottle.setSelected(prefs.getThrottleDownloads());
163 		}
164 		else if (p.equals("throttleUploads")) {
165 			acUlThrottle.setSelected(prefs.getThrottleUploads());
166 		}
167 		else if (p.equals("uploadThrottle")) {
168 			jtUlThrottle.setText(prefs.getUploadThrottle() + "");
169 		}
170     }
171 
172     //--- Inner Class(es) ---
173 
174     /***
175      * 
176      */
177     private class ApplyAction extends AbstractAction {
178 
179         public ApplyAction() 
180 		{
181             putValue(Action.NAME, XNap.tr("Apply"));
182             putValue(Action.SHORT_DESCRIPTION, XNap.tr("Apply settings"));
183             putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_A));
184         }
185 
186         public void actionPerformed(ActionEvent event) 
187 		{
188 			prefs.setLimitDownloads(acMaxDl.isSelected());
189 			prefs.setMaxDownloads(jtMaxDl.getIntValue());
190 			prefs.setLimitUploads(acMaxUl.isSelected());
191 			prefs.setMaxUploads(jtMaxUl.getIntValue());
192 			prefs.setThrottleDownloads(acDlThrottle.isSelected());
193 			prefs.setDownloadThrottle(jtDlThrottle.getIntValue());
194 			prefs.setThrottleUploads(acUlThrottle.isSelected());
195 			prefs.setUploadThrottle(jtUlThrottle.getIntValue());
196 	    
197 			prefs.write();
198 
199 			StatusBar.setText(XNap.tr("Updated transfer settings."));
200         }
201 
202     }
203 
204     /***
205      * Enables components and updates preferences.
206      */
207     private class MyEnableAction extends EnableAction {
208 
209 		public MyEnableAction(String name, Component[] components, 
210 							  boolean selected)
211 		{
212 			super(name, components, selected);
213 		}
214 
215 		public void actionPerformed(ActionEvent event)
216 		{
217 			super.actionPerformed(event);
218 			acApply.actionPerformed(event);
219 		}
220 
221     }
222 
223 }