Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

TransferPanel.java 9.0KB

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