From 742e3aea2d94bdd9d61657334072ef815c261e74 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Fri, 25 Nov 2016 20:47:08 +0800 Subject: [PATCH] Fix error prone code --- source/net/filebot/HistorySpooler.java | 2 +- source/net/filebot/Main.java | 37 ++++++++----------- source/net/filebot/cli/ArgumentBean.java | 14 +++++++ source/net/filebot/cli/GroovyPad.java | 2 +- .../net/filebot/cli/ScriptShellBaseClass.java | 4 +- .../ui/rename/ExpressionFormatter.java | 2 +- .../net/filebot/ui/rename/FormatDialog.java | 30 +++++---------- 7 files changed, 45 insertions(+), 46 deletions(-) diff --git a/source/net/filebot/HistorySpooler.java b/source/net/filebot/HistorySpooler.java index 2d4c9d0c..00194209 100644 --- a/source/net/filebot/HistorySpooler.java +++ b/source/net/filebot/HistorySpooler.java @@ -31,7 +31,7 @@ public final class HistorySpooler { Runtime.getRuntime().addShutdownHook(new Thread(HistorySpooler.getInstance()::commit, "HistorySpoolerShutdownHook")); // commit session history on shutdown } - private final File persistentHistoryFile = ApplicationFolder.AppData.resolve("history.xml"); + private final File persistentHistoryFile = ApplicationFolder.AppData.path("history.xml"); private int persistentHistoryTotalSize = -1; private boolean persistentHistoryEnabled = true; diff --git a/source/net/filebot/Main.java b/source/net/filebot/Main.java index bc2625cb..3564dc30 100644 --- a/source/net/filebot/Main.java +++ b/source/net/filebot/Main.java @@ -40,6 +40,7 @@ import org.w3c.dom.Document; import net.filebot.cli.ArgumentBean; import net.filebot.cli.ArgumentProcessor; +import net.filebot.cli.CmdlineException; import net.filebot.format.ExpressionFormat; import net.filebot.mac.MacAppUtilities; import net.filebot.ui.FileBotMenuBar; @@ -190,27 +191,23 @@ public class Main { setSystemLookAndFeel(); } - // default frame - JFrame frame = new MainFrame(PanelBuilder.defaultSequence()); + // start multi panel or single panel frame + PanelBuilder[] panels = PanelBuilder.defaultSequence(); - // single panel frame if (args.mode != null) { - PanelBuilder[] selection = stream(PanelBuilder.defaultSequence()).filter(p -> p.getName().matches(args.mode)).toArray(PanelBuilder[]::new); - if (selection.length == 1) { - frame = new SinglePanelFrame(selection[0]); - } else if (selection.length > 1) { - frame = new MainFrame(selection); - } else { - throw new IllegalArgumentException("Illegal mode: " + args.mode); + panels = stream(panels).filter(p -> p.getName().matches(args.mode)).toArray(PanelBuilder[]::new); // only selected panels + + if (panels.length == 0) { + throw new CmdlineException("Illegal mode: " + args.mode); } } + JFrame frame = panels.length > 1 ? new MainFrame(panels) : new SinglePanelFrame(panels[0]); + try { - // restore previous size and location - restoreWindowBounds(frame, Settings.forPackage(MainFrame.class)); + restoreWindowBounds(frame, Settings.forPackage(MainFrame.class)); // restore previous size and location } catch (Exception e) { - // make sure the main window is not displayed out of screen bounds - frame.setLocation(120, 80); + frame.setLocation(120, 80); // make sure the main window is not displayed out of screen bounds } frame.addWindowListener(windowClosed(evt -> { @@ -368,19 +365,17 @@ public class Main { System.setProperty("sun.net.client.defaultReadTimeout", "60000"); System.setProperty("swing.crossplatformlaf", "javax.swing.plaf.nimbus.NimbusLookAndFeel"); - System.setProperty("grape.root", ApplicationFolder.AppData.resolve("grape").getAbsolutePath()); + System.setProperty("grape.root", ApplicationFolder.AppData.path("grape").getPath()); System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog"); if (args.unixfs) { System.setProperty("unixfs", "true"); } - if (args.disableExtendedAttributes || "TEST".equalsIgnoreCase(args.action)) { + + if (args.disableExtendedAttributes) { System.setProperty("useExtendedFileAttributes", "false"); System.setProperty("useCreationDate", "false"); } - if ("TEST".equalsIgnoreCase(args.action)) { - System.setProperty("application.rename.history", "false"); // do not keep history of --action test rename operations - } } public static void initializeLogging(ArgumentBean args) throws IOException { @@ -394,7 +389,7 @@ public class Main { // log errors to file try { - Handler error = createSimpleFileHandler(ApplicationFolder.AppData.resolve("error.log"), Level.WARNING); + Handler error = createSimpleFileHandler(ApplicationFolder.AppData.path("error.log"), Level.WARNING); log.addHandler(error); debug.addHandler(error); } catch (Exception e) { @@ -406,7 +401,7 @@ public class Main { if (args.logFile != null) { File logFile = new File(args.logFile); if (!logFile.isAbsolute()) { - logFile = new File(ApplicationFolder.AppData.resolve("logs"), logFile.getPath()).getAbsoluteFile(); // by default resolve relative paths against {applicationFolder}/logs/{logFile} + logFile = new File(ApplicationFolder.AppData.path("logs"), logFile.getPath()).getAbsoluteFile(); // by default resolve relative paths against {applicationFolder}/logs/{logFile} } if (!logFile.exists() && !logFile.getParentFile().mkdirs() && !logFile.createNewFile()) { throw new IOException("Failed to create log file: " + logFile); diff --git a/source/net/filebot/cli/ArgumentBean.java b/source/net/filebot/cli/ArgumentBean.java index 17ffbd53..30e7d2ae 100644 --- a/source/net/filebot/cli/ArgumentBean.java +++ b/source/net/filebot/cli/ArgumentBean.java @@ -21,6 +21,8 @@ import org.kohsuke.args4j.ParserProperties; import org.kohsuke.args4j.spi.ExplicitBooleanOptionHandler; import net.filebot.Language; +import net.filebot.StandardRenameAction; +import net.filebot.web.SortOrder; public class ArgumentBean { @@ -180,6 +182,18 @@ public class ArgumentBean { return files; } + public StandardRenameAction getRenameAction() { + return StandardRenameAction.forName(action); + } + + public ConflictAction getConflictAction() { + return ConflictAction.forName(conflict); + } + + public SortOrder getSortOrder() { + return SortOrder.forName(order); + } + public Locale getLocale() { return new Locale(lang); } diff --git a/source/net/filebot/cli/GroovyPad.java b/source/net/filebot/cli/GroovyPad.java index 2e3b1df6..06a1d87e 100644 --- a/source/net/filebot/cli/GroovyPad.java +++ b/source/net/filebot/cli/GroovyPad.java @@ -118,7 +118,7 @@ public class GroovyPad extends JFrame { try { // use this default value so people can easily submit bug reports with fn:sysinfo logs - File pad = ApplicationFolder.AppData.resolve("pad.groovy"); + File pad = ApplicationFolder.AppData.path("pad.groovy"); if (!pad.exists()) { ScriptShellMethods.saveAs(DEFAULT_SCRIPT, pad); diff --git a/source/net/filebot/cli/ScriptShellBaseClass.java b/source/net/filebot/cli/ScriptShellBaseClass.java index b8b4f828..9460a8a2 100644 --- a/source/net/filebot/cli/ScriptShellBaseClass.java +++ b/source/net/filebot/cli/ScriptShellBaseClass.java @@ -499,8 +499,8 @@ public abstract class ScriptShellBaseClass extends Script { for (Entry it : parameters.entrySet()) { try { options.put(Option.valueOf(it.getKey()), it.getValue()); - } catch (IllegalArgumentException e) { - // just ignore illegal options + } catch (Exception e) { + debug.warning(e::toString); } } diff --git a/source/net/filebot/ui/rename/ExpressionFormatter.java b/source/net/filebot/ui/rename/ExpressionFormatter.java index cd1faf12..7b9ff2c8 100644 --- a/source/net/filebot/ui/rename/ExpressionFormatter.java +++ b/source/net/filebot/ui/rename/ExpressionFormatter.java @@ -83,7 +83,7 @@ class ExpressionFormatter implements MatchFormatter { // resolve against home folder if (destination.startsWith("~")) { - return ApplicationFolder.UserHome.resolve(destination.substring(1)).getAbsolutePath(); + return ApplicationFolder.UserHome.path(destination.substring(1)).getAbsolutePath(); } // try to resolve against structure root folder by default diff --git a/source/net/filebot/ui/rename/FormatDialog.java b/source/net/filebot/ui/rename/FormatDialog.java index 0f99b087..2f3cdc00 100644 --- a/source/net/filebot/ui/rename/FormatDialog.java +++ b/source/net/filebot/ui/rename/FormatDialog.java @@ -67,7 +67,6 @@ import net.filebot.format.MediaBindingBean; import net.filebot.format.SuppressedThrowables; import net.filebot.mac.MacAppUtilities; import net.filebot.media.MetaAttributes; -import net.filebot.mediainfo.MediaInfo; import net.filebot.util.DefaultThreadFactory; import net.filebot.util.ExceptionUtilities; import net.filebot.util.PreferencesList; @@ -305,16 +304,18 @@ public class FormatDialog extends JDialog { } public void setFormatCode(String text) { - // update format code - editor.setText(text); - - // scroll to last character and focus try { - editor.scrollRectToVisible(new Rectangle(0, 0)); // reset scroll - editor.setCaretPosition(text.length()); // scroll to end of format - editor.requestFocusInWindow(); + // update format code + editor.setText(text); + + // scroll to last character and focus + if (text != null && text.length() > 0) { + editor.scrollRectToVisible(new Rectangle(0, 0)); // reset scroll + editor.setCaretPosition(editor.getText().length()); // scroll to end of format + editor.requestFocusInWindow(); + } } catch (Exception e) { - log.warning(e::getMessage); + debug.warning(e::toString); } } @@ -713,15 +714,4 @@ public class FormatDialog extends JDialog { firePropertyChange("sample", null, sample); } - static { - try { - // load all necessarily classes to avoid focus issues with RSyntaxTextArea - new RSyntaxTextArea(new RSyntaxDocument(SyntaxConstants.SYNTAX_STYLE_GROOVY)); - new ExpressionFormat("{n.space('.').lower()}.{s}{e.pad(2)}"); - new MediaInfo(); - } catch (Throwable e) { - debug.log(Level.SEVERE, "Failed to initialize FormatDialog", e); - } - } - }