From 58da1b2cfcf93561ec1f86dd09caec2baec22eb6 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sun, 29 Nov 2015 09:28:54 +0000 Subject: [PATCH] * n00b counter measures: sample file not set issue --- .../net/filebot/format/MediaBindingBean.java | 68 ++++++++----------- .../net/filebot/ui/rename/FormatDialog.java | 10 +-- 2 files changed, 32 insertions(+), 46 deletions(-) diff --git a/source/net/filebot/format/MediaBindingBean.java b/source/net/filebot/format/MediaBindingBean.java index d9c6d578..5d2d80d8 100644 --- a/source/net/filebot/format/MediaBindingBean.java +++ b/source/net/filebot/format/MediaBindingBean.java @@ -78,6 +78,14 @@ public class MediaBindingBean { this.context = context; } + public Object getInfoObject() { + return infoObject; + } + + public File getFileObject() { + return mediaFile; + } + @Define(undefined) public T undefined(String name) { // omit expressions that depend on undefined values @@ -396,12 +404,12 @@ public class MediaBindingBean { @Define("original") public String getOriginalFileName() throws Exception { - return getOriginalFileName(mediaFile); + return getOriginalFileName(getMediaFile()); } @Define("xattr") public Object getMetaAttributesObject() throws Exception { - return new MetaAttributes(mediaFile).getObject(); + return new MetaAttributes(getMediaFile()).getObject(); } @Define("crc32") @@ -442,20 +450,14 @@ public class MediaBindingBean { @Define("fn") public String getFileName() { - // make sure media file is defined - checkMediaFile(); - - // file extension - return FileUtilities.getName(mediaFile); + // name without file extension + return FileUtilities.getName(getMediaFile()); } @Define("ext") public String getExtension() { - // make sure media file is defined - checkMediaFile(); - // file extension - return FileUtilities.getExtension(mediaFile); + return FileUtilities.getExtension(getMediaFile()); } @Define("source") @@ -509,15 +511,12 @@ public class MediaBindingBean { @Define("lang") public Language detectSubtitleLanguage() throws Exception { - // make sure media file is defined - checkMediaFile(); - - Locale languageSuffix = releaseInfo.getLanguageSuffix(FileUtilities.getName(mediaFile)); + Locale languageSuffix = releaseInfo.getLanguageSuffix(FileUtilities.getName(getMediaFile())); if (languageSuffix != null) return Language.getLanguage(languageSuffix); // require subtitle file - if (!SUBTITLE_FILES.accept(mediaFile)) { + if (!SUBTITLE_FILES.accept(getMediaFile())) { return null; } @@ -745,6 +744,11 @@ public class MediaBindingBean { @Define("file") public File getMediaFile() { + // make sure file is not null, and that it is an existing file + if (mediaFile == null) { + throw new IllegalStateException("Path to media file not set. Click \"Change Sample\" and select a sample file."); + } + return mediaFile; } @@ -785,11 +789,6 @@ public class MediaBindingBean { return null; } - @Define("object") - public Object getInfoObject() { - return infoObject; - } - @Define("i") public Integer getModelIndex() { return 1 + identityIndexOf(context.values(), getInfoObject()); @@ -826,16 +825,13 @@ public class MediaBindingBean { } public File getInferredMediaFile() { - // make sure media file is defined - checkMediaFile(); - - if (mediaFile.isDirectory()) { + if (getMediaFile().isDirectory()) { // just select the first video file in the folder as media sample - SortedSet videos = new TreeSet(filter(listFiles(mediaFile), VIDEO_FILES)); + SortedSet videos = new TreeSet(filter(listFiles(getMediaFile()), VIDEO_FILES)); if (videos.size() > 0) { return videos.iterator().next(); } - } else if (SUBTITLE_FILES.accept(mediaFile) || ((infoObject instanceof Episode || infoObject instanceof Movie) && !VIDEO_FILES.accept(mediaFile))) { + } else if (SUBTITLE_FILES.accept(getMediaFile()) || ((infoObject instanceof Episode || infoObject instanceof Movie) && !VIDEO_FILES.accept(getMediaFile()))) { // prefer equal match from current context if possible if (context != null) { for (Entry it : context.entrySet()) { @@ -846,8 +842,8 @@ public class MediaBindingBean { } // file is a subtitle, or nfo, etc - String baseName = stripReleaseInfo(FileUtilities.getName(mediaFile)).toLowerCase(); - List videos = getChildren(mediaFile.getParentFile(), VIDEO_FILES); + String baseName = stripReleaseInfo(FileUtilities.getName(getMediaFile())).toLowerCase(); + List videos = getChildren(getMediaFile().getParentFile(), VIDEO_FILES); // find corresponding movie file for (File movieFile : videos) { @@ -858,7 +854,7 @@ public class MediaBindingBean { // still no good match found -> just take the most probable video from the same folder if (videos.size() > 0) { - sort(videos, new SimilarityComparator(FileUtilities.getName(mediaFile)) { + sort(videos, new SimilarityComparator(FileUtilities.getName(getMediaFile())) { @Override public int compare(Object o1, Object o2) { @@ -869,22 +865,12 @@ public class MediaBindingBean { } } - return mediaFile; - } - - private void checkMediaFile() { - // make sure file is not null, and that it is an existing file - if (mediaFile == null) { - throw new IllegalStateException("Path to media file not set. Click (x)= and select a sample file."); - } + return getMediaFile(); } private static final Map sharedMediaInfoObjects = new WeakValueHashMap(64); private synchronized MediaInfo getMediaInfo() { - // make sure media file is defined - checkMediaFile(); - // lazy initialize if (mediaInfo == null) { // use inferred media file (e.g. actual movie file instead of subtitle file) diff --git a/source/net/filebot/ui/rename/FormatDialog.java b/source/net/filebot/ui/rename/FormatDialog.java index 98a1c15a..ccad03c3 100644 --- a/source/net/filebot/ui/rename/FormatDialog.java +++ b/source/net/filebot/ui/rename/FormatDialog.java @@ -235,8 +235,8 @@ public class FormatDialog extends JDialog { @Override public void propertyChange(PropertyChangeEvent evt) { if (isMacSandbox()) { - if (sample != null && sample.getMediaFile() != null && sample.getMediaFile().exists()) { - MacAppUtilities.askUnlockFolders(getWindow(evt.getSource()), singleton(sample.getMediaFile())); + if (sample != null && sample.getFileObject() != null && sample.getFileObject().exists()) { + MacAppUtilities.askUnlockFolders(getWindow(evt.getSource()), singleton(sample.getFileObject())); } } checkFormatInBackground(); @@ -283,7 +283,7 @@ public class FormatDialog extends JDialog { if (locked) { this.setTitle(String.format("%s Format", mode)); - title.setText(String.format("%s ⇔ %s", mode, bindings.getInfoObject(), bindings.getMediaFile() == null ? null : bindings.getMediaFile().getName())); + title.setText(String.format("%s ⇔ %s", mode, bindings.getInfoObject(), bindings.getFileObject() == null ? null : bindings.getFileObject().getName())); } else { this.setTitle(String.format("%s Format", mode)); title.setText(String.format("%s Format", mode)); @@ -651,7 +651,7 @@ public class FormatDialog extends JDialog { BindingDialog dialog = new BindingDialog(getWindow(evt.getSource()), String.format("%s Bindings", mode), mode.getFormat(), !locked); dialog.setInfoObject(sample.getInfoObject()); - dialog.setMediaFile(sample.getMediaFile()); + dialog.setMediaFile(sample.getFileObject()); // open dialog dialog.setLocationRelativeTo((Component) evt.getSource()); @@ -667,7 +667,7 @@ public class FormatDialog extends JDialog { // remember sample try { mode.persistentSample().setValue(info == null ? "" : JsonWriter.objectToJson(info)); - persistentSampleFile.setValue(file == null ? "" : sample.getMediaFile().getAbsolutePath()); + persistentSampleFile.setValue(file == null ? "" : sample.getFileObject().getAbsolutePath()); } catch (Exception e) { Logger.getLogger(FormatDialog.class.getName()).log(Level.WARNING, e.getMessage(), e); }