|
32 | 32 |
|
33 | 33 | import java.awt.datatransfer.DataFlavor; |
34 | 34 | import java.awt.datatransfer.UnsupportedFlavorException; |
| 35 | +import java.awt.dnd.InvalidDnDOperationException; |
35 | 36 | import java.awt.event.ActionEvent; |
36 | 37 | import java.awt.event.ActionListener; |
37 | 38 | import java.io.File; |
@@ -314,12 +315,34 @@ public FileTransferHandler(final String style) { |
314 | 315 | @Override |
315 | 316 | public boolean canImport(final TransferHandler.TransferSupport support) { |
316 | 317 | if (!hasFiles(support)) return false; |
317 | | - final List<File> allFiles = getFiles(support); |
318 | | - if (allFiles == null || allFiles.size() != 1) return false; |
319 | 318 |
|
320 | | - final FileFilter filter = SwingFileWidget.createFileFilter(style); |
321 | | - final List<File> files = SwingFileWidget.filterFiles(allFiles, filter); |
322 | | - return files.size() == 1; |
| 319 | + // We wish to test the content of the transfer data and |
| 320 | + // determine if they are (a) files and (b) files we are |
| 321 | + // actually interested in processing. So we need to call |
| 322 | + // getTransferData() so that we can inspect the file names. |
| 323 | + // Unfortunately, this will not always work. |
| 324 | + // Under Windows, the Transferable instance |
| 325 | + // will have transfer data ONLY while the mouse button is |
| 326 | + // depressed. However, when the user releases the mouse |
| 327 | + // button, this method will be called one last time. And when |
| 328 | + // when this method attempts to getTransferData, Java will throw |
| 329 | + // an InvalidDnDOperationException. Since we know that the |
| 330 | + // exception is coming, we simply catch it and ignore it. |
| 331 | + // See: |
| 332 | + // https://coderanch.com/t/664525/java/Invalid-Drag-Drop-Exception |
| 333 | + try { |
| 334 | + final List<File> allFiles = getFiles(support); |
| 335 | + if (allFiles == null || allFiles.size() != 1) |
| 336 | + return false; |
| 337 | + |
| 338 | + final FileFilter filter = SwingFileWidget |
| 339 | + .createFileFilter(style); |
| 340 | + final List<File> files = SwingFileWidget.filterFiles(allFiles, |
| 341 | + filter); |
| 342 | + return files.size() == 1; |
| 343 | + } catch (InvalidDnDOperationException exc) { |
| 344 | + return true; |
| 345 | + } |
323 | 346 | } |
324 | 347 |
|
325 | 348 | @Override |
|
0 commit comments