* apply "preserve extension" policy to filename formatter too

This commit is contained in:
Reinhard Pointner 2009-05-10 11:17:13 +00:00
parent 8729d227a1
commit 6d890b27d2
8 changed files with 41 additions and 19 deletions

View File

@ -178,9 +178,10 @@ public class Torrent {
public static class Entry { public static class Entry {
private final String path;
private final String name; private final String name;
private final long length; private final long length;
private final String path;
public Entry(String name, long length, String path) { public Entry(String name, long length, String path) {
@ -190,6 +191,11 @@ public class Torrent {
} }
public String getPath() {
return path;
}
public String getName() { public String getName() {
return name; return name;
} }
@ -200,11 +206,6 @@ public class Torrent {
} }
public String getPath() {
return path;
}
@Override @Override
public String toString() { public String toString() {
return getPath(); return getPath();

View File

@ -2,13 +2,13 @@
package net.sourceforge.filebot.ui.panel.rename; package net.sourceforge.filebot.ui.panel.rename;
class AbstractFileEntry { class AbstractFile {
private final String name; private final String name;
private final long length; private final long length;
public AbstractFileEntry(String name, long length) { public AbstractFile(String name, long length) {
this.name = name; this.name = name;
this.length = length; this.length = length;
} }

View File

@ -13,7 +13,7 @@ import net.sourceforge.filebot.web.Episode;
import net.sourceforge.filebot.web.EpisodeFormat; import net.sourceforge.filebot.web.EpisodeFormat;
public class EpisodeExpressionFormatter extends ExpressionFormat implements MatchFormatter { class EpisodeExpressionFormatter extends ExpressionFormat implements MatchFormatter {
public EpisodeExpressionFormatter(String expression) throws ScriptException { public EpisodeExpressionFormatter(String expression) throws ScriptException {
super(expression); super(expression);

View File

@ -8,11 +8,19 @@ import net.sourceforge.filebot.similarity.Match;
import net.sourceforge.tuned.FileUtilities; import net.sourceforge.tuned.FileUtilities;
public class FileNameFormatter implements MatchFormatter { class FileNameFormatter implements MatchFormatter {
private final boolean preserveExtension;
public FileNameFormatter(boolean preserveExtension) {
this.preserveExtension = preserveExtension;
}
@Override @Override
public boolean canFormat(Match<?, ?> match) { public boolean canFormat(Match<?, ?> match) {
return match.getValue() instanceof File; return match.getValue() instanceof File || match.getValue() instanceof AbstractFile;
} }
@ -24,9 +32,20 @@ public class FileNameFormatter implements MatchFormatter {
@Override @Override
public String format(Match<?, ?> match) { public String format(Match<?, ?> match) {
if (match.getValue() instanceof File) {
File file = (File) match.getValue(); File file = (File) match.getValue();
return FileUtilities.getName(file); return preserveExtension ? FileUtilities.getName(file) : file.getName();
}
if (match.getValue() instanceof AbstractFile) {
AbstractFile file = (AbstractFile) match.getValue();
return preserveExtension ? FileUtilities.getNameWithoutExtension(file.getName()) : file.getName();
}
// type not supported
throw new IllegalArgumentException("Type not supported");
} }
} }

View File

@ -63,8 +63,8 @@ class MatchAction extends AbstractAction {
@Override @Override
protected long getLength(Object object) { protected long getLength(Object object) {
if (object instanceof AbstractFileEntry) { if (object instanceof AbstractFile) {
return ((AbstractFileEntry) object).getLength(); return ((AbstractFile) object).getLength();
} }
return super.getLength(object); return super.getLength(object);

View File

@ -5,7 +5,7 @@ package net.sourceforge.filebot.ui.panel.rename;
import net.sourceforge.filebot.similarity.Match; import net.sourceforge.filebot.similarity.Match;
public interface MatchFormatter { interface MatchFormatter {
public boolean canFormat(Match<?, ?> match); public boolean canFormat(Match<?, ?> match);

View File

@ -7,7 +7,6 @@ import static net.sourceforge.filebot.FileBotUtilities.LIST_FILES;
import static net.sourceforge.filebot.FileBotUtilities.TORRENT_FILES; import static net.sourceforge.filebot.FileBotUtilities.TORRENT_FILES;
import static net.sourceforge.tuned.FileUtilities.FOLDERS; import static net.sourceforge.tuned.FileUtilities.FOLDERS;
import static net.sourceforge.tuned.FileUtilities.containsOnly; import static net.sourceforge.tuned.FileUtilities.containsOnly;
import static net.sourceforge.tuned.FileUtilities.getNameWithoutExtension;
import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable; import java.awt.datatransfer.Transferable;
@ -140,7 +139,7 @@ class NamesListTransferablePolicy extends FileTransferablePolicy {
Torrent torrent = new Torrent(file); Torrent torrent = new Torrent(file);
for (Torrent.Entry entry : torrent.getFiles()) { for (Torrent.Entry entry : torrent.getFiles()) {
values.add(new AbstractFileEntry(getNameWithoutExtension(entry.getName()), entry.getLength())); values.add(new AbstractFile(entry.getName(), entry.getLength()));
} }
} }
} catch (IOException e) { } catch (IOException e) {

View File

@ -80,7 +80,7 @@ public class RenamePanel extends JComponent {
renameModel.setPreserveExtension(Boolean.valueOf(Settings.userRoot().get("rename.preserveExtension", "true"))); renameModel.setPreserveExtension(Boolean.valueOf(Settings.userRoot().get("rename.preserveExtension", "true")));
// filename formatter // filename formatter
renameModel.useFormatter(File.class, new FileNameFormatter()); renameModel.useFormatter(File.class, new FileNameFormatter(renameModel.preserveExtension()));
// restore custom episode formatter // restore custom episode formatter
renameModel.useFormatter(Episode.class, persistentFormatExpression.getValue()); renameModel.useFormatter(Episode.class, persistentFormatExpression.getValue());
@ -220,6 +220,9 @@ public class RenamePanel extends JComponent {
public void actionPerformed(ActionEvent evt) { public void actionPerformed(ActionEvent evt) {
renameModel.setPreserveExtension(activate); renameModel.setPreserveExtension(activate);
// use different file name formatter
renameModel.useFormatter(File.class, new FileNameFormatter(renameModel.preserveExtension()));
// display changed state // display changed state
filesList.repaint(); filesList.repaint();