1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-12-24 08:48:51 -05:00

* enforce good alphabetical order for all dropped data (on Windows files a dropped in selection order which can be confusing)

This commit is contained in:
Reinhard Pointner 2014-07-18 22:03:27 +00:00
parent 093a38593d
commit d69b1da6a3

View File

@ -1,8 +1,7 @@
package net.filebot.ui.transfer;
import static net.filebot.Settings.*;
import static net.filebot.util.FileUtilities.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
@ -22,12 +21,10 @@ import java.util.logging.Logger;
import net.filebot.gio.GVFS;
public class FileTransferable implements Transferable {
public static final DataFlavor uriListFlavor = createUriListFlavor();
private static DataFlavor createUriListFlavor() {
try {
return new DataFlavor("text/uri-list;class=java.nio.CharBuffer");
@ -36,20 +33,17 @@ public class FileTransferable implements Transferable {
throw new RuntimeException(e);
}
}
private final File[] files;
public FileTransferable(File... files) {
this.files = files;
}
public FileTransferable(Collection<File> files) {
this.files = files.toArray(new File[0]);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
if (flavor.isFlavorJavaFileListType())
@ -59,71 +53,65 @@ public class FileTransferable implements Transferable {
else
throw new UnsupportedFlavorException(flavor);
}
/**
* @return line separated list of file URIs
*/
private String getUriList() {
StringBuilder sb = new StringBuilder(80 * files.length);
for (File file : files) {
// use URI encoded path
sb.append("file://").append(file.toURI().getRawPath());
sb.append("\r\n");
}
return sb.toString();
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[] { DataFlavor.javaFileListFlavor, uriListFlavor };
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return isFileListFlavor(flavor);
}
public static boolean isFileListFlavor(DataFlavor flavor) {
return flavor.isFlavorJavaFileListType() || flavor.equals(uriListFlavor);
}
public static boolean hasFileListFlavor(Transferable tr) {
return tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor) || tr.isDataFlavorSupported(FileTransferable.uriListFlavor);
}
@SuppressWarnings("unchecked")
public static List<File> getFilesFromTransferable(Transferable tr) throws IOException, UnsupportedFlavorException {
if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor) && !useGVFS()) {
// file list flavor
return (List<File>) tr.getTransferData(DataFlavor.javaFileListFlavor);
return sortByUniquePath((List<File>) tr.getTransferData(DataFlavor.javaFileListFlavor)); // FORCE NATURAL FILE ORDER
}
if (tr.isDataFlavorSupported(FileTransferable.uriListFlavor)) {
// file URI list flavor (Linux)
Readable transferData = (Readable) tr.getTransferData(FileTransferable.uriListFlavor);
Scanner scanner = new Scanner(transferData);
List<File> files = new ArrayList<File>();
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.startsWith("#")) {
// the line is a comment (as per RFC 2483)
continue;
}
try {
URI uri = new URI(line);
File file = null;
try {
// file URIs
file = new File(uri);
@ -137,23 +125,23 @@ public class FileTransferable implements Transferable {
Logger.getLogger(FileTransferable.class.getName()).log(Level.WARNING, "Unable to resolve GVFS URI", error);
}
}
if (file == null || !file.exists()) {
throw new FileNotFoundException(file != null ? file.getPath() : line);
}
files.add(file);
} catch (Throwable e) {
// URISyntaxException, IllegalArgumentException, FileNotFoundException, LinkageError, etc
Logger.getLogger(FileTransferable.class.getName()).log(Level.WARNING, "Invalid file URI: " + line);
}
}
return files;
return sortByUniquePath(files); // FORCE NATURAL FILE ORDER
}
// cannot get files from transferable
throw new UnsupportedFlavorException(null);
}
}