1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-11-10 11:25:04 -05:00

* properly deal with space sequences by default

This commit is contained in:
Reinhard Pointner 2014-06-28 06:07:45 +00:00
parent 1a4c3691b2
commit 07568068c5
2 changed files with 38 additions and 42 deletions

View File

@ -1,6 +1,5 @@
package net.filebot.format; package net.filebot.format;
import static net.filebot.similarity.Normalization.*;
import static net.filebot.util.ExceptionUtilities.*; import static net.filebot.util.ExceptionUtilities.*;
import static net.filebot.util.FileUtilities.*; import static net.filebot.util.FileUtilities.*;
import groovy.lang.GroovyClassLoader; import groovy.lang.GroovyClassLoader;
@ -24,6 +23,8 @@ import javax.script.ScriptEngine;
import javax.script.ScriptException; import javax.script.ScriptException;
import javax.script.SimpleScriptContext; import javax.script.SimpleScriptContext;
import net.filebot.similarity.Normalization;
import org.codehaus.groovy.control.CompilerConfiguration; import org.codehaus.groovy.control.CompilerConfiguration;
import org.codehaus.groovy.control.MultipleCompilationErrorsException; import org.codehaus.groovy.control.MultipleCompilationErrorsException;
import org.codehaus.groovy.control.customizers.ImportCustomizer; import org.codehaus.groovy.control.customizers.ImportCustomizer;
@ -163,10 +164,10 @@ public class ExpressionFormat extends Format {
@Override @Override
public StringBuffer format(Object object, StringBuffer sb, FieldPosition pos) { public StringBuffer format(Object object, StringBuffer sb, FieldPosition pos) {
return format(getBindings(object), sb); return sb.append(format(getBindings(object)));
} }
public StringBuffer format(Bindings bindings, StringBuffer sb) { public String format(Bindings bindings) {
// use privileged bindings so we are not restricted by the script sandbox // use privileged bindings so we are not restricted by the script sandbox
Bindings priviledgedBindings = PrivilegedInvocation.newProxy(Bindings.class, bindings, AccessController.getContext()); Bindings priviledgedBindings = PrivilegedInvocation.newProxy(Bindings.class, bindings, AccessController.getContext());
@ -177,11 +178,11 @@ public class ExpressionFormat extends Format {
// reset exception state // reset exception state
lastException = null; lastException = null;
StringBuilder sb = new StringBuilder();
for (Object snipped : compilation) { for (Object snipped : compilation) {
if (snipped instanceof CompiledScript) { if (snipped instanceof CompiledScript) {
try { try {
Object value = normalizeExpressionValue(((CompiledScript) snipped).eval(context)); Object value = normalizeExpressionValue(((CompiledScript) snipped).eval(context));
if (value != null) { if (value != null) {
sb.append(value); sb.append(value);
} }
@ -193,13 +194,13 @@ public class ExpressionFormat extends Format {
} }
} }
return sb; return Normalization.replaceSpace(sb.toString(), " ").trim();
} }
protected Object normalizeBindingValue(Object value) { protected Object normalizeBindingValue(Object value) {
// if the binding value is a String, remove illegal characters // if the binding value is a String, remove illegal characters
if (value instanceof CharSequence) { if (value instanceof CharSequence) {
return replaceSpace(replacePathSeparators((CharSequence) value), " ").trim(); return replacePathSeparators((CharSequence) value, " ").trim();
} }
// if the binding value is an Object, just leave it // if the binding value is an Object, just leave it

View File

@ -1,7 +1,5 @@
package net.filebot.ui.list; package net.filebot.ui.list;
import static java.awt.Font.*; import static java.awt.Font.*;
import static java.lang.Math.*; import static java.lang.Math.*;
import static net.filebot.ui.NotificationLogging.*; import static net.filebot.ui.NotificationLogging.*;
@ -30,7 +28,6 @@ import javax.swing.JTextField;
import javax.swing.KeyStroke; import javax.swing.KeyStroke;
import javax.swing.SpinnerNumberModel; import javax.swing.SpinnerNumberModel;
import net.miginfocom.swing.MigLayout;
import net.filebot.format.ExpressionFormat; import net.filebot.format.ExpressionFormat;
import net.filebot.similarity.SeriesNameMatcher; import net.filebot.similarity.SeriesNameMatcher;
import net.filebot.ui.FileBotList; import net.filebot.ui.FileBotList;
@ -39,35 +36,34 @@ import net.filebot.ui.transfer.LoadAction;
import net.filebot.ui.transfer.SaveAction; import net.filebot.ui.transfer.SaveAction;
import net.filebot.util.ExceptionUtilities; import net.filebot.util.ExceptionUtilities;
import net.filebot.util.ui.TunedUtilities; import net.filebot.util.ui.TunedUtilities;
import net.miginfocom.swing.MigLayout;
public class ListPanel extends JComponent { public class ListPanel extends JComponent {
private FileBotList<String> list = new FileBotList<String>(); private FileBotList<String> list = new FileBotList<String>();
private JTextField textField = new JTextField("Name - {i}", 30); private JTextField textField = new JTextField("Name - {i}", 30);
private SpinnerNumberModel fromSpinnerModel = new SpinnerNumberModel(1, 0, Integer.MAX_VALUE, 1); private SpinnerNumberModel fromSpinnerModel = new SpinnerNumberModel(1, 0, Integer.MAX_VALUE, 1);
private SpinnerNumberModel toSpinnerModel = new SpinnerNumberModel(20, 0, Integer.MAX_VALUE, 1); private SpinnerNumberModel toSpinnerModel = new SpinnerNumberModel(20, 0, Integer.MAX_VALUE, 1);
public ListPanel() { public ListPanel() {
list.setTitle("Title"); list.setTitle("Title");
textField.setFont(new Font(MONOSPACED, PLAIN, 11)); textField.setFont(new Font(MONOSPACED, PLAIN, 11));
list.setTransferablePolicy(new FileListTransferablePolicy(list)); list.setTransferablePolicy(new FileListTransferablePolicy(list));
list.setExportHandler(new FileBotListExportHandler(list)); list.setExportHandler(new FileBotListExportHandler(list));
list.getRemoveAction().setEnabled(true); list.getRemoveAction().setEnabled(true);
JSpinner fromSpinner = new JSpinner(fromSpinnerModel); JSpinner fromSpinner = new JSpinner(fromSpinnerModel);
JSpinner toSpinner = new JSpinner(toSpinnerModel); JSpinner toSpinner = new JSpinner(toSpinnerModel);
fromSpinner.setEditor(new NumberEditor(fromSpinner, "#")); fromSpinner.setEditor(new NumberEditor(fromSpinner, "#"));
toSpinner.setEditor(new NumberEditor(toSpinner, "#")); toSpinner.setEditor(new NumberEditor(toSpinner, "#"));
setLayout(new MigLayout("nogrid, fill, insets dialog", "align center", "[pref!, center][fill]")); setLayout(new MigLayout("nogrid, fill, insets dialog", "align center", "[pref!, center][fill]"));
add(new JLabel("Pattern:"), "gapbefore indent"); add(new JLabel("Pattern:"), "gapbefore indent");
add(textField, "gap related, wmin 2cm, sizegroupy editor"); add(textField, "gap related, wmin 2cm, sizegroupy editor");
add(new JLabel("From:"), "gap 5mm"); add(new JLabel("From:"), "gap 5mm");
@ -75,67 +71,66 @@ public class ListPanel extends JComponent {
add(new JLabel("To:"), "gap 5mm"); add(new JLabel("To:"), "gap 5mm");
add(toSpinner, "gap related, wmax 14mm, sizegroup spinner, sizegroupy editor"); add(toSpinner, "gap related, wmax 14mm, sizegroup spinner, sizegroupy editor");
add(new JButton(createAction), "gap 7mm, gapafter indent, wrap paragraph"); add(new JButton(createAction), "gap 7mm, gapafter indent, wrap paragraph");
add(list, "grow"); add(list, "grow");
// panel with buttons that will be added inside the list component // panel with buttons that will be added inside the list component
JPanel buttonPanel = new JPanel(new MigLayout("insets 1.2mm, nogrid, fill", "align center")); JPanel buttonPanel = new JPanel(new MigLayout("insets 1.2mm, nogrid, fill", "align center"));
buttonPanel.add(new JButton(new LoadAction(list.getTransferablePolicy()))); buttonPanel.add(new JButton(new LoadAction(list.getTransferablePolicy())));
buttonPanel.add(new JButton(new SaveAction(list.getExportHandler())), "gap related"); buttonPanel.add(new JButton(new SaveAction(list.getExportHandler())), "gap related");
list.add(buttonPanel, BorderLayout.SOUTH); list.add(buttonPanel, BorderLayout.SOUTH);
TunedUtilities.installAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), createAction); TunedUtilities.installAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), createAction);
} }
private AbstractAction createAction = new AbstractAction("Create") { private AbstractAction createAction = new AbstractAction("Create") {
@Override @Override
public void actionPerformed(ActionEvent evt) { public void actionPerformed(ActionEvent evt) {
// clear selection // clear selection
list.getListComponent().clearSelection(); list.getListComponent().clearSelection();
int from = fromSpinnerModel.getNumber().intValue(); int from = fromSpinnerModel.getNumber().intValue();
int to = toSpinnerModel.getNumber().intValue(); int to = toSpinnerModel.getNumber().intValue();
try { try {
ExpressionFormat format = new ExpressionFormat(textField.getText()); ExpressionFormat format = new ExpressionFormat(textField.getText());
// pad episode numbers with zeros (e.g. %02d) so all numbers have the same number of digits // pad episode numbers with zeros (e.g. %02d) so all numbers have the same number of digits
NumberFormat numberFormat = NumberFormat.getIntegerInstance(); NumberFormat numberFormat = NumberFormat.getIntegerInstance();
numberFormat.setMinimumIntegerDigits(max(2, Integer.toString(max(from, to)).length())); numberFormat.setMinimumIntegerDigits(max(2, Integer.toString(max(from, to)).length()));
numberFormat.setGroupingUsed(false); numberFormat.setGroupingUsed(false);
List<String> names = new ArrayList<String>(); List<String> names = new ArrayList<String>();
int min = min(from, to); int min = min(from, to);
int max = max(from, to); int max = max(from, to);
for (int i = min; i <= max; i++) { for (int i = min; i <= max; i++) {
Bindings bindings = new SimpleBindings(); Bindings bindings = new SimpleBindings();
// strings // strings
bindings.put("i", numberFormat.format(i)); bindings.put("i", numberFormat.format(i));
// numbers // numbers
bindings.put("index", i); bindings.put("index", i);
bindings.put("from", from); bindings.put("from", from);
bindings.put("to", to); bindings.put("to", to);
names.add(format.format(bindings, new StringBuffer()).toString()); names.add(format.format(bindings));
} }
if (signum(to - from) < 0) { if (signum(to - from) < 0) {
Collections.reverse(names); Collections.reverse(names);
} }
// try to match title from the first five names // try to match title from the first five names
Collection<String> title = new SeriesNameMatcher().matchAll((names.size() < 5 ? names : names.subList(0, 4)).toArray(new String[0])); Collection<String> title = new SeriesNameMatcher().matchAll((names.size() < 5 ? names : names.subList(0, 4)).toArray(new String[0]));
list.setTitle(title.isEmpty() ? "List" : title.iterator().next()); list.setTitle(title.isEmpty() ? "List" : title.iterator().next());
list.getModel().clear(); list.getModel().clear();
list.getModel().addAll(names); list.getModel().addAll(names);
} catch (Exception e) { } catch (Exception e) {
@ -143,5 +138,5 @@ public class ListPanel extends JComponent {
} }
} }
}; };
} }