package net.oni2.aeinstaller.gui.downloadwindow; import java.util.ResourceBundle; import java.util.TreeSet; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JProgressBar; import net.oni2.aeinstaller.backend.SizeFormatter; import net.oni2.aeinstaller.backend.mods.Mod; import net.oni2.aeinstaller.backend.mods.download.ModDownloader; import net.oni2.aeinstaller.backend.mods.download.ModDownloader.State; import net.oni2.aeinstaller.backend.mods.download.ModDownloaderListener; import org.javabuilders.BuildResult; import org.javabuilders.swing.SwingJavaBuilder; /** * @author Christian Illy */ public class Downloader extends JDialog implements ModDownloaderListener { private static final long serialVersionUID = 9097967828001263776L; private ResourceBundle bundle = ResourceBundle.getBundle(getClass() .getName()); @SuppressWarnings("unused") private BuildResult result = SwingJavaBuilder.build(this, bundle); private JLabel lblElapsedVal; private JLabel lblRemainingVal; private JLabel lblDownloadedVal; private JLabel lblTotalVal; private JLabel lblRateVal; private JProgressBar progress; private ModDownloader downloader; /** * @param mods * Mods to download */ public Downloader(TreeSet mods) { super(); setResizable(false); setSize(500, (int) getSize().getHeight()); downloader = new ModDownloader(mods, this); progress.setMaximum(downloader.getTotalSize()); progress.setStringPainted(true); progress.setToolTipText(String.format("%d / %d files downloaded", 0, mods.size())); lblDownloadedVal.setText(SizeFormatter.format(0, 3)); lblTotalVal.setText(SizeFormatter.format(downloader.getTotalSize(), 3)); } private void close() { if (!downloader.isFinished()) downloader.abort(); setVisible(false); } @SuppressWarnings("unused") private boolean confirm() { int res = JOptionPane.showConfirmDialog(this, bundle.getString("abort.text"), bundle.getString("abort.title"), JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE); return res == JOptionPane.YES_OPTION; } private String formatTime(int sec) { int min = sec / 60; sec = sec % 60; return String.format("%02d:%02d", min, sec); } @Override public void updateStatus(ModDownloader source, State state, int filesDown, int filesTotal, int bytesDown, int bytesTotal, int duration, int remaining, int speed) { if (state == ModDownloader.State.FINISHED) { close(); } else { progress.setValue(bytesDown); progress.setToolTipText(String.format("%d / %d files downloaded", filesDown, filesTotal)); lblElapsedVal.setText(formatTime(duration)); lblRemainingVal.setText(formatTime(remaining)); lblDownloadedVal.setText(SizeFormatter.format(bytesDown, 3)); lblRateVal.setText(SizeFormatter.format(speed, 3) + "/s"); } } /** * @return were all downloads finished? */ public boolean isFinished() { return downloader.isFinished(); } }