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 UploadSettingsPanel extends JPanel
45      implements PropertyChangeListener
46  {
47  
48      //--- Data field(s) ---
49  
50      private static Preferences prefs = Preferences.getInstance();
51  
52      private ValidatedTextField jtUlThrottle;
53      private ValidatedTextField jtMaxUl;
54  
55      private MyEnableAction acMaxUl;
56      private MyEnableAction acUlThrottle;
57  
58      private ApplyAction acApply = new ApplyAction();
59  
60      //--- Constructor(s) ---
61  
62      public UploadSettingsPanel() 
63      {
64          initialize();
65  
66  		prefs.addPropertyChangeListener(this);
67      }
68  
69      //--- Method(s) ---
70  
71      private void initialize() 
72      {
73  		setLayout(new GridBagLayout());
74  
75  		// limits
76  		JPanel jpLimits = new JPanel();
77  		jpLimits.setLayout(new GridBagLayout());
78  		jpLimits.setBorder(GUIHelper.createDefaultBorder(XNap.tr("Upload Limits")));
79  		GridBagHelper.add(this, jpLimits);
80  
81  		jtMaxUl = new ValidatedTextField
82  			(prefs.getMaxUploads() + "", 3, ValidatedTextField.NUMBERS_INT);
83  		jtMaxUl.setMinimumSize(jtMaxUl.getPreferredSize());
84  		GUIHelper.bindEnterKeyLocally(jtMaxUl, acApply);
85  		acMaxUl = new MyEnableAction(XNap.tr("Limit Uploads"), 
86  									 new Component[] { jtMaxUl },
87  									 prefs.getLimitUploads());
88  		GridBagHelper.addComponent(jpLimits, new XNapCheckBox(acMaxUl));
89  		GridBagHelper.add(jpLimits, jtMaxUl, false);
90  
91  		jtUlThrottle 
92  			= new ValidatedTextField(prefs.getUploadThrottle() + "", 3, 
93  									 ValidatedTextField.NUMBERS_INT);
94  		jtUlThrottle.setMinimumSize(jtUlThrottle.getPreferredSize());
95  		GUIHelper.bindEnterKeyLocally(jtUlThrottle, acApply);
96  		acUlThrottle = new MyEnableAction(XNap.tr("Throttle (KB/s)"), 
97  										  new Component[] {jtUlThrottle},
98  										  prefs.getThrottleUploads());
99  		GridBagHelper.addComponent(jpLimits, new XNapCheckBox(acUlThrottle));
100 		GridBagHelper.add(jpLimits, jtUlThrottle, false);
101     }
102 
103     public void propertyChange(PropertyChangeEvent e)
104     {
105 		String p = e.getPropertyName();
106 
107 		if (p.equals("limitUploads")) {
108 			acMaxUl.setSelected(prefs.getLimitUploads());
109 		}
110 		else if (p.equals("maxUploads")) {
111 			jtMaxUl.setText(prefs.getMaxUploads() + "");
112 		}	    
113 		else if (p.equals("throttleUploads")) {
114 			acUlThrottle.setSelected(prefs.getThrottleUploads());
115 		}
116 		else if (p.equals("uploadThrottle")) {
117 			jtUlThrottle.setText(prefs.getUploadThrottle() + "");
118 		}
119     }
120 
121     //--- Inner Class(es) ---
122 
123     /***
124      * 
125      */
126     private class ApplyAction extends AbstractAction {
127 
128         public ApplyAction() 
129 		{
130             putValue(Action.NAME, XNap.tr("Apply"));
131             putValue(Action.SHORT_DESCRIPTION, XNap.tr("Apply settings"));
132             putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_A));
133         }
134 
135         public void actionPerformed(ActionEvent event) 
136 		{
137 			prefs.setLimitUploads(acMaxUl.isSelected());
138 			prefs.setMaxUploads(jtMaxUl.getIntValue());
139 			prefs.setThrottleUploads(acUlThrottle.isSelected());
140 			prefs.setUploadThrottle(jtUlThrottle.getIntValue());
141 	    
142 			prefs.write();
143 
144 			StatusBar.setText(XNap.tr("Updated transfer settings."));
145         }
146 
147     }
148 
149     /***
150      * Enables components and updates preferences.
151      */
152     private class MyEnableAction extends EnableAction {
153 
154 		public MyEnableAction(String name, Component[] components, 
155 							  boolean selected)
156 		{
157 			super(name, components, selected);
158 		}
159 
160 		public void actionPerformed(ActionEvent event)
161 		{
162 			super.actionPerformed(event);
163 			acApply.actionPerformed(event);
164 		}
165 
166     }
167 
168 }