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 package wjhk.jupload2.upload.helper;
27
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
30
31 import javax.swing.JProgressBar;
32 import javax.swing.Timer;
33
34 import wjhk.jupload2.exception.JUploadException;
35 import wjhk.jupload2.gui.JUploadPanel;
36 import wjhk.jupload2.gui.filepanel.SizeRenderer;
37 import wjhk.jupload2.policies.UploadPolicy;
38 import wjhk.jupload2.upload.FilePreparationThread;
39 import wjhk.jupload2.upload.FileUploadManagerThread;
40 import wjhk.jupload2.upload.UploadFileData;
41 import wjhk.jupload2.upload.UploadFilePacket;
42
43
44
45
46 public class ProgressBarManager implements ActionListener {
47
48
49
50 public final static int DELAY_FOR_UPDATE_OF_PROGRESS_BAR = 100;
51
52
53
54
55
56
57
58
59 long currentRequestStartTime = 0;
60
61
62
63
64
65 UploadFileData currentUploadFileData = null;
66
67
68
69
70 UploadFilePacket currentUploadFilePacket = null;
71
72
73
74
75
76 FilePreparationThread filePreparationThread = null;
77
78
79
80
81
82 long globalStartTime = 0;
83
84
85
86
87
88 long nbBytesUploadedForCurrentFile = 0;
89
90
91
92
93
94 int nbSentFiles = 0;
95
96
97 long nbUploadedBytes = 0;
98
99
100
101
102 JProgressBar preparationProgressBar = null;
103
104
105
106
107 Timer timer;
108
109
110
111
112
113
114 long totalUploadDuration = 0;
115
116
117 UploadPolicy uploadPolicy;
118
119
120
121
122 JProgressBar uploadProgressBar = null;
123
124
125
126
127 int uploadStatus = FileUploadManagerThread.UPLOAD_STATUS_NOT_STARTED;
128
129
130
131
132
133 public ProgressBarManager(UploadPolicy uploadPolicy, FilePreparationThread filePreparationThread) {
134 this.uploadPolicy = uploadPolicy;
135 this.filePreparationThread = filePreparationThread;
136
137 this.timer = new Timer(DELAY_FOR_UPDATE_OF_PROGRESS_BAR, this);
138
139 JUploadPanel uploadPanel = uploadPolicy.getContext().getUploadPanel();
140
141 this.uploadProgressBar = uploadPanel.getUploadProgressBar();
142 this.preparationProgressBar = uploadPanel.getPreparationProgressBar();
143
144 updateUploadProgressBarText(null);
145 }
146
147
148
149
150
151
152 public void actionPerformed(ActionEvent arg0) {
153 updateUploadProgressBarValue();
154 updateUploadStatusBar();
155
156 }
157
158
159
160
161
162
163
164
165
166
167
168 public synchronized void anotherFileHasBeenSent(UploadFilePacket uploadFilePacket, UploadFileData uploadFileData)
169 throws JUploadException {
170 if (uploadFilePacket != this.currentUploadFilePacket) {
171 throw new java.lang.AssertionError("Wrong file packet in " + this.getClass().getName()
172 + ".anotherFileHasBeenSent()");
173 }
174 if (uploadFileData != this.currentUploadFileData) {
175 throw new java.lang.AssertionError("Wrong file packet in " + this.getClass().getName()
176 + ".anotherFileHasBeenSent()");
177 }
178 this.nbSentFiles += 1;
179 this.nbBytesUploadedForCurrentFile = 0;
180 this.uploadPolicy.displayDebug(this.getClass().getName()
181 + ".anotherFileHasBeenSent(): before call to newlyUploadedFileData.getUploadLength()", 100);
182
183
184 this.uploadStatus = FileUploadManagerThread.UPLOAD_STATUS_UPLOADED;
185 updateUploadProgressBarText(uploadFilePacket);
186 }
187
188
189
190
191 public void clearBarContent() {
192
193 this.timer.stop();
194
195 this.preparationProgressBar.setValue(0);
196 this.preparationProgressBar.setString("");
197 this.uploadProgressBar.setValue(0);
198 this.uploadProgressBar.setString("");
199
200 }
201
202
203
204
205 public long getGlobalStartTime() {
206 return this.globalStartTime;
207 }
208
209
210
211
212 public long getNbUploadedBytes() {
213 return this.nbUploadedBytes;
214 }
215
216
217
218
219 public long getUploadDuration() {
220 long currentRequestDuration = 0;
221 if (this.currentRequestStartTime != 0) {
222
223 currentRequestDuration = System.currentTimeMillis() - this.currentRequestStartTime;
224 }
225
226 return this.totalUploadDuration + currentRequestDuration;
227 }
228
229
230
231
232
233
234
235 private void initProgressBar() throws JUploadException {
236
237 this.preparationProgressBar.setMaximum(100 * this.filePreparationThread.getNbFilesToSend());
238 this.preparationProgressBar.setString("");
239
240
241 this.uploadProgressBar.setMaximum(100 * filePreparationThread.getNbFilesToSend());
242 this.uploadProgressBar.setString("");
243 }
244
245
246
247
248
249
250
251
252
253 public synchronized void nbBytesUploaded(long nbBytes, UploadFileData uploadFileData) throws JUploadException {
254 this.nbUploadedBytes += nbBytes;
255 this.nbBytesUploadedForCurrentFile += nbBytes;
256 }
257
258
259
260
261
262
263 public void setErrorMessage(String errorTexte) {
264 this.preparationProgressBar.setString(errorTexte);
265 }
266
267
268
269
270
271
272
273 public synchronized void setUploadStatus(UploadFilePacket uploadFilePacket, UploadFileData uploadFileData,
274 int uploadStatus) throws JUploadException {
275
276 this.currentUploadFileData = uploadFileData;
277 this.currentUploadFilePacket = uploadFilePacket;
278
279 switch (uploadStatus) {
280 case FileUploadManagerThread.UPLOAD_STATUS_CHUNK_UPLOADED_WAITING_FOR_RESPONSE:
281 case FileUploadManagerThread.UPLOAD_STATUS_FILE_UPLOADED_WAITING_FOR_RESPONSE:
282
283
284 this.totalUploadDuration += System.currentTimeMillis() - this.currentRequestStartTime;
285 this.currentRequestStartTime = 0;
286 break;
287 case FileUploadManagerThread.UPLOAD_STATUS_UPLOADING:
288
289 if (this.currentRequestStartTime == 0) {
290 this.currentRequestStartTime = System.currentTimeMillis();
291 }
292 break;
293 case FileUploadManagerThread.UPLOAD_STATUS_UPLOADED:
294
295 break;
296 default:
297 this.uploadPolicy.displayWarn("Unknown value for uploadStatus: " + uploadStatus);
298 }
299 this.uploadStatus = uploadStatus;
300
301 this.updateUploadProgressBarText(uploadFilePacket);
302 }
303
304
305
306
307
308
309
310
311
312
313
314 private void updateUploadProgressBarText(UploadFilePacket uploadFilePacket) {
315
316
317
318
319
320 updateUploadProgressBarValue();
321
322 String msg = null;
323 switch (this.uploadStatus) {
324 case FileUploadManagerThread.UPLOAD_STATUS_NOT_STARTED:
325 msg = "";
326 break;
327 case FileUploadManagerThread.UPLOAD_STATUS_UPLOADING:
328 case FileUploadManagerThread.UPLOAD_STATUS_CHUNK_UPLOADED_WAITING_FOR_RESPONSE:
329
330 msg = this.uploadPolicy.getLocalizedString("infoUploading", (this.nbSentFiles + 1));
331 break;
332 case FileUploadManagerThread.UPLOAD_STATUS_FILE_UPLOADED_WAITING_FOR_RESPONSE:
333
334
335
336
337 int firstFileInPacket = this.nbSentFiles - uploadFilePacket.size() + 1;
338 int currentFile = this.nbSentFiles;
339
340 if (this.currentUploadFilePacket.size() == 1) {
341 msg = currentFile + "/" + this.filePreparationThread.getNbFilesToSend();
342 } else {
343 msg = firstFileInPacket + "-" + currentFile + "/" + this.filePreparationThread.getNbFilesToSend();
344 }
345 msg = this.uploadPolicy.getLocalizedString("infoUploaded", msg);
346
347 break;
348 case FileUploadManagerThread.UPLOAD_STATUS_UPLOADED:
349
350 msg = this.uploadPolicy.getLocalizedString("nbUploadedFiles", (this.nbSentFiles));
351 break;
352 default:
353
354 this.uploadPolicy
355 .displayWarn("Unknown upload status in FileUploadManagerThreadImpl.updateProgressBar(): "
356 + this.uploadStatus);
357 }
358
359
360 this.uploadProgressBar.setString(msg);
361
362
363
364 this.uploadProgressBar.repaint(0);
365 }
366
367
368
369
370
371
372
373
374 private void updateUploadProgressBarValue() {
375
376
377
378
379
380 int percent = 0;
381
382
383 if (this.nbBytesUploadedForCurrentFile == 0
384 || this.nbSentFiles == this.filePreparationThread.getNbFilesToSend()) {
385 percent = 0;
386 } else if (this.currentUploadFileData == null) {
387 percent = 0;
388 } else {
389 if (this.currentUploadFileData.isPreparedForUpload()) {
390 percent = (int) (this.nbBytesUploadedForCurrentFile * 100 / this.currentUploadFileData
391 .getUploadLength());
392 } else {
393
394
395 percent = 0;
396 }
397
398
399 if (percent > 100) {
400 this.uploadPolicy.displayWarn("percent is more than 100 (" + percent
401 + ") in FileUploadManagerThreadImpl.update.UploadProgressBar");
402 percent = 100;
403 }
404 }
405
406 this.uploadProgressBar.setValue(100 * this.nbSentFiles + percent);
407 }
408
409
410
411
412 private void updateUploadStatusBar() {
413
414
415 if (null != this.uploadPolicy.getContext().getUploadPanel().getStatusLabel() && this.nbUploadedBytes > 0) {
416 double percent;
417
418 double uploadSpeed;
419
420
421 double globalCPS;
422 long remaining;
423 String eta;
424
425 try {
426 percent = 100.0 * this.nbUploadedBytes / this.filePreparationThread.getTotalFileBytesToSend();
427
428 } catch (ArithmeticException e1) {
429 percent = 100;
430 }
431
432
433 uploadSpeed = ((double) this.nbUploadedBytes) / ((double) getUploadDuration() / 1000);
434 if (uploadSpeed == Double.POSITIVE_INFINITY) {
435 this.uploadPolicy.displayDebug("uploadSpeed is Infinity, for nbUploadedBytes=" + nbUploadedBytes
436 + " and actualUploadDuration(ms)=" + getUploadDuration(), 80);
437 }
438
439
440 try {
441 globalCPS = ((double) this.nbUploadedBytes) / (System.currentTimeMillis() - this.globalStartTime)
442 * 1000;
443 } catch (ArithmeticException e1) {
444 globalCPS = this.nbUploadedBytes;
445 }
446
447
448 try {
449 remaining = (long) ((this.filePreparationThread.getTotalFileBytesToSend() - this.nbUploadedBytes) / globalCPS);
450 if (remaining > 3600) {
451 eta = this.uploadPolicy.getLocalizedString("timefmt_hms", Long.valueOf(remaining / 3600),
452 Long.valueOf((remaining / 60) % 60), Long.valueOf(remaining % 60));
453 } else if (remaining > 60) {
454 eta = this.uploadPolicy.getLocalizedString("timefmt_ms", Long.valueOf(remaining / 60),
455 Long.valueOf(remaining % 60));
456 } else
457 eta = this.uploadPolicy.getLocalizedString("timefmt_s", Long.valueOf(remaining));
458 } catch (ArithmeticException e1) {
459 eta = this.uploadPolicy.getLocalizedString("timefmt_unknown");
460 }
461 String status = this.uploadPolicy.getLocalizedString("status_msg", Integer.valueOf((int) percent),
462 SizeRenderer.formatFileUploadSpeed(uploadSpeed, this.uploadPolicy), eta);
463 this.uploadPolicy.getContext().getUploadPanel().getStatusLabel().setText(status);
464
465 this.uploadPolicy.getContext().showStatus(status);
466
467
468 }
469 }
470
471
472
473
474 public void uploadIsFinished() {
475
476 this.timer.stop();
477
478 updateUploadProgressBarText(null);
479 updateUploadStatusBar();
480 }
481
482
483
484
485 public void uploadIsStarted() throws JUploadException {
486
487
488 this.globalStartTime = System.currentTimeMillis();
489 initProgressBar();
490
491
492 this.timer.start();
493 }
494
495 }