Refactor SelectDialog

This commit is contained in:
Reinhard Pointner 2016-04-08 22:59:30 +00:00
parent d73934f09b
commit d31d24856c
4 changed files with 38 additions and 37 deletions

View File

@ -367,6 +367,7 @@ public abstract class AbstractSearchPanel<S, E> extends JComponent {
selectDialog.setLocation(getOffsetLocation(selectDialog.getOwner())); selectDialog.setLocation(getOffsetLocation(selectDialog.getOwner()));
selectDialog.setIconImage(getImage(getIcon())); selectDialog.setIconImage(getImage(getIcon()));
selectDialog.setMinimumSize(new Dimension(250, 150)); selectDialog.setMinimumSize(new Dimension(250, 150));
selectDialog.pack();
} }
public long getDuration() { public long getDuration() {

View File

@ -8,11 +8,10 @@ import java.awt.Dimension;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File; import java.io.File;
import java.util.Collection; import java.util.Collection;
import java.util.prefs.Preferences;
import javax.swing.AbstractAction;
import javax.swing.Action; import javax.swing.Action;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JCheckBox; import javax.swing.JCheckBox;
@ -27,7 +26,6 @@ import javax.swing.SwingUtilities;
import net.filebot.ResourceManager; import net.filebot.ResourceManager;
import net.filebot.util.ui.DefaultFancyListCellRenderer; import net.filebot.util.ui.DefaultFancyListCellRenderer;
import net.filebot.util.ui.SwingUI;
import net.filebot.web.SearchResult; import net.filebot.web.SearchResult;
import net.miginfocom.swing.MigLayout; import net.miginfocom.swing.MigLayout;
@ -36,9 +34,8 @@ public class SelectDialog<T> extends JDialog {
private JLabel headerLabel = new JLabel(); private JLabel headerLabel = new JLabel();
private JCheckBox autoRepeatCheckBox = new JCheckBox(); private JCheckBox autoRepeatCheckBox = new JCheckBox();
private JList list; private JList<T> list;
private String command = CANCEL;
private Action selectedAction = null;
public SelectDialog(Component parent, Collection<? extends T> options) { public SelectDialog(Component parent, Collection<? extends T> options) {
this(parent, options, false, false); this(parent, options, false, false);
@ -93,10 +90,9 @@ public class SelectDialog<T> extends JDialog {
// set default size and location // set default size and location
setMinimumSize(new Dimension(220, 240)); setMinimumSize(new Dimension(220, 240));
setSize(new Dimension(240, 260));
// Shortcut Enter // Shortcut Enter
SwingUI.installAction(list, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), selectAction); installAction(list, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), selectAction);
} }
protected String convertValueToString(Object value) { protected String convertValueToString(Object value) {
@ -135,16 +131,12 @@ public class SelectDialog<T> extends JDialog {
return autoRepeatCheckBox; return autoRepeatCheckBox;
} }
public Action getSelectedAction() { public String getSelectedAction() {
return selectedAction; return command;
} }
@SuppressWarnings("unchecked")
public T getSelectedValue() { public T getSelectedValue() {
if (selectedAction != selectAction) return SELECT.equals(command) ? list.getSelectedValue() : null;
return null;
return (T) list.getSelectedValue();
} }
public void close() { public void close() {
@ -160,32 +152,38 @@ public class SelectDialog<T> extends JDialog {
return cancelAction; return cancelAction;
} }
private final Action selectAction = new AbstractAction("Select", ResourceManager.getIcon("dialog.continue")) { public static final String SELECT = "Select";
public static final String CANCEL = "Cancel";
@Override private final Action selectAction = newAction(SELECT, ResourceManager.getIcon("dialog.continue"), evt -> {
public void actionPerformed(ActionEvent e) { command = SELECT;
selectedAction = this;
close(); close();
} });
};
private final Action cancelAction = new AbstractAction("Cancel", ResourceManager.getIcon("dialog.cancel")) { private final Action cancelAction = newAction(CANCEL, ResourceManager.getIcon("dialog.cancel"), evt -> {
command = CANCEL;
@Override
public void actionPerformed(ActionEvent e) {
selectedAction = this;
close(); close();
} });
};
private final MouseAdapter mouseListener = new MouseAdapter() { private final MouseAdapter mouseListener = mouseClicked(evt -> {
if (SwingUtilities.isLeftMouseButton(evt) && (evt.getClickCount() == 2)) {
selectAction.actionPerformed(new ActionEvent(evt.getSource(), ActionEvent.ACTION_PERFORMED, SELECT));
}
});
@Override private static final String KEY_REPEAT = "dialog.select.repeat";
public void mouseClicked(MouseEvent e) { private static final String KEY_WIDTH = "dialog.select.width";
if (SwingUtilities.isLeftMouseButton(e) && (e.getClickCount() == 2)) { private static final String KEY_HEIGHT = "dialog.select.height";
selectAction.actionPerformed(null);
public void saveState(Preferences prefs) {
prefs.putBoolean(KEY_REPEAT, autoRepeatCheckBox.isSelected());
prefs.putInt(KEY_WIDTH, getWidth());
prefs.putInt(KEY_HEIGHT, getHeight());
} }
public void restoreState(Preferences prefs) {
autoRepeatCheckBox.setSelected(prefs.getBoolean(KEY_REPEAT, autoRepeatCheckBox.isSelected()));
setSize(prefs.getInt(KEY_WIDTH, getWidth()), prefs.getInt(KEY_HEIGHT, getHeight()));
} }
};
} }

View File

@ -279,6 +279,7 @@ public class SfvPanel extends JComponent {
}; };
selectDialog.getHeaderLabel().setText("Select checksum column:"); selectDialog.getHeaderLabel().setText("Select checksum column:");
selectDialog.pack();
selectDialog.setLocationRelativeTo(SfvPanel.this); selectDialog.setLocationRelativeTo(SfvPanel.this);
selectDialog.setVisible(true); selectDialog.setVisible(true);

View File

@ -68,6 +68,7 @@ class MovieEditor implements TableCellEditor {
} }
SelectDialog<Movie> dialog = new SelectDialog<Movie>(table, options); SelectDialog<Movie> dialog = new SelectDialog<Movie>(table, options);
dialog.pack();
dialog.setLocation(getOffsetLocation(dialog.getOwner())); dialog.setLocation(getOffsetLocation(dialog.getOwner()));
dialog.setVisible(true); dialog.setVisible(true);
Movie selectedValue = dialog.getSelectedValue(); Movie selectedValue = dialog.getSelectedValue();