Refactor NativeRenameAction

This commit is contained in:
Reinhard Pointner 2016-08-15 02:55:05 +08:00
parent 9f263f7de8
commit 76619dbfdf
2 changed files with 26 additions and 28 deletions

View File

@ -4,8 +4,9 @@ import static java.util.Collections.*;
import static net.filebot.util.FileUtilities.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.CancellationException;
import com.sun.jna.Platform;
@ -26,21 +27,20 @@ public enum NativeRenameAction implements RenameAction {
}
public void rename(Map<File, File> map) {
String[] src = new String[map.size()];
String[] dst = new String[map.size()];
List<File> src = new ArrayList<File>(map.size());
List<File> dst = new ArrayList<File>(map.size());
// resolve paths
int i = 0;
for (Entry<File, File> it : map.entrySet()) {
src[i] = it.getKey().getAbsolutePath();
dst[i] = resolve(it.getKey(), it.getValue()).getAbsolutePath();
i++;
}
map.forEach((from, to) -> {
// resolve relative paths
src.add(from);
dst.add(resolve(from, to));
});
callNative_Shell32(this, src, dst);
// call Windows MOVE / COPY dialog
SHFileOperation(this, getPathArray(src), getPathArray(dst));
}
private static void callNative_Shell32(NativeRenameAction action, String[] src, String[] dst) {
private static void SHFileOperation(NativeRenameAction action, String[] src, String[] dst) {
// configure parameter structure
SHFILEOPSTRUCT op = new SHFILEOPSTRUCT();
op.wFunc = (action == MOVE) ? ShellAPI.FO_MOVE : ShellAPI.FO_COPY;
@ -56,9 +56,13 @@ public enum NativeRenameAction implements RenameAction {
}
}
public static boolean isSupported() {
private static String[] getPathArray(List<File> files) {
return files.stream().map(File::getAbsolutePath).toArray(String[]::new);
}
public static boolean isSupported(StandardRenameAction action) {
try {
return Platform.isWindows();
return Platform.isWindows() && (action == StandardRenameAction.MOVE || action == StandardRenameAction.COPY);
} catch (Throwable e) {
return false;
}

View File

@ -88,7 +88,7 @@ class RenameAction extends AbstractAction {
Map<File, File> renameLog = new LinkedHashMap<File, File>();
try {
if (useNativeShell() && isNativeActionSupported(action)) {
if (useNativeShell() && NativeRenameAction.isSupported(action)) {
// call on EDT
RenameWorker worker = new NativeRenameWorker(renameMap, renameLog, NativeRenameAction.valueOf(action.name()));
worker.call(null, null, null);
@ -191,14 +191,6 @@ class RenameAction extends AbstractAction {
}
}
public boolean isNativeActionSupported(StandardRenameAction action) {
try {
return NativeRenameAction.isSupported() && NativeRenameAction.valueOf(action.name()) != null;
} catch (IllegalArgumentException e) {
return false;
}
}
private Map<File, File> checkRenamePlan(List<Entry<File, File>> renamePlan, Window parent) throws IOException {
// ask for user permissions to output paths
if (isMacSandbox()) {
@ -348,13 +340,15 @@ class RenameAction extends AbstractAction {
// prepare delta, ignore files already named as desired
Map<File, File> renamePlan = new LinkedHashMap<File, File>();
for (Entry<File, File> mapping : renameMap.entrySet()) {
File source = mapping.getKey();
File destination = resolve(mapping.getKey(), mapping.getValue());
if (!source.equals(destination)) {
renameMap.forEach((source, destination) -> {
// resolve relative paths
destination = resolve(source, destination);
if (!equalsCaseSensitive(source, destination)) {
renamePlan.put(source, destination);
}
}
});
// call native shell move/copy
try {