FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
processing/app/src/processing/app/ProgressFrame.java at master · botbit/processing · GitHub
botbit
processing
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
processing
/
app
/
src
/
processing
/
app
/
ProgressFrame.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
379 lines (326 loc) · 12.3 KB
Breadcrumbs
processing
/
app
/
src
/
processing
/
app
/
ProgressFrame.java
Copy path
File metadata and controls
379 lines (326 loc) · 12.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package
processing
.
app
;
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
;
//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
static
final
long
serialVersionUID
=
1L
;
private
JProgressBar
progressBar
;
private
JLabel
saveAsLabel
;
private
TaskSaveAs
t
;
private
TaskAddFile
t2
;
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
+=
getFileLength
(
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
()),
this
,
progress
,
totalSize
);
progress
+=
getFileLength
(
copyable
);
}
else
{
copyFile
(
copyable
,
new
File
(
ProgressFrame
.
this
.
newFolder
,
copyable
.
getName
()),
this
,
progress
,
totalSize
);
if
(
getFileLength
(
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
+=
getFileLength
(
copyable
);
setProgress
((
int
)
Math
.
min
(
Math
.
ceil
(
progress
*
100.0
/
totalSize
),
100
));
}
}
}
return
null
;
}
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"
));
ProgressFrame
.
this
.
closeProgressBar
();
}
}
// 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
,
this
);
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
;
}
public
void
setProgressBarStatus
(
int
status
) {
setProgress
(
status
);
}
@
Override
public
void
done
() {
// to close the progress bar automatically when done, and to
// print that adding file is done in Message Area
editor
.
statusNotice
(
Language
.
text
(
"editor.status.drag_and_drop.files_added.1"
));
ProgressFrame
.
this
.
closeProgressBar
();
}
}
//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
);
saveAsLabel
=
new
JLabel
(
"Saving "
+
oldName
+
" as "
+
newName
+
"..."
);
saveAsLabel
.
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
(
saveAsLabel
);
Toolkit
.
setIcon
(
this
);
this
.
setVisible
(
true
);
// create an instance of TaskSaveAs and run execute() on this
// instance to
// start background thread
t
=
new
TaskSaveAs
();
t
.
addPropertyChangeListener
(
this
);
t
.
execute
();
}
//Use 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
);
saveAsLabel
=
new
JLabel
(
"Adding "
+
addFile
.
getName
());
saveAsLabel
.
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
(
saveAsLabel
);
Toolkit
.
setIcon
(
this
);
this
.
setVisible
(
true
);
// create an instance of TaskAddFile and run execute() on this
// instance to
// start background thread
t2
=
new
TaskAddFile
();
t2
.
addPropertyChangeListener
(
this
);
t2
.
execute
();
}
public
long
getFileLength
(
File
f
)
// function to return the length of
// the file, or
// ENTIRE directory, including the
// component files
// and sub-folders if passed
{
long
fol_len
=
0
;
if
(
f
.
isDirectory
()) {
String
files
[] =
f
.
list
();
for
(
int
i
=
0
;
i
<
files
.
length
;
i
++) {
File
temp
=
new
File
(
f
,
files
[
i
]);
if
(
temp
.
isDirectory
()) {
fol_len
+=
getFileLength
(
temp
);
}
else
{
fol_len
+= (
temp
.
length
());
}
}
}
else
{
return
(
f
.
length
());
}
return
fol_len
;
}
public
void
propertyChange
(
PropertyChangeEvent
evt
)
// detects a change in the property of the background task, i.e., is
// called when the size of files already copied changes
{
if
(
"progress"
==
evt
.
getPropertyName
()) {
int
progress
= (
Integer
)
evt
.
getNewValue
();
progressBar
.
setValue
(
progress
);
}
}
private
void
closeProgressBar
()
// closes progress bar
{
this
.
dispose
();
}
static
public
void
copyFile
(
File
sourceFile
,
File
targetFile
,
ProgressFrame
.
TaskSaveAs
progBar
,
double
progress
,
double
totalSize
)
throws
IOException
{
// Overloaded copyFile that is called whenever a Save As is being done, so that the
// ProgressBar is updated for very large files as well
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
>=
524288
)
//to update progress bar every 0.5MB
{
progress
+=
totalRead
;
progBar
.
setProgressBarStatus
((
int
)
Math
.
min
(
Math
.
ceil
(
progress
*
100.0
/
totalSize
),
100
));
totalRead
=
0
;
}
}
if
(
sourceFile
.
length
() >
524288
) {
// 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
;
progBar
.
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
());
}
static
public
void
copyFile
(
File
sourceFile
,
File
targetFile
,
ProgressFrame
.
TaskAddFile
progBar
)
throws
IOException
{
// Overloaded copyFile that is called whenever a addFile is being done,
// so that the
// ProgressBar is updated
double
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
;
progBar
.
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
;
progBar
.
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
());
}
static
public
double
copyDir
(
File
sourceDir
,
File
targetDir
,
ProgressFrame
.
TaskSaveAs
progBar
,
double
progress
,
double
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
,
progBar
,
progress
,
totalSize
);
progBar
.
setProgressBarStatus
((
int
)
Math
.
min
(
Math
.
ceil
(
progress
*
100.0
/
totalSize
),
100
));
target
.
setLastModified
(
source
.
lastModified
());
}
else
{
copyFile
(
source
,
target
,
progBar
,
progress
,
totalSize
);
// Update SaveAs progress bar
progress
+=
source
.
length
();
progBar
.
setProgressBarStatus
((
int
)
Math
.
min
(
Math
.
ceil
(
progress
*
100.0
/
totalSize
),
100
));
}
}
return
progress
;
}
}
Back
|
FazBrowse Home
|
New Git URL