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.getBundle(getClass() |
27 |
.getName()); |
28 |
@SuppressWarnings("unused") |
29 |
private BuildResult result = SwingJavaBuilder.build(this, bundle); |
30 |
|
31 |
private JLabel lblElapsedVal; |
32 |
private JLabel lblRemainingVal; |
33 |
private JLabel lblDownloadedVal; |
34 |
private JLabel lblTotalVal; |
35 |
private JLabel lblRateVal; |
36 |
private JProgressBar progress; |
37 |
|
38 |
private ModDownloader downloader; |
39 |
|
40 |
/** |
41 |
* @param mods |
42 |
* Mods to download |
43 |
*/ |
44 |
public Downloader(TreeSet<Mod> mods) { |
45 |
super(); |
46 |
|
47 |
setResizable(false); |
48 |
setSize(500, (int) getSize().getHeight()); |
49 |
|
50 |
downloader = new ModDownloader(mods, this); |
51 |
progress.setMaximum(downloader.getTotalSize()); |
52 |
progress.setStringPainted(true); |
53 |
progress.setToolTipText(String.format("%d / %d files downloaded", 0, |
54 |
mods.size())); |
55 |
|
56 |
lblDownloadedVal.setText(SizeFormatter.format(0, 3)); |
57 |
lblTotalVal.setText(SizeFormatter.format(downloader.getTotalSize(), 3)); |
58 |
} |
59 |
|
60 |
private void close() { |
61 |
if (!downloader.isFinished()) |
62 |
downloader.abort(); |
63 |
setVisible(false); |
64 |
} |
65 |
|
66 |
@SuppressWarnings("unused") |
67 |
private boolean confirm() { |
68 |
int res = JOptionPane.showConfirmDialog(this, |
69 |
bundle.getString("abort.text"), |
70 |
bundle.getString("abort.title"), JOptionPane.YES_NO_OPTION, |
71 |
JOptionPane.WARNING_MESSAGE); |
72 |
return res == JOptionPane.YES_OPTION; |
73 |
} |
74 |
|
75 |
private String formatTime(int sec) { |
76 |
int min = sec / 60; |
77 |
sec = sec % 60; |
78 |
return String.format("%02d:%02d", min, sec); |
79 |
} |
80 |
|
81 |
@Override |
82 |
public void updateStatus(ModDownloader source, State state, int filesDown, |
83 |
int filesTotal, int bytesDown, int bytesTotal, int duration, |
84 |
int remaining, int speed) { |
85 |
if (state == ModDownloader.State.FINISHED) { |
86 |
close(); |
87 |
} else { |
88 |
progress.setValue(bytesDown); |
89 |
progress.setToolTipText(String.format("%d / %d files downloaded", |
90 |
filesDown, filesTotal)); |
91 |
|
92 |
lblElapsedVal.setText(formatTime(duration)); |
93 |
lblRemainingVal.setText(formatTime(remaining)); |
94 |
|
95 |
lblDownloadedVal.setText(SizeFormatter.format(bytesDown, 3)); |
96 |
|
97 |
lblRateVal.setText(SizeFormatter.format(speed, 3) + "/s"); |
98 |
} |
99 |
} |
100 |
|
101 |
/** |
102 |
* @return were all downloads finished? |
103 |
*/ |
104 |
public boolean isFinished() { |
105 |
return downloader.isFinished(); |
106 |
} |
107 |
|
108 |
} |