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