From c0f7dc742e672dbccb234e6d84d5e9f469057482 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Tue, 7 Feb 2017 19:23:00 +0800 Subject: [PATCH] Lock the format editor to movie/episode/music/file mode depending on your selected datasource --- .../net/filebot/ui/rename/FormatDialog.java | 26 +++++++++++++------ source/net/filebot/ui/rename/Preset.java | 2 +- .../net/filebot/ui/rename/PresetEditor.java | 9 ++++--- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/source/net/filebot/ui/rename/FormatDialog.java b/source/net/filebot/ui/rename/FormatDialog.java index b95827f9..ce9e5083 100644 --- a/source/net/filebot/ui/rename/FormatDialog.java +++ b/source/net/filebot/ui/rename/FormatDialog.java @@ -21,6 +21,7 @@ import java.io.File; import java.text.Format; import java.util.LinkedHashSet; import java.util.Map; +import java.util.MissingResourceException; import java.util.ResourceBundle; import java.util.Set; import java.util.TreeMap; @@ -140,6 +141,20 @@ public class FormatDialog extends JDialog { return Settings.forPackage(FormatDialog.class).node("format.recent." + key()).asList(); } + public Object getDefaultSampleObject() { + try { + ResourceBundle bundle = ResourceBundle.getBundle(FormatDialog.class.getName()); + String json = bundle.getString(key() + ".sample"); + return MetaAttributes.toObject(json); + } catch (MissingResourceException e) { + // ignore + } catch (Exception e) { + debug.log(Level.SEVERE, "Illegal Sample", e); + } + + return null; + } + public String getDefaultFormatExpression() { return getSampleExpressions().iterator().next(); } @@ -405,18 +420,13 @@ public class FormatDialog extends JDialog { // restore sample from user preferences String sample = mode.persistentSample().getValue(); info = MetaAttributes.toObject(sample); + if (info == null) { throw new NullPointerException(); } } catch (Exception e) { - try { - // restore sample from application properties - ResourceBundle bundle = ResourceBundle.getBundle(getClass().getName()); - String sample = bundle.getString(mode.key() + ".sample"); - info = MetaAttributes.toObject(sample); - } catch (Exception illegalSample) { - debug.log(Level.SEVERE, "Illegal Sample", e); - } + // restore sample from application properties + info = mode.getDefaultSampleObject(); } // restore media file diff --git a/source/net/filebot/ui/rename/Preset.java b/source/net/filebot/ui/rename/Preset.java index 2bb455f3..d7cb28ba 100644 --- a/source/net/filebot/ui/rename/Preset.java +++ b/source/net/filebot/ui/rename/Preset.java @@ -135,7 +135,7 @@ public class Preset { public static final PlainFileMatcher PLAIN_FILE_MATCHER = new PlainFileMatcher(); public static Datasource[] getSupportedServices() { - Stream services = Stream.of(getMovieIdentificationServices(), getEpisodeListProviders(), getMusicIdentificationServices()).flatMap(Stream::of); + Stream services = Stream.of(getEpisodeListProviders(), getMovieIdentificationServices(), getMusicIdentificationServices()).flatMap(Stream::of); services = Stream.concat(services, Stream.of(XATTR_FILE_MATCHER, PLAIN_FILE_MATCHER)); return services.toArray(Datasource[]::new); } diff --git a/source/net/filebot/ui/rename/PresetEditor.java b/source/net/filebot/ui/rename/PresetEditor.java index 038ffa31..6aea299f 100644 --- a/source/net/filebot/ui/rename/PresetEditor.java +++ b/source/net/filebot/ui/rename/PresetEditor.java @@ -302,16 +302,19 @@ public class PresetEditor extends JDialog { private final Action editFormatExpression = newAction("Open Format Editor", ResourceManager.getIcon("action.format"), evt -> { FormatDialog.Mode mode = FormatDialog.Mode.getMode((Datasource) providerCombo.getSelectedItem()); - MediaBindingBean lockOnBinding = null; + + Object sample = mode.getDefaultSampleObject(); + File file = null; + if (mode == FormatDialog.Mode.File) { List files = UserFiles.showLoadDialogSelectFiles(false, false, null, new ExtensionFileFilter(ExtensionFileFilter.WILDCARD), "Select Sample File", evt); if (files.isEmpty()) { return; } - lockOnBinding = new MediaBindingBean(files.get(0), files.get(0)); + sample = file = files.get(0); } - FormatDialog dialog = new FormatDialog(getWindow(evt.getSource()), mode, lockOnBinding); + FormatDialog dialog = new FormatDialog(getWindow(evt.getSource()), mode, new MediaBindingBean(sample, file, singletonMap(file, sample))); dialog.setFormatCode(formatEditor.getText()); dialog.setLocation(getOffsetLocation(dialog.getOwner())); dialog.setVisible(true);