Coverage Report - wjhk.jupload2.gui.DnDListener
 
Classes in this File Line Coverage Branch Coverage Complexity
DnDListener
12 %
5/39
0 %
0/12
2,667
 
 1  
 //
 2  
 // $Id: DnDListener.java 1715 2015-03-10 19:59:42Z 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: 2015-03-10 20:59:42 +0100 (mar., 10 mars 2015) $
 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.gui;
 22  
 
 23  
 import java.awt.datatransfer.DataFlavor;
 24  
 import java.awt.datatransfer.UnsupportedFlavorException;
 25  
 import java.awt.dnd.DnDConstants;
 26  
 import java.awt.dnd.DropTargetDragEvent;
 27  
 import java.awt.dnd.DropTargetDropEvent;
 28  
 import java.awt.dnd.DropTargetEvent;
 29  
 import java.awt.dnd.DropTargetListener;
 30  
 import java.io.File;
 31  
 import java.io.IOException;
 32  
 import java.net.URI;
 33  
 import java.net.URISyntaxException;
 34  
 import java.util.ArrayList;
 35  
 import java.util.List;
 36  
 
 37  
 import wjhk.jupload2.policies.UploadPolicy;
 38  
 
 39  
 /**
 40  
  * Our implementation of DND.
 41  
  * 
 42  
  * @author William JinHua Kwong
 43  
  * @version $Release$
 44  
  */
 45  
 public class DnDListener implements DropTargetListener {
 46  
 
 47  
     private JUploadPanel uploadPanel;
 48  
 
 49  
     private UploadPolicy uploadPolicy;
 50  
 
 51  
     /**
 52  
      * Creates a new instance.
 53  
      * 
 54  
      * @param uploadPanel The corresponding upload panel.
 55  
      * @param uploadPolicy
 56  
      */
 57  33
     public DnDListener(JUploadPanel uploadPanel, UploadPolicy uploadPolicy) {
 58  33
         this.uploadPanel = uploadPanel;
 59  33
         this.uploadPolicy = uploadPolicy;
 60  33
     }
 61  
 
 62  
     // DataFlavor javaFileListFlavor = createConstant("application/x-java-file-list;class=java.util.List", null);
 63  
 
 64  33
     DataFlavor uriListFlavor = new DataFlavor("text/uri-list;class=java.lang.String", null);
 65  
 
 66  
     /**
 67  
      * @see java.awt.dnd.DropTargetListener#dragEnter(java.awt.dnd.DropTargetDragEvent)
 68  
      */
 69  
     public void dragEnter(DropTargetDragEvent e) {
 70  
         // System.out.println(e);
 71  
         // System.out.println(e.getCurrentDataFlavorsAsList());
 72  
 
 73  0
         if (!e.isDataFlavorSupported(DataFlavor.javaFileListFlavor) && !e.isDataFlavorSupported(uriListFlavor)) {
 74  0
             e.rejectDrag();
 75  
         }
 76  0
     }
 77  
 
 78  
     /**
 79  
      * @see java.awt.dnd.DropTargetListener#dragOver(java.awt.dnd.DropTargetDragEvent)
 80  
      */
 81  
     public void dragOver(DropTargetDragEvent e) {
 82  
         // Nothing to do.
 83  0
     }
 84  
 
 85  
     /**
 86  
      * @see java.awt.dnd.DropTargetListener#dropActionChanged(java.awt.dnd.DropTargetDragEvent)
 87  
      */
 88  
     public void dropActionChanged(DropTargetDragEvent e) {
 89  
         // Nothing to do.
 90  0
     }
 91  
 
 92  
     /**
 93  
      * @see java.awt.dnd.DropTargetListener#drop(java.awt.dnd.DropTargetDropEvent)
 94  
      */
 95  
     @SuppressWarnings("unchecked")
 96  
     public void drop(DropTargetDropEvent e) {
 97  0
         if (!e.isDataFlavorSupported(DataFlavor.javaFileListFlavor) && !e.isDataFlavorSupported(uriListFlavor)) {
 98  0
             e.rejectDrop();
 99  
         } else {
 100  0
             e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
 101  
             try {
 102  
                 File[] fileArray;
 103  
 
 104  0
                 if (e.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
 105  0
                     List<File> fileList = (List<File>) e.getTransferable().getTransferData(
 106  
                             DataFlavor.javaFileListFlavor);
 107  
 
 108  0
                     fileArray = (File[]) (fileList.toArray());
 109  0
                 } else {
 110  
                     // uriListFlavor; attempt to convert URIs to files
 111  0
                     String strList = (String) e.getTransferable().getTransferData(uriListFlavor);
 112  
                     // System.out.println(strList);
 113  0
                     List<File> fileList = new ArrayList<File>();
 114  0
                     for (String s : strList.split("\n"))
 115  
                         try {
 116  0
                             fileList.add(new File(new URI(s.trim())));
 117  0
                         } catch (URISyntaxException ex) {
 118  0
                             ex.printStackTrace();
 119  0
                             e.rejectDrop();
 120  0
                             return;
 121  0
                         }
 122  0
                     fileArray = (File[]) (fileList.toArray(new File[fileList.size()]));
 123  
                     // System.out.println("fileArray="+Arrays.toString(fileArray));
 124  
                 }
 125  0
                 this.uploadPanel.getFilePanel().addFiles(fileArray);
 126  
 
 127  0
                 e.getDropTargetContext().dropComplete(true);
 128  
 
 129  
                 // Let's communicate this to the upload policy: there may be
 130  
                 // something to do now.
 131  0
                 this.uploadPolicy.afterFileDropped(e);
 132  
 
 133  0
             } catch (IOException ioe) {
 134  0
                 this.uploadPolicy.displayErr("DnDListener.drop()", ioe);
 135  0
                 e.rejectDrop();
 136  0
             } catch (UnsupportedFlavorException ufe) {
 137  0
                 this.uploadPolicy.displayErr("DnDListener.drop()", ufe);
 138  0
                 e.rejectDrop();
 139  0
             }
 140  
 
 141  
         }
 142  0
     }
 143  
 
 144  
     /**
 145  
      * @see java.awt.dnd.DropTargetListener#dragExit(java.awt.dnd.DropTargetEvent)
 146  
      */
 147  
     public void dragExit(DropTargetEvent e) {
 148  
         // Nothing to do.
 149  0
     }
 150  
 }