* fix dnd on mac (and possibly linux)
This commit is contained in:
parent
d982c0a697
commit
c2bfedd978
|
@ -19,9 +19,12 @@ public abstract class FileTransferablePolicy extends TransferablePolicy {
|
||||||
try {
|
try {
|
||||||
List<File> files = getFilesFromTransferable(tr);
|
List<File> files = getFilesFromTransferable(tr);
|
||||||
|
|
||||||
if (!containsOnly(files, TEMPORARY)) {
|
// ignore temporary files (may not work on all platforms since the DnD data may not be accessible during the drag)
|
||||||
return accept(getFilesFromTransferable(tr));
|
if (files != null && files.size() > 0 && containsOnly(files, TEMPORARY)) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return accept(files);
|
||||||
} catch (UnsupportedFlavorException e) {
|
} catch (UnsupportedFlavorException e) {
|
||||||
// no file list flavor
|
// no file list flavor
|
||||||
}
|
}
|
||||||
|
@ -29,7 +32,7 @@ public abstract class FileTransferablePolicy extends TransferablePolicy {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleTransferable(Transferable tr, TransferAction action) throws Exception {
|
public void handleTransferable(Transferable tr, TransferAction action) throws Exception {
|
||||||
List<File> files = getFilesFromTransferable(tr);
|
List<File> files = getFilesFromTransferable(tr);
|
||||||
|
@ -41,16 +44,16 @@ public abstract class FileTransferablePolicy extends TransferablePolicy {
|
||||||
load(files);
|
load(files);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected abstract boolean accept(List<File> files);
|
protected abstract boolean accept(List<File> files);
|
||||||
|
|
||||||
|
|
||||||
protected abstract void load(List<File> files) throws IOException;
|
protected abstract void load(List<File> files) throws IOException;
|
||||||
|
|
||||||
|
|
||||||
protected abstract void clear();
|
protected abstract void clear();
|
||||||
|
|
||||||
|
|
||||||
public String getFileFilterDescription() {
|
public String getFileFilterDescription() {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,19 +57,19 @@ public final class FileUtilities {
|
||||||
return destination;
|
return destination;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static void renameFileNIO2(File source, File destination) throws IOException {
|
private static void renameFileNIO2(File source, File destination) throws IOException {
|
||||||
Files.move(source.toPath(), destination.toPath());
|
Files.move(source.toPath(), destination.toPath());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static void renameFileIO(File source, File destination) throws IOException {
|
private static void renameFileIO(File source, File destination) throws IOException {
|
||||||
if (!source.renameTo(destination)) {
|
if (!source.renameTo(destination)) {
|
||||||
throw new IOException("Failed to rename file: " + source.getName());
|
throw new IOException("Failed to rename file: " + source.getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static byte[] readFile(File source) throws IOException {
|
public static byte[] readFile(File source) throws IOException {
|
||||||
InputStream in = new FileInputStream(source);
|
InputStream in = new FileInputStream(source);
|
||||||
|
|
||||||
|
@ -89,7 +89,7 @@ public final class FileUtilities {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static String readAll(Reader source) throws IOException {
|
public static String readAll(Reader source) throws IOException {
|
||||||
StringBuilder text = new StringBuilder();
|
StringBuilder text = new StringBuilder();
|
||||||
char[] buffer = new char[2048];
|
char[] buffer = new char[2048];
|
||||||
|
@ -102,7 +102,7 @@ public final class FileUtilities {
|
||||||
return text.toString();
|
return text.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void writeFile(ByteBuffer data, File destination) throws IOException {
|
public static void writeFile(ByteBuffer data, File destination) throws IOException {
|
||||||
FileChannel fileChannel = new FileOutputStream(destination).getChannel();
|
FileChannel fileChannel = new FileOutputStream(destination).getChannel();
|
||||||
|
|
||||||
|
@ -113,7 +113,7 @@ public final class FileUtilities {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static Reader createTextReader(File file) throws IOException {
|
public static Reader createTextReader(File file) throws IOException {
|
||||||
CharsetDetector detector = new CharsetDetector();
|
CharsetDetector detector = new CharsetDetector();
|
||||||
detector.setDeclaredEncoding("UTF-8"); // small boost for UTF-8 as default encoding
|
detector.setDeclaredEncoding("UTF-8"); // small boost for UTF-8 as default encoding
|
||||||
|
@ -127,7 +127,7 @@ public final class FileUtilities {
|
||||||
return new InputStreamReader(new FileInputStream(file), "UTF-8");
|
return new InputStreamReader(new FileInputStream(file), "UTF-8");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static String getText(ByteBuffer data) throws IOException {
|
public static String getText(ByteBuffer data) throws IOException {
|
||||||
CharsetDetector detector = new CharsetDetector();
|
CharsetDetector detector = new CharsetDetector();
|
||||||
detector.setDeclaredEncoding("UTF-8"); // small boost for UTF-8 as default encoding
|
detector.setDeclaredEncoding("UTF-8"); // small boost for UTF-8 as default encoding
|
||||||
|
@ -146,7 +146,7 @@ public final class FileUtilities {
|
||||||
return Charset.forName("UTF-8").decode(data).toString();
|
return Charset.forName("UTF-8").decode(data).toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pattern used for matching file extensions.
|
* Pattern used for matching file extensions.
|
||||||
*
|
*
|
||||||
|
@ -154,7 +154,7 @@ public final class FileUtilities {
|
||||||
*/
|
*/
|
||||||
public static final Pattern EXTENSION = Pattern.compile("(?<=.[.])\\p{Alnum}+$");
|
public static final Pattern EXTENSION = Pattern.compile("(?<=.[.])\\p{Alnum}+$");
|
||||||
|
|
||||||
|
|
||||||
public static String getExtension(File file) {
|
public static String getExtension(File file) {
|
||||||
if (file.isDirectory())
|
if (file.isDirectory())
|
||||||
return null;
|
return null;
|
||||||
|
@ -162,7 +162,7 @@ public final class FileUtilities {
|
||||||
return getExtension(file.getName());
|
return getExtension(file.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static String getExtension(String name) {
|
public static String getExtension(String name) {
|
||||||
Matcher matcher = EXTENSION.matcher(name);
|
Matcher matcher = EXTENSION.matcher(name);
|
||||||
|
|
||||||
|
@ -175,13 +175,13 @@ public final class FileUtilities {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static boolean hasExtension(File file, String... extensions) {
|
public static boolean hasExtension(File file, String... extensions) {
|
||||||
// avoid native call for speed, if possible
|
// avoid native call for speed, if possible
|
||||||
return hasExtension(file.getName(), extensions) && !file.isDirectory();
|
return hasExtension(file.getName(), extensions) && !file.isDirectory();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static boolean hasExtension(String filename, String... extensions) {
|
public static boolean hasExtension(String filename, String... extensions) {
|
||||||
String extension = getExtension(filename);
|
String extension = getExtension(filename);
|
||||||
|
|
||||||
|
@ -193,7 +193,7 @@ public final class FileUtilities {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static String getNameWithoutExtension(String name) {
|
public static String getNameWithoutExtension(String name) {
|
||||||
Matcher matcher = EXTENSION.matcher(name);
|
Matcher matcher = EXTENSION.matcher(name);
|
||||||
|
|
||||||
|
@ -205,7 +205,7 @@ public final class FileUtilities {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static String getName(File file) {
|
public static String getName(File file) {
|
||||||
if (file.isDirectory())
|
if (file.isDirectory())
|
||||||
return getFolderName(file);
|
return getFolderName(file);
|
||||||
|
@ -213,7 +213,7 @@ public final class FileUtilities {
|
||||||
return getNameWithoutExtension(file.getName());
|
return getNameWithoutExtension(file.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static String getFolderName(File file) {
|
public static String getFolderName(File file) {
|
||||||
String name = file.getName();
|
String name = file.getName();
|
||||||
|
|
||||||
|
@ -224,24 +224,24 @@ public final class FileUtilities {
|
||||||
return file.toString();
|
return file.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static boolean isDerived(File derivate, File prime) {
|
public static boolean isDerived(File derivate, File prime) {
|
||||||
return isDerived(getName(derivate), prime);
|
return isDerived(getName(derivate), prime);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static boolean isDerived(String derivate, File prime) {
|
public static boolean isDerived(String derivate, File prime) {
|
||||||
String base = getName(prime).trim().toLowerCase();
|
String base = getName(prime).trim().toLowerCase();
|
||||||
derivate = derivate.trim().toLowerCase();
|
derivate = derivate.trim().toLowerCase();
|
||||||
return derivate.startsWith(base);
|
return derivate.startsWith(base);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static boolean isDerivedByExtension(File derivate, File prime) {
|
public static boolean isDerivedByExtension(File derivate, File prime) {
|
||||||
return isDerivedByExtension(getName(derivate), prime);
|
return isDerivedByExtension(getName(derivate), prime);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static boolean isDerivedByExtension(String derivate, File prime) {
|
public static boolean isDerivedByExtension(String derivate, File prime) {
|
||||||
String base = getName(prime).trim().toLowerCase();
|
String base = getName(prime).trim().toLowerCase();
|
||||||
derivate = derivate.trim().toLowerCase();
|
derivate = derivate.trim().toLowerCase();
|
||||||
|
@ -259,7 +259,7 @@ public final class FileUtilities {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static boolean containsOnly(Iterable<File> files, FileFilter filter) {
|
public static boolean containsOnly(Iterable<File> files, FileFilter filter) {
|
||||||
for (File file : files) {
|
for (File file : files) {
|
||||||
if (!filter.accept(file))
|
if (!filter.accept(file))
|
||||||
|
@ -269,7 +269,7 @@ public final class FileUtilities {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static List<File> filter(Iterable<File> files, FileFilter... filters) {
|
public static List<File> filter(Iterable<File> files, FileFilter... filters) {
|
||||||
List<File> accepted = new ArrayList<File>();
|
List<File> accepted = new ArrayList<File>();
|
||||||
|
|
||||||
|
@ -285,7 +285,7 @@ public final class FileUtilities {
|
||||||
return accepted;
|
return accepted;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static List<File> flatten(Iterable<File> roots, int maxDepth, boolean listHiddenFiles) {
|
public static List<File> flatten(Iterable<File> roots, int maxDepth, boolean listHiddenFiles) {
|
||||||
List<File> files = new ArrayList<File>();
|
List<File> files = new ArrayList<File>();
|
||||||
|
|
||||||
|
@ -301,7 +301,7 @@ public final class FileUtilities {
|
||||||
return files;
|
return files;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static List<File> listPath(File file) {
|
public static List<File> listPath(File file) {
|
||||||
LinkedList<File> nodes = new LinkedList<File>();
|
LinkedList<File> nodes = new LinkedList<File>();
|
||||||
|
|
||||||
|
@ -312,7 +312,7 @@ public final class FileUtilities {
|
||||||
return nodes;
|
return nodes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static List<File> listFiles(Iterable<File> folders, int maxDepth, boolean listHiddenFiles) {
|
public static List<File> listFiles(Iterable<File> folders, int maxDepth, boolean listHiddenFiles) {
|
||||||
List<File> files = new ArrayList<File>();
|
List<File> files = new ArrayList<File>();
|
||||||
|
|
||||||
|
@ -324,7 +324,7 @@ public final class FileUtilities {
|
||||||
return files;
|
return files;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static void listFiles(File folder, int depth, List<File> files, int maxDepth, boolean listHiddenFiles) {
|
private static void listFiles(File folder, int depth, List<File> files, int maxDepth, boolean listHiddenFiles) {
|
||||||
if (depth > maxDepth)
|
if (depth > maxDepth)
|
||||||
return;
|
return;
|
||||||
|
@ -341,7 +341,7 @@ public final class FileUtilities {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static SortedMap<File, List<File>> mapByFolder(Iterable<File> files) {
|
public static SortedMap<File, List<File>> mapByFolder(Iterable<File> files) {
|
||||||
SortedMap<File, List<File>> map = new TreeMap<File, List<File>>();
|
SortedMap<File, List<File>> map = new TreeMap<File, List<File>>();
|
||||||
|
|
||||||
|
@ -363,7 +363,7 @@ public final class FileUtilities {
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static Map<String, List<File>> mapByExtension(Iterable<File> files) {
|
public static Map<String, List<File>> mapByExtension(Iterable<File> files) {
|
||||||
Map<String, List<File>> map = new HashMap<String, List<File>>();
|
Map<String, List<File>> map = new HashMap<String, List<File>>();
|
||||||
|
|
||||||
|
@ -385,13 +385,13 @@ public final class FileUtilities {
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Invalid file name characters: \, /, :, *, ?, ", <, >, |, \r and \n
|
* Invalid file name characters: \, /, :, *, ?, ", <, >, |, \r and \n
|
||||||
*/
|
*/
|
||||||
public static final Pattern ILLEGAL_CHARACTERS = Pattern.compile("[\\\\/:*?\"<>|\\r\\n]");
|
public static final Pattern ILLEGAL_CHARACTERS = Pattern.compile("[\\\\/:*?\"<>|\\r\\n]");
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Strip file name of invalid characters
|
* Strip file name of invalid characters
|
||||||
*
|
*
|
||||||
|
@ -403,13 +403,13 @@ public final class FileUtilities {
|
||||||
return ILLEGAL_CHARACTERS.matcher(filename).replaceAll("");
|
return ILLEGAL_CHARACTERS.matcher(filename).replaceAll("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static boolean isInvalidFileName(CharSequence filename) {
|
public static boolean isInvalidFileName(CharSequence filename) {
|
||||||
// check if file name contains any illegal characters
|
// check if file name contains any illegal characters
|
||||||
return ILLEGAL_CHARACTERS.matcher(filename).find();
|
return ILLEGAL_CHARACTERS.matcher(filename).find();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static File validateFileName(File file) {
|
public static File validateFileName(File file) {
|
||||||
// windows drives (e.g. c:, d:, etc.) are never invalid because name will be an empty string
|
// windows drives (e.g. c:, d:, etc.) are never invalid because name will be an empty string
|
||||||
if (!isInvalidFileName(file.getName()))
|
if (!isInvalidFileName(file.getName()))
|
||||||
|
@ -419,7 +419,7 @@ public final class FileUtilities {
|
||||||
return new File(file.getParentFile(), validateFileName(file.getName()));
|
return new File(file.getParentFile(), validateFileName(file.getName()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static File validateFilePath(File path) {
|
public static File validateFilePath(File path) {
|
||||||
Iterator<File> nodes = listPath(path).iterator();
|
Iterator<File> nodes = listPath(path).iterator();
|
||||||
|
|
||||||
|
@ -434,7 +434,7 @@ public final class FileUtilities {
|
||||||
return validatedPath;
|
return validatedPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static boolean isInvalidFilePath(File path) {
|
public static boolean isInvalidFilePath(File path) {
|
||||||
// check if file name contains any illegal characters
|
// check if file name contains any illegal characters
|
||||||
for (File node = path; node != null; node = node.getParentFile()) {
|
for (File node = path; node != null; node = node.getParentFile()) {
|
||||||
|
@ -445,22 +445,22 @@ public final class FileUtilities {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static String normalizePathSeparators(String path) {
|
public static String normalizePathSeparators(String path) {
|
||||||
return path.replace('\\', '/');
|
return path.replace('\\', '/');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static String replacePathSeparators(CharSequence path) {
|
public static String replacePathSeparators(CharSequence path) {
|
||||||
return Pattern.compile("\\s*[\\\\/]+\\s*").matcher(path).replaceAll(" ");
|
return Pattern.compile("\\s*[\\\\/]+\\s*").matcher(path).replaceAll(" ");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static final long KILO = 1024;
|
public static final long KILO = 1024;
|
||||||
public static final long MEGA = KILO * 1024;
|
public static final long MEGA = KILO * 1024;
|
||||||
public static final long GIGA = MEGA * 1024;
|
public static final long GIGA = MEGA * 1024;
|
||||||
|
|
||||||
|
|
||||||
public static String formatSize(long size) {
|
public static String formatSize(long size) {
|
||||||
if (size >= MEGA)
|
if (size >= MEGA)
|
||||||
return String.format("%,d MB", size / MEGA);
|
return String.format("%,d MB", size / MEGA);
|
||||||
|
@ -470,7 +470,7 @@ public final class FileUtilities {
|
||||||
return String.format("%,d Byte", size);
|
return String.format("%,d Byte", size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static final FileFilter FOLDERS = new FileFilter() {
|
public static final FileFilter FOLDERS = new FileFilter() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -489,42 +489,42 @@ public final class FileUtilities {
|
||||||
|
|
||||||
public static final FileFilter TEMPORARY = new FileFilter() {
|
public static final FileFilter TEMPORARY = new FileFilter() {
|
||||||
|
|
||||||
private final File TEMP_DIR = new File(System.getProperty("java.io.tmpdir"));
|
private final String tmpdir = System.getProperty("java.io.tmpdir");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean accept(File file) {
|
public boolean accept(File file) {
|
||||||
return file.getAbsolutePath().startsWith(TEMP_DIR.getAbsolutePath());
|
return file.getAbsolutePath().startsWith(tmpdir);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
public static class ExtensionFileFilter implements FileFilter {
|
public static class ExtensionFileFilter implements FileFilter {
|
||||||
|
|
||||||
private final String[] extensions;
|
private final String[] extensions;
|
||||||
|
|
||||||
|
|
||||||
public ExtensionFileFilter(String... extensions) {
|
public ExtensionFileFilter(String... extensions) {
|
||||||
this.extensions = extensions;
|
this.extensions = extensions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public ExtensionFileFilter(Collection<String> extensions) {
|
public ExtensionFileFilter(Collection<String> extensions) {
|
||||||
this.extensions = extensions.toArray(new String[0]);
|
this.extensions = extensions.toArray(new String[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean accept(File file) {
|
public boolean accept(File file) {
|
||||||
return hasExtension(file, extensions);
|
return hasExtension(file, extensions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public boolean accept(String name) {
|
public boolean accept(String name) {
|
||||||
return hasExtension(name, extensions);
|
return hasExtension(name, extensions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public boolean acceptExtension(String extension) {
|
public boolean acceptExtension(String extension) {
|
||||||
for (String other : extensions) {
|
for (String other : extensions) {
|
||||||
if (other.equalsIgnoreCase(extension))
|
if (other.equalsIgnoreCase(extension))
|
||||||
|
@ -534,18 +534,18 @@ public final class FileUtilities {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public String extension() {
|
public String extension() {
|
||||||
return extensions[0];
|
return extensions[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public String[] extensions() {
|
public String[] extensions() {
|
||||||
return extensions.clone();
|
return extensions.clone();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dummy constructor to prevent instantiation.
|
* Dummy constructor to prevent instantiation.
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue