From bb4a23cad6996440a07ab469de943e33ee588bb8 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sat, 25 Jul 2015 22:46:47 +0000 Subject: [PATCH] + Preset Editor UI --- source/net/filebot/ui/rename/Preset.java | 7 ++- .../net/filebot/ui/rename/PresetEditor.java | 28 ++++++--- source/net/filebot/ui/rename/RenamePanel.java | 61 ++++++++++++++++--- 3 files changed, 75 insertions(+), 21 deletions(-) diff --git a/source/net/filebot/ui/rename/Preset.java b/source/net/filebot/ui/rename/Preset.java index 95cca7bc..14200534 100644 --- a/source/net/filebot/ui/rename/Preset.java +++ b/source/net/filebot/ui/rename/Preset.java @@ -44,7 +44,7 @@ public class Preset { } public File getInputFolder() { - return new File(path); + return path == null || path.isEmpty() ? null : new File(path); } public ExpressionFilter getIncludeFilter() { @@ -129,4 +129,9 @@ public class Preset { } } + @Override + public String toString() { + return name; + } + } diff --git a/source/net/filebot/ui/rename/PresetEditor.java b/source/net/filebot/ui/rename/PresetEditor.java index 6c6e973d..1c2c937e 100644 --- a/source/net/filebot/ui/rename/PresetEditor.java +++ b/source/net/filebot/ui/rename/PresetEditor.java @@ -51,9 +51,17 @@ import org.fife.ui.rtextarea.RTextScrollPane; public class PresetEditor extends JDialog { - public static void main(String[] args) { + public static void main(String[] args) throws Exception { SwingUtilities.invokeLater(() -> { - new PresetEditor(null).setVisible(true); + PresetEditor presetEditor = new PresetEditor(null); + presetEditor.setVisible(true); + try { + System.out.println(presetEditor.getResult()); + System.out.println(presetEditor.getPreset()); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } }); } @@ -82,7 +90,7 @@ public class PresetEditor extends JDialog { presetNameHeader = new HeaderPanel(); - inheritRadio = new JRadioButton("Use Original Files"); + inheritRadio = new JRadioButton("Use Original Files selection"); selectRadio = new JRadioButton("Do Select"); pathInput = new JTextField(40); @@ -151,13 +159,13 @@ public class PresetEditor extends JDialog { public void setPreset(Preset p) { presetNameHeader.getTitleLabel().setText(p.getName()); - pathInput.setText(p.getInputFolder().getPath()); - filterEditor.setText(p.getIncludeFilter().getExpression()); - formatEditor.setText(p.getFormat().getExpression()); - providerCombo.setSelectedItem(p.getDatabase()); - sortOrderCombo.setSelectedItem(p.getSortOrder()); - matchModeCombo.setSelectedItem(p.getMatchMode()); - actionCombo.setSelectedItem(p.getRenameAction()); + pathInput.setText(p.getInputFolder() == null ? "" : p.getInputFolder().getPath()); + filterEditor.setText(p.getIncludeFilter() == null ? "" : p.getIncludeFilter().getExpression()); + formatEditor.setText(p.getFormat() == null ? "" : p.getFormat().getExpression()); + providerCombo.setSelectedItem(p.getDatabase() == null ? WebServices.TheTVDB : p.getDatabase()); + sortOrderCombo.setSelectedItem(p.getSortOrder() == null ? SortOrder.Airdate : p.getSortOrder()); + matchModeCombo.setSelectedItem(p.getMatchMode() == null ? RenamePanel.MATCH_MODE_OPPORTUNISTIC : p.getMatchMode()); + actionCombo.setSelectedItem(p.getRenameAction() == null ? StandardRenameAction.MOVE : p.getRenameAction()); } public Preset getPreset() throws Exception { diff --git a/source/net/filebot/ui/rename/RenamePanel.java b/source/net/filebot/ui/rename/RenamePanel.java index 5cdc6f26..2f1c557f 100644 --- a/source/net/filebot/ui/rename/RenamePanel.java +++ b/source/net/filebot/ui/rename/RenamePanel.java @@ -48,6 +48,9 @@ import javax.swing.SwingWorker; import javax.swing.border.CompoundBorder; import javax.swing.border.TitledBorder; +import com.cedarsoftware.util.io.JsonReader; +import com.cedarsoftware.util.io.JsonWriter; + import net.filebot.History; import net.filebot.HistorySpooler; import net.filebot.Language; @@ -65,6 +68,7 @@ import net.filebot.ui.rename.FormatDialog.Mode; import net.filebot.ui.rename.RenameModel.FormattedFuture; import net.filebot.ui.transfer.BackgroundFileTransferablePolicy; import net.filebot.util.FileUtilities; +import net.filebot.util.PreferencesMap; import net.filebot.util.PreferencesMap.PreferencesEntry; import net.filebot.util.ui.ActionPopup; import net.filebot.util.ui.LoadingOverlayPane; @@ -379,16 +383,18 @@ public class RenamePanel extends JComponent { } protected ActionPopup createPresetsPopup() { - List presets = new ArrayList(); + PreferencesMap persistentPresets = Settings.forPackage(RenamePanel.class).node("presets").asMap(); + ActionPopup actionPopup = new ActionPopup("Presets", ResourceManager.getIcon("action.script")); - // TODO load presets via prefs - - final ActionPopup actionPopup = new ActionPopup("Presets", ResourceManager.getIcon("action.script")); - - if (presets.size() > 0) { + if (persistentPresets.size() > 0) { actionPopup.addDescription(new JLabel("Apply:")); - for (Preset it : presets) { - actionPopup.add(new ApplyPresetAction(it)); + for (String it : persistentPresets.values()) { + try { + Preset p = (Preset) JsonReader.jsonToJava(it); + actionPopup.add(new ApplyPresetAction(p)); + } catch (Exception e) { + Logger.getLogger(RenamePanel.class.getName()).log(Level.WARNING, e.getMessage(), e); + } } } @@ -396,9 +402,44 @@ public class RenamePanel extends JComponent { actionPopup.add(new AbstractAction("Edit Presets", ResourceManager.getIcon("script.add")) { @Override - public void actionPerformed(ActionEvent e) { - // TODO Auto-generated method stub + public void actionPerformed(ActionEvent evt) { + try { + String newPresetOption = "New Preset …"; + List presetNames = new ArrayList(persistentPresets.keySet()); + presetNames.add(newPresetOption); + String selection = (String) JOptionPane.showInputDialog(getWindow(evt.getSource()), "Edit or create a preset:", "Edit Preset", JOptionPane.PLAIN_MESSAGE, null, presetNames.toArray(), newPresetOption); + if (selection == null) + return; + + Preset preset = null; + if (selection == newPresetOption) { + selection = (String) JOptionPane.showInputDialog(getWindow(evt.getSource()), "Preset Name:", newPresetOption, JOptionPane.PLAIN_MESSAGE, null, null, "My Preset"); + if (selection == null) + return; + + preset = new Preset(selection, null, null, null, null, null, null, null, null); + } else { + preset = (Preset) JsonReader.jsonToJava(persistentPresets.get(selection.toString())); + } + + PresetEditor presetEditor = new PresetEditor(getWindow(evt.getSource())); + presetEditor.setPreset(preset); + presetEditor.setVisible(true); + + switch (presetEditor.getResult()) { + case DELETE: + persistentPresets.remove(selection); + break; + case SET: + persistentPresets.put(selection, JsonWriter.objectToJson(presetEditor.getPreset())); + break; + case CANCEL: + break; + } + } catch (Exception e) { + Logger.getLogger(RenamePanel.class.getName()).log(Level.WARNING, e.getMessage(), e); + } } });