diff --git a/source/net/filebot/ui/rename/ExpressionFormatter.java b/source/net/filebot/ui/rename/ExpressionFormatter.java index db3ab0dd..cd1faf12 100644 --- a/source/net/filebot/ui/rename/ExpressionFormatter.java +++ b/source/net/filebot/ui/rename/ExpressionFormatter.java @@ -57,7 +57,7 @@ class ExpressionFormatter implements MatchFormatter { } @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 if (format == null) { format = new ExpressionFormat(expression); diff --git a/source/net/filebot/ui/rename/FileNameFormatter.java b/source/net/filebot/ui/rename/FileNameFormatter.java index b8afcd1e..12bbf74f 100644 --- a/source/net/filebot/ui/rename/FileNameFormatter.java +++ b/source/net/filebot/ui/rename/FileNameFormatter.java @@ -1,53 +1,42 @@ package net.filebot.ui.rename; +import static net.filebot.util.FileUtilities.*; import java.io.File; import java.util.Map; import net.filebot.similarity.Match; -import net.filebot.util.FileUtilities; import net.filebot.vfs.FileInfo; - class FileNameFormatter implements MatchFormatter { - private boolean preserveExtension; - - - public FileNameFormatter(boolean preserveExtension) { - this.preserveExtension = preserveExtension; - } - - @Override public boolean canFormat(Match match) { return match.getValue() instanceof File || match.getValue() instanceof FileInfo || match.getValue() instanceof String; } - @Override public String preview(Match match) { - return format(match, null); + return format(match, true, null); } - @Override - public String format(Match match, Map context) { + public String format(Match match, boolean extension, Map context) { Object value = match.getValue(); if (value instanceof File) { File file = (File) value; - return preserveExtension ? FileUtilities.getName(file) : file.getName(); + return extension ? file.getName() : getName(file); } if (value instanceof FileInfo) { FileInfo file = (FileInfo) value; - return preserveExtension ? file.getName() : file.getPath(); + return extension ? file.getPath() : file.getName(); } 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 diff --git a/source/net/filebot/ui/rename/MatchFormatter.java b/source/net/filebot/ui/rename/MatchFormatter.java index 9c736fe1..2a595585 100644 --- a/source/net/filebot/ui/rename/MatchFormatter.java +++ b/source/net/filebot/ui/rename/MatchFormatter.java @@ -1,20 +1,16 @@ package net.filebot.ui.rename; - import java.util.Map; import net.filebot.similarity.Match; - public interface MatchFormatter { public boolean canFormat(Match match); - public String preview(Match match); - - public String format(Match match, Map context) throws Exception; + public String format(Match match, boolean extension, Map context) throws Exception; } diff --git a/source/net/filebot/ui/rename/MovieFormatter.java b/source/net/filebot/ui/rename/MovieFormatter.java index 3e3d7db8..cb632276 100644 --- a/source/net/filebot/ui/rename/MovieFormatter.java +++ b/source/net/filebot/ui/rename/MovieFormatter.java @@ -1,43 +1,43 @@ package net.filebot.ui.rename; - import static net.filebot.util.FileUtilities.*; -import java.util.Formatter; import java.util.Map; import net.filebot.similarity.Match; +import net.filebot.web.Movie; import net.filebot.web.MoviePart; - class MovieFormatter implements MatchFormatter { @Override public boolean canFormat(Match match) { - return match.getValue() instanceof MoviePart; + return match.getValue() instanceof Movie; } - @Override public String preview(Match match) { - return format(match, null); - } - - - @Override - public String format(Match match, Map context) { - MoviePart video = (MoviePart) match.getValue(); - Formatter name = new Formatter(new StringBuilder()); + Movie movie = (Movie) match.getValue(); + StringBuilder name = new StringBuilder(); // 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) { - name.format(".CD%d", video.getPartIndex()); + if (movie instanceof MoviePart) { + MoviePart part = (MoviePart) movie; + if (part.getPartCount() > 1) { + name.append(".CD").append(part.getPartIndex()); + } } // 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); + } + } diff --git a/source/net/filebot/ui/rename/NamesListTransferablePolicy.java b/source/net/filebot/ui/rename/NamesListTransferablePolicy.java index 9d4f3aef..cac05265 100644 --- a/source/net/filebot/ui/rename/NamesListTransferablePolicy.java +++ b/source/net/filebot/ui/rename/NamesListTransferablePolicy.java @@ -2,10 +2,12 @@ package net.filebot.ui.rename; import static java.awt.datatransfer.DataFlavor.*; import static java.util.Arrays.*; +import static java.util.stream.Collectors.*; import static net.filebot.MediaTypes.*; import static net.filebot.hash.VerificationUtilities.*; import static net.filebot.ui.transfer.FileTransferable.*; import static net.filebot.util.FileUtilities.*; +import static net.filebot.util.RegularExpressions.*; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; @@ -64,21 +66,12 @@ class NamesListTransferablePolicy extends FileTransferablePolicy { load(getFilesFromTransferable(tr), action); } else if (tr.isDataFlavorSupported(stringFlavor)) { // string transferable - load((String) tr.getTransferData(stringFlavor)); + load(tr.getTransferData(stringFlavor).toString()); } } protected void load(String string) { - List values = new ArrayList(); - Scanner scanner = new Scanner(string); - - while (scanner.hasNextLine()) { - String line = scanner.nextLine().trim(); - if (line.length() > 0) { - values.add(normalizePathSeparators(line)); - } - } - + List values = NEWLINE.splitAsStream(string).map(String::trim).filter(s -> s.length() > 0).map(s -> normalizePathSeparators(s)).collect(toList()); model.addAll(values); } diff --git a/source/net/filebot/ui/rename/RenameModel.java b/source/net/filebot/ui/rename/RenameModel.java index 0b164e83..8d38bade 100644 --- a/source/net/filebot/ui/rename/RenameModel.java +++ b/source/net/filebot/ui/rename/RenameModel.java @@ -45,13 +45,12 @@ public class RenameModel extends MatchModel { @Override public String preview(Match match) { - return format(match, null); + return replacePathSeparators(String.valueOf(match.getValue())).trim(); // clean up path separators like / or \ } @Override - public String format(Match match, Map context) { - // clean up path separators like / or \ - return replacePathSeparators(String.valueOf(match.getValue())).trim(); + public String format(Match match, boolean extension, Map context) { + return preview(match); } }; @@ -71,6 +70,9 @@ public class RenameModel extends MatchModel { public void setPreserveExtension(boolean preserveExtension) { this.preserveExtension = preserveExtension; + + // update formatted names + names.refresh(); } public Map getRenameMap() { @@ -213,7 +215,7 @@ public class RenameModel extends MatchModel { Match match = getMatch(index); // 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 if (type == ListEvent.INSERT) { @@ -261,7 +263,7 @@ public class RenameModel extends MatchModel { for (int i = 0; i < size(); i++) { FormattedFuture obsolete = futures.get(i); Match 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 cancel(futures.set(i, future)); @@ -308,12 +310,14 @@ public class RenameModel extends MatchModel { public static class FormattedFuture extends SwingWorker { private final Match match; + private final boolean extension; private final Map context; private final MatchFormatter formatter; - private FormattedFuture(Match match, MatchFormatter formatter, Map context) { + private FormattedFuture(Match match, boolean extension, MatchFormatter formatter, Map context) { this.match = match; + this.extension = extension; this.formatter = formatter; this.context = context; } @@ -332,7 +336,7 @@ public class RenameModel extends MatchModel { @Override protected String doInBackground() throws Exception { - return formatter.format(match, context).trim(); + return formatter.format(match, extension, context).trim(); } @Override diff --git a/source/net/filebot/ui/rename/RenamePanel.java b/source/net/filebot/ui/rename/RenamePanel.java index 72afbbd7..2b6882f8 100644 --- a/source/net/filebot/ui/rename/RenamePanel.java +++ b/source/net/filebot/ui/rename/RenamePanel.java @@ -154,10 +154,10 @@ public class RenamePanel extends JComponent { renameModel.useFormatter(File.class, new ExpressionFormatter(persistentFileFormat.getValue(), new FileNameFormat(), File.class)); } catch (Exception e) { // make sure to put File formatter at position 3 - renameModel.useFormatter(File.class, new FileNameFormatter(renameModel.preserveExtension())); + renameModel.useFormatter(File.class, new FileNameFormatter()); } finally { // 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()); @@ -560,14 +560,14 @@ public class RenamePanel extends JComponent { } else if (binding.getInfoObject() instanceof File) { return Mode.File; } 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 { return Mode.valueOf(persistentLastFormatState.getValue()); // restore previous mode } catch (Exception e) { - debug.log(Level.WARNING, e.getMessage(), e); + debug.log(Level.WARNING, e, e::getMessage); } return Mode.Episode; // default to Episode mode @@ -788,9 +788,6 @@ public class RenamePanel extends JComponent { public void actionPerformed(ActionEvent evt) { renameModel.setPreserveExtension(!activate); - // use different file name formatter - renameModel.useFormatter(FileInfo.class, new FileNameFormatter(renameModel.preserveExtension())); - // display changed state filesList.repaint(); } diff --git a/source/net/filebot/util/ui/SwingUI.java b/source/net/filebot/util/ui/SwingUI.java index fb74dc6e..87f02e4b 100644 --- a/source/net/filebot/util/ui/SwingUI.java +++ b/source/net/filebot/util/ui/SwingUI.java @@ -321,7 +321,7 @@ public final class SwingUI { window.ifPresent(w -> w.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR))); runnable.run(); } catch (Exception e) { - debug.log(Level.SEVERE, e, e::toString); + debug.log(Level.SEVERE, e, e::getMessage); } finally { window.ifPresent(w -> w.setCursor(Cursor.getDefaultCursor())); }