Refactor trash(File)

This commit is contained in:
Reinhard Pointner 2016-08-17 03:11:56 +08:00
parent 150ce19092
commit fc73de25d7
2 changed files with 42 additions and 36 deletions

View File

@ -1,6 +1,6 @@
package net.filebot;
import static net.filebot.Logging.*;
import static net.filebot.UserFiles.*;
import java.io.File;
import java.io.IOException;
@ -8,10 +8,6 @@ import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.attribute.BasicFileAttributes;
import com.apple.eio.FileManager;
import com.sun.jna.Platform;
import com.sun.jna.platform.FileUtils;
import net.filebot.util.FileUtilities;
public enum StandardRenameAction implements RenameAction {
@ -201,27 +197,4 @@ public enum StandardRenameAction implements RenameAction {
throw new IllegalArgumentException(String.format("Cannot revert file: %s => %s", current, original));
}
public static void trash(File file) throws IOException {
// use system trash if possible
try {
if (Platform.isMac()) {
// use com.apple.eio package on OS X platform
if (FileManager.moveToTrash(file)) {
return;
}
} else if (FileUtils.getInstance().hasTrash()) {
// use com.sun.jna.platform package on Windows and Linux
FileUtils.getInstance().moveToTrash(new File[] { file });
return;
}
} catch (Exception e) {
debug.warning(e::toString);
}
// delete permanently if necessary
if (file.exists()) {
FileUtilities.delete(file);
}
}
}

View File

@ -13,6 +13,7 @@ import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
@ -21,26 +22,58 @@ import java.util.logging.Level;
import javax.swing.JFileChooser;
import com.apple.eio.FileManager;
import com.sun.jna.platform.FileUtils;
import net.filebot.mac.MacAppUtilities;
import net.filebot.util.FileUtilities;
import net.filebot.util.FileUtilities.ExtensionFileFilter;
public class UserFiles {
public static void trash(File file) throws IOException {
// use system trash if possible
try {
if (isMacApp()) {
// use com.apple.eio package on OS X platform
if (FileManager.moveToTrash(file)) {
return;
}
} else if (FileUtils.getInstance().hasTrash()) {
// use com.sun.jna.platform package on Windows and Linux
FileUtils.getInstance().moveToTrash(new File[] { file });
return;
}
} catch (Exception e) {
debug.log(Level.WARNING, e::toString);
}
// delete permanently if necessary
if (file.exists()) {
FileUtilities.delete(file);
}
}
public static void revealFiles(Collection<File> files) {
if (isMacApp()) {
for (File it : files) {
MacAppUtilities.revealInFinder(it);
}
} else {
// if we can't reveal the file in folder, just reveal the parent folder
files.stream().map(it -> it.getParentFile()).distinct().forEach(it -> {
files.forEach(f -> {
try {
Desktop.getDesktop().open(it);
FileManager.revealInFinder(f);
} catch (Exception e) {
debug.log(Level.SEVERE, e.getMessage(), e);
debug.log(Level.WARNING, e::toString);
}
});
return;
}
// if we can't reveal the file in folder, just reveal the parent folder
files.stream().map(it -> it.getParentFile()).distinct().forEach(it -> {
try {
Desktop.getDesktop().open(it);
} catch (Exception e) {
debug.log(Level.WARNING, e::toString);
}
});
}
private static FileChooser defaultFileChooser = getPreferredFileChooser();