source: 3thparty/jupload/src/main/java/wjhk/jupload2/gui/JUploadPanelImpl.java @ 3951

Revision 3951, 20.1 KB checked in by alexandrecorreia, 13 years ago (diff)

Ticket #1709 - Adicao de codigo fonte java do componente jupload

Line 
1//
2// $Id: JUploadPanelImpl.java 303 2007-07-21 07:42:51 +0000 (sam., 21 juil.
3// 2007)
4// etienne_sf $
5//
6// jupload - A file upload applet.
7// Copyright 2007 The JUpload Team
8//
9// Created: ?
10// Creator: William JinHua Kwong
11// Last modified: $Date: 2010-05-14 11:09:27 -0300 (Sex, 14 Mai 2010) $
12//
13// This program is free software; you can redistribute it and/or modify it under
14// the terms of the GNU General Public License as published by the Free Software
15// Foundation; either version 2 of the License, or (at your option) any later
16// version. This program is distributed in the hope that it will be useful, but
17// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19// details. You should have received a copy of the GNU General Public License
20// along with this program; if not, write to the Free Software Foundation, Inc.,
21// 675 Mass Ave, Cambridge, MA 02139, USA.
22
23package wjhk.jupload2.gui;
24
25import java.awt.Frame;
26import java.awt.Point;
27import java.awt.dnd.DnDConstants;
28import java.awt.dnd.DropTarget;
29import java.awt.dnd.DropTargetDropEvent;
30import java.awt.event.ActionEvent;
31import java.awt.event.ActionListener;
32import java.awt.event.InputEvent;
33import java.awt.event.MouseEvent;
34import java.awt.event.MouseListener;
35
36import javax.swing.Action;
37import javax.swing.ActionMap;
38import javax.swing.ImageIcon;
39import javax.swing.JButton;
40import javax.swing.JComponent;
41import javax.swing.JFileChooser;
42import javax.swing.JLabel;
43import javax.swing.JPanel;
44import javax.swing.JProgressBar;
45import javax.swing.JScrollPane;
46import javax.swing.ScrollPaneConstants;
47import javax.swing.SwingConstants;
48import javax.swing.TransferHandler;
49
50import wjhk.jupload2.JUploadApplet;
51import wjhk.jupload2.gui.filepanel.FilePanel;
52import wjhk.jupload2.gui.filepanel.FilePanelTableImp;
53import wjhk.jupload2.policies.UploadPolicy;
54import wjhk.jupload2.upload.FileUploadManagerThread;
55import wjhk.jupload2.upload.FileUploadManagerThreadImpl;
56
57/**
58 *
59 * Standard implementation for the {@link JUploadPanel} interface. It contains
60 * the actual creation of the GUI items.
61 *
62 * @author etienne_sf
63 * @version $Revision: 1165 $
64 */
65public class JUploadPanelImpl extends JPanel implements ActionListener,
66                JUploadPanel, MouseListener {
67
68        /** A generated serialVersionUID, to avoid warning during compilation */
69        private static final long serialVersionUID = -1212601012568225757L;
70
71        /** The debug popup menu of the applet */
72        private JUploadDebugPopupMenu jUploadDebugPopupMenu;
73
74        /** The main popup menu of the applet */
75        private JUploadMainPopupMenu jUploadMainPopupMenu;
76
77        // ------------- VARIABLES ----------------------------------------------
78
79        /**
80         * The Drag and Drop listener, that will manage the drop event. All pplet
81         * element should register this instance, so that the user see the whole
82         * applet as a unique drop target.
83         */
84        private DnDListener dndListener = null;
85
86        private JButton browseButton = null, removeButton = null,
87                        removeAllButton = null, uploadButton = null, stopButton = null;
88
89        private JUploadFileChooser fileChooser = null;
90
91        private FilePanel filePanel = null;
92
93        private JProgressBar preparationProgressBar = null;
94
95        private JProgressBar uploadProgressBar = null;
96
97        private JLabel statusLabel = null;
98
99        /**
100         * The log window. It's created by {@link JUploadApplet}.
101         */
102        private JUploadTextArea logWindow = null;
103
104        /**
105         * The log window pane contains the log window, and the relevant scroll
106         * bars. It's actually this pane that is displayed, as a view on the log
107         * window.
108         */
109        private JScrollPane jLogWindowPane = null;
110
111        private UploadPolicy uploadPolicy = null;
112
113        private FileUploadManagerThread fileUploadManagerThread = null;
114
115        // ------------- CONSTRUCTOR --------------------------------------------
116
117        /**
118         * Standard constructor.
119         *
120         * @param logWindow
121         *            The log window that should already have been created. This
122         *            allows putting text into it, before the effective creation of
123         *            the layout.
124         * @param uploadPolicyParam
125         *            The current UploadPolicy. Null if a new one must be created.
126         * @throws Exception
127         */
128        public JUploadPanelImpl(JUploadTextArea logWindow,
129                        UploadPolicy uploadPolicyParam) throws Exception {
130                this.logWindow = logWindow;
131                this.uploadPolicy = uploadPolicyParam;
132                this.jUploadDebugPopupMenu = new JUploadDebugPopupMenu(
133                                this.uploadPolicy);
134                this.jUploadMainPopupMenu = new JUploadMainPopupMenu(this.uploadPolicy,
135                                this);
136
137                // First: create standard components.
138                createStandardComponents();
139                logWindow.addMouseListener(this);
140
141                // Define the drop target.
142                this.dndListener = new DnDListener(this, this.uploadPolicy);
143                new DropTarget(this, this.dndListener);
144                new DropTarget(this.filePanel.getDropComponent(), this.dndListener);
145                new DropTarget(this.logWindow, this.dndListener);
146
147                // Define the TransfertHandler, to manage paste operations.
148                JUploadTransferHandler jUploadTransfertHandler = new JUploadTransferHandler(
149                                this.uploadPolicy, this);
150                this.setTransferHandler(jUploadTransfertHandler);
151                this.filePanel.setTransferHandler(jUploadTransfertHandler);
152                ActionMap map = this.getActionMap();
153                map.put(TransferHandler.getPasteAction().getValue(Action.NAME),
154                                TransferHandler.getPasteAction());
155
156                // The JUploadPanelImpl will listen to Mouse messages for the standard
157                // component. The current only application of this, it the CTRL+Righ
158                // Click, that triggers the popup menu, which allow to switch debug on.
159                this.browseButton.addMouseListener(this);
160                this.removeAllButton.addMouseListener(this);
161                this.removeButton.addMouseListener(this);
162                this.stopButton.addMouseListener(this);
163                this.uploadButton.addMouseListener(this);
164
165                this.jLogWindowPane.addMouseListener(this);
166                logWindow.addMouseListener(this);
167                this.preparationProgressBar.addMouseListener(this);
168                this.uploadProgressBar.addMouseListener(this);
169                this.statusLabel.addMouseListener(this);
170
171                // Then: display them on the applet
172                this.uploadPolicy.addComponentsToJUploadPanel(this);
173        }
174
175        // ----------------------------------------------------------------------
176
177        /**
178         * Creates all components used by the default upload policy. <BR>
179         * You can change the component position of these components on the applet,
180         * by creating a new upload policy, and override the
181         * {@link UploadPolicy#addComponentsToJUploadPanel(JUploadPanel)} method.<BR>
182         * You should keep these components, as there content is managed by the
183         * internal code of the applet. <BR>
184         * <U>Note:</U> this method will create component only if they were not
185         * already created. That is only if the relevant attribute contain a null
186         * value. If it's not the case, the already created component are keeped
187         * unchanged.
188         */
189        private void createStandardComponents() {
190                // -------- JButton browse --------
191                if (this.browseButton == null) {
192                        this.browseButton = new JButton(this.uploadPolicy
193                                        .getLocalizedString("buttonBrowse"));
194                        this.browseButton.setIcon(new ImageIcon(getClass().getResource(
195                                        "/images/explorer.gif")));
196                }
197                this.browseButton.addActionListener(this);
198
199                // -------- JButton remove --------
200                if (this.removeButton == null) {
201                        this.removeButton = new JButton(this.uploadPolicy
202                                        .getLocalizedString("buttonRemoveSelected"));
203                        this.removeButton.setIcon(new ImageIcon(getClass().getResource(
204                                        "/images/recycle.gif")));
205                }
206                this.removeButton.setEnabled(false);
207                this.removeButton.addActionListener(this);
208
209                // -------- JButton removeAll --------
210                if (this.removeAllButton == null) {
211                        this.removeAllButton = new JButton(this.uploadPolicy
212                                        .getLocalizedString("buttonRemoveAll"));
213                        this.removeAllButton.setIcon(new ImageIcon(getClass().getResource(
214                                        "/images/cross.gif")));
215                }
216                this.removeAllButton.setEnabled(false);
217                this.removeAllButton.addActionListener(this);
218
219                // -------- JButton upload --------
220                if (null == this.uploadButton) {
221                        this.uploadButton = new JButton(this.uploadPolicy
222                                        .getLocalizedString("buttonUpload"));
223                        this.uploadButton.setIcon(new ImageIcon(getClass().getResource(
224                                        "/images/up.gif")));
225                }
226                this.uploadButton.setEnabled(false);
227                this.uploadButton.addActionListener(this);
228
229                // -------- The main thing: the file panel --------
230                this.filePanel = new FilePanelTableImp(this, this.uploadPolicy);
231
232                // -------- JProgressBar progress --------
233                if (null == this.preparationProgressBar) {
234                        this.preparationProgressBar = new JProgressBar(
235                                        SwingConstants.HORIZONTAL);
236                        this.preparationProgressBar.setStringPainted(true);
237                }
238                if (null == this.uploadProgressBar) {
239                        this.uploadProgressBar = new JProgressBar(SwingConstants.HORIZONTAL);
240                        this.uploadProgressBar.setStringPainted(true);
241                }
242
243                // -------- JButton stop --------
244                if (null == this.stopButton) {
245                        this.stopButton = new JButton(this.uploadPolicy
246                                        .getLocalizedString("buttonStop"));
247                        this.stopButton.setIcon(new ImageIcon(getClass().getResource(
248                                        "/images/cross.gif")));
249                }
250                this.stopButton.setEnabled(false);
251                this.stopButton.addActionListener(this);
252
253                // -------- JButton stop --------
254                if (this.jLogWindowPane == null) {
255                        this.jLogWindowPane = new JScrollPane();
256                        this.jLogWindowPane
257                                        .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
258                        this.jLogWindowPane
259                                        .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
260                }
261                this.jLogWindowPane.getViewport().add(this.logWindow);
262                this.jLogWindowPane.setPreferredSize(null);
263
264                // -------- statusLabel --------
265                this.statusLabel = new JLabel("JUpload applet "
266                                + this.uploadPolicy.getContext().getDetailedVersionMessage());
267        }
268
269        /** {@inheritDoc} */
270        public void showOrHideLogWindow() {
271                if ((this.uploadPolicy.getShowLogWindow()
272                                .equals(UploadPolicy.SHOWLOGWINDOW_TRUE))
273                                || (this.uploadPolicy.getShowLogWindow().equals(
274                                                UploadPolicy.SHOWLOGWINDOW_ONERROR) && this.uploadPolicy
275                                                .getLastException() != null)) {
276                        // The log window should be visible.
277                        this.jLogWindowPane.setVisible(true);
278                } else {
279                        // It should be hidden.
280                        this.jLogWindowPane.setVisible(false);
281                }
282                // Let's recalculate the component display
283                validate();
284        }
285
286        // ///////////////////////////////////////////////////////////////////////////////
287        // ///////////////// Action methods
288        // ///////////////////////////////////////////////////////////////////////////////
289
290        /** {@inheritDoc} */
291        public void doBrowse() {
292                // If the file chooser was not created, we create it now.
293                if (null == this.fileChooser) {
294                        // Setup File Chooser.
295                        try {
296                                this.uploadPolicy.displayDebug(
297                                                "Before this.uploadPolicy.createFileChooser()", 80);
298                                this.fileChooser = this.uploadPolicy.createFileChooser();
299                                this.uploadPolicy.displayDebug(
300                                                "After this.uploadPolicy.createFileChooser()", 80);
301                        } catch (Exception e) {
302                                this.uploadPolicy.displayErr(e);
303                        }
304
305                }
306
307                // If the fileChooser succeed (should be the case), we go on.
308                if (null != this.fileChooser) {
309                        try {
310                                int ret = this.fileChooser.showOpenDialog(new Frame());
311                                if (JFileChooser.APPROVE_OPTION == ret)
312                                        this.filePanel.addFiles(
313                                                        this.fileChooser.getSelectedFiles(),
314                                                        this.fileChooser.getCurrentDirectory());
315                                // We stop any running task for the JUploadFileView
316                                this.uploadPolicy.setCurrentBrowsingDirectory(this.fileChooser
317                                                .getCurrentDirectory().getAbsolutePath());
318                                this.fileChooser.shutdownNow();
319                        } catch (Exception ex) {
320                                this.uploadPolicy.displayErr(ex);
321                        }
322                }
323        }
324
325        /** {@inheritDoc} */
326        public void doRemove() {
327                this.filePanel.removeSelected();
328                if (0 >= this.filePanel.getFilesLength()) {
329                        this.removeButton.setEnabled(false);
330                        this.removeAllButton.setEnabled(false);
331                        this.uploadButton.setEnabled(false);
332                }
333        }
334
335        /** {@inheritDoc} */
336        public void doRemoveAll() {
337                this.filePanel.removeAll();
338                this.removeButton.setEnabled(false);
339                this.removeAllButton.setEnabled(false);
340                this.uploadButton.setEnabled(false);
341        }
342
343        /** {@inheritDoc} */
344        public void doStartUpload() {
345                // Check that the upload is ready (we ask the uploadPolicy. Then,
346                // we'll call beforeUpload for each
347                // FileData instance, that exists in allFiles[].
348
349                // ///////////////////////////////////////////////////////////////////////////////////////////////
350                // IMPORTANT: It's up to the UploadPolicy to explain to the user
351                // that the upload is not ready!
352                // ///////////////////////////////////////////////////////////////////////////////////////////////
353                try {
354                        if (this.uploadPolicy.beforeUpload()) {
355                                // The FileUploadManagerThread will manage everything around
356                                // upload, including GUI part.
357                                this.fileUploadManagerThread = new FileUploadManagerThreadImpl(
358                                                this.uploadPolicy);
359                                this.fileUploadManagerThread.start();
360                        } // if isIploadReady()
361                } catch (Exception e) {
362                        // If an exception occurs here, it fails silently. The exception is
363                        // thrown to the AWT event dispatcher.
364                        this.uploadPolicy.displayErr(e.getClass().getName()
365                                        + " in JUploadPanelImpl.doStartUpload()", e);
366                }
367        }
368
369        /** {@inheritDoc} */
370        public void doStopUpload() {
371                this.fileUploadManagerThread.stopUpload();
372        }
373
374        // ///////////////////////////////////////////////////////////////////////////////
375        // ///////////////// Implementation of the ActionListener
376        // ///////////////////////////////////////////////////////////////////////////////
377
378        /**
379         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
380         */
381        public void actionPerformed(ActionEvent e) {
382                // Let's log some info.
383                this.uploadPolicy.displayDebug("Action : " + e.getActionCommand(), 1);
384
385                final String actionPaste = (String) TransferHandler.getPasteAction()
386                                .getValue(Action.NAME);
387
388                if (e.getActionCommand().equals(actionPaste)) {
389                        Action a = getActionMap().get(actionPaste);
390                        if (a != null) {
391                                a.actionPerformed(new ActionEvent(this.filePanel,
392                    ActionEvent.ACTION_PERFORMED, e.getActionCommand()));
393                this.uploadPolicy.afterFileDropped(new DropTargetDropEvent(new DropTarget(
394                    this.filePanel.getDropComponent(), this.dndListener).getDropTargetContext(),
395                    new Point(), DnDConstants.ACTION_MOVE, DnDConstants.ACTION_COPY_OR_MOVE ));
396                        }
397                } else if (e.getActionCommand() == this.browseButton.getActionCommand()) {
398                        doBrowse();
399                } else if (e.getActionCommand() == this.removeButton.getActionCommand()) {
400                        // Remove clicked
401                        doRemove();
402                } else if (e.getActionCommand() == this.removeAllButton
403                                .getActionCommand()) {
404                        // Remove All clicked
405                        doRemoveAll();
406                } else if (e.getActionCommand() == this.uploadButton.getActionCommand()) {
407                        // Upload clicked
408                        doStartUpload();
409                } else if (e.getActionCommand() == this.stopButton.getActionCommand()) {
410                        // We request the thread to stop its job.
411                        doStopUpload();
412                }
413                // focus the table. This is necessary in order to enable mouse
414                // events
415                // for triggering tooltips.
416                this.filePanel.focusTable();
417        }
418
419        // ///////////////////////////////////////////////////////////////////////////////
420        // ///////////////// Implementation of the MouseListener
421        // ///////////////////////////////////////////////////////////////////////////////
422
423        /**
424         * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
425         */
426        public void mouseClicked(MouseEvent mouseEvent) {
427                maybeOpenPopupMenu(mouseEvent);
428        }
429
430        /**
431         * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
432         */
433        public void mouseEntered(MouseEvent mouseEvent) {
434                maybeOpenPopupMenu(mouseEvent);
435        }
436
437        /**
438         * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
439         */
440        public void mouseExited(MouseEvent mouseEvent) {
441                maybeOpenPopupMenu(mouseEvent);
442        }
443
444        /**
445         * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
446         */
447        public void mousePressed(MouseEvent mouseEvent) {
448                maybeOpenPopupMenu(mouseEvent);
449        }
450
451        /**
452         * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
453         */
454        public void mouseReleased(MouseEvent mouseEvent) {
455                if (mouseEvent.getClickCount() == 2) {
456                        // We have a double-click. Let's tell it to the current upload
457                        // policy...
458                        this.uploadPolicy.onFileDoubleClicked(this.filePanel
459                                        .getFileDataAt(mouseEvent.getPoint()));
460                } else {
461                        maybeOpenPopupMenu(mouseEvent);
462                }
463        }
464
465        /** {@inheritDoc} */
466        public boolean maybeOpenPopupMenu(MouseEvent mouseEvent) {
467                // Should we open one out of the numerous (2!) popup menus ?
468                if (mouseEvent.isPopupTrigger()) {
469                        if ((mouseEvent.getModifiersEx() & InputEvent.CTRL_DOWN_MASK) == InputEvent.CTRL_DOWN_MASK) {
470                                // We open the debug menu
471                                if (this.jUploadDebugPopupMenu != null) {
472                                        this.jUploadDebugPopupMenu.show(mouseEvent.getComponent(),
473                                                        mouseEvent.getX(), mouseEvent.getY());
474                                        return true;
475                                }
476                        } else {
477                                // Let's open the main popup menu
478                                if (this.jUploadMainPopupMenu != null) {
479                                        this.jUploadMainPopupMenu.show(mouseEvent.getComponent(),
480                                                        mouseEvent.getX(), mouseEvent.getY());
481                                        return true;
482                                }
483                        }
484                }
485                return false;
486        }
487
488        /** {@inheritDoc} */
489        public void updateButtonState() {
490                if (this.fileUploadManagerThread != null
491                                && this.fileUploadManagerThread.isAlive()
492                                && !this.fileUploadManagerThread.isUploadFinished()) {
493                        // An upload is running on.
494                        this.browseButton.setEnabled(false);
495                        this.removeButton.setEnabled(false);
496                        this.removeAllButton.setEnabled(false);
497                        this.uploadButton.setEnabled(false);
498                        this.stopButton.setEnabled(true);
499                        // Let's delegate any additional work to the upload policy.
500                        this.uploadPolicy
501                                        .updateButtonState(UploadPolicy.EXEC_STATUS_UPLOADING);
502                } else {
503                        // No upload running on.
504                        this.browseButton.setEnabled(true);
505                        this.stopButton.setEnabled(false);
506
507                        boolean enabled = (this.filePanel.getFilesLength() > 0);
508                        this.removeButton.setEnabled(enabled);
509                        this.removeAllButton.setEnabled(enabled);
510                        this.uploadButton.setEnabled(enabled);
511                        // Let's delegate any additional work to the upload policy.
512                        this.uploadPolicy.updateButtonState(UploadPolicy.EXEC_STATUS_READY);
513                }
514
515        }
516
517        /** {@inheritDoc} */
518        public void clearLogWindow() {
519                this.logWindow.setText("");
520        }
521
522        /** {@inheritDoc} */
523        public void copyLogWindow() {
524                this.logWindow.selectAll();
525                this.logWindow.copy();
526        }
527
528        /** {@inheritDoc} */
529        public ActionListener getActionListener() {
530                return this;
531        }
532
533        /** {@inheritDoc} */
534        public JButton getBrowseButton() {
535                return this.browseButton;
536        }
537
538        /** {@inheritDoc} */
539        public JComponent getJComponent() {
540                return this;
541        }
542
543        /** {@inheritDoc} */
544        public DnDListener getDndListener() {
545                return this.dndListener;
546        }
547
548        /** {@inheritDoc} */
549        public FilePanel getFilePanel() {
550                return this.filePanel;
551        }
552
553        /** {@inheritDoc} */
554        public JScrollPane getJLogWindowPane() {
555                return this.jLogWindowPane;
556        }
557
558        /**
559         * Get the log window, that is: the component where messages (debug, info,
560         * error...) are written. You should not use this component directly, but:
561         * <UL>
562         * <LI>To display messages: use the UploadPolicy.displayXxx methods.
563         * <LI>To place this component on the applet, when overriding the
564         * {@link UploadPolicy#addComponentsToJUploadPanel(JUploadPanel)} method:
565         * use the {@link #getJLogWindowPane()} method instead. The
566         * {@link #logWindow} is embbeded in it.
567         * </UL>
568         *
569         * @return the logWindow
570         */
571        protected JUploadTextArea getLogWindow() {
572                return this.logWindow;
573        }
574
575        /** {@inheritDoc} */
576        public MouseListener getMouseListener() {
577                return this;
578        }
579
580        /** {@inheritDoc} */
581        public JProgressBar getPreparationProgressBar() {
582                return this.preparationProgressBar;
583        }
584
585        /** {@inheritDoc} */
586        public JProgressBar getUploadProgressBar() {
587                return this.uploadProgressBar;
588        }
589
590        /** {@inheritDoc} */
591        public JButton getRemoveAllButton() {
592                return this.removeAllButton;
593        }
594
595        /** {@inheritDoc} */
596        public JButton getRemoveButton() {
597                return this.removeButton;
598        }
599
600        /** {@inheritDoc} */
601        public JLabel getStatusLabel() {
602                return this.statusLabel;
603        }
604
605        /** {@inheritDoc} */
606        public JButton getStopButton() {
607                return this.stopButton;
608        }
609
610        /** {@inheritDoc} */
611        public JButton getUploadButton() {
612                return this.uploadButton;
613        }
614
615        /** {@inheritDoc} */
616        public void setFilePanel(FilePanel filePanel) {
617                this.filePanel = filePanel;
618        }
619
620        /** {@inheritDoc} */
621        public FileUploadManagerThread getFileUploadManagerThread() {
622                return fileUploadManagerThread;
623        }
624
625}
Note: See TracBrowser for help on using the repository browser.