TransferSettingsPanel.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.gui |
![]() |
![]() |
XNap 3 |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* XNap - A P2P framework and client.
*
* See the file AUTHORS for copyright information.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.xnap.gui;
import java.awt.Component;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JPanel;
import org.xnap.XNap;
import org.xnap.gui.action.EnableAction;
import org.xnap.gui.component.ValidatedTextField;
import org.xnap.gui.component.XNapCheckBox;
import org.xnap.gui.util.GUIHelper;
import org.xnap.gui.util.GridBagHelper;
import org.xnap.util.Preferences;
/**
* Provides a simple panel with transfer limit and throttle settings.
*/
public class TransferSettingsPanel extends JPanel
implements PropertyChangeListener
{
//--- Data field(s) ---
private static Preferences prefs = Preferences.getInstance();
private ValidatedTextField jtDlThrottle;
private ValidatedTextField jtUlThrottle;
private ValidatedTextField jtMaxDl;
private ValidatedTextField jtMaxUl;
private MyEnableAction acMaxDl;
private MyEnableAction acMaxUl;
private MyEnableAction acDlThrottle;
private MyEnableAction acUlThrottle;
private ApplyAction acApply = new ApplyAction();
//--- Constructor(s) ---
public TransferSettingsPanel()
{
initialize();
prefs.addPropertyChangeListener(this);
}
//--- Method(s) ---
private void initialize()
{
setLayout(new GridBagLayout());
// limits
JPanel jpLimits = new JPanel();
jpLimits.setLayout(new GridBagLayout());
jpLimits.setBorder(GUIHelper.createDefaultBorder(XNap.tr("Transfer Limit")));
GridBagHelper.add(this, jpLimits);
jtMaxDl = new ValidatedTextField
(prefs.getMaxDownloads() + "", 3, ValidatedTextField.NUMBERS_INT);
jtMaxDl.setMinimumSize(jtMaxDl.getPreferredSize());
GUIHelper.bindEnterKeyLocally(jtMaxDl, acApply);
acMaxDl = new MyEnableAction(XNap.tr("Downloads"),
new Component[] { jtMaxDl },
prefs.getLimitDownloads());
GridBagHelper.addComponent(jpLimits, new XNapCheckBox(acMaxDl));
GridBagHelper.add(jpLimits, jtMaxDl, false);
jtMaxUl = new ValidatedTextField
(prefs.getMaxUploads() + "", 3, ValidatedTextField.NUMBERS_INT);
jtMaxUl.setMinimumSize(jtMaxUl.getPreferredSize());
GUIHelper.bindEnterKeyLocally(jtMaxUl, acApply);
acMaxUl = new MyEnableAction(XNap.tr("Uploads"),
new Component[] { jtMaxUl },
prefs.getLimitUploads());
GridBagHelper.addComponent(jpLimits, new XNapCheckBox(acMaxUl));
GridBagHelper.add(jpLimits, jtMaxUl, false);
// throttle
JPanel jpThrottle = new JPanel(new GridBagLayout());
jpThrottle.setBorder(GUIHelper.createDefaultBorder(XNap.tr("Throttle (KB/s)")));
GridBagHelper.add(this, jpThrottle);
jtDlThrottle = new ValidatedTextField
(prefs.getDownloadThrottle() + "", 3,
ValidatedTextField.NUMBERS_INT);
jtDlThrottle.setMinimumSize(jtDlThrottle.getPreferredSize());
GUIHelper.bindEnterKeyLocally(jtDlThrottle, acApply);
acDlThrottle = new MyEnableAction(XNap.tr("Downloads"),
new Component[] {jtDlThrottle},
prefs.getThrottleDownloads());
GridBagHelper.addComponent(jpThrottle, new XNapCheckBox(acDlThrottle));
GridBagHelper.add(jpThrottle, jtDlThrottle, false);
//GridBagHelper.add(jpThrottle, new JLabel(XNap.tr("kb/s", 1)), false);
jtUlThrottle
= new ValidatedTextField(prefs.getUploadThrottle() + "", 3,
ValidatedTextField.NUMBERS_INT);
jtUlThrottle.setMinimumSize(jtUlThrottle.getPreferredSize());
GUIHelper.bindEnterKeyLocally(jtUlThrottle, acApply);
acUlThrottle = new MyEnableAction(XNap.tr("Uploads"),
new Component[] {jtUlThrottle},
prefs.getThrottleUploads());
GridBagHelper.addComponent(jpThrottle, new XNapCheckBox(acUlThrottle));
GridBagHelper.add(jpThrottle, jtUlThrottle, false);
//GridBagHelper.add(jpThrottle, new JLabel(XNap.tr("kb/s")), false);
// button
//boxOne.add(new JButton(acApply));
// JPanel jpTop = new JPanel(new BorderLayout());
//jpTop.setBorder(GUIHelper.createDefaultBorder(XNap.tr("Settings")));
// jpTop.add(boxOne, BorderLayout.CENTER);
}
public void propertyChange(PropertyChangeEvent e)
{
String p = e.getPropertyName();
if (p.equals("downloadThrottle")) {
jtDlThrottle.setText(prefs.getDownloadThrottle() + "");
}
else if (p.equals("limitDownloads")) {
acMaxDl.setSelected(prefs.getLimitDownloads());
}
else if (p.equals("limitUploads")) {
acMaxUl.setSelected(prefs.getLimitUploads());
}
else if (p.equals("maxDownloads")) {
jtMaxDl.setText(prefs.getMaxDownloads() + "");
}
else if (p.equals("maxUploads")) {
jtMaxUl.setText(prefs.getMaxUploads() + "");
}
else if (p.equals("throttleDownloads")) {
acDlThrottle.setSelected(prefs.getThrottleDownloads());
}
else if (p.equals("throttleUploads")) {
acUlThrottle.setSelected(prefs.getThrottleUploads());
}
else if (p.equals("uploadThrottle")) {
jtUlThrottle.setText(prefs.getUploadThrottle() + "");
}
}
//--- Inner Class(es) ---
/**
*
*/
private class ApplyAction extends AbstractAction {
public ApplyAction()
{
putValue(Action.NAME, XNap.tr("Apply"));
putValue(Action.SHORT_DESCRIPTION, XNap.tr("Apply settings"));
putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_A));
}
public void actionPerformed(ActionEvent event)
{
prefs.setLimitDownloads(acMaxDl.isSelected());
prefs.setMaxDownloads(jtMaxDl.getIntValue());
prefs.setLimitUploads(acMaxUl.isSelected());
prefs.setMaxUploads(jtMaxUl.getIntValue());
prefs.setThrottleDownloads(acDlThrottle.isSelected());
prefs.setDownloadThrottle(jtDlThrottle.getIntValue());
prefs.setThrottleUploads(acUlThrottle.isSelected());
prefs.setUploadThrottle(jtUlThrottle.getIntValue());
prefs.write();
StatusBar.setText(XNap.tr("Updated transfer settings."));
}
}
/**
* Enables components and updates preferences.
*/
private class MyEnableAction extends EnableAction {
public MyEnableAction(String name, Component[] components,
boolean selected)
{
super(name, components, selected);
}
public void actionPerformed(ActionEvent event)
{
super.actionPerformed(event);
acApply.actionPerformed(event);
}
}
}
The table below shows all metrics for TransferSettingsPanel.java.



