/oneswarm_gwt_ui/src/edu/washington/cs/oneswarm/ui/gwt/client/newui/settings/Sha1Ed2kSettingsPanel.java

https://github.com/ioerror/OneSwarm · Java · 156 lines · 132 code · 24 blank · 0 comment · 7 complexity · 2d15d457eda9a716470fb630cde52d46 MD5 · raw file

  1. package edu.washington.cs.oneswarm.ui.gwt.client.newui.settings;
  2. import com.google.gwt.event.dom.client.ClickEvent;
  3. import com.google.gwt.event.dom.client.ClickHandler;
  4. import com.google.gwt.user.client.rpc.AsyncCallback;
  5. import com.google.gwt.user.client.ui.Button;
  6. import com.google.gwt.user.client.ui.HorizontalPanel;
  7. import com.google.gwt.user.client.ui.Label;
  8. import com.google.gwt.user.client.ui.TextBox;
  9. import edu.washington.cs.oneswarm.ui.gwt.client.OneSwarmGWT;
  10. import edu.washington.cs.oneswarm.ui.gwt.client.OneSwarmRPCClient;
  11. import edu.washington.cs.oneswarm.ui.gwt.client.newui.HelpButton;
  12. import edu.washington.cs.oneswarm.ui.gwt.client.newui.OneSwarmCss;
  13. import edu.washington.cs.oneswarm.ui.gwt.rpc.StringTools;
  14. public class Sha1Ed2kSettingsPanel extends SettingsPanel {
  15. TextBox speed_box = new TextBox();
  16. Label tempLocationLabel = new Label("...");
  17. SettingsCheckBox multiSwarmDownloadsEnabled = new SettingsCheckBox(
  18. msg.settings_multiswarm_enabled(), "oneswarm.multi.torrent.enabled");
  19. protected String mTempSaveLocation;
  20. public Sha1Ed2kSettingsPanel() {
  21. speed_box.setText("...");
  22. HorizontalPanel p = new HorizontalPanel();
  23. p.setSpacing(5);
  24. Label l = new Label(msg.settings_files_max_rehash_speed());
  25. p.add(l);
  26. p.setCellVerticalAlignment(l, ALIGN_MIDDLE);
  27. p.add(speed_box);
  28. speed_box.setWidth("55px");
  29. HelpButton h = new HelpButton(msg.settings_files_max_rehash_speed_help());
  30. p.add(h);
  31. p.setCellVerticalAlignment(h, ALIGN_MIDDLE);
  32. this.add(p);
  33. this.setWidth("100%");
  34. OneSwarmRPCClient.getService().getIntegerParameterValue(OneSwarmRPCClient.getSessionID(),
  35. "oneswarm.max.sha1.hash.rate.kbps", new AsyncCallback<Integer>() {
  36. public void onFailure(Throwable caught) {
  37. caught.printStackTrace();
  38. }
  39. public void onSuccess(Integer result) {
  40. speed_box.setText((result / 1024.0) + "");
  41. if (speed_box.getText().equals("...") == false) {
  42. loadNotify();
  43. }
  44. }
  45. });
  46. HorizontalPanel multiSwarmPanel = new HorizontalPanel();
  47. multiSwarmPanel.add(multiSwarmDownloadsEnabled);
  48. multiSwarmPanel.add(new HelpButton(msg.settings_multiswarm_help()));
  49. multiSwarmPanel.setSpacing(3);
  50. this.add(multiSwarmPanel);
  51. this.add(new Label(msg.settings_multiswarm_save_location()));
  52. HorizontalPanel browsePanel = new HorizontalPanel();
  53. this.add(browsePanel);
  54. final Button multiSwarmBrowse = new Button(msg.button_browse());
  55. multiSwarmBrowse.addStyleName(OneSwarmCss.SMALL_BUTTON);
  56. browsePanel.add(multiSwarmBrowse);
  57. browsePanel.add(tempLocationLabel);
  58. browsePanel.setCellVerticalAlignment(tempLocationLabel, ALIGN_MIDDLE);
  59. if (OneSwarmGWT.isRemoteAccess()) {
  60. multiSwarmBrowse.setEnabled(false);
  61. } else {
  62. multiSwarmBrowse.addClickHandler(new ClickHandler() {
  63. public void onClick(ClickEvent event) {
  64. multiSwarmBrowse.setEnabled(false);
  65. OneSwarmRPCClient.getService().selectFileOrDirectory(
  66. OneSwarmRPCClient.getSessionID(), true, new AsyncCallback<String>() {
  67. public void onFailure(Throwable caught) {
  68. caught.printStackTrace();
  69. }
  70. public void onSuccess(String result) {
  71. multiSwarmBrowse.setEnabled(true);
  72. if (result == null) {
  73. return;
  74. }
  75. if (result.length() > 0) {
  76. mTempSaveLocation = result;
  77. tempLocationLabel.setText(StringTools.truncate(result, 50,
  78. false));
  79. }
  80. }
  81. });
  82. }
  83. });
  84. }
  85. browsePanel.setSpacing(3);
  86. OneSwarmRPCClient.getService().getMultiTorrentSourceTemp(OneSwarmRPCClient.getSessionID(),
  87. new AsyncCallback<String>() {
  88. public void onFailure(Throwable caught) {
  89. caught.printStackTrace();
  90. }
  91. public void onSuccess(String result) {
  92. mTempSaveLocation = result;
  93. tempLocationLabel.setText(StringTools.truncate(result, 50, false));
  94. }
  95. });
  96. }
  97. @Override
  98. public void sync() {
  99. int limit = (int) Math.round(1024 * Double.parseDouble(speed_box.getText()));
  100. OneSwarmRPCClient.getService().setIntegerParameterValue(OneSwarmRPCClient.getSessionID(),
  101. "oneswarm.max.sha1.hash.rate.kbps", limit, new AsyncCallback<Void>() {
  102. public void onFailure(Throwable caught) {
  103. caught.printStackTrace();
  104. }
  105. public void onSuccess(Void result) {
  106. System.out.println("success for UL sync");
  107. }
  108. });
  109. OneSwarmRPCClient.getService().setStringParameterValue(OneSwarmRPCClient.getSessionID(),
  110. "oneswarm.multi.torrent.download.temp.dir", mTempSaveLocation,
  111. new AsyncCallback<Void>() {
  112. public void onFailure(Throwable caught) {
  113. caught.printStackTrace();
  114. }
  115. public void onSuccess(Void result) {
  116. System.out.println("success for UL sync");
  117. }
  118. });
  119. multiSwarmDownloadsEnabled.save();
  120. }
  121. @Override
  122. String validData() {
  123. try {
  124. Double.parseDouble(speed_box.getText());
  125. } catch (NumberFormatException e) {
  126. return msg.settings_files_max_rehash_number_error();
  127. } catch (Exception e) {
  128. return e.toString();
  129. }
  130. return null;
  131. }
  132. }