Refactor Extension Override/Preserve default String/FileInfo formatters

This commit is contained in:
Reinhard Pointner 2016-11-04 21:08:43 +08:00
parent d1a5c01037
commit 73b2c9e5e7
8 changed files with 46 additions and 67 deletions

View File

@ -57,7 +57,7 @@ class ExpressionFormatter implements MatchFormatter {
} }
@Override @Override
public synchronized String format(Match<?, ?> match, Map<?, ?> context) throws ScriptException { public synchronized String format(Match<?, ?> match, boolean extension, Map<?, ?> context) throws ScriptException {
// lazy initialize script engine // lazy initialize script engine
if (format == null) { if (format == null) {
format = new ExpressionFormat(expression); format = new ExpressionFormat(expression);

View File

@ -1,53 +1,42 @@
package net.filebot.ui.rename; package net.filebot.ui.rename;
import static net.filebot.util.FileUtilities.*;
import java.io.File; import java.io.File;
import java.util.Map; import java.util.Map;
import net.filebot.similarity.Match; import net.filebot.similarity.Match;
import net.filebot.util.FileUtilities;
import net.filebot.vfs.FileInfo; import net.filebot.vfs.FileInfo;
class FileNameFormatter implements MatchFormatter { class FileNameFormatter implements MatchFormatter {
private 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 || match.getValue() instanceof FileInfo || match.getValue() instanceof String; return match.getValue() instanceof File || match.getValue() instanceof FileInfo || match.getValue() instanceof String;
} }
@Override @Override
public String preview(Match<?, ?> match) { public String preview(Match<?, ?> match) {
return format(match, null); return format(match, true, null);
} }
@Override @Override
public String format(Match<?, ?> match, Map<?, ?> context) { public String format(Match<?, ?> match, boolean extension, Map<?, ?> context) {
Object value = match.getValue(); Object value = match.getValue();
if (value instanceof File) { if (value instanceof File) {
File file = (File) value; File file = (File) value;
return preserveExtension ? FileUtilities.getName(file) : file.getName(); return extension ? file.getName() : getName(file);
} }
if (value instanceof FileInfo) { if (value instanceof FileInfo) {
FileInfo file = (FileInfo) value; FileInfo file = (FileInfo) value;
return preserveExtension ? file.getName() : file.getPath(); return extension ? file.getPath() : file.getName();
} }
if (value instanceof String) { if (value instanceof String) {
return preserveExtension ? FileUtilities.getNameWithoutExtension(value.toString()) : value.toString(); return extension ? value.toString() : getNameWithoutExtension(new File(value.toString()).getName());
} }
// cannot format value // cannot format value

View File

@ -1,20 +1,16 @@
package net.filebot.ui.rename; package net.filebot.ui.rename;
import java.util.Map; import java.util.Map;
import net.filebot.similarity.Match; import net.filebot.similarity.Match;
public interface MatchFormatter { public interface MatchFormatter {
public boolean canFormat(Match<?, ?> match); public boolean canFormat(Match<?, ?> match);
public String preview(Match<?, ?> match); public String preview(Match<?, ?> match);
public String format(Match<?, ?> match, boolean extension, Map<?, ?> context) throws Exception;
public String format(Match<?, ?> match, Map<?, ?> context) throws Exception;
} }

View File

@ -1,43 +1,43 @@
package net.filebot.ui.rename; package net.filebot.ui.rename;
import static net.filebot.util.FileUtilities.*; import static net.filebot.util.FileUtilities.*;
import java.util.Formatter;
import java.util.Map; import java.util.Map;
import net.filebot.similarity.Match; import net.filebot.similarity.Match;
import net.filebot.web.Movie;
import net.filebot.web.MoviePart; import net.filebot.web.MoviePart;
class MovieFormatter implements MatchFormatter { class MovieFormatter implements MatchFormatter {
@Override @Override
public boolean canFormat(Match<?, ?> match) { public boolean canFormat(Match<?, ?> match) {
return match.getValue() instanceof MoviePart; return match.getValue() instanceof Movie;
} }
@Override @Override
public String preview(Match<?, ?> match) { public String preview(Match<?, ?> match) {
return format(match, null); Movie movie = (Movie) match.getValue();
} StringBuilder name = new StringBuilder();
@Override
public String format(Match<?, ?> match, Map<?, ?> context) {
MoviePart video = (MoviePart) match.getValue();
Formatter name = new Formatter(new StringBuilder());
// format as single-file or multi-part movie // format as single-file or multi-part movie
name.format("%s (%d)", video.getName(), video.getYear()); name.append(movie.getName()).append(" (").append(movie.getYear()).append(")");
if (video.getPartCount() > 1) { if (movie instanceof MoviePart) {
name.format(".CD%d", video.getPartIndex()); MoviePart part = (MoviePart) movie;
if (part.getPartCount() > 1) {
name.append(".CD").append(part.getPartIndex());
}
} }
// remove path separators if the name contains any / or \ // remove path separators if the name contains any / or \
return replacePathSeparators(name.out().toString()); return replacePathSeparators(name);
} }
@Override
public String format(Match<?, ?> match, boolean extension, Map<?, ?> context) {
return preview(match);
}
} }

View File

@ -2,10 +2,12 @@ package net.filebot.ui.rename;
import static java.awt.datatransfer.DataFlavor.*; import static java.awt.datatransfer.DataFlavor.*;
import static java.util.Arrays.*; import static java.util.Arrays.*;
import static java.util.stream.Collectors.*;
import static net.filebot.MediaTypes.*; import static net.filebot.MediaTypes.*;
import static net.filebot.hash.VerificationUtilities.*; import static net.filebot.hash.VerificationUtilities.*;
import static net.filebot.ui.transfer.FileTransferable.*; import static net.filebot.ui.transfer.FileTransferable.*;
import static net.filebot.util.FileUtilities.*; import static net.filebot.util.FileUtilities.*;
import static net.filebot.util.RegularExpressions.*;
import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable; import java.awt.datatransfer.Transferable;
@ -64,21 +66,12 @@ class NamesListTransferablePolicy extends FileTransferablePolicy {
load(getFilesFromTransferable(tr), action); load(getFilesFromTransferable(tr), action);
} else if (tr.isDataFlavorSupported(stringFlavor)) { } else if (tr.isDataFlavorSupported(stringFlavor)) {
// string transferable // string transferable
load((String) tr.getTransferData(stringFlavor)); load(tr.getTransferData(stringFlavor).toString());
} }
} }
protected void load(String string) { protected void load(String string) {
List<String> values = new ArrayList<String>(); List<String> values = NEWLINE.splitAsStream(string).map(String::trim).filter(s -> s.length() > 0).map(s -> normalizePathSeparators(s)).collect(toList());
Scanner scanner = new Scanner(string);
while (scanner.hasNextLine()) {
String line = scanner.nextLine().trim();
if (line.length() > 0) {
values.add(normalizePathSeparators(line));
}
}
model.addAll(values); model.addAll(values);
} }

View File

@ -45,13 +45,12 @@ public class RenameModel extends MatchModel<Object, File> {
@Override @Override
public String preview(Match<?, ?> match) { public String preview(Match<?, ?> match) {
return format(match, null); return replacePathSeparators(String.valueOf(match.getValue())).trim(); // clean up path separators like / or \
} }
@Override @Override
public String format(Match<?, ?> match, Map<?, ?> context) { public String format(Match<?, ?> match, boolean extension, Map<?, ?> context) {
// clean up path separators like / or \ return preview(match);
return replacePathSeparators(String.valueOf(match.getValue())).trim();
} }
}; };
@ -71,6 +70,9 @@ public class RenameModel extends MatchModel<Object, File> {
public void setPreserveExtension(boolean preserveExtension) { public void setPreserveExtension(boolean preserveExtension) {
this.preserveExtension = preserveExtension; this.preserveExtension = preserveExtension;
// update formatted names
names.refresh();
} }
public Map<File, File> getRenameMap() { public Map<File, File> getRenameMap() {
@ -213,7 +215,7 @@ public class RenameModel extends MatchModel<Object, File> {
Match<Object, File> match = getMatch(index); Match<Object, File> match = getMatch(index);
// create new future // create new future
final FormattedFuture future = new FormattedFuture(match, getFormatter(match), getMatchContext(match)); FormattedFuture future = new FormattedFuture(match, !preserveExtension, getFormatter(match), getMatchContext(match));
// update data // update data
if (type == ListEvent.INSERT) { if (type == ListEvent.INSERT) {
@ -261,7 +263,7 @@ public class RenameModel extends MatchModel<Object, File> {
for (int i = 0; i < size(); i++) { for (int i = 0; i < size(); i++) {
FormattedFuture obsolete = futures.get(i); FormattedFuture obsolete = futures.get(i);
Match<Object, File> match = obsolete.getMatch(); Match<Object, File> match = obsolete.getMatch();
FormattedFuture future = new FormattedFuture(match, getFormatter(match), getMatchContext(match)); FormattedFuture future = new FormattedFuture(match, !preserveExtension, getFormatter(match), getMatchContext(match));
// replace and cancel old future // replace and cancel old future
cancel(futures.set(i, future)); cancel(futures.set(i, future));
@ -308,12 +310,14 @@ public class RenameModel extends MatchModel<Object, File> {
public static class FormattedFuture extends SwingWorker<String, Void> { public static class FormattedFuture extends SwingWorker<String, Void> {
private final Match<Object, File> match; private final Match<Object, File> match;
private final boolean extension;
private final Map<File, Object> context; private final Map<File, Object> context;
private final MatchFormatter formatter; private final MatchFormatter formatter;
private FormattedFuture(Match<Object, File> match, MatchFormatter formatter, Map<File, Object> context) { private FormattedFuture(Match<Object, File> match, boolean extension, MatchFormatter formatter, Map<File, Object> context) {
this.match = match; this.match = match;
this.extension = extension;
this.formatter = formatter; this.formatter = formatter;
this.context = context; this.context = context;
} }
@ -332,7 +336,7 @@ public class RenameModel extends MatchModel<Object, File> {
@Override @Override
protected String doInBackground() throws Exception { protected String doInBackground() throws Exception {
return formatter.format(match, context).trim(); return formatter.format(match, extension, context).trim();
} }
@Override @Override

View File

@ -154,10 +154,10 @@ public class RenamePanel extends JComponent {
renameModel.useFormatter(File.class, new ExpressionFormatter(persistentFileFormat.getValue(), new FileNameFormat(), File.class)); renameModel.useFormatter(File.class, new ExpressionFormatter(persistentFileFormat.getValue(), new FileNameFormat(), File.class));
} catch (Exception e) { } catch (Exception e) {
// make sure to put File formatter at position 3 // make sure to put File formatter at position 3
renameModel.useFormatter(File.class, new FileNameFormatter(renameModel.preserveExtension())); renameModel.useFormatter(File.class, new FileNameFormatter());
} finally { } finally {
// use default filename formatter // use default filename formatter
renameModel.useFormatter(FileInfo.class, new FileNameFormatter(renameModel.preserveExtension())); renameModel.useFormatter(FileInfo.class, new FileNameFormatter());
} }
RenameListCellRenderer cellrenderer = new RenameListCellRenderer(renameModel, ApplicationFolder.UserHome.getCanonicalFile()); RenameListCellRenderer cellrenderer = new RenameListCellRenderer(renameModel, ApplicationFolder.UserHome.getCanonicalFile());
@ -560,14 +560,14 @@ public class RenamePanel extends JComponent {
} else if (binding.getInfoObject() instanceof File) { } else if (binding.getInfoObject() instanceof File) {
return Mode.File; return Mode.File;
} else { } else {
throw new IllegalArgumentException("Cannot format class: " + binding.getClass()); // ignore objects that cannot be formatted throw new IllegalArgumentException("Cannot format class: " + binding.getInfoObjectType()); // ignore objects that cannot be formatted
} }
} }
try { try {
return Mode.valueOf(persistentLastFormatState.getValue()); // restore previous mode return Mode.valueOf(persistentLastFormatState.getValue()); // restore previous mode
} catch (Exception e) { } catch (Exception e) {
debug.log(Level.WARNING, e.getMessage(), e); debug.log(Level.WARNING, e, e::getMessage);
} }
return Mode.Episode; // default to Episode mode return Mode.Episode; // default to Episode mode
@ -788,9 +788,6 @@ 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(FileInfo.class, new FileNameFormatter(renameModel.preserveExtension()));
// display changed state // display changed state
filesList.repaint(); filesList.repaint();
} }

View File

@ -321,7 +321,7 @@ public final class SwingUI {
window.ifPresent(w -> w.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR))); window.ifPresent(w -> w.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)));
runnable.run(); runnable.run();
} catch (Exception e) { } catch (Exception e) {
debug.log(Level.SEVERE, e, e::toString); debug.log(Level.SEVERE, e, e::getMessage);
} finally { } finally {
window.ifPresent(w -> w.setCursor(Cursor.getDefaultCursor())); window.ifPresent(w -> w.setCursor(Cursor.getDefaultCursor()));
} }