Coverage Report - wjhk.jupload2.JUploadApplet
 
Classes in this File Line Coverage Branch Coverage Complexity
JUploadApplet
4 %
2/45
0 %
0/4
1,833
JUploadApplet$1JUploadAppletDestroyer
0 %
0/6
N/A
1,833
JUploadApplet$1JUploadAppletInitializer
0 %
0/5
N/A
1,833
 
 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  7
 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  7
         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  0
                         JUploadAppletInitializer(JUploadApplet applet) {
 70  0
                                 this.applet = applet;
 71  0
                         }
 72  
 
 73  
                         public void run() {
 74  0
                                 juploadContext = new JUploadContextApplet(applet);
 75  0
                         }
 76  
                 }
 77  
 
 78  
                 try {
 79  0
                         SwingUtilities.invokeAndWait(new JUploadAppletInitializer(this));
 80  0
                 } 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  0
                         e.printStackTrace();
 84  0
                 } 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  0
                         e.printStackTrace();
 88  0
                 }
 89  
 
 90  0
                 if (this.juploadContext == null) {
 91  0
                         JOptionPane
 92  0
                                         .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  0
                                 this.juploadContext.getUploadPolicy().displayInfo(
 100  
                                                 "JUploadApplet is now initialized.");
 101  0
                         } catch (JUploadException e) {
 102  
                                 // Can't use standard JUpload log mode...
 103  0
                                 System.out.println("JUploadApplet is now initialized.");
 104  0
                         }
 105  
                 }
 106  0
         }
 107  
 
 108  
         /**
 109  
          * Called each time the applet is shown on the web page.
 110  
          */
 111  
         @Override
 112  
         public void start() {
 113  0
                 if (this.juploadContext == null) {
 114  0
                         String msg = "An error occured during applet initialization. Please check the java console output (juploadContext is null in applet.start())";
 115  0
                         JOptionPane.showMessageDialog(null, msg, "Alert",
 116  
                                         JOptionPane.ERROR_MESSAGE);
 117  0
                         throw new java.lang.IllegalStateException(msg);
 118  
                 } else {
 119  
                         try {
 120  0
                                 this.juploadContext.getUploadPolicy().start();
 121  0
                                 this.juploadContext.getUploadPolicy().displayInfo(
 122  
                                                 "JUploadApplet is now started.");
 123  0
                         } catch (JUploadException e) {
 124  
                                 // Can't use standard JUpload log mode...
 125  0
                                 System.out.println("JUploadApplet is now started.");
 126  0
                         }
 127  0
                         this.validate();
 128  
                 }
 129  0
         }
 130  
 
 131  
         /**
 132  
          * @see java.applet.Applet#stop()
 133  
          */
 134  
         @Override
 135  
         public void stop() {
 136  
                 try {
 137  0
                         this.juploadContext.getUploadPolicy().displayInfo(
 138  
                                         "JUploadApplet is now stopped.");
 139  0
                 } catch (JUploadException e) {
 140  
                         // Can't use standard JUpload log mode...
 141  0
                         System.out.println("JUploadApplet is now stopped.");
 142  0
                 }
 143  0
         }
 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  0
                         JUploadAppletDestroyer(JUploadApplet applet) {
 154  0
                                 this.applet = applet;
 155  0
                         }
 156  
 
 157  
                         public void run() {
 158  0
                                 applet.juploadContext.runUnload();
 159  0
                                 applet.getContentPane().removeAll();
 160  0
                         }
 161  
                 }
 162  
                 try {
 163  0
                         this.juploadContext.getUploadPolicy().displayInfo(
 164  
                                         "JUploadApplet is being destroyed.");
 165  0
                 } catch (JUploadException e1) {
 166  
                         // Can't use standard JUpload log mode...
 167  0
                         System.out.println("JUploadApplet is now destroyed.");
 168  0
                 }
 169  
 
 170  
                 // Execute a job on the event-dispatching thread:
 171  
                 // destroying this applet's GUI.
 172  
                 try {
 173  0
                         SwingUtilities.invokeAndWait(new JUploadAppletDestroyer(this));
 174  0
                 } catch (Exception e) {
 175  0
                 }
 176  0
         }
 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  0
                 this.juploadContext.setProperty(prop, value);
 192  0
         }
 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  0
                 return this.juploadContext.startUpload();
 202  
         }
 203  
 
 204  
         /**
 205  
          * @see java.applet.Applet#getParameterInfo()
 206  
          */
 207  
         public String[][] getParameterInfo() {
 208  
                 // FIXME Implement Applet.getParameterInfo()
 209  0
                 return null;
 210  
         }
 211  
 
 212  
         /**
 213  
          * @see java.applet.Applet#getAppletInfo()
 214  
          */
 215  
         public String getAppletInfo() {
 216  0
                 return "JUpload applet, available at http://jupload.sourceforge.net";
 217  
         }
 218  
 
 219  
 }