Coverage Report - wjhk.jupload2.gui.JUploadTransferHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
JUploadTransferHandler
19 %
9/46
0 %
0/20
3,667
 
 1  
 package wjhk.jupload2.gui;
 2  
 
 3  
 /**
 4  
  * The JUploadTransferHandler allows easy management of pasted files onto the applet. It just checks that the pasted
 5  
  * selection is compatible (that is: it's a file list), and calls the addFile methods, to let the core applet work.
 6  
  */
 7  
 import java.awt.datatransfer.DataFlavor;
 8  
 import java.awt.datatransfer.Transferable;
 9  
 import java.awt.datatransfer.UnsupportedFlavorException;
 10  
 import java.io.BufferedReader;
 11  
 import java.io.File;
 12  
 import java.io.IOException;
 13  
 import java.io.Reader;
 14  
 import java.net.URI;
 15  
 import java.net.URISyntaxException;
 16  
 import java.util.ArrayList;
 17  
 import java.util.List;
 18  
 
 19  
 import javax.swing.JComponent;
 20  
 import javax.swing.TransferHandler;
 21  
 
 22  
 import wjhk.jupload2.policies.UploadPolicy;
 23  
 
 24  
 class JUploadTransferHandler extends TransferHandler {
 25  
 
 26  
     /** A generated serialVersionUID, to avoid warning during compilation */
 27  
     private static final long serialVersionUID = -1241261479500810699L;
 28  
 
 29  33
     DataFlavor fileListFlavor = DataFlavor.javaFileListFlavor;
 30  
 
 31  
     /** Specific data flavor for Linux where the clipboard contains URLs to files instead of the files themselves */
 32  
     DataFlavor uriListFlavor;
 33  
 
 34  
     /**
 35  
      * The JUpload panel for this applet.
 36  
      */
 37  33
     JUploadPanel uploadPanel = null;
 38  
 
 39  
     /**
 40  
      * The current upload policy.
 41  
      */
 42  33
     UploadPolicy uploadPolicy = null;
 43  
 
 44  
     /**
 45  
      * The standard constructor.
 46  
      *
 47  
      * @param uploadPolicy The current uploadPolicy
 48  
      * @param uploadPanel The JUploadPanel. Must given here, as this constructor is called in the JUploadPanel
 49  
      *            construction. So the uploadPolicy.getUploadPanel() returns null.
 50  
      */
 51  33
     public JUploadTransferHandler(UploadPolicy uploadPolicy, JUploadPanel uploadPanel) {
 52  33
         this.uploadPolicy = uploadPolicy;
 53  33
         this.uploadPanel = uploadPanel;
 54  
         try {
 55  33
             this.uriListFlavor = new DataFlavor("text/uri-list;class=java.lang.String");
 56  0
         } catch (ClassNotFoundException ex) {
 57  0
             this.uriListFlavor = DataFlavor.javaFileListFlavor;
 58  33
         }
 59  33
     }
 60  
 
 61  
     /**
 62  
      * @see javax.swing.TransferHandler#importData(javax.swing.JComponent, java.awt.datatransfer.Transferable)
 63  
      */
 64  
     @Override
 65  
     @SuppressWarnings("unchecked")
 66  
     public boolean importData(JComponent c, Transferable t) {
 67  0
         DataFlavor[] flavors = t.getTransferDataFlavors();
 68  0
         boolean importAccepted = false;
 69  0
         if (canImport(c, flavors)) {
 70  
             try {
 71  0
                 List<File> fileList = new ArrayList<File>();
 72  0
                 if (isFile(flavors)) {
 73  0
                     fileList = (List<File>) t.getTransferData(this.fileListFlavor);
 74  0
                     importAccepted = true;
 75  0
                 } else if (isUrl(flavors)) {
 76  0
                     Reader in = uriListFlavor.getReaderForText(t);
 77  0
                     BufferedReader br = new BufferedReader(in);
 78  
                     String uriStr;
 79  0
                     while ((uriStr = br.readLine()) != null) {
 80  
                         try {
 81  0
                             fileList.add(new File(new URI(uriStr)));
 82  0
                         } catch (URISyntaxException use) {
 83  0
                             this.uploadPolicy.displayErr(this.getClass().getName() + ".importData()", use);
 84  0
                         }
 85  
                     }
 86  0
                     importAccepted = true;
 87  
                 }
 88  0
                 File[] fileArray = fileList.toArray(new File[fileList.size()]);
 89  0
                 this.uploadPanel.getFilePanel().addFiles(fileArray);
 90  0
                 return importAccepted;
 91  0
             } catch (UnsupportedFlavorException ufe) {
 92  0
                 this.uploadPolicy.displayErr(this.getClass().getName() + ".importData()", ufe);
 93  0
             } catch (IOException ioe) {
 94  0
                 this.uploadPolicy.displayErr(this.getClass().getName() + ".importData()", ioe);
 95  0
             }
 96  
         }
 97  
 
 98  0
         return importAccepted;
 99  
     }
 100  
 
 101  
     /**
 102  
      * @see javax.swing.TransferHandler#getSourceActions(javax.swing.JComponent)
 103  
      */
 104  
     @Override
 105  
     public int getSourceActions(JComponent c) {
 106  0
         return MOVE;
 107  
     }
 108  
 
 109  
     /**
 110  
      * @see javax.swing.TransferHandler#canImport(javax.swing.JComponent, java.awt.datatransfer.DataFlavor[])
 111  
      */
 112  
     @Override
 113  
     public boolean canImport(JComponent c, DataFlavor[] flavors) {
 114  0
         return isFile(flavors) || isUrl(flavors);
 115  
     }
 116  
 
 117  
     /**
 118  
      * Indicates if this data flavor is for a File type of data.
 119  
      * 
 120  
      * @param flavors the flavors
 121  
      * @return true if the data is of type file.
 122  
      */
 123  
     protected boolean isFile(DataFlavor[] flavors) {
 124  0
         for (DataFlavor flavor : flavors) {
 125  0
             if (this.fileListFlavor.equals(flavor)) {
 126  0
                 return true;
 127  
             }
 128  
         }
 129  0
         return false;
 130  
     }
 131  
 
 132  
     /**
 133  
      * Indicates if this data flavor is for a URL type of data.
 134  
      * 
 135  
      * @param flavors the flavors
 136  
      * @return true if the data is of type url.
 137  
      */
 138  
     protected boolean isUrl(DataFlavor[] flavors) {
 139  0
         for (DataFlavor flavor : flavors) {
 140  0
             if (this.uriListFlavor.equals(flavor)) {
 141  0
                 return true;
 142  
             }
 143  
         }
 144  0
         return false;
 145  
     }
 146  
 }