⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 juploadpanel.java

📁 [linux.rar] - 嵌入式linux开发教程
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//
// $Id: JUploadPanel.java 303 2007-07-21 07:42:51 +0000 (sam., 21 juil. 2007)
// etienne_sf $
//
// jupload - A file upload applet.
// Copyright 2007 The JUpload Team
//
// Created: ?
// Creator: William JinHua Kwong
// Last modified: $Date: 2009-02-09 10:42:51 +0100 $
//
// This program is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation; either version 2 of the License, or (at your option) any later
// version. This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details. You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation, Inc.,
// 675 Mass Ave, Cambridge, MA 02139, USA.

package wjhk.jupload2.gui;

import java.awt.Container;
import java.awt.Frame;
import java.awt.dnd.DropTarget;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.TransferHandler;

import wjhk.jupload2.JUploadApplet;
import wjhk.jupload2.gui.filepanel.FilePanel;
import wjhk.jupload2.gui.filepanel.FilePanelTableImp;
import wjhk.jupload2.policies.UploadPolicy;
import wjhk.jupload2.policies.UploadPolicyFactory;
import wjhk.jupload2.upload.FileUploadManagerThread;

/**
 * Main code for the applet (or frame) creation. It contains all creation of
 * necessary elements, and calls to {@link wjhk.jupload2.policies.UploadPolicy}
 * methods to allow easy personalization. <BR>
 * This class remain in the current project structure, even if it's not really
 * used any more. The original reason for this class was that the code would
 * work from within a navigator (an applet) or from a standard java application.
 * <BR>
 * This compatibility is no more maintained, as a lot of code suppose access to
 * navigator parameters. Hope it will be restored...
 * 
 * @author William JinHua Kwong
 * @version $Revision: 604 $
 */
public class JUploadPanel extends JPanel implements ActionListener,
        MouseListener {

    /** A generated serialVersionUID, to avoid warning during compilation */
    private static final long serialVersionUID = -1212601012568225757L;

    /** The debug popup menu of the applet */
    private JUploadDebugPopupMenu jUploadDebugPopupMenu;

    /** The main popup menu of the applet */
    private JUploadMainPopupMenu jUploadMainPopupMenu;

    // ------------- VARIABLES ----------------------------------------------

    /**
     * The Drag and Drop listener, that will manage the drop event. All pplet
     * element should register this instance, so that the user see the whole
     * applet as a unique drop target.
     */
    private DnDListener dndListener = null;

    private JButton browseButton = null, removeButton = null,
            removeAllButton = null, uploadButton = null, stopButton = null;

    private JUploadFileChooser fileChooser = null;

    private FilePanel filePanel = null;

    private JProgressBar preparationProgressBar = null;

    private JProgressBar uploadProgressBar = null;

    private JLabel statusLabel = null;

    /**
     * The log window. It's created by {@link JUploadApplet}.
     */
    private JUploadTextArea logWindow = null;

    /**
     * The log window pane contains the log window, and the relevant scroll
     * bars. It's actually this pane that is displayed, as a view on the log
     * window.
     */
    private JScrollPane jLogWindowPane = null;

    private UploadPolicy uploadPolicy = null;

    private FileUploadManagerThread fileUploadManagerThread = null;

    // ------------- CONSTRUCTOR --------------------------------------------

    /**
     * Standard constructor.
     * 
     * @param containerParam The container, where all GUI elements are to be
     *            created.
     * @param logWindow The log window that should already have been created.
     *            This allows putting text into it, before the effective
     *            creation of the layout.
     * @param uploadPolicyParam The current UploadPolicy. Null if a new one must
     *            be created.
     * @throws Exception
     * @see UploadPolicyFactory#getUploadPolicy(wjhk.jupload2.JUploadApplet)
     */
    public JUploadPanel(Container containerParam, JUploadTextArea logWindow,
            UploadPolicy uploadPolicyParam) throws Exception {
        this.logWindow = logWindow;
        this.uploadPolicy = uploadPolicyParam;
        this.jUploadDebugPopupMenu = new JUploadDebugPopupMenu(
                this.uploadPolicy);
        this.jUploadMainPopupMenu = new JUploadMainPopupMenu(this.uploadPolicy,
                this);

        // First: create standard components.
        createStandardComponents();
        logWindow.addMouseListener(this);

        // Then: display them on the applet
        this.uploadPolicy.addComponentsToJUploadPanel(this);

        // Define the drop target.
        this.dndListener = new DnDListener(this, this.uploadPolicy);
        new DropTarget(this, this.dndListener);
        new DropTarget(this.filePanel.getDropComponent(), this.dndListener);
        new DropTarget(this.logWindow, this.dndListener);

        // Define the TransfertHandler, to manage paste operations.
        JUploadTransferHandler jUploadTransfertHandler = new JUploadTransferHandler(
                this.uploadPolicy, this);
        this.setTransferHandler(jUploadTransfertHandler);
        this.filePanel.setTransferHandler(jUploadTransfertHandler);
        ActionMap map = this.getActionMap();
        map.put(TransferHandler.getPasteAction().getValue(Action.NAME),
                TransferHandler.getPasteAction());

        // The JUploadPanel will listen to Mouse messages for the standard
        // component. The current only application of this, it the CTRL+Righ
        // Click, that triggers the popup menu, which allow to switch debug on.
        this.browseButton.addMouseListener(this);
        this.removeAllButton.addMouseListener(this);
        this.removeButton.addMouseListener(this);
        this.stopButton.addMouseListener(this);
        this.uploadButton.addMouseListener(this);

        this.jLogWindowPane.addMouseListener(this);
        logWindow.addMouseListener(this);
        this.preparationProgressBar.addMouseListener(this);
        this.uploadProgressBar.addMouseListener(this);
        this.statusLabel.addMouseListener(this);

        // Setup File Chooser.
        try {
            this.fileChooser = uploadPolicyParam.createFileChooser();
        } catch (Exception e) {
            this.uploadPolicy.displayErr(e);
        }
    }

    // ----------------------------------------------------------------------

    /**
     * Creates all components used by the default upload policy. <BR>
     * You can change the component position of these components on the applet,
     * by creating a new upload policy, and override the
     * {@link UploadPolicy#addComponentsToJUploadPanel(JUploadPanel)} method.<BR>
     * You should keep these components, as there content is managed by the
     * internal code of the applet. <BR>
     * <U>Note:</U> this method will create component only if they were not
     * already created. That is only if the relevant attribute contain a null
     * value. If it's not the case, the already created component are keeped
     * unchanged.
     */
    private void createStandardComponents() {
        // -------- JButton browse --------
        if (this.browseButton == null) {
            this.browseButton = new JButton(this.uploadPolicy
                    .getString("buttonBrowse"));
            this.browseButton.setIcon(new ImageIcon(getClass().getResource(
                    "/images/explorer.gif")));
        }
        this.browseButton.addActionListener(this);

        // -------- JButton remove --------
        if (this.removeButton == null) {
            this.removeButton = new JButton(this.uploadPolicy
                    .getString("buttonRemoveSelected"));
            this.removeButton.setIcon(new ImageIcon(getClass().getResource(
                    "/images/recycle.gif")));
        }
        this.removeButton.setEnabled(false);
        this.removeButton.addActionListener(this);

        // -------- JButton removeAll --------
        if (this.removeAllButton == null) {
            this.removeAllButton = new JButton(this.uploadPolicy
                    .getString("buttonRemoveAll"));
            this.removeAllButton.setIcon(new ImageIcon(getClass().getResource(
                    "/images/cross.gif")));
        }
        this.removeAllButton.setEnabled(false);
        this.removeAllButton.addActionListener(this);

        // -------- JButton upload --------
        if (null == this.uploadButton) {
            this.uploadButton = new JButton(this.uploadPolicy
                    .getString("buttonUpload"));
            this.uploadButton.setIcon(new ImageIcon(getClass().getResource(
                    "/images/up.gif")));
        }
        this.uploadButton.setEnabled(false);
        this.uploadButton.addActionListener(this);

        // -------- The main thing: the file panel --------
        this.filePanel = new FilePanelTableImp(this, this.uploadPolicy);

        // -------- JProgressBar progress --------
        if (null == this.preparationProgressBar) {
            this.preparationProgressBar = new JProgressBar(
                    SwingConstants.HORIZONTAL);
            this.preparationProgressBar.setStringPainted(true);
        }
        if (null == this.uploadProgressBar) {
            this.uploadProgressBar = new JProgressBar(SwingConstants.HORIZONTAL);
            this.uploadProgressBar.setStringPainted(true);
        }

        // -------- JButton stop --------
        if (null == this.stopButton) {
            this.stopButton = new JButton(this.uploadPolicy
                    .getString("buttonStop"));
            this.stopButton.setIcon(new ImageIcon(getClass().getResource(
                    "/images/cross.gif")));
        }
        this.stopButton.setEnabled(false);
        this.stopButton.addActionListener(this);

        // -------- JButton stop --------
        if (this.jLogWindowPane == null) {
            this.jLogWindowPane = new JScrollPane();
            this.jLogWindowPane
                    .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
            this.jLogWindowPane
                    .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        }
        this.jLogWindowPane.getViewport().add(this.logWindow);
        this.jLogWindowPane.setPreferredSize(null);

        // -------- statusLabel --------
        this.statusLabel = new JLabel("JUpload applet V"
                + this.uploadPolicy.getApplet().VERSION);
    }

    /**
     * This methods show or hides the logWindow, depending on the following
     * applet parameters. The following conditions must be met, to hide the log
     * window: <DIR>
     * <LI>showLogWindow (must be False)
     * <LI>debugLevel (must be 0 or less) </DIR>
     */
    public void showOrHideLogWindow() {
        if ((this.uploadPolicy.getShowLogWindow()
                .equals(UploadPolicy.SHOWLOGWINDOW_TRUE))
                || (this.uploadPolicy.getShowLogWindow().equals(
                        UploadPolicy.SHOWLOGWINDOW_ONERROR) && this.uploadPolicy
                        .getLastException() != null)) {
            // The log window should be visible.
            this.jLogWindowPane.setVisible(true);
        } else {
            // It should be hidden.
            this.jLogWindowPane.setVisible(false);
        }
        // Let's recalculate the component display
        validate();
    }

    // ///////////////////////////////////////////////////////////////////////////////
    // ///////////////// Action methods
    // ///////////////////////////////////////////////////////////////////////////////

    /**
     * Reaction to a click on the browse button.
     */
    public void doBrowse() {
        // Browse clicked
        if (null != this.fileChooser) {
            try {
                int ret = this.fileChooser.showOpenDialog(new Frame());
                if (JFileChooser.APPROVE_OPTION == ret)
                    this.filePanel.addFiles(
                            this.fileChooser.getSelectedFiles(),
                            this.fileChooser.getCurrentDirectory());
                // We stop any running task for the JUploadFileView
                this.uploadPolicy.setCurrentBrowsingDirectory(this.fileChooser
                        .getCurrentDirectory());
                this.fileChooser.shutdownNow();
            } catch (Exception ex) {
                this.uploadPolicy.displayErr(ex);
            }
        }
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -