From 8dfb69aa4759e8b2dd632e5bab9095acfe51ed74 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Thu, 24 Nov 2016 04:51:40 +0800 Subject: [PATCH] Refactor --- source/net/filebot/CacheManager.java | 17 +++--- source/net/filebot/Main.java | 52 +++++++------------ source/net/filebot/ui/MainFrame.java | 6 ++- .../net/filebot/ui/rename/BindingDialog.java | 8 +-- source/net/filebot/util/ui/SwingUI.java | 12 +++++ 5 files changed, 46 insertions(+), 49 deletions(-) diff --git a/source/net/filebot/CacheManager.java b/source/net/filebot/CacheManager.java index 7d7d4ce9..a7b9e445 100644 --- a/source/net/filebot/CacheManager.java +++ b/source/net/filebot/CacheManager.java @@ -75,21 +75,24 @@ public class CacheManager { // make sure cache is readable and writable createFolders(cache); - final File lockFile = new File(cache, ".lock"); + File lockFile = new File(cache, ".lock"); boolean isNewCache = !lockFile.exists(); - final FileChannel channel = FileChannel.open(lockFile.toPath(), StandardOpenOption.CREATE, StandardOpenOption.READ, StandardOpenOption.WRITE); - final FileLock lock = channel.tryLock(); + FileChannel channel = FileChannel.open(lockFile.toPath(), StandardOpenOption.CREATE, StandardOpenOption.READ, StandardOpenOption.WRITE); + FileLock lock = channel.tryLock(); if (lock != null) { debug.config(format("Using persistent disk cache %s", cache)); int applicationRevision = getApplicationRevisionNumber(); int cacheRevision = 0; - try { - cacheRevision = new Scanner(channel, "UTF-8").nextInt(); - } catch (Exception e) { - // ignore + + if (channel.size() > 0) { + try { + cacheRevision = new Scanner(channel, "UTF-8").nextInt(); + } catch (Exception e) { + debug.log(Level.WARNING, e, e::toString); + } } if (cacheRevision != applicationRevision && applicationRevision > 0 && !isNewCache) { diff --git a/source/net/filebot/Main.java b/source/net/filebot/Main.java index ed5e8c5b..bc2625cb 100644 --- a/source/net/filebot/Main.java +++ b/source/net/filebot/Main.java @@ -11,8 +11,6 @@ import static net.filebot.util.XPathUtilities.*; import static net.filebot.util.ui.SwingUI.*; import java.awt.Dialog.ModalityType; -import java.awt.event.WindowAdapter; -import java.awt.event.WindowEvent; import java.io.File; import java.io.IOException; import java.io.OutputStream; @@ -215,19 +213,15 @@ public class Main { frame.setLocation(120, 80); } - frame.addWindowListener(new WindowAdapter() { + frame.addWindowListener(windowClosed(evt -> { + evt.getWindow().setVisible(false); - @Override - public void windowClosing(WindowEvent e) { - e.getWindow().setVisible(false); + // make sure any long running operations are done now and not later on the shutdown hook thread + HistorySpooler.getInstance().commit(); + SupportDialog.maybeShow(); - // make sure any long running operations are done now and not later on the shutdown hook thread - HistorySpooler.getInstance().commit(); - SupportDialog.maybeShow(); - - System.exit(0); - } - }); + System.exit(0); + })); // configure main window if (isMacApp()) { @@ -268,11 +262,7 @@ public class Main { Document dom = cache.xml("update.url", s -> new URL(getApplicationProperty(s))).expire(Cache.ONE_WEEK).retry(0).get(); // parse update xml - final Map update = streamElements(dom.getFirstChild()).collect(toMap(n -> { - return n.getNodeName(); - }, n -> { - return n.getTextContent().trim(); - })); + Map update = streamElements(dom.getFirstChild()).collect(toMap(n -> n.getNodeName(), n -> n.getTextContent().trim())); // check if update is required int latestRev = Integer.parseInt(update.get("revision")); @@ -280,8 +270,8 @@ public class Main { if (latestRev > currentRev && currentRev > 0) { SwingUtilities.invokeLater(() -> { - final JDialog dialog = new JDialog(JFrame.getFrames()[0], update.get("title"), ModalityType.APPLICATION_MODAL); - final JPanel pane = new JPanel(new MigLayout("fill, nogrid, insets dialog")); + JDialog dialog = new JDialog(JFrame.getFrames()[0], update.get("title"), ModalityType.APPLICATION_MODAL); + JPanel pane = new JPanel(new MigLayout("fill, nogrid, insets dialog")); dialog.setContentPane(pane); pane.add(new JLabel(ResourceManager.getIcon("window.icon.medium")), "aligny top"); @@ -321,21 +311,17 @@ public class Main { } } - private static void restoreWindowBounds(final JFrame window, final Settings settings) { + private static void restoreWindowBounds(JFrame window, Settings settings) { // store bounds on close - window.addWindowListener(new WindowAdapter() { - - @Override - public void windowClosing(WindowEvent e) { - // don't save window bounds if window is maximized - if (!isMaximized(window)) { - settings.put("window.x", String.valueOf(window.getX())); - settings.put("window.y", String.valueOf(window.getY())); - settings.put("window.width", String.valueOf(window.getWidth())); - settings.put("window.height", String.valueOf(window.getHeight())); - } + window.addWindowListener(windowClosed(evt -> { + // don't save window bounds if window is maximized + if (!isMaximized(window)) { + settings.put("window.x", String.valueOf(window.getX())); + settings.put("window.y", String.valueOf(window.getY())); + settings.put("window.width", String.valueOf(window.getWidth())); + settings.put("window.height", String.valueOf(window.getHeight())); } - }); + })); // restore bounds int x = Integer.parseInt(settings.get("window.x")); diff --git a/source/net/filebot/ui/MainFrame.java b/source/net/filebot/ui/MainFrame.java index 55cb02c3..58d16b05 100644 --- a/source/net/filebot/ui/MainFrame.java +++ b/source/net/filebot/ui/MainFrame.java @@ -95,8 +95,10 @@ public class MainFrame extends JFrame { // KEYBOARD SHORTCUTS installAction(getRootPane(), getKeyStroke(VK_DELETE, CTRL_MASK | SHIFT_MASK), newAction("Clear Cache", evt -> { - CacheManager.getInstance().clearAll(); - log.info("Cache has been cleared"); + withWaitCursor(getRootPane(), () -> { + CacheManager.getInstance().clearAll(); + log.info("Cache has been cleared"); + }); })); installAction(getRootPane(), getKeyStroke(VK_F5, 0), newAction("Run", evt -> { diff --git a/source/net/filebot/ui/rename/BindingDialog.java b/source/net/filebot/ui/rename/BindingDialog.java index 9ca13774..dd4242cb 100644 --- a/source/net/filebot/ui/rename/BindingDialog.java +++ b/source/net/filebot/ui/rename/BindingDialog.java @@ -142,13 +142,7 @@ class BindingDialog extends JDialog { }); // finish dialog and close window manually - addWindowListener(new WindowAdapter() { - - @Override - public void windowClosing(WindowEvent e) { - finish(false); - } - }); + addWindowListener(windowClosed(evt -> finish(false))); mediaFileTextField.setEditable(editable); infoTextField.setEditable(editable); diff --git a/source/net/filebot/util/ui/SwingUI.java b/source/net/filebot/util/ui/SwingUI.java index 87f02e4b..bef7562e 100644 --- a/source/net/filebot/util/ui/SwingUI.java +++ b/source/net/filebot/util/ui/SwingUI.java @@ -23,6 +23,8 @@ import java.awt.event.InputEvent; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; import java.awt.image.BufferedImage; import java.lang.reflect.InvocationTargetException; import java.net.URI; @@ -327,6 +329,16 @@ public final class SwingUI { } } + public static WindowAdapter windowClosed(Consumer handler) { + return new WindowAdapter() { + + @Override + public void windowClosing(WindowEvent evt) { + handler.accept(evt); + } + }; + } + public static MouseAdapter mouseClicked(Consumer handler) { return new MouseAdapter() {