Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

TransferPanel.java 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (c) 2006-2015 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 com.dmdirc.util.DateUtils;
  35. import java.awt.Desktop;
  36. import java.awt.event.ActionEvent;
  37. import java.awt.event.ActionListener;
  38. import java.io.File;
  39. import java.io.IOException;
  40. import java.util.Date;
  41. import javax.swing.JButton;
  42. import javax.swing.JLabel;
  43. import javax.swing.JPanel;
  44. import javax.swing.JProgressBar;
  45. import net.miginfocom.swing.MigLayout;
  46. /**
  47. * A panel for displaying the progress of DCC transfers.
  48. *
  49. * @since 0.6.6
  50. */
  51. public class TransferPanel extends JPanel implements ActionListener,
  52. SocketCloseListener, DCCTransferHandler, SwingFrameComponent {
  53. /** A version number for this class. */
  54. private static final long serialVersionUID = 1L;
  55. /** Parent container. */
  56. private final TransferContainer transferContainer;
  57. /** Progress Bar */
  58. private final JProgressBar progress = new JProgressBar();
  59. /** Status Label */
  60. private final JLabel status = new JLabel("Status: Waiting");
  61. /** Speed Label */
  62. private final JLabel speed = new JLabel("Speed: Unknown");
  63. /** Time Label */
  64. private final JLabel remaining = new JLabel("Time Remaining: Unknown");
  65. /** Time Taken */
  66. private final JLabel taken = new JLabel("Time Taken: 00:00");
  67. /** Button */
  68. private final JButton button = new JButton("Cancel");
  69. /** Open Button */
  70. private final JButton openButton = new JButton("Open");
  71. /** The transfer that this window is showing. */
  72. private final DCCTransfer dcc;
  73. /** The event bus to post errors. */
  74. private final DMDircMBassador errorBus;
  75. /**
  76. * Creates a new transfer window for the specified UI controller and owner.
  77. *
  78. * @param owner The frame container that owns this frame
  79. * @param errorBus The event bus to post errors to
  80. */
  81. public TransferPanel(final FrameContainer owner, final DMDircMBassador errorBus) {
  82. this.transferContainer = (TransferContainer) owner;
  83. this.errorBus = errorBus;
  84. dcc = transferContainer.getDCC();
  85. dcc.addHandler(this);
  86. transferContainer.addSocketCloseCallback(this);
  87. setLayout(new MigLayout("hidemode 0"));
  88. if (dcc.getType() == DCCTransfer.TransferType.SEND) {
  89. add(new JLabel("Sending: " + dcc.getShortFileName()), "wrap");
  90. add(new JLabel("To: " + transferContainer
  91. .getOtherNickname()), "wrap");
  92. } else {
  93. add(new JLabel("Receiving: " + dcc.getShortFileName()), "wrap");
  94. add(new JLabel("From: " + transferContainer
  95. .getOtherNickname()), "wrap");
  96. }
  97. add(status, "wrap");
  98. add(speed, "wrap");
  99. add(remaining, "wrap");
  100. add(taken, "wrap");
  101. add(progress, "growx, wrap");
  102. button.addActionListener(this);
  103. openButton.addActionListener(this);
  104. openButton.setVisible(false);
  105. add(openButton, "split 2, align right");
  106. add(button, "align right");
  107. }
  108. @Override
  109. public void actionPerformed(final ActionEvent e) {
  110. if (e.getActionCommand().equals("Cancel")) {
  111. if (dcc.getType() == DCCTransfer.TransferType.SEND) {
  112. button.setText("Resend");
  113. } else {
  114. button.setText("Close Window");
  115. }
  116. status.setText("Status: Cancelled");
  117. dcc.close();
  118. } else if (e.getActionCommand().equals("Resend")) {
  119. button.setText("Cancel");
  120. status.setText("Status: Resending...");
  121. if (!(transferContainer.resend())) {
  122. status.setText("Status: Resend failed.");
  123. button.setText("Close Window");
  124. }
  125. } else if (e.getActionCommand().equals("Close Window")) {
  126. transferContainer.close();
  127. } else if (e.getSource() == openButton) {
  128. final File file = new File(dcc.getFileName());
  129. try {
  130. Desktop.getDesktop().open(file);
  131. } catch (IllegalArgumentException ex) {
  132. errorBus.publishAsync(new UserErrorEvent(ErrorLevel.LOW, ex,
  133. "Unable to open file: " + file, ""));
  134. openButton.setEnabled(false);
  135. } catch (IOException ex) {
  136. try {
  137. Desktop.getDesktop().open(file.getParentFile());
  138. } catch (IllegalArgumentException ex1) {
  139. errorBus.publishAsync(new UserErrorEvent(ErrorLevel.LOW, ex1, "Unable to open folder: "
  140. + file.getParentFile(), ""));
  141. openButton.setEnabled(false);
  142. } catch (IOException ex1) {
  143. errorBus.publishAsync(new UserErrorEvent(ErrorLevel.LOW, ex1,
  144. "No associated handler to open file or directory.", ""));
  145. openButton.setEnabled(false);
  146. }
  147. }
  148. }
  149. }
  150. @Override
  151. public void onSocketClosed(final Parser parser, final Date date) {
  152. if ("Resend".equals(button.getText())) {
  153. button.setText("Close Window");
  154. }
  155. }
  156. @Override
  157. public void socketClosed(final DCCTransfer dcc) {
  158. UIUtilities.invokeLater(() -> {
  159. if (transferContainer.isComplete()) {
  160. status.setText("Status: Transfer Complete.");
  161. if (transferContainer.shouldShowOpenButton()) {
  162. openButton.setVisible(true);
  163. }
  164. progress.setValue(100);
  165. button.setText("Close Window");
  166. } else {
  167. status.setText("Status: Transfer Failed.");
  168. if (dcc.getType() == DCCTransfer.TransferType.SEND) {
  169. button.setText("Resend");
  170. } else {
  171. button.setText("Close Window");
  172. }
  173. }
  174. });
  175. }
  176. @Override
  177. public void socketOpened(final DCCTransfer dcc) {
  178. UIUtilities.invokeLater(() -> status.setText("Status: Socket Opened"));
  179. }
  180. @Override
  181. public void dataTransferred(final DCCTransfer dcc, final int bytes) {
  182. UIUtilities.invokeLater(() -> {
  183. if (dcc.getType() == DCCTransfer.TransferType.SEND) {
  184. status.setText("Status: Sending");
  185. } else {
  186. status.setText("Status: Receiving");
  187. }
  188. progress.setValue((int) transferContainer.getPercent());
  189. final double bytesPerSecond = transferContainer
  190. .getBytesPerSecond();
  191. if (bytesPerSecond > 1048576) {
  192. speed.setText(String.format("Speed: %.2f MiB/s",
  193. bytesPerSecond / 1048576));
  194. } else if (bytesPerSecond > 1024) {
  195. speed.setText(String.format("Speed: %.2f KiB/s",
  196. bytesPerSecond / 1024));
  197. } else {
  198. speed.setText(String.format("Speed: %.2f B/s",
  199. bytesPerSecond));
  200. }
  201. remaining.setText(String.format("Time Remaining: %s",
  202. DateUtils.formatDurationAsTime((int) transferContainer.getRemainingTime())));
  203. taken.setText(String.format("Time Taken: %s", transferContainer
  204. .getStartTime() == 0 ? "N/A" : DateUtils.formatDurationAsTime(
  205. (int) transferContainer.getElapsedTime())));
  206. });
  207. }
  208. }