You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TransferPanel.java 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.dcc.ui;
  23. import com.dmdirc.DMDircMBassador;
  24. import com.dmdirc.FrameContainer;
  25. import com.dmdirc.addons.dcc.DCCTransferHandler;
  26. import com.dmdirc.addons.dcc.TransferContainer;
  27. import com.dmdirc.addons.dcc.io.DCCTransfer;
  28. import com.dmdirc.addons.ui_swing.UIUtilities;
  29. import com.dmdirc.addons.ui_swing.components.frames.SwingFrameComponent;
  30. import com.dmdirc.events.UserErrorEvent;
  31. import com.dmdirc.logger.ErrorLevel;
  32. import com.dmdirc.parser.interfaces.Parser;
  33. import com.dmdirc.parser.interfaces.callbacks.SocketCloseListener;
  34. import java.awt.Desktop;
  35. import java.awt.event.ActionEvent;
  36. import java.awt.event.ActionListener;
  37. import java.io.File;
  38. import java.io.IOException;
  39. import java.util.Date;
  40. import javax.swing.JButton;
  41. import javax.swing.JLabel;
  42. import javax.swing.JPanel;
  43. import javax.swing.JProgressBar;
  44. import net.miginfocom.swing.MigLayout;
  45. /**
  46. * A panel for displaying the progress of DCC transfers.
  47. *
  48. * @since 0.6.6
  49. */
  50. public class TransferPanel extends JPanel implements ActionListener,
  51. SocketCloseListener, DCCTransferHandler, SwingFrameComponent {
  52. /** A version number for this class. */
  53. private static final long serialVersionUID = 1L;
  54. /** Parent container. */
  55. private final TransferContainer transferContainer;
  56. /** Progress Bar */
  57. private final JProgressBar progress = new JProgressBar();
  58. /** Status Label */
  59. private final JLabel status = new JLabel("Status: Waiting");
  60. /** Speed Label */
  61. private final JLabel speed = new JLabel("Speed: Unknown");
  62. /** Time Label */
  63. private final JLabel remaining = new JLabel("Time Remaining: Unknown");
  64. /** Time Taken */
  65. private final JLabel taken = new JLabel("Time Taken: 00:00");
  66. /** Button */
  67. private final JButton button = new JButton("Cancel");
  68. /** Open Button */
  69. private final JButton openButton = new JButton("Open");
  70. /** The transfer that this window is showing. */
  71. private final DCCTransfer dcc;
  72. /** The event bus to post errors. */
  73. private final DMDircMBassador errorBus;
  74. /**
  75. * Creates a new transfer window for the specified UI controller and owner.
  76. *
  77. * @param owner The frame container that owns this frame
  78. * @param errorBus The event bus to post errors to
  79. */
  80. public TransferPanel(final FrameContainer owner, final DMDircMBassador errorBus) {
  81. this.transferContainer = (TransferContainer) owner;
  82. this.errorBus = errorBus;
  83. dcc = transferContainer.getDCC();
  84. dcc.addHandler(this);
  85. transferContainer.addSocketCloseCallback(this);
  86. setLayout(new MigLayout("hidemode 0"));
  87. if (dcc.getType() == DCCTransfer.TransferType.SEND) {
  88. add(new JLabel("Sending: " + dcc.getShortFileName()), "wrap");
  89. add(new JLabel("To: " + transferContainer
  90. .getOtherNickname()), "wrap");
  91. } else {
  92. add(new JLabel("Receiving: " + dcc.getShortFileName()), "wrap");
  93. add(new JLabel("From: " + transferContainer
  94. .getOtherNickname()), "wrap");
  95. }
  96. add(status, "wrap");
  97. add(speed, "wrap");
  98. add(remaining, "wrap");
  99. add(taken, "wrap");
  100. add(progress, "growx, wrap");
  101. button.addActionListener(this);
  102. openButton.addActionListener(this);
  103. openButton.setVisible(false);
  104. add(openButton, "split 2, align right");
  105. add(button, "align right");
  106. }
  107. @Override
  108. public void actionPerformed(final ActionEvent e) {
  109. if (e.getActionCommand().equals("Cancel")) {
  110. if (dcc.getType() == DCCTransfer.TransferType.SEND) {
  111. button.setText("Resend");
  112. } else {
  113. button.setText("Close Window");
  114. }
  115. status.setText("Status: Cancelled");
  116. dcc.close();
  117. } else if (e.getActionCommand().equals("Resend")) {
  118. button.setText("Cancel");
  119. status.setText("Status: Resending...");
  120. if (!(transferContainer.resend())) {
  121. status.setText("Status: Resend failed.");
  122. button.setText("Close Window");
  123. }
  124. } else if (e.getActionCommand().equals("Close Window")) {
  125. transferContainer.close();
  126. } else if (e.getSource() == openButton) {
  127. final File file = new File(dcc.getFileName());
  128. try {
  129. Desktop.getDesktop().open(file);
  130. } catch (IllegalArgumentException ex) {
  131. errorBus.publishAsync(new UserErrorEvent(ErrorLevel.LOW, ex,
  132. "Unable to open file: " + file, ""));
  133. openButton.setEnabled(false);
  134. } catch (IOException ex) {
  135. try {
  136. Desktop.getDesktop().open(file.getParentFile());
  137. } catch (IllegalArgumentException ex1) {
  138. errorBus.publishAsync(new UserErrorEvent(ErrorLevel.LOW, ex1, "Unable to open folder: "
  139. + file.getParentFile(), ""));
  140. openButton.setEnabled(false);
  141. } catch (IOException ex1) {
  142. errorBus.publishAsync(new UserErrorEvent(ErrorLevel.LOW, ex1,
  143. "No associated handler to open file or directory.", ""));
  144. openButton.setEnabled(false);
  145. }
  146. }
  147. }
  148. }
  149. @Override
  150. public void onSocketClosed(final Parser parser, final Date date) {
  151. if ("Resend".equals(button.getText())) {
  152. button.setText("Close Window");
  153. }
  154. }
  155. @Override
  156. public void socketClosed(final DCCTransfer dcc) {
  157. UIUtilities.invokeLater(new Runnable() {
  158. @Override
  159. public void run() {
  160. if (transferContainer.isComplete()) {
  161. status.setText("Status: Transfer Complete.");
  162. if (transferContainer.shouldShowOpenButton()) {
  163. openButton.setVisible(true);
  164. }
  165. progress.setValue(100);
  166. button.setText("Close Window");
  167. } else {
  168. status.setText("Status: Transfer Failed.");
  169. if (dcc.getType() == DCCTransfer.TransferType.SEND) {
  170. button.setText("Resend");
  171. } else {
  172. button.setText("Close Window");
  173. }
  174. }
  175. }
  176. });
  177. }
  178. @Override
  179. public void socketOpened(final DCCTransfer dcc) {
  180. UIUtilities.invokeLater(new Runnable() {
  181. @Override
  182. public void run() {
  183. status.setText("Status: Socket Opened");
  184. }
  185. });
  186. }
  187. @Override
  188. public void dataTransferred(final DCCTransfer dcc, final int bytes) {
  189. UIUtilities.invokeLater(new Runnable() {
  190. @Override
  191. public void run() {
  192. if (dcc.getType() == DCCTransfer.TransferType.SEND) {
  193. status.setText("Status: Sending");
  194. } else {
  195. status.setText("Status: Receiving");
  196. }
  197. progress.setValue((int) transferContainer.getPercent());
  198. final double bytesPerSecond = transferContainer
  199. .getBytesPerSecond();
  200. if (bytesPerSecond > 1048576) {
  201. speed.setText(String.format("Speed: %.2f MiB/s",
  202. bytesPerSecond / 1048576));
  203. } else if (bytesPerSecond > 1024) {
  204. speed.setText(String.format("Speed: %.2f KiB/s",
  205. bytesPerSecond / 1024));
  206. } else {
  207. speed.setText(String.format("Speed: %.2f B/s",
  208. bytesPerSecond));
  209. }
  210. remaining.setText(String.format("Time Remaining: %s",
  211. duration((int) transferContainer.getRemainingTime())));
  212. taken.setText(String.format("Time Taken: %s", transferContainer
  213. .getStartTime() == 0 ? "N/A" : duration(
  214. transferContainer.getElapsedTime())));
  215. }
  216. });
  217. }
  218. /**
  219. * Get the duration in seconds as a string.
  220. *
  221. * @param secondsInput to get duration for
  222. *
  223. * @return Duration as a string
  224. */
  225. private String duration(final long secondsInput) {
  226. final StringBuilder result = new StringBuilder();
  227. final long hours = (secondsInput / 3600);
  228. final long minutes = (secondsInput / 60 % 60);
  229. final long seconds = (secondsInput % 60);
  230. if (hours > 0) {
  231. result.append(hours);
  232. result.append(':');
  233. }
  234. result.append(String.format("%0,2d:%0,2d", minutes, seconds));
  235. return result.toString();
  236. }
  237. }