forked from processing/processing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgressFrame.java
More file actions
340 lines (287 loc) · 11 KB
/
Copy pathProgressFrame.java
File metadata and controls
340 lines (287 loc) · 11 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package processing.app.ui;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
import processing.app.Language;
import processing.app.Util;
// TODO This code was contributed and needs a lot of work. [fry]
// + It probably shouldn't a "Frame" object at all.
// + It should be more general to the tasks at hand (why Save As and Add File
// in here?) We'd have to copy the code again for a new "Task"
// + Instead of multiple implementations of file and directory copy, it should
// just use a single version from Base that takes a ProgressMonitor as an arg
/**
* Class used to handle progress bar, and run Save As or Add File in
* background so that progress bar can update without freezing.
*/
public class ProgressFrame extends JFrame implements PropertyChangeListener {
private JProgressBar progressBar;
private JLabel label;
private File[] copyItems;
private File newFolder;
private File addFile, sourceFile;
private Editor editor;
/** Create a new background thread to Save As */
public class TaskSaveAs extends SwingWorker<Void, Void> {
@Override
protected Void doInBackground() throws Exception {
// a large part of the file copying happens in this background
// thread
long totalSize = 0;
for (File copyable : copyItems) {
totalSize += calcSize(copyable);
}
long progress = 0;
setProgress(0);
for (File copyable : ProgressFrame.this.copyItems) {
// loop to copy over the items that make sense, and to set the
// current progress
if (copyable.isDirectory()) {
copyDir(copyable,
new File(ProgressFrame.this.newFolder, copyable.getName()),
progress, totalSize);
progress += calcSize(copyable);
} else {
copyFile(copyable,
new File(ProgressFrame.this.newFolder, copyable.getName()),
progress, totalSize);
if (calcSize(copyable) < 524288) {
// If the file length > 0.5MB, the copyFile() function has
// been redesigned to change progress every 0.5MB so that
// the progress bar doesn't stagnate during that time
progress += calcSize(copyable);
setProgress((int) (progress * 100L / totalSize));
}
}
}
return null;
}
/**
* Overloaded copyFile that is called whenever a Save As is being done,
* so that the ProgressBar is updated for very large files as well.
*/
private void copyFile(File sourceFile, File targetFile,
double progress, long totalSize) throws IOException {
BufferedInputStream from = new BufferedInputStream(new FileInputStream(sourceFile));
BufferedOutputStream to = new BufferedOutputStream(new FileOutputStream(targetFile));
byte[] buffer = new byte[16 * 1024];
int bytesRead;
int totalRead = 0;
while ((bytesRead = from.read(buffer)) != -1) {
to.write(buffer, 0, bytesRead);
totalRead += bytesRead;
if (totalRead >= 512 * 1024) { // to update progress bar every 0.5MB
progress += totalRead;
setProgressBarStatus((int) Math.min(Math.ceil(progress * 100.0 / totalSize), 100));
totalRead = 0;
}
}
if (sourceFile.length() > 512 * 1024) {
// Update the progress bar one final time if file size is more than 0.5MB,
// otherwise, the update is handled either by the copyDir function,
// or directly by ProgressFrame.TaskSaveAs.doInBackground()
progress += totalRead;
setProgressBarStatus((int) Math.min(Math.ceil(progress * 100.0 / totalSize), 100));
}
from.close();
from = null;
to.flush();
to.close();
to = null;
targetFile.setLastModified(sourceFile.lastModified());
targetFile.setExecutable(sourceFile.canExecute());
}
private double copyDir(File sourceDir, File targetDir,
double progress, long totalSize) throws IOException {
// Overloaded copyDir so that the Save As progress bar gets updated when the
// files are in folders as well (like in the data folder)
if (sourceDir.equals(targetDir)) {
final String urDum = "source and target directories are identical";
throw new IllegalArgumentException(urDum);
}
targetDir.mkdirs();
String files[] = sourceDir.list();
for (int i = 0; i < files.length; i++) {
// Ignore dot files (.DS_Store), dot folders (.svn) while copying
if (files[i].charAt(0) == '.')
continue;
//if (files[i].equals(".") || files[i].equals("..")) continue;
File source = new File(sourceDir, files[i]);
File target = new File(targetDir, files[i]);
if (source.isDirectory()) {
//target.mkdirs();
progress = copyDir(source, target, progress, totalSize);
setProgressBarStatus((int) Math.min(Math.ceil(progress * 100.0 / totalSize), 100));
target.setLastModified(source.lastModified());
} else {
copyFile(source, target, progress, totalSize);
// Update SaveAs progress bar
progress += source.length();
setProgressBarStatus((int) Math.min(Math.ceil(progress * 100.0 / totalSize), 100));
}
}
return progress;
}
public void setProgressBarStatus(int status) {
setProgress(status);
}
@Override
public void done() {
// to close the progress bar automatically when done, and to
// print that Saving is done in Message Area
editor.statusNotice(Language.text("editor.status.saving.done"));
dispose();
}
}
/** Create a new background thread to add a file. */
public class TaskAddFile extends SwingWorker<Void, Void> {
@Override
protected Void doInBackground() throws Exception {
// a large part of the file copying happens in this background
// thread
setProgress(0);
copyFile(sourceFile, addFile);
if (addFile.length() < 1024) {
// If the file length > 1kB, the copyFile() function has
// been redesigned to change progress every 1kB so that
// the progress bar doesn't stagnate during that time
// If file <1 kB, just fill up Progress Bar to 100%
// directly, since time to copy is now negligable (when
// perceived by a human, anyway)
setProgress(100);
}
return null;
}
/**
* Overloaded copyFile that is called whenever a addFile is being done,
* so that the ProgressBar is updated.
*/
private void copyFile(File sourceFile, File targetFile) throws IOException {
long totalSize = sourceFile.length();
int progress = 0;
BufferedInputStream from = new BufferedInputStream(new FileInputStream(sourceFile));
BufferedOutputStream to = new BufferedOutputStream(new FileOutputStream(targetFile));
byte[] buffer = new byte[16 * 1024];
int bytesRead;
int totalRead = 0;
while ((bytesRead = from.read(buffer)) != -1) {
to.write(buffer, 0, bytesRead);
totalRead += bytesRead;
if (totalRead >= 1024) { // to update progress bar every 1kB
progress += totalRead;
setProgressBarStatus((int) Math.min(Math.ceil(progress * 100.0 / totalSize), 100));
totalRead = 0;
}
}
if (sourceFile.length() > 1024) {
// Update the progress bar one final time if file size is more than
// 1kB, otherwise, the update is handled directly by
// ProgressFrame.TaskAddFile.doInBackground()
progress += totalRead;
setProgressBarStatus((int) Math.min(Math.ceil(progress * 100.0 / totalSize), 100));
}
from.close();
from = null;
to.flush();
to.close();
to = null;
targetFile.setLastModified(sourceFile.lastModified());
targetFile.setExecutable(sourceFile.canExecute());
}
public void setProgressBarStatus(int status) {
setProgress(status);
}
@Override
public void done() {
dispose();
}
}
/** Use for Save As */
public ProgressFrame(File[] c, File nf, String oldName, String newName,
Editor editor) {
// initialize a copyItems and newFolder, which are used for file
// copying in the background thread
copyItems = c;
newFolder = nf;
this.editor = editor;
// the UI of the progress bar follows
setDefaultCloseOperation(HIDE_ON_CLOSE);
setBounds(200, 200, 400, 140);
setResizable(false);
setTitle("Saving As...");
JPanel panel = new JPanel(null);
add(panel);
setContentPane(panel);
label = new JLabel("Saving " + oldName + " as " + newName + "...");
label.setBounds(40, 20, 300, 20);
progressBar = new JProgressBar(0, 100);
progressBar.setValue(0);
progressBar.setBounds(40, 50, 300, 30);
progressBar.setStringPainted(true);
panel.add(progressBar);
panel.add(label);
Toolkit.setIcon(this);
this.setVisible(true);
// create an instance of TaskSaveAs and run execute() on this
// instance to start background thread
TaskSaveAs t = new TaskSaveAs();
t.addPropertyChangeListener(this);
t.execute();
}
/** Used for Add File */
public ProgressFrame(File sf, File add, Editor editor) {
addFile = add;
sourceFile = sf;
this.editor = editor;
// the UI of the progress bar follows
setDefaultCloseOperation(HIDE_ON_CLOSE);
setBounds(200, 200, 400, 140);
setResizable(false);
setTitle("Adding File...");
JPanel panel = new JPanel(null);
add(panel);
setContentPane(panel);
label = new JLabel("Adding " + addFile.getName());
label.setBounds(40, 20, 300, 20);
progressBar = new JProgressBar(0, 100);
progressBar.setValue(0);
progressBar.setBounds(40, 50, 300, 30);
progressBar.setStringPainted(true);
panel.add(progressBar);
panel.add(label);
Toolkit.setIcon(this);
this.setVisible(true);
// create an instance of TaskAddFile and run execute() on this
// instance to
// start background thread
TaskAddFile task = new TaskAddFile();
task.addPropertyChangeListener(this);
task.execute();
}
/**
* Function to return the length of the file, or entire directory, including
* the component files and sub-folders if passed.
*/
long calcSize(File file) {
return file.isFile() ? file.length() : Util.calcFolderSize(file);
}
/**
* Detects a change in the property of the background task,
* i.e., is called when the size of files already copied changes.
*/
public void propertyChange(PropertyChangeEvent evt) {
if ("progress".equals(evt.getPropertyName())) {
progressBar.setValue((Integer) evt.getNewValue());
}
}
}