diff --git a/source/net/filebot/format/ExpressionFormat.java b/source/net/filebot/format/ExpressionFormat.java index 1b7da84a..9a825ae9 100644 --- a/source/net/filebot/format/ExpressionFormat.java +++ b/source/net/filebot/format/ExpressionFormat.java @@ -1,6 +1,5 @@ package net.filebot.format; -import static net.filebot.similarity.Normalization.*; import static net.filebot.util.ExceptionUtilities.*; import static net.filebot.util.FileUtilities.*; import groovy.lang.GroovyClassLoader; @@ -24,6 +23,8 @@ import javax.script.ScriptEngine; import javax.script.ScriptException; import javax.script.SimpleScriptContext; +import net.filebot.similarity.Normalization; + import org.codehaus.groovy.control.CompilerConfiguration; import org.codehaus.groovy.control.MultipleCompilationErrorsException; import org.codehaus.groovy.control.customizers.ImportCustomizer; @@ -163,10 +164,10 @@ public class ExpressionFormat extends Format { @Override 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 Bindings priviledgedBindings = PrivilegedInvocation.newProxy(Bindings.class, bindings, AccessController.getContext()); @@ -177,11 +178,11 @@ public class ExpressionFormat extends Format { // reset exception state lastException = null; + StringBuilder sb = new StringBuilder(); for (Object snipped : compilation) { if (snipped instanceof CompiledScript) { try { Object value = normalizeExpressionValue(((CompiledScript) snipped).eval(context)); - if (value != null) { 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) { // if the binding value is a String, remove illegal characters 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 diff --git a/source/net/filebot/ui/list/ListPanel.java b/source/net/filebot/ui/list/ListPanel.java index b6d4bfad..469974ee 100644 --- a/source/net/filebot/ui/list/ListPanel.java +++ b/source/net/filebot/ui/list/ListPanel.java @@ -1,7 +1,5 @@ - package net.filebot.ui.list; - import static java.awt.Font.*; import static java.lang.Math.*; import static net.filebot.ui.NotificationLogging.*; @@ -30,7 +28,6 @@ import javax.swing.JTextField; import javax.swing.KeyStroke; import javax.swing.SpinnerNumberModel; -import net.miginfocom.swing.MigLayout; import net.filebot.format.ExpressionFormat; import net.filebot.similarity.SeriesNameMatcher; import net.filebot.ui.FileBotList; @@ -39,35 +36,34 @@ import net.filebot.ui.transfer.LoadAction; import net.filebot.ui.transfer.SaveAction; import net.filebot.util.ExceptionUtilities; import net.filebot.util.ui.TunedUtilities; - +import net.miginfocom.swing.MigLayout; public class ListPanel extends JComponent { - + private FileBotList list = new FileBotList(); - + private JTextField textField = new JTextField("Name - {i}", 30); private SpinnerNumberModel fromSpinnerModel = new SpinnerNumberModel(1, 0, Integer.MAX_VALUE, 1); private SpinnerNumberModel toSpinnerModel = new SpinnerNumberModel(20, 0, Integer.MAX_VALUE, 1); - public ListPanel() { list.setTitle("Title"); - + textField.setFont(new Font(MONOSPACED, PLAIN, 11)); - + list.setTransferablePolicy(new FileListTransferablePolicy(list)); list.setExportHandler(new FileBotListExportHandler(list)); - + list.getRemoveAction().setEnabled(true); - + JSpinner fromSpinner = new JSpinner(fromSpinnerModel); JSpinner toSpinner = new JSpinner(toSpinnerModel); - + fromSpinner.setEditor(new NumberEditor(fromSpinner, "#")); toSpinner.setEditor(new NumberEditor(toSpinner, "#")); - + setLayout(new MigLayout("nogrid, fill, insets dialog", "align center", "[pref!, center][fill]")); - + add(new JLabel("Pattern:"), "gapbefore indent"); add(textField, "gap related, wmin 2cm, sizegroupy editor"); add(new JLabel("From:"), "gap 5mm"); @@ -75,67 +71,66 @@ public class ListPanel extends JComponent { add(new JLabel("To:"), "gap 5mm"); add(toSpinner, "gap related, wmax 14mm, sizegroup spinner, sizegroupy editor"); add(new JButton(createAction), "gap 7mm, gapafter indent, wrap paragraph"); - + add(list, "grow"); - + // panel with buttons that will be added inside the list component 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 SaveAction(list.getExportHandler())), "gap related"); - + list.add(buttonPanel, BorderLayout.SOUTH); - + TunedUtilities.installAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), createAction); } - private AbstractAction createAction = new AbstractAction("Create") { - + @Override public void actionPerformed(ActionEvent evt) { - + // clear selection list.getListComponent().clearSelection(); - + int from = fromSpinnerModel.getNumber().intValue(); int to = toSpinnerModel.getNumber().intValue(); - + try { ExpressionFormat format = new ExpressionFormat(textField.getText()); - + // pad episode numbers with zeros (e.g. %02d) so all numbers have the same number of digits NumberFormat numberFormat = NumberFormat.getIntegerInstance(); numberFormat.setMinimumIntegerDigits(max(2, Integer.toString(max(from, to)).length())); numberFormat.setGroupingUsed(false); - + List names = new ArrayList(); - + int min = min(from, to); int max = max(from, to); - + for (int i = min; i <= max; i++) { Bindings bindings = new SimpleBindings(); - + // strings bindings.put("i", numberFormat.format(i)); - + // numbers bindings.put("index", i); bindings.put("from", from); bindings.put("to", to); - - names.add(format.format(bindings, new StringBuffer()).toString()); + + names.add(format.format(bindings)); } - + if (signum(to - from) < 0) { Collections.reverse(names); } - + // try to match title from the first five names Collection 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.getModel().clear(); list.getModel().addAll(names); } catch (Exception e) { @@ -143,5 +138,5 @@ public class ListPanel extends JComponent { } } }; - + }