use of com.biglybt.pif.ui.UIInputReceiverListener 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();
}
});
}
});
}
use of com.biglybt.pif.ui.UIInputReceiverListener in project BiglyBT by BiglySoftware.
the class FilesViewMenuUtil method changePriorityManual.
private static void changePriorityManual(final List<DiskManagerFileInfo> file_list) {
SimpleTextEntryWindow entryWindow = new SimpleTextEntryWindow("FilesView.dialog.priority.title", "FilesView.dialog.priority.text");
entryWindow.prompt(new UIInputReceiverListener() {
@Override
public void UIInputReceiverClosed(UIInputReceiver entryWindow) {
if (!entryWindow.hasSubmittedInput()) {
return;
}
String sReturn = entryWindow.getSubmittedInput();
if (sReturn == null)
return;
int priority = 0;
try {
priority = Integer.valueOf(sReturn).intValue();
} catch (NumberFormatException er) {
Debug.out("Invalid priority: " + sReturn);
new MessageBoxShell(SWT.ICON_ERROR | SWT.OK, MessageText.getString("FilePriority.invalid.title"), MessageText.getString("FilePriority.invalid.text", new String[] { sReturn })).open(null);
return;
}
Map<DownloadManager, ArrayList<DiskManagerFileInfo>> mapDMtoDMFI = new IdentityHashMap<>();
for (DiskManagerFileInfo file : file_list) {
DownloadManager dm = file.getDownloadManager();
ArrayList<DiskManagerFileInfo> listFileInfos = mapDMtoDMFI.get(dm);
if (listFileInfos == null) {
listFileInfos = new ArrayList<>(1);
mapDMtoDMFI.put(dm, listFileInfos);
}
listFileInfos.add(file);
file.setPriority(priority);
}
for (DownloadManager dm : mapDMtoDMFI.keySet()) {
ArrayList<DiskManagerFileInfo> list = mapDMtoDMFI.get(dm);
DiskManagerFileInfo[] fileInfos = list.toArray(new DiskManagerFileInfo[0]);
boolean paused = setSkipped(dm, fileInfos, false, false);
if (paused) {
dm.resume();
}
}
}
});
}
use of com.biglybt.pif.ui.UIInputReceiverListener in project BiglyBT by BiglySoftware.
the class FilesViewMenuUtil method askForRenameFilenameAndExec.
private static void askForRenameFilenameAndExec(final DiskManagerFileInfo fileInfo, final TableView tv) {
final SimpleTextEntryWindow dialog = new SimpleTextEntryWindow("FilesView.rename.filename.title", "FilesView.rename.filename.text");
String file_name = fileInfo.getFile(true).getName();
// false -> it's not "suggested", it's a previous value
dialog.setPreenteredText(file_name, false);
int pos = file_name.lastIndexOf('.');
if (pos > 0) {
String suffix = fileInfo.getDownloadManager().getDownloadState().getAttribute(DownloadManagerState.AT_INCOMP_FILE_SUFFIX);
if (suffix != null && file_name.substring(pos).equals(suffix)) {
pos--;
while (pos > 0 && file_name.charAt(pos) != '.') {
pos--;
}
}
if (pos > 0) {
dialog.selectPreenteredTextRange(new int[] { 0, pos });
}
}
dialog.allowEmptyInput(false);
dialog.prompt(new UIInputReceiverListener() {
@Override
public void UIInputReceiverClosed(UIInputReceiver receiver) {
if (!receiver.hasSubmittedInput()) {
return;
}
File existing_file = fileInfo.getFile(true);
File f_target = new File(existing_file.getParentFile(), receiver.getSubmittedInput());
DownloadManager manager = fileInfo.getDownloadManager();
boolean needsUnpause = manager.pause();
moveFile(fileInfo.getDownloadManager(), fileInfo, f_target, false, new Runnable() {
@Override
public void run() {
if (needsUnpause) {
manager.resume();
}
invalidateRows(tv, Arrays.asList(fileInfo));
}
});
}
});
}
use of com.biglybt.pif.ui.UIInputReceiverListener in project BiglyBT by BiglySoftware.
the class PeersViewBase method fillMenu.
private static void fillMenu(final Menu menu, final PEPeer[] peers, final Shell shell, DownloadManager download_specific) {
boolean hasSelection = (peers.length > 0);
boolean downSpeedDisabled = false;
boolean downSpeedUnlimited = false;
long totalDownSpeed = 0;
long downSpeedSetMax = 0;
long maxDown = 0;
boolean upSpeedDisabled = false;
boolean upSpeedUnlimited = false;
long totalUpSpeed = 0;
long upSpeedSetMax = 0;
long maxUp = 0;
GlobalManager gm = CoreFactory.getSingleton().getGlobalManager();
final IdentityHashSet<DownloadManager> download_managers = new IdentityHashSet<>();
Map<PEPeer, DownloadManager> peer_dm_map = new HashMap<>();
if (hasSelection) {
for (int i = 0; i < peers.length; i++) {
PEPeer peer = peers[i];
PEPeerManager m = peer.getManager();
if (m != null) {
if (gm != null) {
DownloadManager dm = gm.getDownloadManager(new HashWrapper(m.getHash()));
if (dm != null) {
peer_dm_map.put(peer, dm);
download_managers.add(dm);
}
}
}
try {
int maxul = peer.getStats().getUploadRateLimitBytesPerSecond();
maxUp += maxul * 4;
if (maxul == 0) {
upSpeedUnlimited = true;
} else {
if (maxul > upSpeedSetMax) {
upSpeedSetMax = maxul;
}
}
if (maxul == -1) {
maxul = 0;
upSpeedDisabled = true;
}
totalUpSpeed += maxul;
int maxdl = peer.getStats().getDownloadRateLimitBytesPerSecond();
maxDown += maxdl * 4;
if (maxdl == 0) {
downSpeedUnlimited = true;
} else {
if (maxdl > downSpeedSetMax) {
downSpeedSetMax = maxdl;
}
}
if (maxdl == -1) {
maxdl = 0;
downSpeedDisabled = true;
}
totalDownSpeed += maxdl;
} catch (Exception ex) {
Debug.printStackTrace(ex);
}
}
}
if (download_specific != null) {
final MenuItem block_item = new MenuItem(menu, SWT.CHECK);
PEPeer peer = peers.length == 0 ? null : peers[0];
if (peer == null || peer.getManager().getDiskManager().getRemainingExcludingDND() > 0) {
// disallow peer upload blocking when downloading
block_item.setSelection(false);
block_item.setEnabled(false);
} else {
block_item.setEnabled(true);
block_item.setSelection(peer.isSnubbed());
}
if (peer != null) {
final boolean newSnubbedValue = !peer.isSnubbed();
Messages.setLanguageText(block_item, "PeersView.menu.blockupload");
block_item.addListener(SWT.Selection, new PeersRunner(peers) {
@Override
public void run(PEPeer peer) {
peer.setSnubbed(newSnubbedValue);
}
});
}
} else {
if (download_managers.size() > 0) {
MenuItem itemDetails = new MenuItem(menu, SWT.PUSH);
Messages.setLanguageText(itemDetails, "PeersView.menu.showdownload");
Utils.setMenuItemImage(itemDetails, "details");
itemDetails.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event event) {
UIFunctions uiFunctions = UIFunctionsManager.getUIFunctions();
if (uiFunctions != null) {
for (DownloadManager dm : download_managers) {
uiFunctions.getMDI().showEntryByID(MultipleDocumentInterface.SIDEBAR_SECTION_TORRENT_DETAILS, dm);
}
}
}
});
new MenuItem(menu, SWT.SEPARATOR);
}
}
BuddyPlugin bp = BuddyPluginUtils.getPlugin();
if (bp != null) {
boolean has_pb = false;
boolean has_public = false;
for (PEPeer peer : peers) {
if (AENetworkClassifier.categoriseAddress(peer.getIp()) == AENetworkClassifier.AT_PUBLIC) {
has_public = true;
DownloadManager dm = peer_dm_map.get(peer);
if (dm != null && bp.isPartialBuddy(PluginCoreUtils.wrap(dm), PluginCoreUtils.wrap(peer))) {
has_pb = true;
}
}
}
MenuItem boost_item = new MenuItem(menu, SWT.CHECK);
Messages.setLanguageText(boost_item, "PeersView.menu.boost");
boost_item.setSelection(has_pb);
boost_item.setEnabled(has_public);
boost_item.addListener(SWT.Selection, new PeersRunner(peers) {
@Override
public void run(PEPeer peer) {
boolean sel = boost_item.getSelection();
if (AENetworkClassifier.categoriseAddress(peer.getIp()) == AENetworkClassifier.AT_PUBLIC) {
DownloadManager dm = peer_dm_map.get(peer);
if (dm != null) {
bp.setPartialBuddy(PluginCoreUtils.wrap(dm), PluginCoreUtils.wrap(peer), sel);
}
}
}
});
}
final MenuItem kick_item = new MenuItem(menu, SWT.PUSH);
Messages.setLanguageText(kick_item, "PeersView.menu.kick");
kick_item.addListener(SWT.Selection, new PeersRunner(peers) {
@Override
public void run(PEPeer peer) {
peer.getManager().removePeer(peer, "Peer kicked");
}
});
final MenuItem ban_item = new MenuItem(menu, SWT.PUSH);
Messages.setLanguageText(ban_item, "PeersView.menu.kickandban");
ban_item.addListener(SWT.Selection, new PeersRunner(peers) {
@Override
public void run(PEPeer peer) {
String msg = MessageText.getString("PeersView.menu.kickandban.reason");
IpFilterManagerFactory.getSingleton().getIPFilter().ban(peer.getIp(), msg, true);
peer.getManager().removePeer(peer, "Peer kicked and banned");
}
});
final MenuItem ban_for_item = new MenuItem(menu, SWT.PUSH);
Messages.setLanguageText(ban_for_item, "PeersView.menu.kickandbanfor");
ban_for_item.addListener(SWT.Selection, new PeersRunner(peers) {
@Override
public boolean run(final PEPeer[] peers) {
String text = MessageText.getString("dialog.ban.for.period.text");
SimpleTextEntryWindow entryWindow = new SimpleTextEntryWindow("dialog.ban.for.period.title", "!" + text + "!");
int def = COConfigurationManager.getIntParameter("ban.for.period.default", 60);
entryWindow.setPreenteredText(String.valueOf(def), false);
entryWindow.prompt(new UIInputReceiverListener() {
@Override
public void UIInputReceiverClosed(UIInputReceiver entryWindow) {
if (!entryWindow.hasSubmittedInput()) {
return;
}
String sReturn = entryWindow.getSubmittedInput();
if (sReturn == null) {
return;
}
int mins = -1;
try {
mins = Integer.valueOf(sReturn).intValue();
} catch (NumberFormatException er) {
// Ignore
}
if (mins <= 0) {
MessageBox mb = new MessageBox(Utils.findAnyShell(), SWT.ICON_ERROR | SWT.OK);
mb.setText(MessageText.getString("MyTorrentsView.dialog.NumberError.title"));
mb.setMessage(MessageText.getString("MyTorrentsView.dialog.NumberError.text"));
mb.open();
return;
}
COConfigurationManager.setParameter("ban.for.period.default", mins);
IpFilter filter = IpFilterManagerFactory.getSingleton().getIPFilter();
for (PEPeer peer : peers) {
String msg = MessageText.getString("PeersView.menu.kickandbanfor.reason", new String[] { String.valueOf(mins) });
filter.ban(peer.getIp(), msg, true, mins);
peer.getManager().removePeer(peer, "Peer kicked and banned");
}
}
});
return (true);
}
});
// === advanced menu ===
final MenuItem itemAdvanced = new MenuItem(menu, SWT.CASCADE);
Messages.setLanguageText(itemAdvanced, "MyTorrentsView.menu.advancedmenu");
itemAdvanced.setEnabled(hasSelection);
final Menu menuAdvanced = new Menu(shell, SWT.DROP_DOWN);
itemAdvanced.setMenu(menuAdvanced);
// advanced > Download Speed Menu //
Map<String, Object> menu_properties = new HashMap<>();
menu_properties.put(ViewUtils.SM_PROP_PERMIT_UPLOAD_DISABLE, true);
menu_properties.put(ViewUtils.SM_PROP_PERMIT_DOWNLOAD_DISABLE, true);
ViewUtils.addSpeedMenu(shell, menuAdvanced, true, true, false, hasSelection, downSpeedDisabled, downSpeedUnlimited, totalDownSpeed, downSpeedSetMax, maxDown, upSpeedDisabled, upSpeedUnlimited, totalUpSpeed, upSpeedSetMax, maxUp, peers.length, menu_properties, new ViewUtils.SpeedAdapter() {
@Override
public void setDownSpeed(int speed) {
if (peers.length > 0) {
for (int i = 0; i < peers.length; i++) {
try {
PEPeer peer = (PEPeer) peers[i];
peer.getStats().setDownloadRateLimitBytesPerSecond(speed);
} catch (Exception e) {
Debug.printStackTrace(e);
}
}
}
}
@Override
public void setUpSpeed(int speed) {
if (peers.length > 0) {
for (int i = 0; i < peers.length; i++) {
try {
PEPeer peer = (PEPeer) peers[i];
peer.getStats().setUploadRateLimitBytesPerSecond(speed);
} catch (Exception e) {
Debug.printStackTrace(e);
}
}
}
}
});
addPeersMenu(download_specific, "", menu);
}
use of com.biglybt.pif.ui.UIInputReceiverListener in project BiglyBT by BiglySoftware.
the class ViewUtils method getManualSpeedValue.
public static void getManualSpeedValue(final Shell shell, boolean for_download, final manualSpeedValueListener l) {
String kbps_str = MessageText.getString("MyTorrentsView.dialog.setNumber.inKbps", new String[] { DisplayFormatters.getRateUnit(DisplayFormatters.UNIT_KB) });
String set_num_str = MessageText.getString("MyTorrentsView.dialog.setNumber." + ((for_download) ? "download" : "upload"));
final SimpleTextEntryWindow entryWindow = new SimpleTextEntryWindow();
entryWindow.initTexts("MyTorrentsView.dialog.setSpeed.title", new String[] { set_num_str }, "MyTorrentsView.dialog.setNumber.text", new String[] { kbps_str, set_num_str });
entryWindow.prompt(new UIInputReceiverListener() {
@Override
public void UIInputReceiverClosed(UIInputReceiver receiver) {
if (!receiver.hasSubmittedInput()) {
l.manualSpeedValueResult(-1);
return;
}
String sReturn = receiver.getSubmittedInput();
if (sReturn == null) {
l.manualSpeedValueResult(-1);
return;
}
try {
int result = (int) (Double.valueOf(sReturn).doubleValue() * DisplayFormatters.getKinB());
if (DisplayFormatters.isRateUsingBits()) {
result /= 8;
}
if (result <= 0) {
l.error("non-positive number entered");
return;
}
l.manualSpeedValueResult(result);
} catch (NumberFormatException er) {
MessageBox mb = new MessageBox(shell, SWT.ICON_ERROR | SWT.OK);
mb.setText(MessageText.getString("MyTorrentsView.dialog.NumberError.title"));
mb.setMessage(MessageText.getString("MyTorrentsView.dialog.NumberError.text"));
mb.open();
l.manualSpeedValueResult(-1);
}
}
});
}
Aggregations