1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-12-23 16:28:51 -05:00

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
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);

View File

@ -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

View File

@ -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;
}

View File

@ -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);
}
}

View File

@ -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<String> values = new ArrayList<String>();
Scanner scanner = new Scanner(string);
while (scanner.hasNextLine()) {
String line = scanner.nextLine().trim();
if (line.length() > 0) {
values.add(normalizePathSeparators(line));
}
}
List<String> values = NEWLINE.splitAsStream(string).map(String::trim).filter(s -> s.length() > 0).map(s -> normalizePathSeparators(s)).collect(toList());
model.addAll(values);
}

View File

@ -45,13 +45,12 @@ public class RenameModel extends MatchModel<Object, File> {
@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<Object, File> {
public void setPreserveExtension(boolean preserveExtension) {
this.preserveExtension = preserveExtension;
// update formatted names
names.refresh();
}
public Map<File, File> getRenameMap() {
@ -213,7 +215,7 @@ public class RenameModel extends MatchModel<Object, File> {
Match<Object, File> 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<Object, File> {
for (int i = 0; i < size(); i++) {
FormattedFuture obsolete = futures.get(i);
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
cancel(futures.set(i, future));
@ -308,12 +310,14 @@ public class RenameModel extends MatchModel<Object, File> {
public static class FormattedFuture extends SwingWorker<String, Void> {
private final Match<Object, File> match;
private final boolean extension;
private final Map<File, Object> context;
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.extension = extension;
this.formatter = formatter;
this.context = context;
}
@ -332,7 +336,7 @@ public class RenameModel extends MatchModel<Object, File> {
@Override
protected String doInBackground() throws Exception {
return formatter.format(match, context).trim();
return formatter.format(match, extension, context).trim();
}
@Override

View File

@ -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();
}

View File

@ -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()));
}