wjhk.jupload2.filedata
Class PictureFileData

java.lang.Object
  extended by wjhk.jupload2.filedata.DefaultFileData
      extended by wjhk.jupload2.filedata.PictureFileData
All Implemented Interfaces:
FileData

public class PictureFileData
extends DefaultFileData

This class contains all data about files to upload as a picture. It adds the following elements to the FileData class :

Version:
$Revision: 920 $
Author:
etienne_sf

Field Summary
(package private)  int originalHeight
          Same as originalWidth, for the height of the first image in the picture file.
(package private)  int originalWidth
          Width of the original picture.
(package private)  int quarterRotation
          quarterRotation contains the current rotation that will be applied to the picture.
 
Fields inherited from class wjhk.jupload2.filedata.DefaultFileData
mimeType, preparedForUpload, uploadPolicy
 
Constructor Summary
PictureFileData(File file, File root, PictureUploadPolicy uploadPolicy)
          Standard constructor: needs a PictureFileDataPolicy.
 
Method Summary
 void addRotation(int quarter)
          This function is used to rotate the picture.
 void afterUpload()
          Cleaning of the temporary file on the hard drive, if any.
 void beforeUpload()
          If this pictures needs transformation, a temporary file is created.
(package private)  void createTranformedPictureFile(wjhk.jupload2.filedata.helper.ImageHelper imageHelper)
          Creates a transformed picture file of the given max width and max height.
 void deleteTransformedPictureFile()
          File.deleteOnExit() is pretty unreliable, especially in applets.
 void deleteWorkingCopyPictureFile()
          File.deleteOnExit() is pretty unreliable, especially in applets.
static void freeMemory(String caller, UploadPolicy uploadPolicy)
          Free any available memory.
 Image getImage(Canvas canvas, boolean shadow)
          This method creates a new Image, from the current picture.
static ImageIcon getImageIcon(File pictureFile, int maxWidth, int maxHeight)
          Returns an ImageIcon for the given file, resized according to the given dimensions.
 InputStream getInputStream()
          This function create an input stream for this file.
 String getMimeType()
          This function return the FileData content type.
 int getOriginalHeight()
           
 int getOriginalWidth()
           
 long getUploadLength()
          Returns the number of bytes, for this upload.
 File getWorkingSourceFile()
          Get the file that contains the original picture.
static boolean isFileAPictrue(File file)
          Indicates whether a file is a picture or not.
 boolean isPicture()
          Indicates if this file is actually a picture or not.
 
Methods inherited from class wjhk.jupload2.filedata.DefaultFileData
appendFileProperties, canRead, getDirectory, getExtension, getFile, getFileExtension, getFileLength, getFileName, getLastModified, getRelativeDir, getRoot, isPreparedForUpload
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

quarterRotation

int quarterRotation
quarterRotation contains the current rotation that will be applied to the picture. Its value should be one of 0, 1, 2, 3. It is controled by the addRotation(int) method.


originalWidth

int originalWidth
Width of the original picture. The width is taken from the first image of the file. We expect that all pictures in a file are of the same size (for instance, for animated gif). Calculated in the PictureFileData(File, File, PictureUploadPolicy) constructor.


originalHeight

int originalHeight
Same as originalWidth, for the height of the first image in the picture file.

Constructor Detail

PictureFileData

public PictureFileData(File file,
                       File root,
                       PictureUploadPolicy uploadPolicy)
                throws JUploadIOException
Standard constructor: needs a PictureFileDataPolicy.

Parameters:
file - The files which data are to be handled by this instance.
root - The root directory, to calculate the relative dir (see DefaultFileData.getRelativeDir().
uploadPolicy - The current upload policy
Throws:
JUploadIOException - Encapsulation of the IOException, if any would occurs.
Method Detail

freeMemory

public static void freeMemory(String caller,
                              UploadPolicy uploadPolicy)
Free any available memory. This method is called very often here, to be sure that we don't use too much memory. But we still run out of memory in some case.

Parameters:
caller - Indicate the method or treatment from which this method is called.
uploadPolicy - The current upload policy is not available, to this static method...

beforeUpload

public void beforeUpload()
                  throws JUploadException
If this pictures needs transformation, a temporary file is created. This can occurs if the original picture is bigger than the maxWidth or maxHeight, of if it has to be rotated. This temporary file contains the transformed picture.
The call to this method is optional, if the caller calls getUploadLength(). This method calls beforeUpload() if the uploadLength is unknown.

Specified by:
beforeUpload in interface FileData
Overrides:
beforeUpload in class DefaultFileData
Throws:
JUploadException - Encapsulation of the Exception, if any would occurs.
See Also:
FileUploadThread

getUploadLength

public long getUploadLength()
Returns the number of bytes, for this upload. If needed, that is, if uploadlength is unknown, beforeUpload() is called.

Specified by:
getUploadLength in interface FileData
Overrides:
getUploadLength in class DefaultFileData
Returns:
The length of upload. In this class, this is ... the size of the original file, or the transformed file!
See Also:
PictureFileData

getInputStream

public InputStream getInputStream()
                           throws JUploadException
This function create an input stream for this file. The caller is responsible for closing this input stream.
This function assumes that the getUploadLength() method has already be called : it is responsible for creating the temporary file (if needed). If not called, the original file will be sent.

Specified by:
getInputStream in interface FileData
Overrides:
getInputStream in class DefaultFileData
Returns:
An inputStream
Throws:
JUploadException - Encapsulation of the Exception, if any would occurs.

afterUpload

public void afterUpload()
Cleaning of the temporary file on the hard drive, if any.
Note: if the debugLevel is 100 (or more) this temporary file is not removed. This allow control of this created file.

Specified by:
afterUpload in interface FileData
Overrides:
afterUpload in class DefaultFileData

getImage

public Image getImage(Canvas canvas,
                      boolean shadow)
               throws JUploadException
This method creates a new Image, from the current picture. The resulting width and height will be less or equal than the given maximum width and height. The scale is maintained. Thus the width or height may be inferior than the given values.

Parameters:
canvas - The canvas on which the picture will be displayed.
shadow - True if the pictureFileData should store this picture. False if the pictureFileData instance should not store this picture. Store this picture avoid calculating the image each time the user selects it in the file panel.
Returns:
The rescaled image.
Throws:
JUploadException - Encapsulation of the Exception, if any would occurs.

addRotation

public void addRotation(int quarter)
This function is used to rotate the picture. The current rotation state is kept in the quarterRotation private attribute.

Parameters:
quarter - Number of quarters (90 degrees) the picture should rotate. 1 means rotating of 90 degrees clockwise. Can be negative.

isPicture

public boolean isPicture()
Indicates if this file is actually a picture or not.

Returns:
the isPicture flag.

getMimeType

public String getMimeType()
Description copied from class: DefaultFileData
This function return the FileData content type.

Specified by:
getMimeType in interface FileData
Overrides:
getMimeType in class DefaultFileData
Returns:
The mimeType for the file.
See Also:
FileData.getMimeType()

deleteTransformedPictureFile

public void deleteTransformedPictureFile()
File.deleteOnExit() is pretty unreliable, especially in applets. Therefore the applet provides a callback which is executed during applet termination. This method performs the actual cleanup.


createTranformedPictureFile

void createTranformedPictureFile(wjhk.jupload2.filedata.helper.ImageHelper imageHelper)
                           throws JUploadException
Creates a transformed picture file of the given max width and max height. If the transformedPictureFile attribute is not set before calling this method, it will be set. If set before, the existing transformedPictureFile is replaced by the newly transformed picture file. It is cleared if an error occured.

Parameters:
imageHelper - The ImageHelper that was initialized with current parameters.
Throws:
JUploadException

deleteWorkingCopyPictureFile

public void deleteWorkingCopyPictureFile()
File.deleteOnExit() is pretty unreliable, especially in applets. Therefore the applet provides a callback which is executed during applet termination. This method performs the actual cleanup.


getWorkingSourceFile

public File getWorkingSourceFile()
                          throws JUploadIOException
Get the file that contains the original picture. This is used as a workaround for the following JVM bug: once in the navigator, it can't transform picture read from a file whose name contains non-ASCII characters, like French accents.

Returns:
The file that contains the original picture, as the source for picture transformation
Throws:
JUploadIOException

getOriginalWidth

public int getOriginalWidth()
                     throws JUploadIOException
Returns:
the originalWidth of the picture
Throws:
JUploadIOException

getOriginalHeight

public int getOriginalHeight()
                      throws JUploadIOException
Returns:
the originalHeight of the picture
Throws:
JUploadIOException

getImageIcon

public static ImageIcon getImageIcon(File pictureFile,
                                     int maxWidth,
                                     int maxHeight)
Returns an ImageIcon for the given file, resized according to the given dimensions. If the original file contains a pictures smaller than these width and height, the picture is returned as is (nor resized).

Parameters:
pictureFile - The file, containing a picture, from which the user wants to extract a static picture.
maxWidth - The maximum allowed width for the static picture to generate.
maxHeight - The maximum allowed height for the static picture to generate.
Returns:
The created static picture, or null if the file is null.

isFileAPictrue

public static boolean isFileAPictrue(File file)
Indicates whether a file is a picture or not. The information is based on the fact the an ImageRead is found, or not, for this file. This test uses the core Java API. As in the windows world, file extension may be in uppercase, the test is based on the lowercase value for the given file extension.

Parameters:
file -
Returns:
true if the file can be opened as a picture, false otherwise.