-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathFileDialog.java
More file actions
executable file
·427 lines (398 loc) · 12.5 KB
/
Copy pathFileDialog.java
File metadata and controls
executable file
·427 lines (398 loc) · 12.5 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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package javaforce.utils;
import java.io.*;
import java.util.*;
import javaforce.awt.*;
/** File Dialog (used by jfFile)
*
* @author pquiring
*/
public class FileDialog extends javax.swing.JFrame {
/**
* Creates new form
*/
public FileDialog() {
initComponents();
JFAWT.centerWindow(this);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
txt1 = new javax.swing.JLabel();
progress = new javax.swing.JProgressBar();
cancel = new javax.swing.JButton();
txt2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
setResizable(false);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
formWindowClosing(evt);
}
});
txt1.setText("Status : ...");
cancel.setText("Cancel");
cancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cancelActionPerformed(evt);
}
});
txt2.setText("To : ...");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txt1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(progress, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(cancel))
.addComponent(txt2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(txt1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txt2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(progress, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(cancel)
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
if (worker != null) {
worker.abort = true;
}
}//GEN-LAST:event_cancelActionPerformed
private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
if (worker != null) {
if (worker.abort == true) {
System.exit(0);
}
worker.abort = true;
}
}//GEN-LAST:event_formWindowClosing
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
if (args == null || args.length == 0) {
System.out.println("Usage:jcp src(s) dest");
System.out.println("Usage:jmv src(s) dest");
System.out.println("Usage:jrm file(s)");
return;
}
final String[] _args = args;
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
FileDialog fop = new FileDialog();
fop.setVisible(true);
fop.start(_args);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton cancel;
private javax.swing.JProgressBar progress;
private javax.swing.JLabel txt1;
private javax.swing.JLabel txt2;
// End of variables declaration//GEN-END:variables
private String[] cmd;
private Worker worker;
private Timer timer;
private boolean done;
private String msg1, msg2;
private boolean hidetxt2;
private long amtComplete, amtTodo;
public void start(String[] cmd) {
this.cmd = cmd;
worker = new Worker();
worker.start();
timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
update();
}
});
}
}, 500, 500);
}
private String fit(String in) {
if (in.length() < 25) {
return in;
}
int idx = in.indexOf(" ");
String p1 = in.substring(0,idx+1);
return p1 + "..." + in.substring(in.length() - (25 - p1.length()));
}
private synchronized void update() {
if (msg1 != null) txt1.setText(fit(msg1));
if (msg2 != null) txt2.setText(fit(msg2));
if (hidetxt2) {
hidetxt2 = false;
txt2.setVisible(false);
}
if (amtTodo == 0) return;
int percent;
if (done) {
percent = 100;
} else {
percent = (int)(amtComplete * 100 / amtTodo);
}
progress.setValue(percent);
}
public static class Op {
public String src, dest;
public Op(String s, String d) {
src=s;
dest=d;
}
}
public class Worker extends Thread {
ArrayList<Op> ops = new ArrayList<Op>();
boolean abort;
public void run() {
//do the actual work
try {
if (cmd[0].equals("cp")) {
setTitle("Copying...");
msg1 = "Scanning files...";
String dest = cmd[cmd.length-1];
for(int a=1;a<cmd.length-1;a++) {
ops.add(new Op(cmd[a], dest));
}
amtTodo = getFileSize();
copy();
} else if (cmd[0].equals("mv")) {
setTitle("Moving...");
msg1 = "Scanning files...";
String dest = cmd[cmd.length-1];
for(int a=1;a<cmd.length-1;a++) {
ops.add(new Op(cmd[a], dest));
}
amtTodo = getFileCount();
move();
} else if (cmd[0].equals("rm")) {
setTitle("Deleting...");
hidetxt2 = true;
amtTodo = getFileCount();
for(int a=1;a<cmd.length;a++) {
remove(cmd[a]);
}
}
} catch (Exception e) {}
System.exit(0);
}
public long getFileSize() {
long size = 0;
for (int a=0;a<ops.size();a++) {
if (abort) return 0;
String src = ops.get(a).src;
if (new File(src).isDirectory()) {
size += getDirectorySize(src);
} else {
size += getFileSize(src);
}
}
return size;
}
public long getFileSize(String file) {
return new File(file).length();
}
public long getDirectorySize(String dir) {
long size = 4096;
File[] files = new File(dir).listFiles();
if (files == null) return 0;
for(int a=0;a<files.length;a++) {
if (abort) return 0;
if (files[a].isDirectory()) {
size += getDirectorySize(files[a].getAbsolutePath());
} else {
size += getFileSize(files[a].getAbsolutePath());
}
}
return size;
}
public long getFileCount() {
long size = 0;
for (int a=0;a<ops.size();a++) {
if (abort) return 0;
String src = ops.get(a).src;
if (new File(src).isDirectory()) {
size += getDirectoryCount(src);
} else {
size += getFileCount(src);
}
}
return size;
}
public long getFileCount(String file) {
return 1;
}
public long getDirectoryCount(String dir) {
long size = 1;
File[] files = new File(dir).listFiles();
if (files == null) return 0;
for(int a=0;a<files.length;a++) {
if (abort) return 0;
if (files[a].isDirectory()) {
size += getDirectorySize(files[a].getAbsolutePath());
} else {
size += getFileSize(files[a].getAbsolutePath());
}
}
return size;
}
public void copy() {
while (ops.size() > 0) {
if (abort) return;
Op op = ops.remove(0);
File srcFile = new File(op.src);
if (srcFile.isDirectory()) {
copyDirectory(op.src, op.dest + "/" + srcFile.getName());
} else {
copyFile(op.src, op.dest);
}
}
}
public final static int bufsiz = 64 * 1024;
public byte[] buf = new byte[bufsiz];
public void copyFile(String src, String destFolder) {
msg1 = "Copying " + src;
msg2 = "To " + destFolder;
long amtBefore = amtComplete;
FileInputStream fis = null;
FileOutputStream fos = null;
File srcFile = new File(src);
File destFile = new File(destFolder, srcFile.getName());
long size = srcFile.length();
try {
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(destFile);
long copied = 0;
long left = size;
int toRead;
while (copied < size) {
if (abort) throw new Exception("abort");
if (left > bufsiz) {
toRead = bufsiz;
} else {
toRead = (int)left;
}
int read = fis.read(buf, 0, toRead);
if (read <= 0) {
throw new Exception("read error");
}
fos.write(buf, 0, read);
copied += read;
left -= read;
}
fis.close();
fis = null;
fos.close();
fos = null;
} catch (Exception e) {
try { if (fis != null) fis.close(); } catch (Exception e1) {}
try { if (fos != null) fos.close(); } catch (Exception e2) {}
destFile.delete();
if (abort) return;
if (JFAWT.showConfirm("Copy Error", "Failed to copy:" + src + "\nRetry?")) {
ops.add(new Op(src, destFolder));
amtComplete = amtBefore;
} else {
amtComplete = amtBefore + size;
}
}
}
public void copyDirectory(String src, String dest) {
msg1 = "Copying " + src;
msg2 = "To " + dest;
File srcFolder = new File(src);
File destFolder = new File(dest);
while ((!destFolder.exists()) && !destFolder.mkdir()) {
if (abort) return;
if (!JFAWT.showConfirm("Copy Error", "Failed to create folder:" + destFolder.getAbsolutePath() + "\nRetry?")) {
return;
}
}
amtComplete += 4096;
File[] files = srcFolder.listFiles();
if (files == null) return;
for(int a=0;a<files.length;a++) {
if (abort) return;
boolean isDir = files[a].isDirectory();
if (isDir) {
copyDirectory(files[a].getAbsolutePath(), dest + "/" + files[a].getName());
} else {
copyFile(files[a].getAbsolutePath(), dest);
}
}
}
public void move() {
while (ops.size() > 0) {
if (abort) return;
Op op = ops.remove(0);
move(op.src, op.dest);
}
}
public void move(String src, String dest) {
msg1 = "Moving " + src;
msg2 = "To " + dest;
File srcFile = new File(src);
File destFile = new File(dest, srcFile.getName());
while (!srcFile.renameTo(destFile)) {
if (!JFAWT.showConfirm("Move Error", "Failed to move:" + src + "\nRetry?")) {
return;
}
}
amtComplete++;
}
public void remove(String name) {
if (abort) return;
msg1 = "Deleting " + name;
File file = new File(name);
if (file.isDirectory()) {
removeDirectory(name);
} else {
while (!file.delete()) {
if (!JFAWT.showConfirm("Delete Error", "Failed to delete:" + name + "\nRetry?")) {
return;
}
}
amtComplete++;
}
}
public void removeDirectory(String name) {
File[] files = new File(name).listFiles();
msg1 = "Deleting " + name;
if (files != null) {
for(int a=0;a<files.length;a++) {
if (abort) return;
remove(files[a].getAbsolutePath());
}
}
File dir = new File(name);
while (!dir.delete()) {
if (!JFAWT.showConfirm("Delete Error", "Failed to delete:" + name + "\nRetry?")) {
return;
}
}
amtComplete++;
}
}
}