Refactor ValidateDialog

This commit is contained in:
Reinhard Pointner 2016-08-17 05:24:27 +08:00
parent ad4befb36b
commit 51bc124062
1 changed files with 79 additions and 91 deletions

View File

@ -1,5 +1,6 @@
package net.filebot.ui.rename; package net.filebot.ui.rename;
import static java.util.Arrays.*;
import static java.util.Collections.*; import static java.util.Collections.*;
import static net.filebot.Logging.*; import static net.filebot.Logging.*;
import static net.filebot.Settings.*; import static net.filebot.Settings.*;
@ -15,7 +16,6 @@ import java.awt.event.KeyEvent;
import java.io.File; import java.io.File;
import java.util.AbstractList; import java.util.AbstractList;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
@ -26,10 +26,10 @@ import javax.swing.Action;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JComponent; import javax.swing.JComponent;
import javax.swing.JDialog; import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList; import javax.swing.JList;
import javax.swing.JScrollPane; import javax.swing.JScrollPane;
import javax.swing.KeyStroke; import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.text.BadLocationException; import javax.swing.text.BadLocationException;
import net.filebot.ResourceManager; import net.filebot.ResourceManager;
@ -41,17 +41,78 @@ class ValidateDialog extends JDialog {
private File[] model; private File[] model;
private boolean cancelled = true; private boolean cancel = true;
public ValidateDialog(Window owner, Collection<File> source) { public ValidateDialog(Window owner, Collection<File> source) {
super(owner, "Invalid Names", ModalityType.DOCUMENT_MODAL); super(owner, "Illegal Characters", ModalityType.DOCUMENT_MODAL);
model = source.toArray(new File[0]); model = source.toArray(new File[0]);
list = new JList(model); list = new JList(model);
list.setEnabled(false); list.setEnabled(false);
list.setCellRenderer(new IllegalCharactersListCellRenderer());
list.setCellRenderer(new HighlightListCellRenderer(ILLEGAL_CHARACTERS, new CharacterHighlightPainter(new Color(0xFF4200), new Color(0xFF1200)), 4) { JComponent c = (JComponent) getContentPane();
c.setLayout(new MigLayout("insets dialog, nogrid, fill", "", "[fill][pref!]"));
c.add(new JScrollPane(list), "grow, wrap");
c.add(new JButton(cancelAction), "tag left");
c.add(new JButton(validateAction), "tag next");
c.add(new JButton(continueAction), "tag ok");
// focus "Validate" button
SwingUtilities.invokeLater(() -> c.getComponent(2).requestFocusInWindow());
installAction(c, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelAction);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setMinimumSize(new Dimension(365, 280));
pack();
}
public List<File> getModel() {
return unmodifiableList(asList(model));
}
public boolean cancel() {
return cancel;
}
private void finish(boolean cancel) {
this.cancel = cancel;
setVisible(false);
}
private final Action validateAction = new AbstractAction("Validate", ResourceManager.getIcon("dialog.continue")) {
@Override
public void actionPerformed(ActionEvent evt) {
// validate names
for (int i = 0; i < model.length; i++) {
// remove illegal characters
model[i] = validateFilePath(model[i]);
}
// update view
list.repaint();
// switch icon
continueAction.putValue(SMALL_ICON, getValue(SMALL_ICON));
// disable this action
setEnabled(false);
}
};
private final Action continueAction = newAction("Continue", ResourceManager.getIcon("dialog.continue.invalid"), evt -> finish(false));
private final Action cancelAction = newAction("Cancel", ResourceManager.getIcon("dialog.cancel"), evt -> finish(true));
private static class IllegalCharactersListCellRenderer extends HighlightListCellRenderer {
public IllegalCharactersListCellRenderer() {
super(ILLEGAL_CHARACTERS, new CharacterHighlightPainter(new Color(0xFF4200), new Color(0xFF1200)), 4);
}
@Override @Override
protected void updateHighlighter() { protected void updateHighlighter() {
@ -75,76 +136,38 @@ class ValidateDialog extends JDialog {
} }
} }
} }
});
JLabel label = new JLabel("Some filenames contain invalid characters:");
JComponent content = (JComponent) getContentPane();
content.setLayout(new MigLayout("insets dialog, nogrid, fill", "", "[pref!][fill][pref!]"));
content.add(label, "wrap");
content.add(new JScrollPane(list), "grow, wrap 2mm");
content.add(new JButton(validateAction), "align center");
content.add(new JButton(continueAction), "gap related");
content.add(new JButton(cancelAction), "gap 12mm");
installAction(content, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelAction);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setMinimumSize(new Dimension(365, 280));
pack();
} }
public List<File> getModel() { private static class IndexView<E> extends AbstractList<E> {
return unmodifiableList(Arrays.asList(model));
private final List<Integer> mapping = new ArrayList<Integer>();
private final List<E> source;
public IndexView(List<E> source) {
this.source = source;
} }
public boolean isCancelled() { public boolean addIndex(int index) {
return cancelled; return mapping.add(index);
} }
private void finish(boolean cancelled) {
this.cancelled = cancelled;
setVisible(false);
dispose();
}
private final Action validateAction = new AbstractAction("Validate", ResourceManager.getIcon("dialog.continue")) {
@Override @Override
public void actionPerformed(ActionEvent e) { public E get(int index) {
// validate names int sourceIndex = mapping.get(index);
for (int i = 0; i < model.length; i++) { return sourceIndex >= 0 ? source.get(sourceIndex) : null;
// remove illegal characters
model[i] = validateFilePath(model[i]);
} }
// update view
list.repaint();
// switch icon
continueAction.putValue(SMALL_ICON, getValue(SMALL_ICON));
// disable this action
setEnabled(false);
}
};
private final Action continueAction = new AbstractAction("Continue", ResourceManager.getIcon("dialog.continue.invalid")) {
@Override @Override
public void actionPerformed(ActionEvent e) { public E set(int index, E element) {
finish(false); return source.set(mapping.get(index), element);
} }
};
private final Action cancelAction = new AbstractAction("Cancel", ResourceManager.getIcon("dialog.cancel")) {
@Override @Override
public void actionPerformed(ActionEvent e) { public int size() {
finish(true); return mapping.size();
}
} }
};
public static boolean validate(Component parent, List<File> source) { public static boolean validate(Component parent, List<File> source) {
IndexView<File> invalidFilePaths = new IndexView<File>(source); IndexView<File> invalidFilePaths = new IndexView<File>(source);
@ -167,7 +190,7 @@ class ValidateDialog extends JDialog {
// show and block // show and block
dialog.setVisible(true); dialog.setVisible(true);
if (dialog.isCancelled()) { if (dialog.cancel()) {
// no output // no output
return false; return false;
} }
@ -182,39 +205,4 @@ class ValidateDialog extends JDialog {
return true; return true;
} }
private static class IndexView<E> extends AbstractList<E> {
private final List<Integer> mapping = new ArrayList<Integer>();
private final List<E> source;
public IndexView(List<E> source) {
this.source = source;
}
public boolean addIndex(int index) {
return mapping.add(index);
}
@Override
public E get(int index) {
int sourceIndex = mapping.get(index);
if (sourceIndex >= 0)
return source.get(sourceIndex);
return null;
}
@Override
public E set(int index, E element) {
return source.set(mapping.get(index), element);
}
@Override
public int size() {
return mapping.size();
}
}
} }