Use com.google.common.eventbus.EventBus for handling application global FileTransferable events

This commit is contained in:
Reinhard Pointner 2016-03-19 14:42:40 +00:00
parent d5b6c404a7
commit 22ece907de
7 changed files with 134 additions and 107 deletions

View File

@ -28,6 +28,7 @@ import java.security.PermissionCollection;
import java.security.Permissions;
import java.security.Policy;
import java.security.ProtectionDomain;
import java.util.List;
import java.util.Map;
import java.util.logging.Handler;
import java.util.logging.Level;
@ -43,6 +44,8 @@ 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;
@ -170,20 +173,27 @@ public class Main {
}
// default frame
JFrame frame = new MainFrame(MainFrame.createPanelBuilders());
EventBus eventBus = new EventBus();
JFrame frame = new MainFrame(PanelBuilder.defaultSequence(), eventBus);
// single panel frame
if (args.mode != null) {
PanelBuilder[] selection = stream(MainFrame.createPanelBuilders()).filter(p -> p.getName().matches(args.mode)).toArray(PanelBuilder[]::new);
if (selection.length == 1) {
frame = new SinglePanelFrame(selection[0]).publish(new FileTransferable(args.getFiles(false)));
frame = new SinglePanelFrame(selection[0], eventBus);
} else if (selection.length > 1) {
frame = new MainFrame(selection);
frame = new MainFrame(selection, eventBus);
} else {
throw new IllegalArgumentException("Illegal mode: " + args.mode);
}
}
// handle file arguments
List<File> files = args.getFiles(false);
if (files.size() > 0) {
eventBus.post(new FileTransferable(files));
}
try {
// restore previous size and location
restoreWindowBounds(frame, Settings.forPackage(MainFrame.class));

View File

@ -37,6 +37,8 @@ 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;
@ -55,22 +57,26 @@ 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;
private static final PreferencesEntry<String> persistentSelectedPanel = Settings.forPackage(MainFrame.class).entry("panel.selected").defaultValue("0");
public MainFrame(PanelBuilder[] panels) {
public MainFrame(PanelBuilder[] panels, EventBus eventBus) {
super(isInstalled() ? getApplicationName() : String.format("%s %s", getApplicationName(), getApplicationVersion()));
this.eventBus = eventBus;
selectionList = new PanelSelectionList(panels);
headerPanel = new HeaderPanel();
// restore selected panel
try {
// restore selected panel
selectionList.setSelectedIndex(Integer.parseInt(persistentSelectedPanel.getValue()));
} catch (NumberFormatException e) {
// ignore
} catch (Exception e) {
debug.warning(e.getMessage());
}
JScrollPane selectionListScrollPane = new JScrollPane(selectionList, VERTICAL_SCROLLBAR_NEVER, HORIZONTAL_SCROLLBAR_NEVER);
@ -164,35 +170,34 @@ public class MainFrame extends JFrame {
}
protected void showPanel(PanelBuilder selectedBuilder) {
final JComponent contentPane = (JComponent) getContentPane();
JComponent panel = null;
JComponent contentPane = (JComponent) getContentPane();
JComponent selectedPanel = null;
for (int i = 0; i < contentPane.getComponentCount(); i++) {
JComponent c = (JComponent) contentPane.getComponent(i);
PanelBuilder builder = (PanelBuilder) c.getClientProperty("panelBuilder");
JComponent panel = (JComponent) contentPane.getComponent(i);
PanelBuilder builder = (PanelBuilder) panel.getClientProperty(PanelBuilder.class.getName());
if (builder != null) {
if (builder.equals(selectedBuilder)) {
panel = c;
selectedPanel = panel;
} else {
c.setVisible(false);
panel.setVisible(false);
eventBus.unregister(panel);
}
}
}
if (panel == null) {
panel = selectedBuilder.create();
panel.setVisible(false); // invisible by default
panel.putClientProperty("panelBuilder", selectedBuilder);
contentPane.add(panel);
if (selectedPanel == null) {
selectedPanel = selectedBuilder.create();
selectedPanel.setVisible(false); // invisible by default
selectedPanel.putClientProperty(PanelBuilder.class.getName(), selectedBuilder);
contentPane.add(selectedPanel);
}
// make visible, ignore action is visible already
if (!panel.isVisible()) {
if (!selectedPanel.isVisible()) {
headerPanel.setTitle(selectedBuilder.getName());
panel.setVisible(true);
selectedPanel.setVisible(true);
eventBus.register(selectedPanel);
}
}

View File

@ -1,19 +1,26 @@
package net.filebot.ui;
import javax.swing.Icon;
import javax.swing.JComponent;
import net.filebot.ui.analyze.AnalyzePanelBuilder;
import net.filebot.ui.episodelist.EpisodeListPanelBuilder;
import net.filebot.ui.list.ListPanelBuilder;
import net.filebot.ui.rename.RenamePanelBuilder;
import net.filebot.ui.sfv.SfvPanelBuilder;
import net.filebot.ui.subtitle.SubtitlePanelBuilder;
public interface PanelBuilder {
public String getName();
public Icon getIcon();
public JComponent create();
public static PanelBuilder[] defaultSequence() {
return new PanelBuilder[] { new RenamePanelBuilder(), new EpisodeListPanelBuilder(), new SubtitlePanelBuilder(), new SfvPanelBuilder(), new AnalyzePanelBuilder(), new ListPanelBuilder() };
}
}

View File

@ -3,24 +3,20 @@ package net.filebot.ui;
import static net.filebot.Settings.*;
import java.awt.Dimension;
import java.awt.datatransfer.Transferable;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.border.EmptyBorder;
import net.filebot.ui.transfer.TransferablePolicy;
import net.filebot.ui.transfer.TransferablePolicy.TransferAction;
import net.filebot.util.ExceptionUtilities;
import com.google.common.eventbus.EventBus;
import net.miginfocom.swing.MigLayout;
public class SinglePanelFrame extends JFrame {
private final JComponent panel;
public SinglePanelFrame(PanelBuilder builder) {
public SinglePanelFrame(PanelBuilder builder, EventBus eventBus) {
super(String.format("%s %s %s", getApplicationName(), builder.getName(), getApplicationVersion()));
panel = builder.create();
JComponent panel = builder.create();
JComponent c = (JComponent) getContentPane();
c.setLayout(new MigLayout("insets 0, nogrid, fill", "fill", "fill"));
@ -35,20 +31,8 @@ public class SinglePanelFrame extends JFrame {
setSize(850, 600);
setMinimumSize(new Dimension(800, 400));
}
public SinglePanelFrame publish(Transferable transferable) {
TransferablePolicy policy = (TransferablePolicy) panel.getClientProperty("transferablePolicy");
try {
if (policy != null && policy.accept(transferable)) {
policy.handleTransferable(transferable, TransferAction.ADD);
}
} catch (Exception e) {
throw ExceptionUtilities.asRuntimeException(e);
}
return this;
eventBus.register(panel);
}
}

View File

@ -1,8 +1,17 @@
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;
import com.google.common.eventbus.Subscribe;
import net.filebot.ui.transfer.TransferablePolicy;
import net.filebot.ui.transfer.TransferablePolicy.TransferAction;
import net.miginfocom.swing.MigLayout;
public class AnalyzePanel extends JComponent {
@ -15,8 +24,6 @@ public class AnalyzePanel extends JComponent {
add(fileTreePanel, "grow 1, w 300:pref:500");
add(toolsPanel, "grow 2");
putClientProperty("transferablePolicy", fileTreePanel.getTransferablePolicy());
fileTreePanel.addPropertyChangeListener("filetree", evt -> {
// stopped loading, refresh tools
for (int i = 0; i < toolsPanel.getTabCount(); i++) {
@ -30,4 +37,17 @@ public class AnalyzePanel extends JComponent {
toolsPanel.addTab(tool.getName(), tool);
}
@Subscribe
public void handle(Transferable transferable) {
TransferablePolicy handler = fileTreePanel.getTransferablePolicy();
try {
if (handler != null && handler.accept(transferable)) {
handler.handleTransferable(transferable, TransferAction.ADD);
}
} catch (Exception e) {
debug.log(Level.WARNING, "Failed to handle transferable: " + transferable, e);
}
}
}

View File

@ -14,8 +14,8 @@ import java.awt.Component;
import java.awt.Cursor;
import java.awt.Insets;
import java.awt.Window;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
@ -47,6 +47,13 @@ import javax.swing.SwingWorker;
import javax.swing.border.CompoundBorder;
import javax.swing.border.TitledBorder;
import com.cedarsoftware.util.io.JsonReader;
import com.cedarsoftware.util.io.JsonWriter;
import com.google.common.eventbus.Subscribe;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.ListSelection;
import ca.odell.glazedlists.swing.DefaultEventSelectionModel;
import net.filebot.History;
import net.filebot.HistorySpooler;
import net.filebot.Language;
@ -62,6 +69,8 @@ import net.filebot.similarity.Match;
import net.filebot.ui.rename.FormatDialog.Mode;
import net.filebot.ui.rename.RenameModel.FormattedFuture;
import net.filebot.ui.transfer.BackgroundFileTransferablePolicy;
import net.filebot.ui.transfer.TransferablePolicy;
import net.filebot.ui.transfer.TransferablePolicy.TransferAction;
import net.filebot.util.PreferencesMap.PreferencesEntry;
import net.filebot.util.ui.ActionPopup;
import net.filebot.util.ui.LoadingOverlayPane;
@ -77,12 +86,6 @@ import net.filebot.web.MovieIdentificationService;
import net.filebot.web.MusicIdentificationService;
import net.filebot.web.SortOrder;
import net.miginfocom.swing.MigLayout;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.ListSelection;
import ca.odell.glazedlists.swing.DefaultEventSelectionModel;
import com.cedarsoftware.util.io.JsonReader;
import com.cedarsoftware.util.io.JsonWriter;
public class RenamePanel extends JComponent {
@ -168,41 +171,37 @@ public class RenamePanel extends JComponent {
new ScrollPaneSynchronizer(namesList, filesList);
// delete items from both lists
Action removeAction = new AbstractAction("Exclude Selected Items", ResourceManager.getIcon("dialog.cancel")) {
Action removeAction = newAction("Exclude Selected Items", ResourceManager.getIcon("dialog.cancel"), evt -> {
RenameList list = null;
boolean deleteCell;
@Override
public void actionPerformed(ActionEvent e) {
RenameList list = null;
boolean deleteCell;
if (evt.getSource() instanceof JButton) {
list = filesList;
deleteCell = isShiftOrAltDown(evt);
} else {
list = ((RenameList) evt.getSource());
deleteCell = isShiftOrAltDown(evt);
}
if (e.getSource() instanceof JButton) {
list = filesList;
deleteCell = isShiftOrAltDown(e);
int index = list.getListComponent().getSelectedIndex();
if (index >= 0) {
if (deleteCell) {
EventList eventList = list.getModel();
if (index < eventList.size()) {
list.getModel().remove(index);
}
} else {
list = ((RenameList) e.getSource());
deleteCell = isShiftOrAltDown(e);
renameModel.matches().remove(index);
}
int maxIndex = list.getModel().size() - 1;
if (index > maxIndex) {
index = maxIndex;
}
int index = list.getListComponent().getSelectedIndex();
if (index >= 0) {
if (deleteCell) {
EventList eventList = list.getModel();
if (index < eventList.size()) {
list.getModel().remove(index);
}
} else {
renameModel.matches().remove(index);
}
int maxIndex = list.getModel().size() - 1;
if (index > maxIndex) {
index = maxIndex;
}
if (index >= 0) {
list.getListComponent().setSelectedIndex(index);
}
list.getListComponent().setSelectedIndex(index);
}
}
};
});
namesList.setRemoveAction(removeAction);
filesList.setRemoveAction(removeAction);
@ -219,7 +218,7 @@ public class RenamePanel extends JComponent {
// create fetch popup
ActionPopup fetchPopup = createFetchPopup();
final Action fetchPopupAction = new ShowPopupAction("Fetch Data", ResourceManager.getIcon("action.fetch"));
Action fetchPopupAction = new ShowPopupAction("Fetch Data", ResourceManager.getIcon("action.fetch"));
JButton fetchButton = new JButton(fetchPopupAction);
filesList.getListComponent().setComponentPopupMenu(fetchPopup);
namesList.getListComponent().setComponentPopupMenu(fetchPopup);
@ -248,14 +247,10 @@ public class RenamePanel extends JComponent {
JButton macrosButton = createImageButton(macrosAction);
filesList.getButtonPanel().add(macrosButton, "gap 0");
matchButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// show popup on actionPerformed only when names list is empty
if (renameModel.names().isEmpty()) {
fetchPopupAction.actionPerformed(e);
}
// show popup on actionPerformed only when names list is empty
matchButton.addActionListener(evt -> {
if (renameModel.names().isEmpty()) {
fetchPopupAction.actionPerformed(evt);
}
});
@ -309,16 +304,11 @@ public class RenamePanel extends JComponent {
add(new LoadingOverlayPane(filesList, filesList, "37px", "30px"), "grow, sizegroupx list");
BackgroundFileTransferablePolicy<?> transferablePolicy = (BackgroundFileTransferablePolicy<?>) filesList.getTransferablePolicy();
transferablePolicy.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (BackgroundFileTransferablePolicy.LOADING_PROPERTY.equals(evt.getPropertyName())) {
filesList.firePropertyChange(LoadingOverlayPane.LOADING_PROPERTY, (boolean) evt.getOldValue(), (boolean) evt.getNewValue());
}
transferablePolicy.addPropertyChangeListener(evt -> {
if (BackgroundFileTransferablePolicy.LOADING_PROPERTY.equals(evt.getPropertyName())) {
filesList.firePropertyChange(LoadingOverlayPane.LOADING_PROPERTY, (boolean) evt.getOldValue(), (boolean) evt.getNewValue());
}
});
this.putClientProperty("transferablePolicy", transferablePolicy);
// make buttons larger
matchButton.setMargin(new Insets(3, 14, 2, 14));
@ -655,6 +645,19 @@ public class RenamePanel extends JComponent {
}
};
@Subscribe
public void handle(Transferable transferable) {
TransferablePolicy handler = filesList.getTransferablePolicy();
try {
if (handler != null && handler.accept(transferable)) {
handler.handleTransferable(transferable, TransferAction.ADD);
}
} catch (Exception e) {
debug.log(Level.WARNING, "Failed to handle transferable: " + transferable, e);
}
}
protected static class ShowPopupAction extends AbstractAction {
public ShowPopupAction(String name, Icon icon) {

View File

@ -4,6 +4,7 @@ import static java.lang.Math.*;
import static net.filebot.ui.sfv.ChecksumTableModel.*;
import static net.filebot.ui.transfer.BackgroundFileTransferablePolicy.*;
import static net.filebot.util.FileUtilities.*;
import static net.filebot.util.ui.SwingUI.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
@ -36,7 +37,6 @@ import net.filebot.ui.transfer.LoadAction;
import net.filebot.ui.transfer.SaveAction;
import net.filebot.ui.transfer.TransferablePolicy;
import net.filebot.util.FileUtilities;
import net.filebot.util.ui.SwingUI;
import net.miginfocom.swing.MigLayout;
public class SfvPanel extends JComponent {
@ -86,10 +86,8 @@ public class SfvPanel extends JComponent {
}
});
putClientProperty("transferablePolicy", transferablePolicy);
// Shortcut DELETE
SwingUI.installAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), removeAction);
installAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), removeAction);
}
public TransferablePolicy getTransferablePolicy() {