1 |
package net.oni2.aeinstaller.gui.downloadwindow; |
2 |
|
3 |
import java.util.ResourceBundle; |
4 |
import java.util.TreeSet; |
5 |
|
6 |
import javax.swing.JDialog; |
7 |
import javax.swing.JLabel; |
8 |
import javax.swing.JOptionPane; |
9 |
import javax.swing.JProgressBar; |
10 |
|
11 |
import net.oni2.aeinstaller.backend.SizeFormatter; |
12 |
import net.oni2.aeinstaller.backend.mods.Mod; |
13 |
import net.oni2.aeinstaller.backend.mods.download.ModDownloader; |
14 |
import net.oni2.aeinstaller.backend.mods.download.ModDownloader.State; |
15 |
import net.oni2.aeinstaller.backend.mods.download.ModDownloaderListener; |
16 |
|
17 |
import org.javabuilders.BuildResult; |
18 |
import org.javabuilders.swing.SwingJavaBuilder; |
19 |
|
20 |
/** |
21 |
* @author Christian Illy |
22 |
*/ |
23 |
public class Downloader extends JDialog implements ModDownloaderListener { |
24 |
private static final long serialVersionUID = 9097967828001263776L; |
25 |
|
26 |
private ResourceBundle bundle = ResourceBundle |
27 |
.getBundle("net.oni2.aeinstaller.localization." |
28 |
+ getClass().getSimpleName()); |
29 |
@SuppressWarnings("unused") |
30 |
private BuildResult result = SwingJavaBuilder.build(this, bundle); |
31 |
|
32 |
private JLabel lblElapsedVal; |
33 |
private JLabel lblRemainingVal; |
34 |
private JLabel lblDownloadedVal; |
35 |
private JLabel lblTotalVal; |
36 |
private JLabel lblRateVal; |
37 |
private JProgressBar progress; |
38 |
|
39 |
private ModDownloader downloader; |
40 |
|
41 |
/** |
42 |
* @param mods |
43 |
* Mods to download |
44 |
*/ |
45 |
public Downloader(TreeSet<Mod> mods) { |
46 |
super(); |
47 |
|
48 |
setResizable(false); |
49 |
setSize(500, (int) getSize().getHeight()); |
50 |
|
51 |
downloader = new ModDownloader(mods, this); |
52 |
progress.setMaximum(downloader.getTotalSize()); |
53 |
progress.setStringPainted(true); |
54 |
progress.setToolTipText(String.format("%d / %d files downloaded", 0, |
55 |
mods.size())); |
56 |
|
57 |
lblDownloadedVal.setText(SizeFormatter.format(0, 3)); |
58 |
lblTotalVal.setText(SizeFormatter.format(downloader.getTotalSize(), 3)); |
59 |
} |
60 |
|
61 |
private void close() { |
62 |
if (!downloader.isFinished()) |
63 |
downloader.abort(); |
64 |
setVisible(false); |
65 |
} |
66 |
|
67 |
@SuppressWarnings("unused") |
68 |
private boolean confirm() { |
69 |
int res = JOptionPane.showConfirmDialog(this, |
70 |
bundle.getString("abort.text"), |
71 |
bundle.getString("abort.title"), JOptionPane.YES_NO_OPTION, |
72 |
JOptionPane.WARNING_MESSAGE); |
73 |
return res == JOptionPane.YES_OPTION; |
74 |
} |
75 |
|
76 |
private String formatTime(int sec) { |
77 |
int min = sec / 60; |
78 |
sec = sec % 60; |
79 |
return String.format("%02d:%02d", min, sec); |
80 |
} |
81 |
|
82 |
@Override |
83 |
public void updateStatus(ModDownloader source, State state, int filesDown, |
84 |
int filesTotal, int bytesDown, int bytesTotal, int duration, |
85 |
int remaining, int speed) { |
86 |
if (state == ModDownloader.State.FINISHED) { |
87 |
close(); |
88 |
} else { |
89 |
progress.setValue(bytesDown); |
90 |
progress.setToolTipText(String.format("%d / %d files downloaded", |
91 |
filesDown, filesTotal)); |
92 |
|
93 |
lblElapsedVal.setText(formatTime(duration)); |
94 |
lblRemainingVal.setText(formatTime(remaining)); |
95 |
|
96 |
lblDownloadedVal.setText(SizeFormatter.format(bytesDown, 3)); |
97 |
|
98 |
lblRateVal.setText(SizeFormatter.format(speed, 3) + "/s"); |
99 |
} |
100 |
} |
101 |
|
102 |
/** |
103 |
* @return were all downloads finished? |
104 |
*/ |
105 |
public boolean isFinished() { |
106 |
return downloader.isFinished(); |
107 |
} |
108 |
|
109 |
} |