diff --git a/source/net/sourceforge/filebot/ui/panel/list/ListPanel.java b/source/net/sourceforge/filebot/ui/panel/list/ListPanel.java index fd5e14b4..dccee8f5 100644 --- a/source/net/sourceforge/filebot/ui/panel/list/ListPanel.java +++ b/source/net/sourceforge/filebot/ui/panel/list/ListPanel.java @@ -2,20 +2,25 @@ package net.sourceforge.filebot.ui.panel.list; +import static java.awt.Font.MONOSPACED; +import static java.awt.Font.PLAIN; import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.signum; import java.awt.BorderLayout; +import java.awt.Font; import java.awt.event.ActionEvent; import java.text.NumberFormat; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.logging.Level; import java.util.logging.Logger; -import java.util.regex.Pattern; +import javax.script.Bindings; +import javax.script.SimpleBindings; import javax.swing.AbstractAction; import javax.swing.JButton; import javax.swing.JComponent; @@ -28,21 +33,21 @@ import javax.swing.SpinnerNumberModel; import javax.swing.JSpinner.NumberEditor; import net.miginfocom.swing.MigLayout; +import net.sourceforge.filebot.format.ExpressionFormat; import net.sourceforge.filebot.similarity.SeriesNameMatcher; import net.sourceforge.filebot.ui.FileBotList; import net.sourceforge.filebot.ui.FileBotListExportHandler; import net.sourceforge.filebot.ui.transfer.LoadAction; import net.sourceforge.filebot.ui.transfer.SaveAction; +import net.sourceforge.tuned.ExceptionUtilities; import net.sourceforge.tuned.ui.TunedUtilities; public class ListPanel extends JComponent { - private static final String INDEX_VARIABLE = ""; - private FileBotList list = new FileBotList(); - private JTextField textField = new JTextField(String.format("Name - %s", INDEX_VARIABLE), 25); + private JTextField textField = new JTextField("Name - {i}", 25); private SpinnerNumberModel fromSpinnerModel = new SpinnerNumberModel(1, 0, Integer.MAX_VALUE, 1); private SpinnerNumberModel toSpinnerModel = new SpinnerNumberModel(20, 0, Integer.MAX_VALUE, 1); @@ -51,6 +56,8 @@ public class ListPanel extends JComponent { list.setTitle("Title"); + textField.setFont(new Font(MONOSPACED, PLAIN, 11)); + list.setTransferablePolicy(new FileListTransferablePolicy(list)); list.setExportHandler(new FileBotListExportHandler(list)); @@ -62,14 +69,14 @@ public class ListPanel extends JComponent { fromSpinner.setEditor(new NumberEditor(fromSpinner, "#")); toSpinner.setEditor(new NumberEditor(toSpinner, "#")); - setLayout(new MigLayout("nogrid, fill, insets dialog", "align center", "[pref!][fill]")); + setLayout(new MigLayout("nogrid, fill, insets dialog", "align center", "[pref!, center][fill]")); add(new JLabel("Pattern:"), "gapbefore indent"); - add(textField, "gap related, wmin 2cm"); + add(textField, "gap related, wmin 2cm, sizegroupy editor"); add(new JLabel("From:"), "gap 5mm"); - add(fromSpinner, "gap related, wmax 14mm, sizegroup spinner"); + add(fromSpinner, "gap related, wmax 14mm, sizegroup spinner, sizegroupy editor"); add(new JLabel("To:"), "gap 5mm"); - add(toSpinner, "gap related, wmax 14mm, sizegroup spinner"); + add(toSpinner, "gap related, wmax 14mm, sizegroup spinner, sizegroupy editor"); add(new JButton(createAction), "gap 7mm, gapafter indent, wrap paragraph"); add(list, "grow"); @@ -86,40 +93,52 @@ public class ListPanel extends JComponent { private AbstractAction createAction = new AbstractAction("Create") { - public void actionPerformed(ActionEvent e) { + public void actionPerformed(ActionEvent evt) { int from = fromSpinnerModel.getNumber().intValue(); int to = toSpinnerModel.getNumber().intValue(); - String pattern = textField.getText(); - - if (!pattern.contains(INDEX_VARIABLE)) { - Logger.getLogger("ui").warning(String.format("Pattern must contain index variable %s.", INDEX_VARIABLE)); - return; + 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("min", min); + bindings.put("max", max); + + names.add(format.format(bindings, new StringBuffer()).toString()); + } + + 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) { + Logger.getLogger("ui").log(Level.WARNING, ExceptionUtilities.getMessage(e), e); } - - // pad episode numbers with zeros (e.g. %02d) so all episode 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(); - - for (int i = min(from, to); i <= max(from, to); i++) { - names.add(pattern.replaceAll(Pattern.quote(INDEX_VARIABLE), numberFormat.format(i))); - } - - 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); } };