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

📄 pictureuploadpolicy.java

📁 [linux.rar] - 嵌入式linux开发教程
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//
// $Id: PictureUploadPolicy.java 295 2007-06-27 08:43:25 +0000 (mer., 27 juin
// 2007) etienne_sf $
//
// jupload - A file upload applet.
// Copyright 2007 The JUpload Team
//
// Created: 2006-05-06
// Creator: etienne_sf
// Last modified: $Date: 2009-02-16 12:42:50 +0100 (lun., 16 févr. 2009) $
//
// 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.policies;

import java.awt.Cursor;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.ImageObserver;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import wjhk.jupload2.JUploadApplet;
import wjhk.jupload2.exception.JUploadException;
import wjhk.jupload2.exception.JUploadExceptionStopAddingFiles;
import wjhk.jupload2.exception.JUploadIOException;
import wjhk.jupload2.filedata.FileData;
import wjhk.jupload2.filedata.PictureFileData;
import wjhk.jupload2.gui.JUploadFileChooser;
import wjhk.jupload2.gui.JUploadPanel;
import wjhk.jupload2.gui.image.JUploadImagePreview;
import wjhk.jupload2.gui.image.PictureDialog;
import wjhk.jupload2.gui.image.PicturePanel;

/**
 * This class add handling of pictures to upload. <BR>
 * <BR>
 * <H4>Functionalities:</H4>
 * <UL>
 * <LI>The top panel (upper part of the applet display) is modified, by using
 * UploadPolicy.
 * {@link wjhk.jupload2.policies.UploadPolicy#createTopPanel(JButton, JButton, JButton, JUploadPanel)}
 * . It contains a <B>preview</B> picture panel, and two additional buttons to
 * rotate the selected picture in one direction or the other.
 * <LI>Ability to set maximum width or height to a picture (with maxPicWidth and
 * maxPicHeight applet parameters, see the global explanation on the <a
 * href="UploadPolicy.html#parameters">parameters</a> section) of the
 * UploadPolicy API page.
 * <LI>Rotation of pictures, by quarter of turn.
 * <LI><I>(To be implemented)</I> A target picture format can be used, to force
 * all uploaded pictures to be in one picture format, jpeg for instance. All
 * details are in the UploadPolicy <a
 * href="UploadPolicy.html#parameters">parameters</a> section.
 * </UL>
 * <BR>
 * <BR>
 * See an example of HTML that calls this applet, just below. <H4>Parameters</H4>
 * The description for all parameters of all polices has been grouped in the
 * UploadPolicy <a href="UploadPolicy.html#parameters">parameters</a> section. <BR>
 * The parameters implemented in this class are:
 * <UL>
 * <LI>maxPicWidth: Maximum width for the uploaded picture.
 * <LI>maxPicHeight: Maximum height for the uploaded picture.
 * <LI><I>(To be implemented)</I> targetPictureFormat : Define the target
 * picture format. Eg: jpeg, png, gif...
 * </UL>
 * <A NAME="example"> <H4>HTML call example</H4> </A> You'll find below an
 * example of how to put the applet into a PHP page: <BR>
 * <XMP> <APPLET NAME="JUpload" CODE="wjhk.jupload2.JUploadApplet"
 * ARCHIVE="plugins/jupload/wjhk.jupload.jar" <!-- Applet display size, on the
 * navigator page --> WIDTH="500" HEIGHT="700" <!-- The applet call some
 * javascript function, so we must allow it : --> MAYSCRIPT > <!-- First,
 * mandatory parameters --> <PARAM NAME="postURL"
 * VALUE="http://some.host.com/youruploadpage.php"> <PARAM NAME="uploadPolicy"
 * VALUE="PictureUploadPolicy"> <!-- Then, optional parameters --> <PARAM
 * NAME="lang" VALUE="fr"> <PARAM NAME="maxPicHeight" VALUE="768"> <PARAM
 * NAME="maxPicWidth" VALUE="1024"> <PARAM NAME="debugLevel" VALUE="0"> Java 1.4
 * or higher plugin required. </APPLET> </XMP>
 * 
 * @author etienne_sf
 * @version $Revision: 612 $
 */

public class PictureUploadPolicy extends DefaultUploadPolicy implements
        ActionListener, ImageObserver {

    /**
     * Indicates that a BufferedImage is to be created when the user selects the
     * file. <BR>
     * If true : the Image is loaded once from the hard drive. This consumes
     * memory, but is interesting for big pictures, when they are resized (see
     * {@link #maxWidth} and {@link #maxHeight}). <BR>
     * If false : it is loaded for each display on the applet, then once for the
     * upload. <BR>
     * <BR>
     * Default : false, because the applet, while in the navigator, runs too
     * quickly out of memory.
     * 
     * @see wjhk.jupload2.policies.UploadPolicy#DEFAULT_STORE_BUFFERED_IMAGE
     */
    private boolean storeBufferedImage;

    /**
     * Iimage type that should be uploaded (JPG, GIF...). It should be a
     * standard type, as the JVM will create this file. If null, the same format
     * as the original file is used. <BR>
     * Currently <B>this flag is ignored when createBufferedImage is false</B> . <BR>
     * Default: null.
     * 
     * @see wjhk.jupload2.policies.UploadPolicy#DEFAULT_TARGET_PICTURE_FORMAT
     */
    private String targetPictureFormat;

    /**
     * Stored value for the fileChooserIconFromFileContent applet property.
     * 
     * @see UploadPolicy#PROP_FILE_CHOOSER_IMAGE_PREVIEW
     */
    private boolean fileChooserImagePreview = UploadPolicy.DEFAULT_FILE_CHOOSER_IMAGE_PREVIEW;

    /**
     * Indicates wether or not the preview pictures must be calculated by the
     * BufferedImage.getScaledInstance() method.
     */
    private boolean highQualityPreview;

    /**
     * Maximal width for the uploaded picture. If the actual width for the
     * picture is more than maxWidth, the picture is resized. The proportion
     * between widht and height are maintained. Negative if no maximum width (no
     * resizing). <BR>
     * Default: -1.
     * 
     * @see wjhk.jupload2.policies.UploadPolicy#DEFAULT_MAX_WIDTH
     */
    private int maxWidth = -1;

    /**
     * Maximal height for the uploaded picture. If the actual height for the
     * picture is more than maxHeight, the picture is resized. The proportion
     * between width and height are maintained. Negative if no maximum height
     * (no resizing). <BR>
     * Default: -1.
     * 
     * @see wjhk.jupload2.policies.UploadPolicy#DEFAULT_MAX_HEIGHT
     */
    private int maxHeight = -1;

    /**
     * Used to control the compression of a jpeg written file, after
     * transforming a picture.
     * 
     * @see UploadPolicy#PROP_PICTURE_COMPRESSION_QUALITY
     */
    private float pictureCompressionQuality = UploadPolicy.DEFAULT_PICTURE_COMPRESSION_QUALITY;

    /**
     * Used to control whether PictureFileData should add metadata to
     * transformed picture files, before upload (or remove metadata from
     * normally untransformed picture files).
     */
    private boolean pictureTransmitMetadata;

    /**
     * @see UploadPolicy
     */
    private int realMaxWidth = -1;

    /**
     * @see UploadPolicy
     */
    private int realMaxHeight = -1;

    /**
     * Button to allow the user to rotate the picture one quarter
     * counter-clockwise.
     */
    private JButton rotateLeftButton;

    /**
     * Button to allow the user to rotate the picture one quarter clockwise.
     */
    private JButton rotateRightButton;

    /**
     * The picture panel, where the selected picture is displayed.
     */
    private PicturePanel picturePanel;

    /**
     * The standard constructor, which transmit most informations to the
     * super.Constructor().
     * 
     * @param theApplet Reference to the current applet. Allows access to
     *            javascript functions.
     * @throws JUploadException
     */
    public PictureUploadPolicy(JUploadApplet theApplet) throws JUploadException {
        super(theApplet);

        // Creation of the PictureFileDataPolicy, from parameters given to the
        // applet, or from default values.
        setFileChooserImagePreview(UploadPolicyFactory.getParameter(theApplet,
                PROP_FILE_CHOOSER_IMAGE_PREVIEW,
                DEFAULT_FILE_CHOOSER_IMAGE_PREVIEW, this));
        setHighQualityPreview(UploadPolicyFactory.getParameter(theApplet,
                PROP_HIGH_QUALITY_PREVIEW, DEFAULT_HIGH_QUALITY_PREVIEW, this));
        setMaxHeight(UploadPolicyFactory.getParameter(theApplet,
                PROP_MAX_HEIGHT, DEFAULT_MAX_HEIGHT, this));
        setMaxWidth(UploadPolicyFactory.getParameter(theApplet, PROP_MAX_WIDTH,
                DEFAULT_MAX_WIDTH, this));
        setPictureCompressionQuality(UploadPolicyFactory.getParameter(
                theApplet, PROP_PICTURE_COMPRESSION_QUALITY,
                DEFAULT_PICTURE_COMPRESSION_QUALITY, this));
        setPictureTransmitMetadata(UploadPolicyFactory.getParameter(theApplet,
                PROP_PICTURE_TRANSMIT_METADATA,
                DEFAULT_PICTURE_TRANSMIT_METADATA, this));
        setRealMaxHeight(UploadPolicyFactory.getParameter(theApplet,
                PROP_REAL_MAX_HEIGHT, DEFAULT_REAL_MAX_HEIGHT, this));
        setRealMaxWidth(UploadPolicyFactory.getParameter(theApplet,
                PROP_REAL_MAX_WIDTH, DEFAULT_REAL_MAX_WIDTH, this));
        setStoreBufferedImage(UploadPolicyFactory.getParameter(theApplet,
                PROP_STORE_BUFFERED_IMAGE, DEFAULT_STORE_BUFFERED_IMAGE, this));
        setTargetPictureFormat(UploadPolicyFactory
                .getParameter(theApplet, PROP_TARGET_PICTURE_FORMAT,
                        DEFAULT_TARGET_PICTURE_FORMAT, this));

        displayDebug("[PictureUploadPolicy] end of constructor", 30);
    }

    /**
     * This methods actually returns a {@link PictureFileData} instance. It
     * allows only pictures: if the file is not a picture, this method returns
     * null, thus preventing the file to be added to the list of files to be
     * uploaded.
     * 
     * @param file The file selected by the user (called once for each added
     *            file).
     * @return An instance of {@link PictureFileData} or null if file is not a
     *         picture.
     * @see wjhk.jupload2.policies.UploadPolicy#createFileData(File,File)
     */
    @Override
    public FileData createFileData(File file, File root)
            throws JUploadExceptionStopAddingFiles {
        // Do standard rules accept this file ?
        FileData defaultFileData = super.createFileData(file, root);

        if (defaultFileData == null) {
            // The file is not allowed.
            return null;
        } else {
            // Ok, the file is to be accepted. Is it a picture?
            PictureFileData pfd = null;
            try {
                pfd = new PictureFileData(file, root, this);
            } catch (JUploadIOException e) {
                displayErr(e);
            }

            // If we get a pfd, let' check that it's a picture.
            if (pfd != null && pfd.isPicture()) {
                return pfd;
            } else if (getAllowedFileExtensions() != null) {
                // A list of allowed extensions has been given, and, as we got
                // here, defaultFileData is not null, that is: this files match
                // the allowedFileEXtensions parameter. We return it.
                return defaultFileData;
            } else {
                // We now use the JUploadExceptionStopAddingFiles exception, to
                // allow the user to stop adding files.
                String msg = String.format(getString("notAPicture"), file
                        .getName());

                // Alert only once, when several files are not pictures... hum,
                displayWarn(msg);
                if (JOptionPane.showConfirmDialog(null, msg, "alert",
                        JOptionPane.OK_CANCEL_OPTION,
                        JOptionPane.WARNING_MESSAGE) == JOptionPane.CANCEL_OPTION) {
                    // The user want to stop to add files to the list. For
                    // instance, when he/she added a whole directory, and it
                    // contains a lot of files that don't match the allowed file
                    // extension.
                    throw new JUploadExceptionStopAddingFiles(
                            "Stopped by the user");
                }
                return null;
            }
        }
    }

    /**
     * This method override the default topPanel, and adds:<BR>
     * <UL>
     * <LI>Two rotation buttons, to rotate the currently selected picture.
     * <LI>A Preview area, to view the selected picture
     * </UL>
     * 
     * @see wjhk.jupload2.policies.UploadPolicy#createTopPanel(JButton, JButton,
     *      JButton, JUploadPanel)
     */
    @Override
    public JPanel createTopPanel(JButton browse, JButton remove,
            JButton removeAll, JUploadPanel jUploadPanel) {
        // The top panel is verticaly divided in :
        // - On the left, the button bar (buttons one above another)
        // - On the right, the preview PicturePanel.

        // Creation of specific buttons
        this.rotateLeftButton = new JButton(getString("buttonRotateLeft"));
        this.rotateLeftButton.setIcon(new ImageIcon(getClass().getResource(
                "/images/rotateLeft.gif")));
        this.rotateLeftButton.addActionListener(this);
        this.rotateLeftButton.addMouseListener(jUploadPanel);
        this.rotateLeftButton.setEnabled(false);

        this.rotateRightButton = new JButton(getString("buttonRotateRight"));
        this.rotateRightButton.setIcon(new ImageIcon(getClass().getResource(
                "/images/rotateRight.gif")));
        this.rotateRightButton.addActionListener(this);
        this.rotateRightButton.addMouseListener(jUploadPanel);
        this.rotateRightButton.setEnabled(false);

        // The button bar
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(5, 1, 5, 5));
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 5));
        buttonPanel.add(browse);
        buttonPanel.add(this.rotateLeftButton);
        buttonPanel.add(this.rotateRightButton);
        buttonPanel.add(removeAll);
        buttonPanel.add(remove);

        // The preview PicturePanel
        JPanel pPanel = new JPanel();
        pPanel.setLayout(new GridLayout(1, 1));
        pPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 10));

        this.picturePanel = new PicturePanel(true, this);
        this.picturePanel.addMouseListener(jUploadPanel);
        pPanel.add(this.picturePanel);
        // Setting specific cursor for this panel, default for other parts of
        // the applet.
        setCursor(null);

        // And last but not least ... creation of the top panel:
        JPanel topPanel = new JPanel();
        topPanel.setLayout(new GridLayout(1, 2));
        topPanel.add(buttonPanel);
        topPanel.add(pPanel);

        jUploadPanel.setBorder(BorderFactory
                .createLineBorder(SystemColor.controlDkShadow));

        return topPanel;
    }// createTopPanel

⌨️ 快捷键说明

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