📄 selectablespeedmenu.java
字号:
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space
// Source File Name: SelectableSpeedMenu.java
package org.gudy.azureus2.ui.swt.mainwindow;
import com.aelitis.azureus.core.AzureusCore;
import com.aelitis.azureus.core.AzureusCoreFactory;
import java.util.*;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.*;
import org.gudy.azureus2.core3.config.COConfigurationManager;
import org.gudy.azureus2.core3.config.impl.TransferSpeedValidator;
import org.gudy.azureus2.core3.global.GlobalManager;
import org.gudy.azureus2.core3.global.GlobalManagerStats;
import org.gudy.azureus2.core3.internat.MessageText;
import org.gudy.azureus2.core3.util.Constants;
import org.gudy.azureus2.core3.util.DisplayFormatters;
import org.gudy.azureus2.ui.swt.Messages;
import org.gudy.azureus2.ui.swt.shells.InputShell;
import org.gudy.azureus2.ui.swt.shells.SpeedScaleShell;
public class SelectableSpeedMenu
{
private static final int increases[] = {
1, 1, 3, 5, 10, 40, 50, 50, 50, 100
};
private static Map parseSpeedPartitionStringCache = new HashMap();
public SelectableSpeedMenu()
{
}
public static void generateMenuItems(Menu parent, AzureusCore core, GlobalManager globalManager, boolean up_menu)
{
MenuItem oldItems[] = parent.getItems();
for (int i = 0; i < oldItems.length; i++)
oldItems[i].dispose();
String configKey = up_menu ? TransferSpeedValidator.getActiveUploadParameter(globalManager) : "Max Download Speed KBs";
int speedPartitions = 12;
int maxBandwidth = COConfigurationManager.getIntParameter(configKey);
boolean unlim = maxBandwidth == 0;
maxBandwidth = adjustMaxBandWidth(maxBandwidth, globalManager, up_menu);
boolean auto = false;
if (up_menu)
{
String configAutoKey = TransferSpeedValidator.getActiveAutoUploadParameter(globalManager);
auto = COConfigurationManager.getBooleanParameter(configAutoKey);
MenuItem auto_item = new MenuItem(parent, 32);
auto_item.setText(MessageText.getString("ConfigView.auto"));
auto_item.addListener(13, new Listener(configAutoKey, auto_item) {
final String val$configAutoKey;
final MenuItem val$auto_item;
public void handleEvent(Event e)
{
COConfigurationManager.setParameter(configAutoKey, auto_item.getSelection());
COConfigurationManager.save();
}
{
configAutoKey = s;
auto_item = menuitem;
super();
}
});
if (auto)
auto_item.setSelection(true);
auto_item.setEnabled(TransferSpeedValidator.isAutoUploadAvailable(core));
new MenuItem(parent, 2);
}
MenuItem item = new MenuItem(parent, 16);
item.setText(MessageText.getString("MyTorrentsView.menu.setSpeed.unlimited"));
item.setData("maxkb", new Integer(0));
item.setSelection(unlim && !auto);
item.addListener(13, getLimitMenuItemListener(up_menu, parent, globalManager, configKey));
Integer speed_limits[] = null;
String config_prefix = (new StringBuilder()).append("config.ui.speed.partitions.manual.").append(up_menu ? "upload" : "download").append(".").toString();
if (COConfigurationManager.getBooleanParameter((new StringBuilder()).append(config_prefix).append("enabled").toString(), false))
speed_limits = parseSpeedPartitionString(COConfigurationManager.getStringParameter((new StringBuilder()).append(config_prefix).append("values").toString(), ""));
if (speed_limits == null)
speed_limits = getGenericSpeedList(12, maxBandwidth);
for (int i = 0; i < speed_limits.length; i++)
{
Integer i_value = speed_limits[i];
int value = i_value.intValue();
if (value >= 5)
{
item = new MenuItem(parent, 16);
item.setText(DisplayFormatters.formatByteCountToKiBEtcPerSec(value * 1024, true));
item.setData("maxkb", i_value);
item.addListener(13, getLimitMenuItemListener(up_menu, parent, globalManager, configKey));
item.setSelection(!unlim && value == maxBandwidth && !auto);
}
}
new MenuItem(parent, 2);
MenuItem itemDownSpeedManual = new MenuItem(parent, 8);
Messages.setLanguageText(itemDownSpeedManual, "MyTorrentsView.menu.manual");
itemDownSpeedManual.addSelectionListener(new SelectionAdapter(up_menu, parent, globalManager, configKey) {
final boolean val$up_menu;
final Menu val$parent;
final GlobalManager val$globalManager;
final String val$configKey;
public void widgetSelected(SelectionEvent e)
{
String kbps_str = MessageText.getString("MyTorrentsView.dialog.setNumber.inKbps", new String[] {
DisplayFormatters.getRateUnit(1)
});
InputShell is = new InputShell("MyTorrentsView.dialog.setSpeed.title", new String[] {
MessageText.getString(up_menu ? "MyTorrentsView.dialog.setNumber.upload" : "MyTorrentsView.dialog.setNumber.download")
}, "MyTorrentsView.dialog.setNumber.text", new String[] {
kbps_str, MessageText.getString(up_menu ? "MyTorrentsView.dialog.setNumber.upload" : "MyTorrentsView.dialog.setNumber.download")
});
String sReturn = is.open();
if (sReturn == null)
return;
int newSpeed;
try
{
newSpeed = (int)Double.valueOf(sReturn).doubleValue();
}
catch (NumberFormatException er)
{
MessageBox mb = new MessageBox(parent.getShell(), 33);
mb.setText(MessageText.getString("MyTorrentsView.dialog.NumberError.title"));
mb.setMessage(MessageText.getString("MyTorrentsView.dialog.NumberError.text"));
mb.open();
return;
}
if (!up_menu);
if (up_menu)
{
String configAutoKey = TransferSpeedValidator.getActiveAutoUploadParameter(globalManager);
COConfigurationManager.setParameter(configAutoKey, false);
}
int cValue = ((Integer)(new TransferSpeedValidator(configKey, new Integer(newSpeed))).getValue()).intValue();
COConfigurationManager.setParameter(configKey, cValue);
COConfigurationManager.save();
}
{
up_menu = flag;
parent = menu;
globalManager = globalmanager;
configKey = s;
super();
}
});
}
private static int adjustMaxBandWidth(int maxBandwidth, GlobalManager globalManager, boolean up_menu)
{
if (maxBandwidth == 0 && !up_menu)
{
GlobalManagerStats stats = globalManager.getStats();
int dataReceive = stats.getDataReceiveRate();
if (dataReceive < 1024)
maxBandwidth = 275;
else
maxBandwidth = dataReceive / 1024;
}
return maxBandwidth;
}
private static synchronized Integer[] parseSpeedPartitionString(String s)
{
Integer result[] = (Integer[])(Integer[])parseSpeedPartitionStringCache.get(s);
if (result == null)
{
try
{
result = parseSpeedPartitionString0(s);
}
catch (NumberFormatException nfe)
{
result = new Integer[0];
}
parseSpeedPartitionStringCache.put(s, result);
}
if (result.length == 0)
return null;
else
return result;
}
private static Integer[] parseSpeedPartitionString0(String s)
{
StringTokenizer tokeniser = new StringTokenizer(s.trim(), ",");
TreeSet values = new TreeSet();
for (; tokeniser.hasMoreTokens(); values.add(new Integer(Integer.parseInt(tokeniser.nextToken().trim()))));
return (Integer[])(Integer[])values.toArray(new Integer[values.size()]);
}
private static final Listener getLimitMenuItemListener(boolean up_menu, Menu parent, GlobalManager globalManager, String configKey)
{
return new Listener(parent, up_menu, globalManager, configKey) {
final Menu val$parent;
final boolean val$up_menu;
final GlobalManager val$globalManager;
final String val$configKey;
public void handleEvent(Event event)
{
MenuItem items[] = parent.getItems();
for (int i = 0; i < items.length; i++)
if (items[i] == event.widget)
{
items[i].setSelection(true);
if (up_menu)
{
String configAutoKey = TransferSpeedValidator.getActiveAutoUploadParameter(globalManager);
COConfigurationManager.setParameter(configAutoKey, false);
}
int cValue = ((Integer)(new TransferSpeedValidator(configKey, (Number)items[i].getData("maxkb"))).getValue()).intValue();
COConfigurationManager.setParameter(configKey, cValue);
COConfigurationManager.save();
} else
{
items[i].setSelection(false);
}
}
{
parent = menu;
up_menu = flag;
globalManager = globalmanager;
configKey = s;
super();
}
};
}
public static Integer[] getGenericSpeedList(int speedPartitions, int maxBandwidth)
{
List l = new ArrayList();
int delta = 0;
int increaseLevel = 0;
for (int i = 0; i < speedPartitions; i++)
{
int valuePair[];
if (delta == 0)
valuePair = (new int[] {
maxBandwidth
});
else
valuePair = (new int[] {
maxBandwidth - delta * (maxBandwidth > 1024 ? 1024 : '\001'), maxBandwidth + delta * (maxBandwidth >= 1024 ? 1024 : '\001')
});
for (int j = 0; j < valuePair.length; j++)
if (j == 0)
l.add(0, new Integer(valuePair[j]));
else
l.add(new Integer(valuePair[j]));
delta += increases[increaseLevel];
if (increaseLevel < increases.length - 1)
increaseLevel++;
}
return (Integer[])(Integer[])l.toArray(new Integer[l.size()]);
}
public static void invokeSlider(boolean isUpSpeed)
{
String prefix = MessageText.getString(isUpSpeed ? "GeneralView.label.maxuploadspeed" : "GeneralView.label.maxdownloadspeed");
GlobalManager gm = AzureusCoreFactory.getSingleton().getGlobalManager();
String configAutoKey = TransferSpeedValidator.getActiveAutoUploadParameter(gm);
boolean auto = COConfigurationManager.getBooleanParameter(configAutoKey);
String configKey = isUpSpeed ? TransferSpeedValidator.getActiveUploadParameter(gm) : "Max Download Speed KBs";
int maxBandwidth = COConfigurationManager.getIntParameter(configKey);
boolean unlim = maxBandwidth == 0;
if (unlim && !isUpSpeed)
{
GlobalManagerStats stats = gm.getStats();
int dataReceive = stats.getDataReceiveRate();
if (dataReceive >= 1024)
maxBandwidth = dataReceive / 1024;
}
SpeedScaleShell speedScale = new SpeedScaleShell(prefix) {
final String val$prefix;
public String getStringValue(int value, String sValue)
{
if (sValue != null)
return (new StringBuilder()).append(prefix).append(": ").append(sValue).toString();
if (value == 0)
return MessageText.getString("MyTorrentsView.menu.setSpeed.unlimited");
if (value == -1)
return MessageText.getString("ConfigView.auto");
else
return (new StringBuilder()).append(prefix).append(": ").append(value != 0 ? DisplayFormatters.formatByteCountToKiBEtcPerSec(getValue() * 1024, true) : MessageText.getString("ConfigView.unlimited")).toString();
}
{
prefix = s;
super();
}
};
int max = unlim ? isUpSpeed ? 100 : 800 : maxBandwidth * 5;
if (max < 50)
max = 50;
speedScale.setMaxValue(max);
speedScale.setMaxTextValue(0x98967f);
String config_prefix = (new StringBuilder()).append("config.ui.speed.partitions.manual.").append(isUpSpeed ? "upload" : "download").append(".").toString();
int lastValue = COConfigurationManager.getIntParameter((new StringBuilder()).append(config_prefix).append("last").toString(), -10);
Integer speed_limits[];
if (COConfigurationManager.getBooleanParameter((new StringBuilder()).append(config_prefix).append("enabled").toString(), false))
speed_limits = parseSpeedPartitionString(COConfigurationManager.getStringParameter((new StringBuilder()).append(config_prefix).append("values").toString(), ""));
else
speed_limits = getGenericSpeedList(6, maxBandwidth);
if (speed_limits != null)
{
for (int i = 0; i < speed_limits.length; i++)
{
int value = speed_limits[i].intValue();
if (value <= 0)
continue;
speedScale.addOption(DisplayFormatters.formatByteCountToKiBEtcPerSec(value * 1024, true), value);
if (value == lastValue)
lastValue = -10;
}
}
speedScale.addOption(MessageText.getString("MyTorrentsView.menu.setSpeed.unlimited"), 0);
speedScale.addOption(MessageText.getString("ConfigView.auto"), -1);
if (lastValue > 0)
speedScale.addOption(DisplayFormatters.formatByteCountToKiBEtcPerSec(lastValue * 1024, true), lastValue);
if (speedScale.open(auto ? -1 : maxBandwidth, Constants.isWindows || Constants.isLinux))
{
int value = speedScale.getValue();
if (!speedScale.wasMenuChosen() || lastValue == value)
COConfigurationManager.setParameter((new StringBuilder()).append(config_prefix).append("last").toString(), maxBandwidth);
if (value >= 0)
{
if (auto)
COConfigurationManager.setParameter(configAutoKey, false);
COConfigurationManager.setParameter(configKey, value);
COConfigurationManager.save();
} else
{
COConfigurationManager.setParameter(configAutoKey, true);
COConfigurationManager.save();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -