Coverage Report - wjhk.jupload2.policies.SilverpeasUploadPolicy
 
Classes in this File Line Coverage Branch Coverage Complexity
SilverpeasUploadPolicy
0 %
0/76
0 %
0/6
2
 
 1  
 // $Id: SilverpeasUploadPolicy.java 143  2010-01-27 11:01:06 +0100 (mer. 27 janv. 2010) ehsavoie $
 2  
 //
 3  
 // jupload - A file upload applet.
 4  
 // Copyright 2010 The JUpload Team
 5  
 //
 6  
 // Created: 2010-05-14
 7  
 // Creator: ehsavoie
 8  
 // Last modified: $Date: 2010-01-27 11:01:06 +0100 (mer. 27 janv. 2010) $
 9  
 //
 10  
 // This program is free software; you can redistribute it and/or modify it under
 11  
 // the terms of the GNU General Public License as published by the Free Software
 12  
 // Foundation; either version 2 of the License, or (at your option) any later
 13  
 // version. This program is distributed in the hope that it will be useful, but
 14  
 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 15  
 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 16  
 // details. You should have received a copy of the GNU General Public License
 17  
 // along with this program; if not, write to the Free Software Foundation, Inc.,
 18  
 // 675 Mass Ave, Cambridge, MA 02139, USA.
 19  
 package wjhk.jupload2.policies;
 20  
 
 21  
 import java.awt.Color;
 22  
 import java.awt.Component;
 23  
 import java.awt.Dimension;
 24  
 import java.awt.dnd.DropTargetDropEvent;
 25  
 import java.io.BufferedReader;
 26  
 import java.io.CharArrayWriter;
 27  
 import java.io.IOException;
 28  
 import java.io.InputStreamReader;
 29  
 import java.net.HttpURLConnection;
 30  
 import java.net.URL;
 31  
 import javax.swing.BoxLayout;
 32  
 import javax.swing.JLabel;
 33  
 import wjhk.jupload2.context.JUploadContext;
 34  
 import wjhk.jupload2.exception.JUploadException;
 35  
 import wjhk.jupload2.exception.JUploadExceptionTooBigFile;
 36  
 import wjhk.jupload2.gui.JUploadPanel;
 37  
 
 38  
 /**
 39  
  * Personnalised Policy with no button : use only DragAndDrop or Copy/Paste with automatic upload.<br/>
 40  
  * This is given as an example of a personnalized policy to show how JUpload is easy to adapt to your
 41  
  * specific needs.
 42  
  * <ul>
 43  
  *   <li>support for updating the background color.</li>
 44  
  *   <li>load an HTML page to be displayed as a message into the Applet: allows a 'richer' label.</li>
 45  
  *   <li>pre-validation of files size (when they are added, instead of before upload).</li>
 46  
  * </ul>
 47  
  * @author ehsavoie
 48  
  */
 49  
 public class SilverpeasUploadPolicy extends DefaultUploadPolicy {
 50  
 
 51  
   /** */
 52  
   public static final String MESSAGE_URL = "message";
 53  
   /** */
 54  
   public static final String COLOR_RED = "bgcolor_r";
 55  
   /** */
 56  
   public static final String COLOR_BLUE = "bgcolor_b";
 57  
   /** */
 58  
   public static final String COLOR_GREEN = "bgcolor_g";
 59  
   /** */
 60  
   public static final String COLOR_ALPHA = "bgcolor_a";
 61  
 
 62  
   /**
 63  
    * The JUpload constructor for this upload policy. Like all upload policies,
 64  
    * this constructor is called by the {@link UploadPolicyFactory}
 65  
    *
 66  
    * @param juploadContext
 67  
    * @throws JUploadException
 68  
    */
 69  
   public SilverpeasUploadPolicy(JUploadContext juploadContext)
 70  
           throws JUploadException {
 71  0
     super(juploadContext);
 72  0
     setNbFilesPerRequest(20);
 73  0
     displayInfo("Loading SilverpeasUploadPolicy ...");
 74  0
   }
 75  
 
 76  
   /**
 77  
    * This methods allow the upload policy to override the default disposition
 78  
    * of the components on the applet.
 79  
    *
 80  
    * @see UploadPolicy#addComponentsToJUploadPanel(JUploadPanel)
 81  
    */
 82  
   @Override
 83  
   public void addComponentsToJUploadPanel(JUploadPanel jUploadPanel) {
 84  
     // Set the global layout of the panel.
 85  0
     displayInfo("Displaying SilverpeasUploadPolicy ...");
 86  0
     jUploadPanel.getJComponent().setLayout(new BoxLayout(jUploadPanel.getJComponent(),
 87  
             BoxLayout.Y_AXIS));
 88  0
     displayInfo("Accessing the message " + getMessage());
 89  0
     Color color = new Color(getContext().getParameter(COLOR_RED, Color.LIGHT_GRAY.getRed()),
 90  0
             getContext().getParameter(COLOR_GREEN, Color.LIGHT_GRAY.getGreen()),
 91  0
             getContext().getParameter(COLOR_BLUE, Color.LIGHT_GRAY.getBlue()),
 92  0
             getContext().getParameter(COLOR_ALPHA, Color.LIGHT_GRAY.getAlpha()));
 93  0
     jUploadPanel.getJComponent().setBackground(color);
 94  0
     jUploadPanel.getStatusLabel().setBackground(color);
 95  0
     jUploadPanel.getUploadProgressBar().setBackground(color);
 96  
 
 97  0
     jUploadPanel.getStatusLabel().setText(getMessage());
 98  0
     jUploadPanel.getStatusLabel().setHorizontalAlignment(JLabel.CENTER);
 99  0
     jUploadPanel.getStatusLabel().setVerticalAlignment(JLabel.CENTER);
 100  0
     Dimension appletSize = getContext().getApplet().getSize();
 101  0
     Dimension preferredAppletSize = getContext().getApplet().getPreferredSize();
 102  0
     Dimension maxAppletSize = getContext().getApplet().getMaximumSize();
 103  0
     jUploadPanel.getStatusLabel().setAlignmentX(Component.CENTER_ALIGNMENT);
 104  0
     jUploadPanel.getStatusLabel().setAlignmentY(Component.CENTER_ALIGNMENT);
 105  0
     jUploadPanel.getStatusLabel().setPreferredSize(new Dimension(preferredAppletSize.width, preferredAppletSize.height
 106  
             - 25));
 107  0
     jUploadPanel.getStatusLabel().setSize(new Dimension(appletSize.width, appletSize.height - 25));
 108  0
     jUploadPanel.getStatusLabel().setMaximumSize(new Dimension(maxAppletSize.width, maxAppletSize.height
 109  
             - 25));
 110  0
     jUploadPanel.getStatusLabel().setMinimumSize(new Dimension(appletSize.width, appletSize.height
 111  
             - 25));
 112  0
     jUploadPanel.getStatusLabel().setBackground(new Color(DEFAULT_ALBUM_ID));
 113  0
     jUploadPanel.getJComponent().add(jUploadPanel.getStatusLabel());
 114  0
     jUploadPanel.getJComponent().setAlignmentX(Component.CENTER_ALIGNMENT);
 115  0
     jUploadPanel.getUploadProgressBar().setAlignmentX(Component.CENTER_ALIGNMENT);
 116  0
     jUploadPanel.getUploadProgressBar().setAlignmentY(Component.CENTER_ALIGNMENT);
 117  0
     jUploadPanel.getUploadProgressBar().setPreferredSize(
 118  
             new Dimension(preferredAppletSize.width, 20));
 119  0
     jUploadPanel.getUploadProgressBar().setSize(new Dimension(appletSize.width, 20));
 120  0
     jUploadPanel.getUploadProgressBar().setMaximumSize(new Dimension(maxAppletSize.width, 20));
 121  0
     jUploadPanel.getUploadProgressBar().setMinimumSize(new Dimension(appletSize.width, 20));
 122  0
     jUploadPanel.getUploadProgressBar().setStringPainted(false);
 123  0
     jUploadPanel.getUploadProgressBar().setBorderPainted(false);
 124  
     // Then, add on the screen of the only component that is visible.
 125  0
     jUploadPanel.getJComponent().add(jUploadPanel.getUploadProgressBar());
 126  
     // Now, we add the log window.
 127  0
     jUploadPanel.showOrHideLogWindow();
 128  0
     jUploadPanel.getJComponent().add(jUploadPanel.getJLogWindowPane());
 129  0
     displayInfo("Displaying SilverpeasUploadPolicy ...");
 130  0
   }
 131  
 
 132  
   /**
 133  
    * Loads the message from the specified URL.
 134  
    * @return the remote message.
 135  
    */
 136  
   protected String getMessage() {
 137  0
     displayInfo("The message to be displayed is " + juploadContext.getParameter(MESSAGE_URL, ""));
 138  0
     String urlContent = juploadContext.getParameter(MESSAGE_URL, "");
 139  0
     String content = urlContent;
 140  0
     HttpURLConnection connection = null;
 141  0
     BufferedReader in = null;
 142  
     try {
 143  0
       URL url = new URL(urlContent);
 144  0
       connection = (HttpURLConnection) url.openConnection();
 145  0
       connection.connect();
 146  0
       in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
 147  0
       displayInfo("Getting data ...");
 148  0
       CharArrayWriter text = new CharArrayWriter();
 149  0
       int size = 0;
 150  0
       char[] buffer = new char[8];
 151  0
       while ((size = in.read(buffer)) >= 0) {
 152  0
         text.write(buffer, 0, size);
 153  
       }
 154  0
       content = text.toString();
 155  0
     } catch (IOException ioex) {
 156  0
       displayErr(ioex);
 157  0
     } finally {
 158  0
       try {
 159  0
         if (in != null) {
 160  0
           in.close();
 161  
         }
 162  0
       } catch (IOException ex) {
 163  0
         displayErr(ex);
 164  0
       }
 165  0
       connection.disconnect();
 166  0
     }
 167  0
     return content;
 168  
   }
 169  
 
 170  
   /**
 171  
    * Default reaction after a successful drop operation: no action.
 172  
    *
 173  
    * @see UploadPolicy#afterFileDropped(DropTargetDropEvent)
 174  
    */
 175  
   @Override
 176  
   public void afterFileDropped(DropTargetDropEvent dropEvent) {
 177  0
     getContext().getUploadPanel().doStartUpload();
 178  0
   }
 179  
 
 180  
   /**
 181  
    * @param description
 182  
    * @see UploadPolicy#sendDebugInformation(String, Exception) */
 183  
   @Override
 184  
   public void sendDebugInformation(String description, Exception exception) {
 185  0
     if (exception instanceof JUploadExceptionTooBigFile) {
 186  0
        getContext().getUploadPanel().getFilePanel().removeAll();
 187  
     }
 188  0
     super.sendDebugInformation(description, exception);
 189  0
   }
 190  
 }