001: /*
002: * de.jwic.controls.FileUploadControl
003: */
004: package de.jwic.controls;
005:
006: import java.io.IOException;
007: import java.io.InputStream;
008: import java.util.ArrayList;
009: import java.util.Iterator;
010: import java.util.List;
011:
012: import de.jwic.base.Control;
013: import de.jwic.base.Field;
014: import de.jwic.base.IControlContainer;
015: import de.jwic.base.IFileReciever;
016: import de.jwic.events.FileReceivedEvent;
017: import de.jwic.events.FileReceivedListener;
018: import de.jwic.upload.UploadFile;
019:
020: /**
021: * This control displays a field to upload a file. The file is saved and a resource
022: * is created when a file is recieved from the client.<br>
023: *
024: * A user of the control must set the temporary flag to <b>false</b> if the file is
025: * used and should not be deleted when the FileUploadControl is destroyed.
026: *
027: * @author Florian Lippisch
028: */
029: public class FileUploadControl extends Control implements IFileReciever {
030:
031: private static final long serialVersionUID = 1L;
032:
033: protected UploadFile fileHandle = null;
034: /** List of listender to inform */
035: protected List selectionListeners = null;
036:
037: private String width = "";
038:
039: /**
040: * @param container
041: */
042: public FileUploadControl(IControlContainer container) {
043: this (container, null);
044: }
045:
046: /**
047: * @param container
048: * @param name
049: */
050: public FileUploadControl(IControlContainer container, String name) {
051: super (container, name);
052: new Field(this , "file"); // create a field that stores the filename.
053: }
054:
055: /**
056: * Add a listener to the file received event.
057: * @param listener
058: */
059: public void addFileReceivedListener(FileReceivedListener listener) {
060: if (selectionListeners == null) {
061: selectionListeners = new ArrayList();
062: }
063: selectionListeners.add(listener);
064: }
065:
066: /**
067: * Send the file received event to the registerd listeners.
068: */
069: protected void sendFileReceivedEvent() {
070:
071: if (selectionListeners != null) {
072: FileReceivedEvent e = new FileReceivedEvent(this ,
073: fileHandle);
074: for (Iterator it = selectionListeners.iterator(); it
075: .hasNext();) {
076: FileReceivedListener listener = (FileReceivedListener) it
077: .next();
078: listener.fileReceived(e);
079: }
080: }
081:
082: }
083:
084: /**
085: * Handle the UploadFile.
086: * @param req
087: * @param filename
088: * @param file
089: */
090: public void handleFile(String fieldname, UploadFile file) {
091: log.debug("handleFile (" + fieldname + ", " + file.getName()
092: + ")");
093: // destroy previous uploaded file
094: reset();
095: fileHandle = file;
096: sendFileReceivedEvent();
097: }
098:
099: /**
100: * Returns the name of the file that has been uploaded to this control. Returns
101: * <code>null</code> if no file has been uploaded yet.
102: * @return
103: */
104: public String getFileName() {
105: return fileHandle != null ? fileHandle.getName() : null;
106: }
107:
108: /**
109: * Returns an InputStream of the uploaded file.
110: * <code>null</code> if no file has been uploaded yet.
111: * @return
112: * @throws IOException
113: */
114: public InputStream getInputStream() throws IOException {
115: return fileHandle != null ? fileHandle.getInputStream() : null;
116: }
117:
118: /* (non-Javadoc)
119: * @see de.jwic.base.Control#actionPerformed(java.lang.String, java.lang.String)
120: */
121: public void actionPerformed(String actionId, String parameter) {
122:
123: if (actionId.equals("discard") && fileHandle != null) {
124: reset();
125: }
126: }
127:
128: /**
129: * Resets this FileUploadControl.
130: */
131: public void reset() {
132: if (fileHandle != null) {
133: fileHandle.destroy();
134: fileHandle = null;
135: requireRedraw();
136: }
137: }
138:
139: /* (non-Javadoc)
140: * @see de.jwic.base.IControl#destroy()
141: */
142: public void destroy() {
143: reset();
144: super .destroy();
145: }
146:
147: /**
148: * @return Returns the width.
149: */
150: public String getWidth() {
151: return width;
152: }
153:
154: /**
155: * The width is set as a css style argument. If you specify the width
156: * in pixel, you must add 'px' at the end. Otherwise its no valid css
157: * parameter.
158: * @param width The width to set.
159: */
160: public void setWidth(String width) {
161: this .width = width;
162: }
163:
164: /**
165: * Returns the size of the uploaded file or -1 if no file has been uploaded yet.
166: * @return
167: */
168: public long getFileSize() {
169: return fileHandle != null ? fileHandle.length() : -1;
170: }
171:
172: /**
173: * Returns true if a file has been assigned to this control.
174: * @return
175: */
176: public boolean isFileUploaded() {
177: return fileHandle != null;
178: }
179: }
|