View Javadoc
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      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      JUploadPanel uploadPanel = null;
38  
39      /**
40       * The current upload policy.
41       */
42      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      public JUploadTransferHandler(UploadPolicy uploadPolicy, JUploadPanel uploadPanel) {
52          this.uploadPolicy = uploadPolicy;
53          this.uploadPanel = uploadPanel;
54          try {
55              this.uriListFlavor = new DataFlavor("text/uri-list;class=java.lang.String");
56          } catch (ClassNotFoundException ex) {
57              this.uriListFlavor = DataFlavor.javaFileListFlavor;
58          }
59      }
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          DataFlavor[] flavors = t.getTransferDataFlavors();
68          boolean importAccepted = false;
69          if (canImport(c, flavors)) {
70              try {
71                  List<File> fileList = new ArrayList<File>();
72                  if (isFile(flavors)) {
73                      fileList = (List<File>) t.getTransferData(this.fileListFlavor);
74                      importAccepted = true;
75                  } else if (isUrl(flavors)) {
76                      Reader in = uriListFlavor.getReaderForText(t);
77                      BufferedReader br = new BufferedReader(in);
78                      String uriStr;
79                      while ((uriStr = br.readLine()) != null) {
80                          try {
81                              fileList.add(new File(new URI(uriStr)));
82                          } catch (URISyntaxException use) {
83                              this.uploadPolicy.displayErr(this.getClass().getName() + ".importData()", use);
84                          }
85                      }
86                      importAccepted = true;
87                  }
88                  File[] fileArray = fileList.toArray(new File[fileList.size()]);
89                  this.uploadPanel.getFilePanel().addFiles(fileArray);
90                  return importAccepted;
91              } catch (UnsupportedFlavorException ufe) {
92                  this.uploadPolicy.displayErr(this.getClass().getName() + ".importData()", ufe);
93              } catch (IOException ioe) {
94                  this.uploadPolicy.displayErr(this.getClass().getName() + ".importData()", ioe);
95              }
96          }
97  
98          return importAccepted;
99      }
100 
101     /**
102      * @see javax.swing.TransferHandler#getSourceActions(javax.swing.JComponent)
103      */
104     @Override
105     public int getSourceActions(JComponent c) {
106         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         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         for (DataFlavor flavor : flavors) {
125             if (this.fileListFlavor.equals(flavor)) {
126                 return true;
127             }
128         }
129         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         for (DataFlavor flavor : flavors) {
140             if (this.uriListFlavor.equals(flavor)) {
141                 return true;
142             }
143         }
144         return false;
145     }
146 }