source: 3thparty/jupload/src/main/java/wjhk/jupload2/policies/PictureUploadPolicy.java @ 3951

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

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

Line 
1//
2// $Id: PictureUploadPolicy.java 295 2007-06-27 08:43:25 +0000 (mer., 27 juin
3// 2007) etienne_sf $
4//
5// jupload - A file upload applet.
6// Copyright 2007 The JUpload Team
7//
8// Created: 2006-05-06
9// Creator: etienne_sf
10// Last modified: $Date: 2010-03-23 05:01:21 -0300 (Ter, 23 Mar 2010) $
11//
12// This program is free software; you can redistribute it and/or modify it under
13// the terms of the GNU General Public License as published by the Free Software
14// Foundation; either version 2 of the License, or (at your option) any later
15// version. This program is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18// details. You should have received a copy of the GNU General Public License
19// along with this program; if not, write to the Free Software Foundation, Inc.,
20// 675 Mass Ave, Cambridge, MA 02139, USA.
21
22package wjhk.jupload2.policies;
23
24import java.awt.Cursor;
25import java.awt.GridLayout;
26import java.awt.Image;
27import java.awt.SystemColor;
28import java.awt.event.ActionEvent;
29import java.awt.event.ActionListener;
30import java.awt.image.ImageObserver;
31import java.io.File;
32
33import javax.swing.BorderFactory;
34import javax.swing.Icon;
35import javax.swing.ImageIcon;
36import javax.swing.JButton;
37import javax.swing.JOptionPane;
38import javax.swing.JPanel;
39
40import wjhk.jupload2.context.JUploadContext;
41import wjhk.jupload2.exception.JUploadException;
42import wjhk.jupload2.exception.JUploadExceptionStopAddingFiles;
43import wjhk.jupload2.exception.JUploadIOException;
44import wjhk.jupload2.filedata.FileData;
45import wjhk.jupload2.filedata.PictureFileData;
46import wjhk.jupload2.filedata.helper.ImageFileConversionInfo;
47import wjhk.jupload2.gui.JUploadFileChooser;
48import wjhk.jupload2.gui.JUploadPanel;
49import wjhk.jupload2.gui.image.JUploadImagePreview;
50import wjhk.jupload2.gui.image.PictureDialog;
51import wjhk.jupload2.gui.image.PicturePanel;
52
53/**
54 * This class add handling of pictures to upload. <BR>
55 * <BR>
56 * <H4>Functionalities:</H4>
57 * <UL>
58 * <LI>The top panel (upper part of the applet display) is modified, by using
59 * UploadPolicy.
60 * {@link wjhk.jupload2.policies.UploadPolicy#createTopPanel(JButton, JButton, JButton, JUploadPanel)}
61 * . It contains a <B>preview</B> picture panel, and two additional buttons to
62 * rotate the selected picture in one direction or the other.
63 * <LI>Ability to set maximum width or height to a picture (with maxPicWidth and
64 * maxPicHeight applet parameters, see the global explanation on the <a
65 * href="UploadPolicy.html#parameters">parameters</a> section) of the
66 * UploadPolicy API page.
67 * <LI>Rotation of pictures, by quarter of turn.
68 * <LI><I>(To be implemented)</I> A target picture format can be used, to force
69 * all uploaded pictures to be in one picture format, jpeg for instance. All
70 * details are in the UploadPolicy <a
71 * href="UploadPolicy.html#parameters">parameters</a> section.
72 * </UL>
73 * <BR>
74 * <BR>
75 * See an example of HTML that calls this applet, just below. <H4>Parameters</H4>
76 * The description for all parameters of all polices has been grouped in the
77 * UploadPolicy <a href="UploadPolicy.html#parameters">parameters</a> section. <BR>
78 * The parameters implemented in this class are:
79 * <UL>
80 * <LI>maxPicWidth: Maximum width for the uploaded picture.
81 * <LI>maxPicHeight: Maximum height for the uploaded picture.
82 * <LI>targetPictureFormat : Define picture format conversions.
83 * <LI>keepOriginalFileExtensionForConvertedImages : handling of file extensions
84 * for image conversions.
85 * </UL>
86 * <A NAME="example"> <H4>HTML call example</H4> </A> You'll find below an
87 * example of how to put the applet into a PHP page: <BR>
88 * <XMP> <APPLET NAME="JUpload" CODE="wjhk.jupload2.JUploadApplet"
89 * ARCHIVE="plugins/jupload/wjhk.jupload.jar" <!-- Applet display size, on the
90 * navigator page --> WIDTH="500" HEIGHT="700" <!-- The applet call some
91 * javascript function, so we must allow it : --> MAYSCRIPT > <!-- First,
92 * mandatory parameters --> <PARAM NAME="postURL"
93 * VALUE="http://some.host.com/youruploadpage.php"> <PARAM NAME="uploadPolicy"
94 * VALUE="PictureUploadPolicy"> <!-- Then, optional parameters --> <PARAM
95 * NAME="lang" VALUE="fr"> <PARAM NAME="maxPicHeight" VALUE="768"> <PARAM
96 * NAME="maxPicWidth" VALUE="1024"> <PARAM NAME="debugLevel" VALUE="0"> Java 1.4
97 * or higher plugin required. </APPLET> </XMP>
98 *
99 * @author etienne_sf
100 * @version $Revision: 1057 $
101 */
102
103public class PictureUploadPolicy extends DefaultUploadPolicy implements
104        ActionListener, ImageObserver {
105
106    /**
107     * Indicates that a BufferedImage is to be created when the user selects the
108     * file. <BR>
109     * If true : the Image is loaded once from the hard drive. This consumes
110     * memory, but is interesting for big pictures, when they are resized (see
111     * {@link #maxWidth} and {@link #maxHeight}). <BR>
112     * If false : it is loaded for each display on the applet, then once for the
113     * upload. <BR>
114     * <BR>
115     * Default : false, because the applet, while in the navigator, runs too
116     * quickly out of memory.
117     *
118     * @see wjhk.jupload2.policies.UploadPolicy#DEFAULT_STORE_BUFFERED_IMAGE
119     */
120    private boolean storeBufferedImage;
121
122    /**
123     * This parameter can contain a list to convert image formats. <br />
124     * see class description of {@link UploadPolicy} for details
125     *
126     * @see wjhk.jupload2.policies.UploadPolicy#DEFAULT_TARGET_PICTURE_FORMAT
127     */
128    private String targetPictureFormat;
129
130    /**
131     * see class description of {@link UploadPolicy} for details
132     *
133     * @see wjhk.jupload2.policies.UploadPolicy#DEFAULT_KEEP_ORIG_EXTENSION
134     */
135    private boolean keepOrigExtension;
136
137    /**
138     * the parsed {@link #targetPictureFormat} list
139     */
140    private ImageFileConversionInfo imageFileConversionInfo = new ImageFileConversionInfo(
141            "");
142
143    /**
144     * Stored value for the fileChooserIconFromFileContent applet property.
145     *
146     * @see UploadPolicy#PROP_FILE_CHOOSER_IMAGE_PREVIEW
147     */
148    private boolean fileChooserImagePreview = UploadPolicy.DEFAULT_FILE_CHOOSER_IMAGE_PREVIEW;
149
150    /**
151     * Indicates wether or not the preview pictures must be calculated by the
152     * BufferedImage.getScaledInstance() method.
153     */
154    private boolean highQualityPreview;
155
156    /**
157     * Maximal width for the uploaded picture. If the actual width for the
158     * picture is more than maxWidth, the picture is resized. The proportion
159     * between widht and height are maintained. Negative if no maximum width (no
160     * resizing). <BR>
161     * Default: -1.
162     *
163     * @see wjhk.jupload2.policies.UploadPolicy#DEFAULT_MAX_WIDTH
164     */
165    private int maxWidth = -1;
166
167    /**
168     * Maximal height for the uploaded picture. If the actual height for the
169     * picture is more than maxHeight, the picture is resized. The proportion
170     * between width and height are maintained. Negative if no maximum height
171     * (no resizing). <BR>
172     * Default: -1.
173     *
174     * @see wjhk.jupload2.policies.UploadPolicy#DEFAULT_MAX_HEIGHT
175     */
176    private int maxHeight = -1;
177
178    /**
179     * Used to control the compression of a jpeg written file, after
180     * transforming a picture.
181     *
182     * @see UploadPolicy#PROP_PICTURE_COMPRESSION_QUALITY
183     */
184    private float pictureCompressionQuality = UploadPolicy.DEFAULT_PICTURE_COMPRESSION_QUALITY;
185
186    /**
187     * Used to control whether PictureFileData should add metadata to
188     * transformed picture files, before upload (or remove metadata from
189     * normally untransformed picture files).
190     */
191    private boolean pictureTransmitMetadata;
192
193    /**
194     * @see UploadPolicy
195     */
196    private int realMaxWidth = -1;
197
198    /**
199     * @see UploadPolicy
200     */
201    private int realMaxHeight = -1;
202
203    /**
204     * Button to allow the user to rotate the picture one quarter
205     * counter-clockwise.
206     */
207    private JButton rotateLeftButton;
208
209    /**
210     * Button to allow the user to rotate the picture one quarter clockwise.
211     */
212    private JButton rotateRightButton;
213
214    /**
215     * The picture panel, where the selected picture is displayed.
216     */
217    private PicturePanel picturePanel;
218
219    /**
220     * The standard constructor, which transmit most informations to the
221     * super.Constructor().
222     *
223     * @param juploadContext Reference to the current applet. Allows access to
224     *            javascript functions.
225     * @throws JUploadException
226     */
227    public PictureUploadPolicy(JUploadContext juploadContext)
228            throws JUploadException {
229        super(juploadContext);
230
231        // Creation of the PictureFileDataPolicy, from parameters given to the
232        // applet, or from default values.
233        setFileChooserImagePreview(juploadContext.getParameter(
234                PROP_FILE_CHOOSER_IMAGE_PREVIEW,
235                DEFAULT_FILE_CHOOSER_IMAGE_PREVIEW));
236        setHighQualityPreview(juploadContext.getParameter(
237                PROP_HIGH_QUALITY_PREVIEW, DEFAULT_HIGH_QUALITY_PREVIEW));
238        setMaxHeight(juploadContext.getParameter(PROP_MAX_HEIGHT,
239                DEFAULT_MAX_HEIGHT));
240        setMaxWidth(juploadContext.getParameter(PROP_MAX_WIDTH,
241                DEFAULT_MAX_WIDTH));
242        setPictureCompressionQuality(juploadContext.getParameter(
243                PROP_PICTURE_COMPRESSION_QUALITY,
244                DEFAULT_PICTURE_COMPRESSION_QUALITY));
245        setPictureTransmitMetadata(juploadContext.getParameter(
246                PROP_PICTURE_TRANSMIT_METADATA,
247                DEFAULT_PICTURE_TRANSMIT_METADATA));
248        setRealMaxHeight(juploadContext.getParameter(PROP_REAL_MAX_HEIGHT,
249                DEFAULT_REAL_MAX_HEIGHT));
250        setRealMaxWidth(juploadContext.getParameter(PROP_REAL_MAX_WIDTH,
251                DEFAULT_REAL_MAX_WIDTH));
252        setTargetPictureFormat(juploadContext.getParameter(
253                PROP_TARGET_PICTURE_FORMAT, DEFAULT_TARGET_PICTURE_FORMAT));
254
255        setKeepOrigExtension(juploadContext.getParameter(
256                PROP_KEEP_ORIG_EXTENSION, DEFAULT_KEEP_ORIG_EXTENSION));
257
258        displayDebug("[PictureUploadPolicy] end of constructor", 30);
259    }
260
261    /**
262     * This methods actually returns a {@link PictureFileData} instance. It
263     * allows only pictures: if the file is not a picture, this method returns
264     * null, thus preventing the file to be added to the list of files to be
265     * uploaded.
266     *
267     * @param file The file selected by the user (called once for each added
268     *            file).
269     * @return An instance of {@link PictureFileData} or null if file is not a
270     *         picture.
271     * @see wjhk.jupload2.policies.UploadPolicy#createFileData(File,File)
272     */
273    @Override
274    public FileData createFileData(File file, File root)
275            throws JUploadExceptionStopAddingFiles {
276        // Do standard rules accept this file ?
277        FileData defaultFileData = super.createFileData(file, root);
278
279        if (defaultFileData == null) {
280            // The file is not allowed.
281            return null;
282        } else {
283            // Ok, the file is to be accepted. Is it a picture?
284            PictureFileData pfd = null;
285            try {
286                pfd = new PictureFileData(file, root, this);
287            } catch (JUploadIOException e) {
288                displayErr(e);
289            }
290
291            // If we get a pfd, let' check that it's a picture.
292            if (pfd != null && pfd.isPicture()) {
293                return pfd;
294            } else if (getAllowedFileExtensions() != null) {
295                // A list of allowed extensions has been given, and, as we got
296                // here, defaultFileData is not null, that is: this files match
297                // the allowedFileEXtensions parameter. We return it.
298                return defaultFileData;
299            } else {
300                // We now use the JUploadExceptionStopAddingFiles exception, to
301                // allow the user to stop adding files.
302                String msg = getLocalizedString("notAPicture", file.getName());
303
304                // Alert only once, when several files are not pictures... hum,
305                displayWarn(msg);
306                if (JOptionPane.showConfirmDialog(null, msg, "alert",
307                        JOptionPane.OK_CANCEL_OPTION,
308                        JOptionPane.WARNING_MESSAGE) == JOptionPane.CANCEL_OPTION) {
309                    // The user want to stop to add files to the list. For
310                    // instance, when he/she added a whole directory, and it
311                    // contains a lot of files that don't match the allowed file
312                    // extension.
313                    throw new JUploadExceptionStopAddingFiles(
314                            "Stopped by the user");
315                }
316                return null;
317            }
318        }
319    }
320
321    /**
322     * This method override the default topPanel, and adds:<BR>
323     * <UL>
324     * <LI>Two rotation buttons, to rotate the currently selected picture.
325     * <LI>A Preview area, to view the selected picture
326     * </UL>
327     *
328     * @see wjhk.jupload2.policies.UploadPolicy#createTopPanel(JButton, JButton,
329     *      JButton, JUploadPanel)
330     */
331    @Override
332    public JPanel createTopPanel(JButton browse, JButton remove,
333            JButton removeAll, JUploadPanel jUploadPanel) {
334        // The top panel is verticaly divided in :
335        // - On the left, the button bar (buttons one above another)
336        // - On the right, the preview PicturePanel.
337
338        // Creation of specific buttons
339        this.rotateLeftButton = new JButton(
340                getLocalizedString("buttonRotateLeft"));
341        this.rotateLeftButton.setIcon(new ImageIcon(getClass().getResource(
342                "/images/rotateLeft.gif")));
343        this.rotateLeftButton.addActionListener(this);
344        this.rotateLeftButton.addMouseListener(jUploadPanel.getMouseListener());
345        this.rotateLeftButton.setEnabled(false);
346
347        this.rotateRightButton = new JButton(
348                getLocalizedString("buttonRotateRight"));
349        this.rotateRightButton.setIcon(new ImageIcon(getClass().getResource(
350                "/images/rotateRight.gif")));
351        this.rotateRightButton.addActionListener(this);
352        this.rotateRightButton
353                .addMouseListener(jUploadPanel.getMouseListener());
354        this.rotateRightButton.setEnabled(false);
355
356        // The button bar
357        JPanel buttonPanel = new JPanel();
358        buttonPanel.setLayout(new GridLayout(5, 1, 5, 5));
359        buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 5));
360        buttonPanel.add(browse);
361        buttonPanel.add(this.rotateLeftButton);
362        buttonPanel.add(this.rotateRightButton);
363        buttonPanel.add(removeAll);
364        buttonPanel.add(remove);
365
366        // The preview PicturePanel
367        JPanel pPanel = new JPanel();
368        pPanel.setLayout(new GridLayout(1, 1));
369        pPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 10));
370
371        this.picturePanel = new PicturePanel(true, this);
372        this.picturePanel.addMouseListener(jUploadPanel.getMouseListener());
373        pPanel.add(this.picturePanel);
374        // Setting specific cursor for this panel, default for other parts of
375        // the applet.
376        setCursor(null);
377
378        // And last but not least ... creation of the top panel:
379        JPanel topPanel = new JPanel();
380        topPanel.setLayout(new GridLayout(1, 2));
381        topPanel.add(buttonPanel);
382        topPanel.add(pPanel);
383
384        jUploadPanel.getJComponent().setBorder(
385                BorderFactory.createLineBorder(SystemColor.controlDkShadow));
386
387        return topPanel;
388    }// createTopPanel
389
390    /**
391     * This method handles the clicks on the rotation buttons. All other actions
392     * are managed by the {@link DefaultUploadPolicy}.
393     *
394     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
395     */
396    public void actionPerformed(ActionEvent e) {
397        displayInfo("Action : " + e.getActionCommand());
398        if (e.getActionCommand() == this.rotateLeftButton.getActionCommand()) {
399            this.picturePanel.rotate(-1);
400        } else if (e.getActionCommand() == this.rotateRightButton
401                .getActionCommand()) {
402            this.picturePanel.rotate(1);
403        }
404    }// actionPerformed
405
406    /**
407     * @see wjhk.jupload2.policies.UploadPolicy#onFileSelected(wjhk.jupload2.filedata.FileData)
408     */
409    @Override
410    public void onFileSelected(FileData fileData) {
411        if (fileData != null) {
412            displayDebug("File selected: " + fileData.getFileName(), 30);
413        }
414        if (this.picturePanel != null) {
415            // If this file is a picture, we display it.
416            if (fileData instanceof PictureFileData) {
417                Cursor previousCursor = setWaitCursor();
418                this.picturePanel.setPictureFile((PictureFileData) fileData,
419                        this.rotateLeftButton, this.rotateRightButton);
420                setCursor(previousCursor);
421            } else {
422                this.picturePanel.setPictureFile(null, this.rotateLeftButton,
423                        this.rotateRightButton);
424            }
425        }
426    }
427
428    /**
429     * Open the 'big' preview dialog box. It allows the user to see a full
430     * screen preview of the choosen picture.<BR>
431     * This method does nothing if the panel has no selected picture, that is
432     * when pictureFileData is null.
433     *
434     * @see UploadPolicy#onFileDoubleClicked(FileData)
435     */
436    @Override
437    public void onFileDoubleClicked(FileData pictureFileData) {
438        if (pictureFileData == null) {
439            // No action
440        } else if (!(pictureFileData instanceof PictureFileData)) {
441            displayWarn("PictureUploadPolicy: received a non PictureFileData in onFileDoubleClicked");
442        } else {
443            new PictureDialog(null, (PictureFileData) pictureFileData, this);
444        }
445    }
446
447    /** @see UploadPolicy#beforeUpload() */
448    @Override
449    public boolean beforeUpload() {
450        // We clear the current picture selection. This insures a correct
451        // managing of enabling/disabling of
452        // buttons, even if the user stops the upload.
453        getContext().getUploadPanel().getFilePanel().clearSelection();
454        if (this.picturePanel != null) {
455            this.picturePanel.setPictureFile(null, this.rotateLeftButton,
456                    this.rotateRightButton);
457        }
458
459        // Then, we call the standard action, if any.
460        return super.beforeUpload();
461    }
462
463    // ////////////////////////////////////////////////////////////////////////////////////////////////////
464    // /////////////////////// Getters and Setters
465    // ////////////////////////////////////////////////
466    // ////////////////////////////////////////////////////////////////////////////////////////////////////
467
468    /**
469     * Getter for fileChooserImagePreview.
470     *
471     * @return Current value for the applet parameter: fileChooserImagePreview
472     * @see UploadPolicy#PROP_FILE_CHOOSER_IMAGE_PREVIEW
473     */
474    public boolean getFileChooserImagePreview() {
475        return this.fileChooserImagePreview;
476    }
477
478    /**
479     * Setter for fileChooserIconFromFileContent. Current allowed values are:
480     * -1, 0, 1. Default value is 0.
481     *
482     * @param fileChooserImagePreview new value to store, for the applet
483     *            parameter: fileChooserImagePreview.
484     * @see UploadPolicy#PROP_FILE_CHOOSER_IMAGE_PREVIEW
485     */
486    public void setFileChooserImagePreview(boolean fileChooserImagePreview) {
487        this.fileChooserImagePreview = fileChooserImagePreview;
488    }
489
490    /** @return the applet parameter <I>highQualityPreview</I>. */
491    public boolean getHighQualityPreview() {
492        return this.highQualityPreview;
493    }
494
495    /** @param highQualityPreview the highQualityPreview to set */
496    void setHighQualityPreview(boolean highQualityPreview) {
497        this.highQualityPreview = highQualityPreview;
498    }
499
500    /**
501     * @return Returns the maxHeight, that should be used by pictures non
502     *         transformed (rotated...) by the applet.
503     */
504    public int getMaxHeight() {
505        return this.maxHeight;
506    }
507
508    /** @param maxHeight the maxHeight to set */
509    void setMaxHeight(int maxHeight) {
510        if (maxHeight <= 0) {
511            this.maxHeight = Integer.MAX_VALUE;
512            displayWarn("[setMaxHeight] maxHeight switched from " + maxHeight
513                    + " to " + this.maxHeight);
514        } else {
515            this.maxHeight = maxHeight;
516        }
517    }
518
519    /**
520     * @return Returns the maxWidth, that should be used by pictures non
521     *         transformed (rotated...) by the applet.
522     */
523    public int getMaxWidth() {
524        return this.maxWidth;
525    }
526
527    /** @param maxWidth the maxWidth to set */
528    void setMaxWidth(int maxWidth) {
529        if (maxWidth <= 0) {
530            this.maxWidth = Integer.MAX_VALUE;
531            displayWarn("[setMaxWidth] maxWidth switched from " + maxWidth
532                    + " to " + this.maxWidth);
533        } else {
534            this.maxWidth = maxWidth;
535        }
536    }
537
538    /**
539     * @return The current value for picture compression.
540     */
541    public float getPictureCompressionQuality() {
542        return this.pictureCompressionQuality;
543    }
544
545    /**
546     * @see #pictureCompressionQuality
547     * @param pictureCompressionQuality The new value for picture compression.
548     */
549    void setPictureCompressionQuality(float pictureCompressionQuality) {
550        this.pictureCompressionQuality = pictureCompressionQuality;
551    }
552
553    /**
554     * @return The current value for transmission (or no transmission) of
555     *         picture metadata.
556     */
557    public boolean getPictureTransmitMetadata() {
558        return this.pictureTransmitMetadata;
559    }
560
561    /**
562     * @see #pictureTransmitMetadata
563     * @param pictureTransmitMetadata The new value for this attribute.
564     */
565    void setPictureTransmitMetadata(boolean pictureTransmitMetadata) {
566        this.pictureTransmitMetadata = pictureTransmitMetadata;
567    }
568
569    /**
570     * @return Returns the maxHeight, that should be used by pictures that are
571     *         transformed (rotated...) by the applet.
572     */
573    public int getRealMaxHeight() {
574        return (this.realMaxHeight == Integer.MAX_VALUE) ? this.maxHeight
575                : this.realMaxHeight;
576    }
577
578    /** @param realMaxHeight the realMaxHeight to set */
579    void setRealMaxHeight(int realMaxHeight) {
580        this.realMaxHeight = realMaxHeight;
581    }
582
583    /**
584     * @return Returns the maxWidth, that should be used by pictures that are
585     *         transformed (rotated...) by the applet.
586     */
587    public int getRealMaxWidth() {
588        return (this.realMaxWidth == Integer.MAX_VALUE) ? this.maxWidth
589                : this.realMaxWidth;
590    }
591
592    /** @param realMaxWidth the realMaxWidth to set */
593    void setRealMaxWidth(int realMaxWidth) {
594        this.realMaxWidth = realMaxWidth;
595    }
596
597    /** @return Returns the targetPictureFormat. */
598    public String getTargetPictureFormat() {
599        return this.targetPictureFormat;
600    }
601
602    /**
603     * @return The current ImageFileConversionInfo
604     */
605    public ImageFileConversionInfo getImageFileConversionInfo() {
606        return this.imageFileConversionInfo;
607    }
608
609    /**
610     * we expect e.g. "png,bmp:jpg;gif:png;"
611     *
612     * @param targetPictureFormat the targetPictureFormat to set
613     * @throws JUploadException if the conversionList is erroneous
614     */
615    void setTargetPictureFormat(String targetPictureFormat)
616            throws JUploadException {
617        this.targetPictureFormat = targetPictureFormat;
618        this.imageFileConversionInfo = new ImageFileConversionInfo(
619                targetPictureFormat);
620    }
621
622    /**
623     * @return <ul>
624     *         <li><code>true</code>, if the the original file extension should
625     *         be kept</li>
626     *         <li><code>false</code>, if the the original file extension should
627     *         be changed to the target picture format, that the file has been
628     *         converted to</li>
629     *         </ul>
630     */
631    public boolean getKeepOrigExtension() {
632        return this.keepOrigExtension;
633    }
634
635    /**
636     * @param keepOrigExtension if the original file extension should be kept '
637     *            <code>true</code>', or changed '<code>false</code>' (if the
638     *            image was converted)
639     */
640    void setKeepOrigExtension(boolean keepOrigExtension)
641            throws JUploadException {
642        this.keepOrigExtension = keepOrigExtension;
643    }
644
645    /**
646     * This method manages the applet parameters that are specific to this
647     * class. The super.setProperty method is called for other properties.
648     *
649     * @param prop The property which value should change
650     * @param value The new value for this property. If invalid, the default
651     *            value is used.
652     * @see wjhk.jupload2.policies.UploadPolicy#setProperty(java.lang.String,
653     *      java.lang.String)
654     */
655    @Override
656    public void setProperty(String prop, String value) throws JUploadException {
657        // The, we check the local properties.
658        if (prop.equals(PROP_FILE_CHOOSER_IMAGE_PREVIEW)) {
659            setFileChooserImagePreview(getContext().parseBoolean(value,
660                    getFileChooserImagePreview()));
661        } else if (prop.equals(PROP_HIGH_QUALITY_PREVIEW)) {
662            setHighQualityPreview(getContext().parseBoolean(value,
663                    this.highQualityPreview));
664        } else if (prop.equals(PROP_MAX_HEIGHT)) {
665            setMaxHeight(getContext().parseInt(value, this.maxHeight));
666        } else if (prop.equals(PROP_MAX_WIDTH)) {
667            setMaxWidth(getContext().parseInt(value, this.maxWidth));
668        } else if (prop.equals(PROP_PICTURE_COMPRESSION_QUALITY)) {
669            setPictureCompressionQuality(getContext().parseFloat(value,
670                    this.pictureCompressionQuality));
671        } else if (prop.equals(PROP_PICTURE_TRANSMIT_METADATA)) {
672            setPictureTransmitMetadata(getContext().parseBoolean(value,
673                    this.pictureTransmitMetadata));
674        } else if (prop.equals(PROP_REAL_MAX_HEIGHT)) {
675            setRealMaxHeight(getContext().parseInt(value, this.realMaxHeight));
676        } else if (prop.equals(PROP_REAL_MAX_WIDTH)) {
677            setRealMaxWidth(getContext().parseInt(value, this.realMaxWidth));
678        } else if (prop.equals(PROP_TARGET_PICTURE_FORMAT)) {
679            setTargetPictureFormat(value);
680        } else if (prop.equals(PROP_KEEP_ORIG_EXTENSION)) {
681            setKeepOrigExtension(getContext().parseBoolean(value,
682                    this.keepOrigExtension));
683        } else {
684            // Otherwise, transmission to the mother class.
685            super.setProperty(prop, value);
686        }
687    }
688
689    /** @see DefaultUploadPolicy#displayParameterStatus() */
690    @Override
691    public void displayParameterStatus() {
692        super.displayParameterStatus();
693
694        displayDebug("======= Parameters managed by PictureUploadPolicy", 30);
695        displayDebug(PROP_FILE_CHOOSER_IMAGE_PREVIEW + ": "
696                + getFileChooserImagePreview(), 30);
697        displayDebug(PROP_HIGH_QUALITY_PREVIEW + " : "
698                + this.highQualityPreview, 30);
699        displayDebug(PROP_PICTURE_COMPRESSION_QUALITY + " : "
700                + getPictureCompressionQuality(), 30);
701        displayDebug(PROP_PICTURE_TRANSMIT_METADATA + " : "
702                + getPictureTransmitMetadata(), 30);
703        displayDebug(PROP_MAX_WIDTH + " : " + this.maxWidth + ", "
704                + PROP_MAX_HEIGHT + " : " + this.maxHeight, 30);
705        displayDebug(PROP_REAL_MAX_WIDTH + " : " + this.realMaxWidth + ", "
706                + PROP_REAL_MAX_HEIGHT + " : " + this.realMaxHeight, 30);
707        displayDebug(PROP_STORE_BUFFERED_IMAGE + " : "
708                + this.storeBufferedImage, 30);
709        displayDebug(PROP_TARGET_PICTURE_FORMAT + " : "
710                + this.targetPictureFormat, 30);
711        displayDebug("Format conversions : " + getImageFileConversionInfo(), 40);
712        displayDebug("", 30);
713    }
714
715    /**
716     * Calls the {@link DefaultUploadPolicy#setWaitCursor()} method, then erases
717     * the picture panel specific cursor.
718     *
719     * @see DefaultUploadPolicy#setCursor(Cursor)
720     */
721    @Override
722    public Cursor setWaitCursor() {
723        Cursor previousCursor = super.setWaitCursor();
724        this.picturePanel.setCursor(null);
725        return previousCursor;
726    }
727
728    /**
729     * Calls the {@link DefaultUploadPolicy#setCursor(Cursor)} method, then set
730     * the picture panel specific cursor.
731     *
732     * @see DefaultUploadPolicy#setCursor(Cursor)
733     */
734    @Override
735    public Cursor setCursor(Cursor cursor) {
736        Cursor oldCursor = super.setCursor(null);
737        this.picturePanel.setCursor(new Cursor(Cursor.HAND_CURSOR));
738        return oldCursor;
739    }
740
741    /**
742     * Creates the file chooser, from the default implementation, then add an
743     * accessory to preview pictures.
744     *
745     * @see UploadPolicy#createFileChooser()
746     */
747    @Override
748    public JUploadFileChooser createFileChooser() {
749        JUploadFileChooser jufc = super.createFileChooser();
750        if (getFileChooserImagePreview()) {
751            jufc.setAccessory(new JUploadImagePreview(jufc, this));
752        }
753        return jufc;
754    }
755
756    /**
757     * Returns an icon, calculated from the image content. Currently only
758     * pictures managed by ImageIO can be displayed. Once upon a day, extracting
759     * the first picture of a video may become reality... ;-) <BR>
760     * Note: this method is called in a dedicated thread by the
761     * JUploadFileChooser, to avoid to calculate the icon for all pictures, when
762     * opening a new folder.
763     *
764     * @return The calculated ImageIcon, or null if no picture can be extracted.
765     * @see UploadPolicy#fileViewGetIcon(File)
766     * @see UploadPolicy#PROP_FILE_CHOOSER_ICON_FROM_FILE_CONTENT
767     */
768    @Override
769    public Icon fileViewGetIcon(File file) {
770        try {
771            return PictureFileData.getImageIcon(file, getFileChooserIconSize(),
772                    getFileChooserIconSize());
773        } catch (JUploadException e) {
774            displayErr(e);
775            return null;
776        }
777    }
778
779    /**
780     * @see wjhk.jupload2.policies.DefaultUploadPolicy#getUploadFilename(wjhk.jupload2.filedata.FileData,
781     *      int)
782     */
783    @Override
784    public String getUploadFilename(FileData fileData, int index)
785            throws JUploadException {
786        String fileName = fileData.getFileName();
787        if (!this.keepOrigExtension) {
788            String targetFormatOrNull = this.imageFileConversionInfo
789                    .getTargetFormatOrNull(fileData.getFileExtension());
790            if (targetFormatOrNull != null) {
791
792                int endIndex = fileName.length()
793                        - fileData.getFileExtension().length();
794                StringBuilder newFilename = new StringBuilder(fileName
795                        .substring(0, endIndex));
796                newFilename.append(targetFormatOrNull);
797                fileName = newFilename.toString();
798            }
799        }
800
801        return getEncodedFilename(fileName);
802    }
803
804    /**
805     * Implementation of the ImageObserver interface
806     *
807     * @param arg0
808     * @param arg1
809     * @param arg2
810     * @param arg3
811     * @param arg4
812     * @param arg5
813     * @return true or false
814     */
815    public boolean imageUpdate(Image arg0, int arg1, int arg2, int arg3,
816            int arg4, int arg5) {
817        return true;
818    }
819}
Note: See TracBrowser for help on using the repository browser.