1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package wjhk.jupload2.gui;
24
25 import java.awt.Frame;
26 import java.awt.Point;
27 import java.awt.dnd.DnDConstants;
28 import java.awt.dnd.DropTarget;
29 import java.awt.dnd.DropTargetDropEvent;
30 import java.awt.event.ActionEvent;
31 import java.awt.event.ActionListener;
32 import java.awt.event.InputEvent;
33 import java.awt.event.MouseEvent;
34 import java.awt.event.MouseListener;
35
36 import javax.swing.Action;
37 import javax.swing.ActionMap;
38 import javax.swing.ImageIcon;
39 import javax.swing.JButton;
40 import javax.swing.JComponent;
41 import javax.swing.JFileChooser;
42 import javax.swing.JLabel;
43 import javax.swing.JPanel;
44 import javax.swing.JProgressBar;
45 import javax.swing.JScrollPane;
46 import javax.swing.ScrollPaneConstants;
47 import javax.swing.SwingConstants;
48 import javax.swing.TransferHandler;
49
50 import wjhk.jupload2.JUploadApplet;
51 import wjhk.jupload2.gui.filepanel.FilePanel;
52 import wjhk.jupload2.gui.filepanel.FilePanelTableImp;
53 import wjhk.jupload2.policies.UploadPolicy;
54 import wjhk.jupload2.upload.FileUploadManagerThread;
55 import wjhk.jupload2.upload.FileUploadManagerThreadImpl;
56
57
58
59
60
61
62
63 public class JUploadPanelImpl extends JPanel implements ActionListener, JUploadPanel, MouseListener {
64
65
66 private static final long serialVersionUID = -1212601012568225757L;
67
68
69 private JUploadDebugPopupMenu jUploadDebugPopupMenu;
70
71
72 private JUploadMainPopupMenu jUploadMainPopupMenu;
73
74
75
76
77
78
79
80 private DnDListener dndListener = null;
81
82 private JButton browseButton = null, removeButton = null, removeAllButton = null, uploadButton = null,
83 stopButton = null;
84
85 private JUploadFileChooser fileChooser = null;
86
87 private FilePanel filePanel = null;
88
89 private JProgressBar preparationProgressBar = null;
90
91 private JProgressBar uploadProgressBar = null;
92
93 private JLabel statusLabel = null;
94
95
96
97
98 private JUploadTextArea logWindow = null;
99
100
101
102
103
104 private JScrollPane jLogWindowPane = null;
105
106 private UploadPolicy uploadPolicy = null;
107
108 private FileUploadManagerThread fileUploadManagerThread = null;
109
110
111
112
113
114
115
116
117
118
119
120 public JUploadPanelImpl(JUploadTextArea logWindow, UploadPolicy uploadPolicyParam) throws Exception {
121 this.logWindow = logWindow;
122 this.uploadPolicy = uploadPolicyParam;
123 this.jUploadDebugPopupMenu = new JUploadDebugPopupMenu(this.uploadPolicy);
124 this.jUploadMainPopupMenu = new JUploadMainPopupMenu(this.uploadPolicy, this);
125
126
127 createStandardComponents();
128 logWindow.addMouseListener(this);
129
130
131 this.dndListener = new DnDListener(this, this.uploadPolicy);
132 new DropTarget(this, this.dndListener);
133 new DropTarget(this.filePanel.getDropComponent(), this.dndListener);
134 new DropTarget(this.logWindow, this.dndListener);
135
136
137 JUploadTransferHandler jUploadTransfertHandler = new JUploadTransferHandler(this.uploadPolicy, this);
138 this.setTransferHandler(jUploadTransfertHandler);
139 this.filePanel.setTransferHandler(jUploadTransfertHandler);
140 ActionMap map = this.getActionMap();
141 map.put(TransferHandler.getPasteAction().getValue(Action.NAME), TransferHandler.getPasteAction());
142
143
144
145
146 this.browseButton.addMouseListener(this);
147 this.removeAllButton.addMouseListener(this);
148 this.removeButton.addMouseListener(this);
149 this.stopButton.addMouseListener(this);
150 this.uploadButton.addMouseListener(this);
151
152 this.jLogWindowPane.addMouseListener(this);
153 logWindow.addMouseListener(this);
154 this.preparationProgressBar.addMouseListener(this);
155 this.uploadProgressBar.addMouseListener(this);
156 this.statusLabel.addMouseListener(this);
157
158
159 this.uploadPolicy.addComponentsToJUploadPanel(this);
160 }
161
162
163
164
165
166
167
168
169
170
171
172
173 private void createStandardComponents() {
174
175 if (this.browseButton == null) {
176 this.browseButton = new JButton(this.uploadPolicy.getLocalizedString("buttonBrowse"));
177 this.browseButton.setIcon(new ImageIcon(getClass().getResource("/images/explorer.gif")));
178 }
179 this.browseButton.addActionListener(this);
180
181
182 if (this.removeButton == null) {
183 this.removeButton = new JButton(this.uploadPolicy.getLocalizedString("buttonRemoveSelected"));
184 this.removeButton.setIcon(new ImageIcon(getClass().getResource("/images/recycle.gif")));
185 }
186 this.removeButton.setEnabled(false);
187 this.removeButton.addActionListener(this);
188
189
190 if (this.removeAllButton == null) {
191 this.removeAllButton = new JButton(this.uploadPolicy.getLocalizedString("buttonRemoveAll"));
192 this.removeAllButton.setIcon(new ImageIcon(getClass().getResource("/images/cross.gif")));
193 }
194 this.removeAllButton.setEnabled(false);
195 this.removeAllButton.addActionListener(this);
196
197
198 if (null == this.uploadButton) {
199 this.uploadButton = new JButton(this.uploadPolicy.getLocalizedString("buttonUpload"));
200 this.uploadButton.setIcon(new ImageIcon(getClass().getResource("/images/up.gif")));
201 }
202 this.uploadButton.setEnabled(false);
203 this.uploadButton.addActionListener(this);
204
205
206 this.filePanel = new FilePanelTableImp(this, this.uploadPolicy);
207
208
209 if (null == this.preparationProgressBar) {
210 this.preparationProgressBar = new JProgressBar(SwingConstants.HORIZONTAL);
211 this.preparationProgressBar.setStringPainted(true);
212 }
213 if (null == this.uploadProgressBar) {
214 this.uploadProgressBar = new JProgressBar(SwingConstants.HORIZONTAL);
215 this.uploadProgressBar.setStringPainted(true);
216 }
217
218
219 if (null == this.stopButton) {
220 this.stopButton = new JButton(this.uploadPolicy.getLocalizedString("buttonStop"));
221 this.stopButton.setIcon(new ImageIcon(getClass().getResource("/images/cross.gif")));
222 }
223 this.stopButton.setEnabled(false);
224 this.stopButton.addActionListener(this);
225
226
227 if (this.jLogWindowPane == null) {
228 this.jLogWindowPane = new JScrollPane();
229 this.jLogWindowPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
230 this.jLogWindowPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
231 }
232 this.jLogWindowPane.getViewport().add(this.logWindow);
233 this.jLogWindowPane.setPreferredSize(null);
234
235
236 this.statusLabel = new JLabel("JUpload applet " + this.uploadPolicy.getContext().getDetailedVersionMessage());
237 }
238
239
240 public void showOrHideLogWindow() {
241 if ((this.uploadPolicy.getShowLogWindow().equals(UploadPolicy.SHOWLOGWINDOW_TRUE))
242 || (this.uploadPolicy.getShowLogWindow().equals(UploadPolicy.SHOWLOGWINDOW_ONERROR) && this.uploadPolicy
243 .getLastException() != null)) {
244
245 this.jLogWindowPane.setVisible(true);
246 } else {
247
248 this.jLogWindowPane.setVisible(false);
249 }
250
251 validate();
252 }
253
254
255
256
257
258
259 public void doBrowse() {
260
261 if (null == this.fileChooser) {
262
263 try {
264 this.uploadPolicy.displayDebug("Before this.uploadPolicy.createFileChooser()", 80);
265 this.fileChooser = this.uploadPolicy.createFileChooser();
266 this.uploadPolicy.displayDebug("After this.uploadPolicy.createFileChooser()", 80);
267 } catch (Exception e) {
268 this.uploadPolicy.displayErr(e);
269 }
270
271 }
272
273
274 if (null != this.fileChooser) {
275 try {
276 int ret = this.fileChooser.showOpenDialog(new Frame());
277 if (JFileChooser.APPROVE_OPTION == ret)
278 this.filePanel.addFiles(this.fileChooser.getSelectedFiles());
279
280 this.uploadPolicy.setCurrentBrowsingDirectory(this.fileChooser.getCurrentDirectory().getAbsolutePath());
281 this.fileChooser.shutdownNow();
282 } catch (Exception ex) {
283 this.uploadPolicy.displayErr(ex);
284 }
285 }
286 }
287
288
289 public void doRemove() {
290 this.filePanel.removeSelected();
291 if (0 >= this.filePanel.getFilesLength()) {
292 this.removeButton.setEnabled(false);
293 this.removeAllButton.setEnabled(false);
294 this.uploadButton.setEnabled(false);
295 }
296 }
297
298
299 public void doRemoveAll() {
300 this.filePanel.removeAll();
301 this.removeButton.setEnabled(false);
302 this.removeAllButton.setEnabled(false);
303 this.uploadButton.setEnabled(false);
304 }
305
306
307 public void doStartUpload() {
308
309
310
311
312
313
314
315
316 try {
317 if (this.uploadPolicy.beforeUpload()) {
318
319
320 if (getFilePanel().getFilesLength() > 0) {
321
322
323 this.fileUploadManagerThread = new FileUploadManagerThreadImpl(this.uploadPolicy);
324 this.fileUploadManagerThread.start();
325 }
326 }
327 } catch (Exception e) {
328
329
330 this.uploadPolicy.displayErr(e.getClass().getName() + " in JUploadPanelImpl.doStartUpload()", e);
331 }
332 }
333
334
335 public void doStopUpload() {
336 this.fileUploadManagerThread.stopUpload();
337 }
338
339
340
341
342
343
344
345
346 public void actionPerformed(ActionEvent e) {
347
348 this.uploadPolicy.displayDebug("Action : " + e.getActionCommand(), 1);
349
350 final String actionPaste = (String) TransferHandler.getPasteAction().getValue(Action.NAME);
351
352 if (e.getActionCommand().equals(actionPaste)) {
353 Action a = getActionMap().get(actionPaste);
354 if (a != null) {
355 a.actionPerformed(new ActionEvent(this.filePanel, ActionEvent.ACTION_PERFORMED, e.getActionCommand()));
356 this.uploadPolicy.afterFileDropped(new DropTargetDropEvent(new DropTarget(this.filePanel
357 .getDropComponent(), this.dndListener).getDropTargetContext(), new Point(),
358 DnDConstants.ACTION_MOVE, DnDConstants.ACTION_COPY_OR_MOVE));
359 }
360 } else if (e.getActionCommand() == this.browseButton.getActionCommand()) {
361 doBrowse();
362 } else if (e.getActionCommand() == this.removeButton.getActionCommand()) {
363
364 doRemove();
365 } else if (e.getActionCommand() == this.removeAllButton.getActionCommand()) {
366
367 doRemoveAll();
368 } else if (e.getActionCommand() == this.uploadButton.getActionCommand()) {
369
370 doStartUpload();
371 } else if (e.getActionCommand() == this.stopButton.getActionCommand()) {
372
373 doStopUpload();
374 }
375
376
377
378 this.filePanel.focusTable();
379 }
380
381
382
383
384
385
386
387
388 public void mouseClicked(MouseEvent mouseEvent) {
389 maybeOpenPopupMenu(mouseEvent);
390 }
391
392
393
394
395 public void mouseEntered(MouseEvent mouseEvent) {
396 maybeOpenPopupMenu(mouseEvent);
397 }
398
399
400
401
402 public void mouseExited(MouseEvent mouseEvent) {
403 maybeOpenPopupMenu(mouseEvent);
404 }
405
406
407
408
409 public void mousePressed(MouseEvent mouseEvent) {
410 maybeOpenPopupMenu(mouseEvent);
411 }
412
413
414
415
416 public void mouseReleased(MouseEvent mouseEvent) {
417 if (mouseEvent.getClickCount() == 2) {
418
419
420 this.uploadPolicy.onFileDoubleClicked(this.filePanel.getFileDataAt(mouseEvent.getPoint()));
421 } else {
422 maybeOpenPopupMenu(mouseEvent);
423 }
424 }
425
426
427 public boolean maybeOpenPopupMenu(MouseEvent mouseEvent) {
428
429 if (mouseEvent.isPopupTrigger()) {
430 if ((mouseEvent.getModifiersEx() & InputEvent.CTRL_DOWN_MASK) == InputEvent.CTRL_DOWN_MASK) {
431
432 if (this.jUploadDebugPopupMenu != null) {
433 this.jUploadDebugPopupMenu.show(mouseEvent.getComponent(), mouseEvent.getX(), mouseEvent.getY());
434 return true;
435 }
436 } else {
437
438 if (this.jUploadMainPopupMenu != null) {
439 this.jUploadMainPopupMenu.show(mouseEvent.getComponent(), mouseEvent.getX(), mouseEvent.getY());
440 return true;
441 }
442 }
443 }
444 return false;
445 }
446
447
448 public void updateButtonState() {
449 if (this.fileUploadManagerThread != null && this.fileUploadManagerThread.isAlive()
450 && !this.fileUploadManagerThread.isUploadFinished()) {
451
452 this.browseButton.setEnabled(false);
453 this.removeButton.setEnabled(false);
454 this.removeAllButton.setEnabled(false);
455 this.uploadButton.setEnabled(false);
456 this.stopButton.setEnabled(true);
457
458 this.uploadPolicy.updateButtonState(UploadPolicy.EXEC_STATUS_UPLOADING);
459 } else {
460
461 this.browseButton.setEnabled(true);
462 this.stopButton.setEnabled(false);
463
464 boolean enabled = (this.filePanel.getFilesLength() > 0);
465 this.removeButton.setEnabled(enabled);
466 this.removeAllButton.setEnabled(enabled);
467 this.uploadButton.setEnabled(enabled);
468
469 this.uploadPolicy.updateButtonState(UploadPolicy.EXEC_STATUS_READY);
470 }
471
472 }
473
474
475 public void clearLogWindow() {
476 this.logWindow.setText("");
477 }
478
479
480 public void copyLogWindow() {
481 this.logWindow.copyLogWindow();
482 }
483
484
485 public ActionListener getActionListener() {
486 return this;
487 }
488
489
490 public JButton getBrowseButton() {
491 return this.browseButton;
492 }
493
494
495 public JComponent getJComponent() {
496 return this;
497 }
498
499
500 public DnDListener getDndListener() {
501 return this.dndListener;
502 }
503
504
505 public FilePanel getFilePanel() {
506 return this.filePanel;
507 }
508
509
510 public JScrollPane getJLogWindowPane() {
511 return this.jLogWindowPane;
512 }
513
514
515
516
517
518
519
520
521
522
523
524
525
526 protected JUploadTextArea getLogWindow() {
527 return this.logWindow;
528 }
529
530
531 public MouseListener getMouseListener() {
532 return this;
533 }
534
535
536 public JProgressBar getPreparationProgressBar() {
537 return this.preparationProgressBar;
538 }
539
540
541 public JProgressBar getUploadProgressBar() {
542 return this.uploadProgressBar;
543 }
544
545
546 public JButton getRemoveAllButton() {
547 return this.removeAllButton;
548 }
549
550
551 public JButton getRemoveButton() {
552 return this.removeButton;
553 }
554
555
556 public JLabel getStatusLabel() {
557 return this.statusLabel;
558 }
559
560
561 public JButton getStopButton() {
562 return this.stopButton;
563 }
564
565
566 public JButton getUploadButton() {
567 return this.uploadButton;
568 }
569
570
571 public void setFilePanel(FilePanel filePanel) {
572 this.filePanel = filePanel;
573 }
574
575
576 public FileUploadManagerThread getFileUploadManagerThread() {
577 return fileUploadManagerThread;
578 }
579
580 }