-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownloadMenu.java
More file actions
226 lines (169 loc) · 5.47 KB
/
DownloadMenu.java
File metadata and controls
226 lines (169 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package VAMIX;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.SwingWorker;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileFilter;
import uk.co.caprica.vlcj.filter.swing.SwingFileFilterFactory;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
public class DownloadMenu extends SwingWorker<Void, Integer> {
private static boolean downloadPressed = false;
private static boolean cancelPressed = false;
private JFrame downloadFrame;
private JPanel contentPane;
private JTextField URL_field;
private JButton downloadButton;
private JButton continueButton;
private JButton cancelButton;
private JProgressBar progress;
//Store variable need to download in swingworker
private static String URL;
private static String saveLocation;
private static Process process2;
/**
* Create the frame.
*/
public DownloadMenu() {
//create frame
downloadFrame = new JFrame("Download");
downloadFrame.getContentPane().setBackground(Color.BLACK);
downloadFrame.setBounds(100, 100, 450, 150);
downloadFrame.setVisible(true);
createUI();
}
private void createUI(){
createControls();
createLayout();
}
private void createControls(){
URL_field = new JTextField(40);
downloadButton = new JButton("Download");
downloadButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
URL = URL_field.getText();
System.out.println(URL);
if(URL.equals("")){
JOptionPane.showMessageDialog(null, "No URL typed in","VAMIX", JOptionPane.INFORMATION_MESSAGE);
} else {
saveLocation = fileSaver.savePath();
//check if open source
int value = JOptionPane.showConfirmDialog(null, "Is this file open source?", "VAMIX", JOptionPane.YES_NO_OPTION);
if (value == JOptionPane.YES_OPTION){
downloadPressed = true;
execute();
} else {
JOptionPane.showMessageDialog(null, "Sorry not allowed to Download","VAMIX", JOptionPane.INFORMATION_MESSAGE);
}
}
}
});
continueButton = new JButton("Continue");
continueButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(cancelPressed){
execute();
} else {
JOptionPane.showMessageDialog(null, "No previous task cancelled","VAMIX", JOptionPane.INFORMATION_MESSAGE);
}
}
});
cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cancel(true);
}
});
progress = new JProgressBar();
progress.setStringPainted(true);
}
private void createLayout(){
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(4, 4, 4, 4));
contentPane.setLayout(new GridLayout(3,1));
JPanel textPanel = new JPanel();
textPanel.setBackground(Color.BLACK);
textPanel.add(URL_field);
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.BLACK);
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(downloadButton);
buttonPanel.add(continueButton);
buttonPanel.add(cancelButton);
JPanel progressPanel = new JPanel();
progressPanel.setBackground(Color.BLACK);
progressPanel.add(progress);
//add to frame
contentPane.add(textPanel);
contentPane.add(buttonPanel);
contentPane.add(progressPanel);
downloadFrame.getContentPane().add(contentPane);
}
protected Void doInBackground() throws Exception {
String cmd="wget --progress=dot -c " + URL +" -O " + saveLocation;
ProcessBuilder builder2 = new ProcessBuilder("/bin/bash", "-c", cmd);
process2 = builder2.start();
InputStream stdout = process2.getErrorStream();
BufferedReader stdoutBuffered = new BufferedReader(new InputStreamReader(stdout));
builder2.redirectErrorStream(true);
String line = null;
while ((line = stdoutBuffered.readLine()) != null ) {
if(isCancelled()){
process2.destroy();
cancelPressed = true;
} else {
String[] splitedLine = line.split("\\s+");
if(splitedLine.length == 10){
String valuePercent = splitedLine[7];
String [] valueArray = valuePercent.split("%");
String value = valueArray[0];
int v = Integer.parseInt(value);
publish(v);
}
}
};
return null;
}
@Override
protected void process(final List<Integer> chunks) {
// Updates the progressBar
int position = chunks.size();
int value = chunks.get(position-1);
progress.setValue(value);
}
protected void done(){
if(isCancelled()){
if(!downloadPressed){
JOptionPane.showMessageDialog(null, "Nothing to cancel","VAMIX", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "Download Cancelled","VAMIX", JOptionPane.INFORMATION_MESSAGE);
downloadFrame.dispose();
}
} else {
progress.setValue(100);
JFrame messageFrame = new JFrame();
JOptionPane.showMessageDialog(messageFrame, "Downloading Finished","VAMIX", JOptionPane.INFORMATION_MESSAGE);
//reset to 0
progress.setValue(0);
process2.destroy();
downloadFrame.dispose();
}
new DownloadMenu();
}
}