source: 3thparty/jupload/src/main/java/wjhk/jupload2/gui/JUploadTransferHandler.java @ 3951

Revision 3951, 4.7 KB checked in by alexandrecorreia, 13 years ago (diff)

Ticket #1709 - Adicao de codigo fonte java do componente jupload

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