View Javadoc
1   //
2   // $Id: JUploadApplet.java 1376 2010-07-28 21:47:39Z etienne_sf $
3   //
4   // jupload - A file upload applet.
5   // Copyright 2007 The JUpload Team
6   //
7   // Created: ?
8   // Creator: William JinHua Kwong
9   // Last modified: $Date: 2010-07-28 23:47:39 +0200 (mer., 28 juil. 2010) $
10  //
11  // This program is free software; you can redistribute it and/or modify it under
12  // the terms of the GNU General Public License as published by the Free Software
13  // Foundation; either version 2 of the License, or (at your option) any later
14  // version. This program is distributed in the hope that it will be useful, but
15  // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17  // details. You should have received a copy of the GNU General Public License
18  // along with this program; if not, write to the Free Software Foundation, Inc.,
19  // 675 Mass Ave, Cambridge, MA 02139, USA.
20  
21  package wjhk.jupload2;
22  
23  import java.lang.reflect.InvocationTargetException;
24  
25  import javax.swing.JApplet;
26  import javax.swing.JOptionPane;
27  import javax.swing.SwingUtilities;
28  
29  import wjhk.jupload2.context.JUploadContext;
30  import wjhk.jupload2.context.JUploadContextApplet;
31  import wjhk.jupload2.context.JavascriptHandler;
32  import wjhk.jupload2.exception.JUploadException;
33  import wjhk.jupload2.policies.UploadPolicy;
34  
35  /**
36   * The applet. It contains quite only the call to creation of the
37   * {@link JUploadContextApplet}, which contains the technical context. This
38   * context is responsible for loading the relevant {@link UploadPolicy}. <BR>
39   * <BR>
40   * The behavior of the applet can easily be adapted, by : <DIR> <LI>Using an
41   * existing {@link wjhk.jupload2.policies.UploadPolicy}, and specifying
42   * parameters. <LI>Creating a new upload policy, based on the
43   * {@link wjhk.jupload2.policies.DefaultUploadPolicy}, or created from scratch.
44   * <BR>
45   * For all details on this point, please read the <a
46   * href="../../../howto-customization.html">howto-customization.html</a> page.
47   * 
48   * @author William JinHua Kwong (largely updated by etienne_sf)
49   * @version $Revision: 1376 $
50   */
51  public class JUploadApplet extends JApplet {
52  
53  	/** A generated serialVersionUID, to avoid warning during compilation */
54  	private static final long serialVersionUID = -3207851532114846776L;
55  
56  	/**
57  	 * The current execution context.
58  	 */
59  	transient JUploadContext juploadContext = null;
60  
61  	/**
62  	 * Called each time the applet is shown on the web page.
63  	 */
64  	@Override
65  	public void init() {
66  		class JUploadAppletInitializer implements Runnable {
67  			JUploadApplet applet;
68  
69  			JUploadAppletInitializer(JUploadApplet applet) {
70  				this.applet = applet;
71  			}
72  
73  			public void run() {
74  				juploadContext = new JUploadContextApplet(applet);
75  			}
76  		}
77  
78  		try {
79  			SwingUtilities.invokeAndWait(new JUploadAppletInitializer(this));
80  		} catch (InterruptedException e) {
81  			// Hum, if we get here, there may be no logging system built ..
82  			// Let's output something in the Java consoles
83  			e.printStackTrace();
84  		} catch (InvocationTargetException e) {
85  			// Hum, if we get here, there may be no logging system built ..
86  			// Let's output something in the Java consoles
87  			e.printStackTrace();
88  		}
89  
90  		if (this.juploadContext == null) {
91  			JOptionPane
92  					.showMessageDialog(
93  							null,
94  							"An error occured during applet initialization. Please check the java console output",
95  							"Alert", JOptionPane.ERROR_MESSAGE);
96  		} else {
97  			// Let's refresh the display, and have the caret well placed.
98  			try {
99  				this.juploadContext.getUploadPolicy().displayInfo(
100 						"JUploadApplet is now initialized.");
101 			} catch (JUploadException e) {
102 				// Can't use standard JUpload log mode...
103 				System.out.println("JUploadApplet is now initialized.");
104 			}
105 		}
106 	}
107 
108 	/**
109 	 * Called each time the applet is shown on the web page.
110 	 */
111 	@Override
112 	public void start() {
113 		if (this.juploadContext == null) {
114 			String msg = "An error occured during applet initialization. Please check the java console output (juploadContext is null in applet.start())";
115 			JOptionPane.showMessageDialog(null, msg, "Alert",
116 					JOptionPane.ERROR_MESSAGE);
117 			throw new java.lang.IllegalStateException(msg);
118 		} else {
119 			try {
120 				this.juploadContext.getUploadPolicy().start();
121 				this.juploadContext.getUploadPolicy().displayInfo(
122 						"JUploadApplet is now started.");
123 			} catch (JUploadException e) {
124 				// Can't use standard JUpload log mode...
125 				System.out.println("JUploadApplet is now started.");
126 			}
127 			this.validate();
128 		}
129 	}
130 
131 	/**
132 	 * @see java.applet.Applet#stop()
133 	 */
134 	@Override
135 	public void stop() {
136 		try {
137 			this.juploadContext.getUploadPolicy().displayInfo(
138 					"JUploadApplet is now stopped.");
139 		} catch (JUploadException e) {
140 			// Can't use standard JUpload log mode...
141 			System.out.println("JUploadApplet is now stopped.");
142 		}
143 	}
144 
145 	/**
146 	 * @see java.applet.Applet#destroy()
147 	 */
148 	@Override
149 	public void destroy() {
150 		class JUploadAppletDestroyer implements Runnable {
151 			JUploadApplet applet;
152 
153 			JUploadAppletDestroyer(JUploadApplet applet) {
154 				this.applet = applet;
155 			}
156 
157 			public void run() {
158 				applet.juploadContext.runUnload();
159 				applet.getContentPane().removeAll();
160 			}
161 		}
162 		try {
163 			this.juploadContext.getUploadPolicy().displayInfo(
164 					"JUploadApplet is being destroyed.");
165 		} catch (JUploadException e1) {
166 			// Can't use standard JUpload log mode...
167 			System.out.println("JUploadApplet is now destroyed.");
168 		}
169 
170 		// Execute a job on the event-dispatching thread:
171 		// destroying this applet's GUI.
172 		try {
173 			SwingUtilities.invokeAndWait(new JUploadAppletDestroyer(this));
174 		} catch (Exception e) {
175 		}
176 	}
177 
178 	/**
179 	 * This allow runtime modifications of properties, from javascript.
180 	 * Currently, this can only be used after full initialization. This method
181 	 * only calls the UploadPolicy.setProperty method. <BR>
182 	 * Ex: document.jupload.setProperty(prop, value);
183 	 * 
184 	 * @param prop
185 	 *            The property name that must be set.
186 	 * @param value
187 	 *            The value of this property.
188 	 * @see JUploadContext#setProperty(String, String)
189 	 */
190 	public void setProperty(String prop, String value) {
191 		this.juploadContext.setProperty(prop, value);
192 	}
193 
194 	/**
195 	 * Javascript can call this method to start the upload.
196 	 * 
197 	 * @return Returns the upload result. See the constants defined in the
198 	 *         {@link JavascriptHandler} javadoc.
199 	 */
200 	public String startUpload() {
201 		return this.juploadContext.startUpload();
202 	}
203 
204 	/**
205 	 * @see java.applet.Applet#getParameterInfo()
206 	 */
207 	public String[][] getParameterInfo() {
208 		// FIXME Implement Applet.getParameterInfo()
209 		return null;
210 	}
211 
212 	/**
213 	 * @see java.applet.Applet#getAppletInfo()
214 	 */
215 	public String getAppletInfo() {
216 		return "JUpload applet, available at http://jupload.sourceforge.net";
217 	}
218 
219 }