* [Windows] allow renaming of files where just the upper/lower case is different

This commit is contained in:
Reinhard Pointner 2014-01-17 14:07:38 +00:00
parent c75b376140
commit 3ed58bda08
3 changed files with 62 additions and 79 deletions

View File

@ -303,7 +303,7 @@ class RenameAction extends AbstractAction {
// rename file, throw exception on failure
File source = mapping.getKey();
File destination = resolveDestination(mapping.getKey(), mapping.getValue(), false);
if (!source.equals(destination)) {
if (!source.getAbsolutePath().equals(destination.getAbsolutePath())) {
action.rename(source, destination);
}

View File

@ -1,7 +1,5 @@
package net.sourceforge.filebot.ui.rename;
import static net.sourceforge.filebot.similarity.EpisodeMetrics.*;
import static net.sourceforge.tuned.FileUtilities.*;
import static net.sourceforge.tuned.ui.TunedUtilities.*;
@ -34,7 +32,6 @@ import net.sourceforge.tuned.FileUtilities;
import net.sourceforge.tuned.ui.DefaultFancyListCellRenderer;
import net.sourceforge.tuned.ui.GradientStyle;
class RenameListCellRenderer extends DefaultFancyListCellRenderer {
private RenameModel renameModel;
@ -50,7 +47,6 @@ class RenameListCellRenderer extends DefaultFancyListCellRenderer {
private Color pathRainbowBeginColor = new Color(0xCC3300);
private Color pathRainbowEndColor = new Color(0x008080);
public RenameListCellRenderer(RenameModel renameModel) {
super(new Insets(4, 7, 4, 7));
@ -61,7 +57,6 @@ class RenameListCellRenderer extends DefaultFancyListCellRenderer {
this.add(typeRenderer, "gap rel:push, hidemode 3");
}
@Override
public void configureListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
super.configureListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
@ -154,22 +149,20 @@ class RenameListCellRenderer extends DefaultFancyListCellRenderer {
if (pathFuture.isDone() && !pathFuture.isCancelled()) {
File from = renameModel.getMatch(index).getCandidate();
File to = resolveAbsolutePath(from.getParentFile(), pathFuture.toString(), renameModel.preserveExtension() ? getExtension(from) : null);
if (from.equals(to)) {
if (from.getAbsolutePath().equals(to.getAbsolutePath())) {
setIcon(ResourceManager.getIcon("dialog.continue"));
} else if (to.exists()) {
setIcon(ResourceManager.getIcon("dialog.cancel"));
} else if (to.exists() && !to.equals(from)) {
setIcon(ResourceManager.getIcon("dialog.cancel")); // take into account that on Windows equals/exists is case-insensitive which we have to work around
}
}
}
}
}
protected String formatPath(File file) {
return normalizePathSeparators(file.getPath());
}
protected String colorizePath(File file, boolean hasExtension) {
List<File> path = listPath(file);
StringBuilder html = new StringBuilder(512);
@ -196,20 +189,14 @@ class RenameListCellRenderer extends DefaultFancyListCellRenderer {
return html.append("</nobr></html>").toString();
}
protected File resolveAbsolutePath(File targetDir, String path, String extension) {
File f = new File(extension == null || extension.isEmpty() ? path : String.format("%s.%s", path, extension));
if (!f.isAbsolute()) {
f = new File(targetDir, f.getPath()); // resolve path against target folder
}
try {
return f.getCanonicalFile();
} catch (Exception e) {
return f.getAbsoluteFile();
}
}
protected float getMatchProbablity(Match<Object, File> match) {
if (match.getValue() instanceof Episode) {
@ -226,7 +213,6 @@ class RenameListCellRenderer extends DefaultFancyListCellRenderer {
return 1; // assume match is OK
}
protected String getType(File file) {
if (file.isDirectory()) {
return "Folder";
@ -241,7 +227,6 @@ class RenameListCellRenderer extends DefaultFancyListCellRenderer {
return "File";
}
private static class TypeRenderer extends DefaultListCellRenderer {
private final Insets margin = new Insets(0, 10, 0, 0);
@ -253,7 +238,6 @@ class RenameListCellRenderer extends DefaultFancyListCellRenderer {
private float alpha = 1.0f;
public TypeRenderer() {
setOpaque(false);
setForeground(new Color(0x141414));
@ -261,7 +245,6 @@ class RenameListCellRenderer extends DefaultFancyListCellRenderer {
setBorder(new CompoundBorder(new EmptyBorder(margin), new EmptyBorder(padding)));
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
@ -282,7 +265,6 @@ class RenameListCellRenderer extends DefaultFancyListCellRenderer {
g2d.drawString(getText(), (float) (shape.getCenterX() - textBounds.getX() - (textBounds.getWidth() / 2f)), (float) (shape.getCenterY() - textBounds.getY() - (textBounds.getHeight() / 2)));
}
public void setAlpha(float alpha) {
this.alpha = alpha;
}

View File

@ -52,7 +52,8 @@ public final class FileUtilities {
} else {
// move file
try {
java.nio.file.Files.move(source.toPath(), destination.toPath(), StandardCopyOption.REPLACE_EXISTING);
// * On Windows ATOMIC_MOVE allows us to rename files even if only lower/upper-case changes (without ATOMIC_MOVE the operation would be ignored)
java.nio.file.Files.move(source.toPath(), destination.toPath(), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
} catch (LinkageError e) {
org.apache.commons.io.FileUtils.moveFile(source, destination); // use "copy and delete" as fallback if standard rename fails
}