From 4bd0ed265d251af83a4dde4336d2ec43d6dd3faa Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sun, 20 Mar 2016 07:52:13 +0000 Subject: [PATCH] Use EventBus singleton that runs tasks on the EDT --- source/net/filebot/Main.java | 28 ++++++++--------- source/net/filebot/mac/MacAppUtilities.java | 3 +- source/net/filebot/ui/MainFrame.java | 13 +++----- source/net/filebot/ui/SinglePanelFrame.java | 7 ++--- .../net/filebot/ui/analyze/AnalyzePanel.java | 13 ++------ source/net/filebot/ui/list/ListPanel.java | 11 ++----- source/net/filebot/ui/rename/RenamePanel.java | 10 ++---- source/net/filebot/ui/sfv/SfvPanel.java | 12 ++----- .../filebot/ui/subtitle/SubtitlePanel.java | 14 +++------ source/net/filebot/util/ui/SwingEventBus.java | 31 +++++++++++++++++++ 10 files changed, 70 insertions(+), 72 deletions(-) create mode 100644 source/net/filebot/util/ui/SwingEventBus.java diff --git a/source/net/filebot/Main.java b/source/net/filebot/Main.java index d71d7543..db7de1c2 100644 --- a/source/net/filebot/Main.java +++ b/source/net/filebot/Main.java @@ -44,8 +44,6 @@ import javax.swing.UIManager; import org.kohsuke.args4j.CmdLineException; import org.w3c.dom.Document; -import com.google.common.eventbus.EventBus; - import net.filebot.Settings.ApplicationFolder; import net.filebot.cli.ArgumentBean; import net.filebot.cli.ArgumentProcessor; @@ -59,6 +57,7 @@ import net.filebot.ui.PanelBuilder; import net.filebot.ui.SinglePanelFrame; import net.filebot.ui.transfer.FileTransferable; import net.filebot.util.PreferencesMap.PreferencesEntry; +import net.filebot.util.ui.SwingEventBus; import net.filebot.util.TeePrintStream; import net.miginfocom.swing.MigLayout; @@ -126,7 +125,15 @@ public class Main { } // GUI mode => start user interface - SwingUtilities.invokeAndWait(() -> startUserInterface(args)); + SwingUtilities.invokeAndWait(() -> { + startUserInterface(args); + }); + + // publish file arguments + List files = args.getFiles(false); + if (files.size() > 0) { + SwingEventBus.getInstance().post(new FileTransferable(files)); + } // preload media.types (when loaded during DnD it will freeze the UI for a few hundred milliseconds) MediaTypes.getDefault(); @@ -173,16 +180,15 @@ public class Main { } // default frame - EventBus eventBus = new EventBus(); - JFrame frame = new MainFrame(PanelBuilder.defaultSequence(), eventBus); + JFrame frame = new MainFrame(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], eventBus); + frame = new SinglePanelFrame(selection[0]); } else if (selection.length > 1) { - frame = new MainFrame(selection, eventBus); + frame = new MainFrame(selection); } else { throw new IllegalArgumentException("Illegal mode: " + args.mode); } @@ -226,7 +232,7 @@ public class Main { MacAppUtilities.initializeApplication(); MacAppUtilities.setWindowCanFullScreen(frame); MacAppUtilities.setDefaultMenuBar(FileBotMenuBar.createHelp()); - MacAppUtilities.setOpenFileHandler(openFiles -> eventBus.post(new FileTransferable(openFiles))); + MacAppUtilities.setOpenFileHandler(openFiles -> SwingEventBus.getInstance().post(new FileTransferable(openFiles))); } else if (isUbuntuApp()) { // Ubuntu specific configuration String options = System.getenv("JAVA_TOOL_OPTIONS"); @@ -240,12 +246,6 @@ public class Main { frame.setIconImages(ResourceManager.getApplicationIcons()); } - // handle file arguments - List files = args.getFiles(false); - if (files.size() > 0) { - eventBus.post(new FileTransferable(files)); - } - // start application frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setVisible(true); diff --git a/source/net/filebot/mac/MacAppUtilities.java b/source/net/filebot/mac/MacAppUtilities.java index ad321344..d322071a 100644 --- a/source/net/filebot/mac/MacAppUtilities.java +++ b/source/net/filebot/mac/MacAppUtilities.java @@ -15,7 +15,6 @@ import java.util.function.Consumer; import java.util.logging.Level; import javax.swing.JMenuBar; -import javax.swing.SwingUtilities; import javax.swing.UIManager; import com.apple.eawt.Application; @@ -142,7 +141,7 @@ public class MacAppUtilities { Application.getApplication().setOpenFileHandler(evt -> { List files = evt.getFiles(); if (files.size() > 0) { - SwingUtilities.invokeLater(() -> handler.accept(files)); + handler.accept(files); } }); } catch (Throwable t) { diff --git a/source/net/filebot/ui/MainFrame.java b/source/net/filebot/ui/MainFrame.java index 888efcc0..50e32883 100644 --- a/source/net/filebot/ui/MainFrame.java +++ b/source/net/filebot/ui/MainFrame.java @@ -36,8 +36,6 @@ import javax.swing.border.LineBorder; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; -import com.google.common.eventbus.EventBus; - import net.filebot.CacheManager; import net.filebot.Settings; import net.filebot.cli.GroovyPad; @@ -45,22 +43,19 @@ import net.filebot.mac.MacAppUtilities; import net.filebot.util.PreferencesMap.PreferencesEntry; import net.filebot.util.ui.DefaultFancyListCellRenderer; import net.filebot.util.ui.ShadowBorder; +import net.filebot.util.ui.SwingEventBus; import net.miginfocom.swing.MigLayout; public class MainFrame extends JFrame { private static final PreferencesEntry persistentSelectedPanel = Settings.forPackage(MainFrame.class).entry("panel.selected").defaultValue("0"); - private EventBus eventBus; - private JList selectionList; private HeaderPanel headerPanel; - public MainFrame(PanelBuilder[] panels, EventBus eventBus) { + public MainFrame(PanelBuilder[] panels) { super(isInstalled() ? getApplicationName() : String.format("%s %s", getApplicationName(), getApplicationVersion())); - this.eventBus = eventBus; - selectionList = new PanelSelectionList(panels); headerPanel = new HeaderPanel(); @@ -156,7 +151,7 @@ public class MainFrame extends JFrame { selectedPanel = panel; } else if (panel.isVisible()) { panel.setVisible(false); - eventBus.unregister(panel); + SwingEventBus.getInstance().unregister(panel); } } } @@ -172,7 +167,7 @@ public class MainFrame extends JFrame { if (!selectedPanel.isVisible()) { headerPanel.setTitle(selectedBuilder.getName()); selectedPanel.setVisible(true); - eventBus.register(selectedPanel); + SwingEventBus.getInstance().register(selectedPanel); } } diff --git a/source/net/filebot/ui/SinglePanelFrame.java b/source/net/filebot/ui/SinglePanelFrame.java index 3f893679..2a33e974 100644 --- a/source/net/filebot/ui/SinglePanelFrame.java +++ b/source/net/filebot/ui/SinglePanelFrame.java @@ -8,13 +8,12 @@ import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.border.EmptyBorder; -import com.google.common.eventbus.EventBus; - +import net.filebot.util.ui.SwingEventBus; import net.miginfocom.swing.MigLayout; public class SinglePanelFrame extends JFrame { - public SinglePanelFrame(PanelBuilder builder, EventBus eventBus) { + public SinglePanelFrame(PanelBuilder builder) { super(String.format("%s %s %s", getApplicationName(), builder.getName(), getApplicationVersion())); JComponent panel = builder.create(); @@ -32,7 +31,7 @@ public class SinglePanelFrame extends JFrame { setSize(850, 600); setMinimumSize(new Dimension(800, 400)); - eventBus.register(panel); + SwingEventBus.getInstance().register(panel); } } diff --git a/source/net/filebot/ui/analyze/AnalyzePanel.java b/source/net/filebot/ui/analyze/AnalyzePanel.java index 2c11956e..a902bb20 100644 --- a/source/net/filebot/ui/analyze/AnalyzePanel.java +++ b/source/net/filebot/ui/analyze/AnalyzePanel.java @@ -1,9 +1,6 @@ package net.filebot.ui.analyze; -import static net.filebot.Logging.*; - import java.awt.datatransfer.Transferable; -import java.util.logging.Level; import javax.swing.JComponent; import javax.swing.JTabbedPane; @@ -38,15 +35,11 @@ public class AnalyzePanel extends JComponent { } @Subscribe - public void handle(Transferable transferable) { + public void handle(Transferable transferable) throws Exception { TransferablePolicy handler = fileTreePanel.getTransferablePolicy(); - try { - if (handler != null && handler.accept(transferable)) { - handler.handleTransferable(transferable, TransferAction.PUT); - } - } catch (Exception e) { - debug.log(Level.WARNING, "Failed to handle transferable: " + transferable, e); + if (handler != null && handler.accept(transferable)) { + handler.handleTransferable(transferable, TransferAction.PUT); } } diff --git a/source/net/filebot/ui/list/ListPanel.java b/source/net/filebot/ui/list/ListPanel.java index fd8bf7cd..b13127cc 100644 --- a/source/net/filebot/ui/list/ListPanel.java +++ b/source/net/filebot/ui/list/ListPanel.java @@ -3,7 +3,6 @@ package net.filebot.ui.list; import static java.awt.Font.*; import static java.lang.Math.*; import static net.filebot.Logging.*; -import static net.filebot.Logging.log; import static net.filebot.media.MediaDetection.*; import static net.filebot.util.ui.SwingUI.*; @@ -141,15 +140,11 @@ public class ListPanel extends JComponent { } @Subscribe - public void handle(Transferable transferable) { + public void handle(Transferable transferable) throws Exception { TransferablePolicy handler = list.getTransferablePolicy(); - try { - if (handler != null && handler.accept(transferable)) { - handler.handleTransferable(transferable, TransferAction.PUT); - } - } catch (Exception e) { - debug.log(Level.WARNING, "Failed to handle transferable: " + transferable, e); + if (handler != null && handler.accept(transferable)) { + handler.handleTransferable(transferable, TransferAction.PUT); } } diff --git a/source/net/filebot/ui/rename/RenamePanel.java b/source/net/filebot/ui/rename/RenamePanel.java index f1afaee1..ba0e04e8 100644 --- a/source/net/filebot/ui/rename/RenamePanel.java +++ b/source/net/filebot/ui/rename/RenamePanel.java @@ -646,15 +646,11 @@ public class RenamePanel extends JComponent { }; @Subscribe - public void handle(Transferable transferable) { + public void handle(Transferable transferable) throws Exception { TransferablePolicy handler = filesList.getTransferablePolicy(); - try { - if (handler != null && handler.accept(transferable)) { - handler.handleTransferable(transferable, TransferAction.PUT); - } - } catch (Exception e) { - debug.log(Level.WARNING, "Failed to handle transferable: " + transferable, e); + if (handler != null && handler.accept(transferable)) { + handler.handleTransferable(transferable, TransferAction.PUT); } } diff --git a/source/net/filebot/ui/sfv/SfvPanel.java b/source/net/filebot/ui/sfv/SfvPanel.java index 5e19cb15..c65bf499 100644 --- a/source/net/filebot/ui/sfv/SfvPanel.java +++ b/source/net/filebot/ui/sfv/SfvPanel.java @@ -1,7 +1,6 @@ package net.filebot.ui.sfv; import static java.lang.Math.*; -import static net.filebot.Logging.*; import static net.filebot.ui.sfv.ChecksumTableModel.*; import static net.filebot.ui.transfer.BackgroundFileTransferablePolicy.*; import static net.filebot.util.FileUtilities.*; @@ -19,7 +18,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ExecutorService; -import java.util.logging.Level; import javax.swing.AbstractAction; import javax.swing.ButtonGroup; @@ -134,15 +132,11 @@ public class SfvPanel extends JComponent { } @Subscribe - public void handle(Transferable transferable) { + public void handle(Transferable transferable) throws Exception { TransferablePolicy handler = getTransferablePolicy(); - try { - if (handler != null && handler.accept(transferable)) { - handler.handleTransferable(transferable, TransferAction.PUT); - } - } catch (Exception e) { - debug.log(Level.WARNING, "Failed to handle transferable: " + transferable, e); + if (handler != null && handler.accept(transferable)) { + handler.handleTransferable(transferable, TransferAction.PUT); } } diff --git a/source/net/filebot/ui/subtitle/SubtitlePanel.java b/source/net/filebot/ui/subtitle/SubtitlePanel.java index e0e765f6..fa440f20 100644 --- a/source/net/filebot/ui/subtitle/SubtitlePanel.java +++ b/source/net/filebot/ui/subtitle/SubtitlePanel.java @@ -81,16 +81,12 @@ public class SubtitlePanel extends AbstractSearchPanel files = FileTransferable.getFilesFromTransferable(transferable); + public void handle(Transferable transferable) throws Exception { + SubtitleDropTarget target = downloadDropTarget; + List files = FileTransferable.getFilesFromTransferable(transferable); - if (files != null && files.size() > 0 && target.getDropAction(files) != DropAction.Cancel) { - target.handleDrop(files); - } - } catch (Exception e) { - debug.log(Level.WARNING, "Failed to handle transferable: " + transferable, e); + if (files != null && files.size() > 0 && target.getDropAction(files) != DropAction.Cancel) { + target.handleDrop(files); } } diff --git a/source/net/filebot/util/ui/SwingEventBus.java b/source/net/filebot/util/ui/SwingEventBus.java new file mode 100644 index 00000000..643ca62d --- /dev/null +++ b/source/net/filebot/util/ui/SwingEventBus.java @@ -0,0 +1,31 @@ +package net.filebot.util.ui; + +import static net.filebot.Logging.*; + +import java.util.logging.Level; + +import javax.swing.SwingUtilities; + +import com.google.common.eventbus.AsyncEventBus; +import com.google.common.eventbus.SubscriberExceptionContext; + +public class SwingEventBus extends AsyncEventBus { + + private static SwingEventBus instance; + + public static synchronized SwingEventBus getInstance() { + if (instance == null) { + instance = new SwingEventBus(); + } + return instance; + } + + public SwingEventBus() { + super(SwingUtilities::invokeLater, SwingEventBus::handleException); + } + + protected static void handleException(Throwable throwable, SubscriberExceptionContext context) { + debug.log(Level.WARNING, "Failed to handle event: " + context.getEvent(), throwable); + } + +}