* use ExpressionFormat in ListPanel

This commit is contained in:
Reinhard Pointner 2009-05-13 18:18:58 +00:00
parent a6c82fc8e5
commit cda76bb77a
1 changed files with 56 additions and 37 deletions

View File

@ -2,20 +2,25 @@
package net.sourceforge.filebot.ui.panel.list; 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.max;
import static java.lang.Math.min; import static java.lang.Math.min;
import static java.lang.Math.signum; import static java.lang.Math.signum;
import java.awt.BorderLayout; import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger; 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.AbstractAction;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JComponent; import javax.swing.JComponent;
@ -28,21 +33,21 @@ import javax.swing.SpinnerNumberModel;
import javax.swing.JSpinner.NumberEditor; import javax.swing.JSpinner.NumberEditor;
import net.miginfocom.swing.MigLayout; import net.miginfocom.swing.MigLayout;
import net.sourceforge.filebot.format.ExpressionFormat;
import net.sourceforge.filebot.similarity.SeriesNameMatcher; import net.sourceforge.filebot.similarity.SeriesNameMatcher;
import net.sourceforge.filebot.ui.FileBotList; import net.sourceforge.filebot.ui.FileBotList;
import net.sourceforge.filebot.ui.FileBotListExportHandler; import net.sourceforge.filebot.ui.FileBotListExportHandler;
import net.sourceforge.filebot.ui.transfer.LoadAction; import net.sourceforge.filebot.ui.transfer.LoadAction;
import net.sourceforge.filebot.ui.transfer.SaveAction; import net.sourceforge.filebot.ui.transfer.SaveAction;
import net.sourceforge.tuned.ExceptionUtilities;
import net.sourceforge.tuned.ui.TunedUtilities; import net.sourceforge.tuned.ui.TunedUtilities;
public class ListPanel extends JComponent { public class ListPanel extends JComponent {
private static final String INDEX_VARIABLE = "<i>";
private FileBotList<String> list = new FileBotList<String>(); private FileBotList<String> list = new FileBotList<String>();
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 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);
@ -51,6 +56,8 @@ public class ListPanel extends JComponent {
list.setTitle("Title"); list.setTitle("Title");
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));
@ -62,14 +69,14 @@ public class ListPanel extends JComponent {
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!][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"); add(textField, "gap related, wmin 2cm, sizegroupy editor");
add(new JLabel("From:"), "gap 5mm"); 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(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(new JButton(createAction), "gap 7mm, gapafter indent, wrap paragraph");
add(list, "grow"); add(list, "grow");
@ -86,27 +93,36 @@ public class ListPanel extends JComponent {
private AbstractAction createAction = new AbstractAction("Create") { private AbstractAction createAction = new AbstractAction("Create") {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent evt) {
int from = fromSpinnerModel.getNumber().intValue(); int from = fromSpinnerModel.getNumber().intValue();
int to = toSpinnerModel.getNumber().intValue(); int to = toSpinnerModel.getNumber().intValue();
String pattern = textField.getText(); try {
ExpressionFormat format = new ExpressionFormat(textField.getText());
if (!pattern.contains(INDEX_VARIABLE)) { // pad episode numbers with zeros (e.g. %02d) so all numbers have the same number of digits
Logger.getLogger("ui").warning(String.format("Pattern must contain index variable %s.", INDEX_VARIABLE));
return;
}
// pad episode numbers with zeros (e.g. %02d) so all episode 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>();
for (int i = min(from, to); i <= max(from, to); i++) { int min = min(from, to);
names.add(pattern.replaceAll(Pattern.quote(INDEX_VARIABLE), numberFormat.format(i))); 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) { if (signum(to - from) < 0) {
@ -120,6 +136,9 @@ public class ListPanel extends JComponent {
list.getModel().clear(); list.getModel().clear();
list.getModel().addAll(names); list.getModel().addAll(names);
} catch (Exception e) {
Logger.getLogger("ui").log(Level.WARNING, ExceptionUtilities.getMessage(e), e);
}
} }
}; };