use of com.biglybt.core.config.impl.TransferSpeedValidator in project BiglyBT by BiglySoftware.
the class SelectableSpeedMenu method generateMenuItems.
public static void generateMenuItems(final Menu parent, final Core core, final GlobalManager globalManager, final boolean up_menu) {
// can't currently do this properly as global limits stored as K not B :(
final int kInB = 1024;
final MenuItem[] oldItems = parent.getItems();
for (int i = 0; i < oldItems.length; i++) {
oldItems[i].dispose();
}
final String configKey = up_menu ? TransferSpeedValidator.getActiveUploadParameter(globalManager) : "Max Download Speed KBs";
final int speedPartitions = 12;
int maxBandwidth = COConfigurationManager.getIntParameter(configKey);
final boolean unlim = (maxBandwidth == 0);
maxBandwidth = adjustMaxBandWidth(maxBandwidth, globalManager, up_menu, kInB);
boolean auto = false;
if (up_menu) {
final String configAutoKey = TransferSpeedValidator.getActiveAutoUploadParameter(globalManager);
auto = TransferSpeedValidator.isAutoSpeedActive(globalManager);
// auto
final MenuItem auto_item = new MenuItem(parent, SWT.CHECK);
auto_item.setText(MessageText.getString("ConfigView.auto"));
auto_item.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event e) {
COConfigurationManager.setParameter(configAutoKey, auto_item.getSelection());
COConfigurationManager.save();
}
});
if (auto)
auto_item.setSelection(true);
auto_item.setEnabled(TransferSpeedValidator.isAutoUploadAvailable(core));
new MenuItem(parent, SWT.SEPARATOR);
}
MenuItem item = new MenuItem(parent, SWT.RADIO);
item.setText(MessageText.getString("MyTorrentsView.menu.setSpeed.unlimited"));
item.setData("maxkb", new Integer(0));
item.setSelection(unlim && !auto);
item.addListener(SWT.Selection, getLimitMenuItemListener(up_menu, parent, globalManager, configKey));
Integer[] speed_limits = null;
final String config_prefix = "config.ui.speed.partitions.manual." + ((up_menu) ? "upload" : "download") + ".";
if (COConfigurationManager.getBooleanParameter(config_prefix + "enabled", false)) {
speed_limits = parseSpeedPartitionString(COConfigurationManager.getStringParameter(config_prefix + "values", ""));
}
if (speed_limits == null) {
speed_limits = getGenericSpeedList(speedPartitions, maxBandwidth);
}
for (int i = 0; i < speed_limits.length; i++) {
Integer i_value = speed_limits[i];
int value = i_value.intValue();
// Don't allow the user to easily select slow speeds.
if (value < 5) {
continue;
}
item = new MenuItem(parent, SWT.RADIO);
item.setText(DisplayFormatters.formatByteCountToKiBEtcPerSec(value * kInB, true));
item.setData("maxkb", i_value);
item.addListener(SWT.Selection, getLimitMenuItemListener(up_menu, parent, globalManager, configKey));
item.setSelection(!unlim && value == maxBandwidth && !auto);
}
new MenuItem(parent, SWT.SEPARATOR);
final MenuItem itemDownSpeedManual = new MenuItem(parent, SWT.PUSH);
Messages.setLanguageText(itemDownSpeedManual, "MyTorrentsView.menu.manual");
itemDownSpeedManual.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
String kbps_str = MessageText.getString("MyTorrentsView.dialog.setNumber.inKbps", new String[] { DisplayFormatters.getRateUnit(DisplayFormatters.UNIT_KB) });
SimpleTextEntryWindow entryWindow = new SimpleTextEntryWindow();
entryWindow.initTexts("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") });
entryWindow.prompt(new UIInputReceiverListener() {
@Override
public void UIInputReceiverClosed(UIInputReceiver entryWindow) {
if (!entryWindow.hasSubmittedInput()) {
return;
}
String sReturn = entryWindow.getSubmittedInput();
if (sReturn == null)
return;
int newSpeed;
try {
newSpeed = (int) (Double.valueOf(sReturn).doubleValue());
} catch (NumberFormatException er) {
MessageBox mb = new MessageBox(parent.getShell(), SWT.ICON_ERROR | SWT.OK);
mb.setText(MessageText.getString("MyTorrentsView.dialog.NumberError.title"));
mb.setMessage(MessageText.getString("MyTorrentsView.dialog.NumberError.text"));
mb.open();
return;
}
if (up_menu) {
String configAutoKey = TransferSpeedValidator.getActiveAutoUploadParameter(globalManager);
COConfigurationManager.setParameter(configAutoKey, false);
}
final int cValue = ((Integer) new TransferSpeedValidator(configKey, new Integer(newSpeed)).getValue()).intValue();
COConfigurationManager.setParameter(configKey, cValue);
COConfigurationManager.save();
}
});
}
});
}
Aggregations