From d31d24856cf485fbff19371ed671c0400ea18bdf Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Fri, 8 Apr 2016 22:59:30 +0000 Subject: [PATCH] Refactor SelectDialog --- .../net/filebot/ui/AbstractSearchPanel.java | 1 + source/net/filebot/ui/SelectDialog.java | 72 +++++++++---------- source/net/filebot/ui/sfv/SfvPanel.java | 1 + .../ui/subtitle/upload/MovieEditor.java | 1 + 4 files changed, 38 insertions(+), 37 deletions(-) diff --git a/source/net/filebot/ui/AbstractSearchPanel.java b/source/net/filebot/ui/AbstractSearchPanel.java index e0a21a0f..92a5e580 100644 --- a/source/net/filebot/ui/AbstractSearchPanel.java +++ b/source/net/filebot/ui/AbstractSearchPanel.java @@ -367,6 +367,7 @@ public abstract class AbstractSearchPanel extends JComponent { selectDialog.setLocation(getOffsetLocation(selectDialog.getOwner())); selectDialog.setIconImage(getImage(getIcon())); selectDialog.setMinimumSize(new Dimension(250, 150)); + selectDialog.pack(); } public long getDuration() { diff --git a/source/net/filebot/ui/SelectDialog.java b/source/net/filebot/ui/SelectDialog.java index ff325a63..3536a72d 100644 --- a/source/net/filebot/ui/SelectDialog.java +++ b/source/net/filebot/ui/SelectDialog.java @@ -8,11 +8,10 @@ import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; import java.io.File; import java.util.Collection; +import java.util.prefs.Preferences; -import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.JButton; import javax.swing.JCheckBox; @@ -27,7 +26,6 @@ import javax.swing.SwingUtilities; import net.filebot.ResourceManager; import net.filebot.util.ui.DefaultFancyListCellRenderer; -import net.filebot.util.ui.SwingUI; import net.filebot.web.SearchResult; import net.miginfocom.swing.MigLayout; @@ -36,9 +34,8 @@ public class SelectDialog extends JDialog { private JLabel headerLabel = new JLabel(); private JCheckBox autoRepeatCheckBox = new JCheckBox(); - private JList list; - - private Action selectedAction = null; + private JList list; + private String command = CANCEL; public SelectDialog(Component parent, Collection options) { this(parent, options, false, false); @@ -93,10 +90,9 @@ public class SelectDialog extends JDialog { // set default size and location setMinimumSize(new Dimension(220, 240)); - setSize(new Dimension(240, 260)); // 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) { @@ -135,16 +131,12 @@ public class SelectDialog extends JDialog { return autoRepeatCheckBox; } - public Action getSelectedAction() { - return selectedAction; + public String getSelectedAction() { + return command; } - @SuppressWarnings("unchecked") public T getSelectedValue() { - if (selectedAction != selectAction) - return null; - - return (T) list.getSelectedValue(); + return SELECT.equals(command) ? list.getSelectedValue() : null; } public void close() { @@ -160,32 +152,38 @@ public class SelectDialog extends JDialog { 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 - public void actionPerformed(ActionEvent e) { - selectedAction = this; - close(); + private final Action selectAction = newAction(SELECT, ResourceManager.getIcon("dialog.continue"), evt -> { + command = SELECT; + close(); + }); + + private final Action cancelAction = newAction(CANCEL, ResourceManager.getIcon("dialog.cancel"), evt -> { + command = CANCEL; + close(); + }); + + private final MouseAdapter mouseListener = mouseClicked(evt -> { + if (SwingUtilities.isLeftMouseButton(evt) && (evt.getClickCount() == 2)) { + selectAction.actionPerformed(new ActionEvent(evt.getSource(), ActionEvent.ACTION_PERFORMED, SELECT)); } - }; + }); - private final Action cancelAction = new AbstractAction("Cancel", ResourceManager.getIcon("dialog.cancel")) { + private static final String KEY_REPEAT = "dialog.select.repeat"; + private static final String KEY_WIDTH = "dialog.select.width"; + private static final String KEY_HEIGHT = "dialog.select.height"; - @Override - public void actionPerformed(ActionEvent e) { - selectedAction = this; - close(); - } - }; + public void saveState(Preferences prefs) { + prefs.putBoolean(KEY_REPEAT, autoRepeatCheckBox.isSelected()); + prefs.putInt(KEY_WIDTH, getWidth()); + prefs.putInt(KEY_HEIGHT, getHeight()); + } - private final MouseAdapter mouseListener = new MouseAdapter() { - - @Override - public void mouseClicked(MouseEvent e) { - if (SwingUtilities.isLeftMouseButton(e) && (e.getClickCount() == 2)) { - selectAction.actionPerformed(null); - } - } - }; + public void restoreState(Preferences prefs) { + autoRepeatCheckBox.setSelected(prefs.getBoolean(KEY_REPEAT, autoRepeatCheckBox.isSelected())); + setSize(prefs.getInt(KEY_WIDTH, getWidth()), prefs.getInt(KEY_HEIGHT, getHeight())); + } } diff --git a/source/net/filebot/ui/sfv/SfvPanel.java b/source/net/filebot/ui/sfv/SfvPanel.java index 4ea615d3..1bd86ffe 100644 --- a/source/net/filebot/ui/sfv/SfvPanel.java +++ b/source/net/filebot/ui/sfv/SfvPanel.java @@ -279,6 +279,7 @@ public class SfvPanel extends JComponent { }; selectDialog.getHeaderLabel().setText("Select checksum column:"); + selectDialog.pack(); selectDialog.setLocationRelativeTo(SfvPanel.this); selectDialog.setVisible(true); diff --git a/source/net/filebot/ui/subtitle/upload/MovieEditor.java b/source/net/filebot/ui/subtitle/upload/MovieEditor.java index 1ead271b..2c4c9294 100644 --- a/source/net/filebot/ui/subtitle/upload/MovieEditor.java +++ b/source/net/filebot/ui/subtitle/upload/MovieEditor.java @@ -68,6 +68,7 @@ class MovieEditor implements TableCellEditor { } SelectDialog dialog = new SelectDialog(table, options); + dialog.pack(); dialog.setLocation(getOffsetLocation(dialog.getOwner())); dialog.setVisible(true); Movie selectedValue = dialog.getSelectedValue();