Use EventBus singleton that runs tasks on the EDT

This commit is contained in:
Reinhard Pointner 2016-03-20 07:52:13 +00:00
parent f724e0d949
commit 4bd0ed265d
10 changed files with 70 additions and 72 deletions

View File

@ -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<File> 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<File> files = args.getFiles(false);
if (files.size() > 0) {
eventBus.post(new FileTransferable(files));
}
// start application
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);

View File

@ -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<File> files = evt.getFiles();
if (files.size() > 0) {
SwingUtilities.invokeLater(() -> handler.accept(files));
handler.accept(files);
}
});
} catch (Throwable t) {

View File

@ -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<String> 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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -81,16 +81,12 @@ public class SubtitlePanel extends AbstractSearchPanel<SubtitleProvider, Subtitl
}
@Subscribe
public void handle(Transferable transferable) {
try {
SubtitleDropTarget target = downloadDropTarget;
List<File> files = FileTransferable.getFilesFromTransferable(transferable);
public void handle(Transferable transferable) throws Exception {
SubtitleDropTarget target = downloadDropTarget;
List<File> 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);
}
}

View File

@ -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);
}
}