1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-11-16 06:15:02 -05:00

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.Permissions;
import java.security.Policy; import java.security.Policy;
import java.security.ProtectionDomain; import java.security.ProtectionDomain;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.logging.Handler; import java.util.logging.Handler;
import java.util.logging.Level; import java.util.logging.Level;
@ -43,6 +44,8 @@ import javax.swing.UIManager;
import org.kohsuke.args4j.CmdLineException; import org.kohsuke.args4j.CmdLineException;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import com.google.common.eventbus.EventBus;
import net.filebot.Settings.ApplicationFolder; import net.filebot.Settings.ApplicationFolder;
import net.filebot.cli.ArgumentBean; import net.filebot.cli.ArgumentBean;
import net.filebot.cli.ArgumentProcessor; import net.filebot.cli.ArgumentProcessor;
@ -170,20 +173,27 @@ public class Main {
} }
// default frame // default frame
JFrame frame = new MainFrame(MainFrame.createPanelBuilders()); EventBus eventBus = new EventBus();
JFrame frame = new MainFrame(PanelBuilder.defaultSequence(), eventBus);
// single panel frame // single panel frame
if (args.mode != null) { if (args.mode != null) {
PanelBuilder[] selection = stream(MainFrame.createPanelBuilders()).filter(p -> p.getName().matches(args.mode)).toArray(PanelBuilder[]::new); PanelBuilder[] selection = stream(MainFrame.createPanelBuilders()).filter(p -> p.getName().matches(args.mode)).toArray(PanelBuilder[]::new);
if (selection.length == 1) { 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) { } else if (selection.length > 1) {
frame = new MainFrame(selection); frame = new MainFrame(selection, eventBus);
} else { } else {
throw new IllegalArgumentException("Illegal mode: " + args.mode); 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 { try {
// restore previous size and location // restore previous size and location
restoreWindowBounds(frame, Settings.forPackage(MainFrame.class)); 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.ListSelectionEvent;
import javax.swing.event.ListSelectionListener; import javax.swing.event.ListSelectionListener;
import com.google.common.eventbus.EventBus;
import net.filebot.CacheManager; import net.filebot.CacheManager;
import net.filebot.Settings; import net.filebot.Settings;
import net.filebot.cli.GroovyPad; import net.filebot.cli.GroovyPad;
@ -55,22 +57,26 @@ import net.miginfocom.swing.MigLayout;
public class MainFrame extends JFrame { 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 JList selectionList;
private HeaderPanel headerPanel; private HeaderPanel headerPanel;
private static final PreferencesEntry<String> persistentSelectedPanel = Settings.forPackage(MainFrame.class).entry("panel.selected").defaultValue("0"); public MainFrame(PanelBuilder[] panels, EventBus eventBus) {
public MainFrame(PanelBuilder[] panels) {
super(isInstalled() ? getApplicationName() : String.format("%s %s", getApplicationName(), getApplicationVersion())); super(isInstalled() ? getApplicationName() : String.format("%s %s", getApplicationName(), getApplicationVersion()));
this.eventBus = eventBus;
selectionList = new PanelSelectionList(panels); selectionList = new PanelSelectionList(panels);
headerPanel = new HeaderPanel(); headerPanel = new HeaderPanel();
// restore selected panel
try { try {
// restore selected panel
selectionList.setSelectedIndex(Integer.parseInt(persistentSelectedPanel.getValue())); selectionList.setSelectedIndex(Integer.parseInt(persistentSelectedPanel.getValue()));
} catch (NumberFormatException e) { } catch (Exception e) {
// ignore debug.warning(e.getMessage());
} }
JScrollPane selectionListScrollPane = new JScrollPane(selectionList, VERTICAL_SCROLLBAR_NEVER, HORIZONTAL_SCROLLBAR_NEVER); 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) { protected void showPanel(PanelBuilder selectedBuilder) {
final JComponent contentPane = (JComponent) getContentPane(); JComponent contentPane = (JComponent) getContentPane();
JComponent selectedPanel = null;
JComponent panel = null;
for (int i = 0; i < contentPane.getComponentCount(); i++) { for (int i = 0; i < contentPane.getComponentCount(); i++) {
JComponent c = (JComponent) contentPane.getComponent(i); JComponent panel = (JComponent) contentPane.getComponent(i);
PanelBuilder builder = (PanelBuilder) c.getClientProperty("panelBuilder"); PanelBuilder builder = (PanelBuilder) panel.getClientProperty(PanelBuilder.class.getName());
if (builder != null) { if (builder != null) {
if (builder.equals(selectedBuilder)) { if (builder.equals(selectedBuilder)) {
panel = c; selectedPanel = panel;
} else { } else {
c.setVisible(false); panel.setVisible(false);
eventBus.unregister(panel);
} }
} }
} }
if (panel == null) { if (selectedPanel == null) {
panel = selectedBuilder.create(); selectedPanel = selectedBuilder.create();
panel.setVisible(false); // invisible by default selectedPanel.setVisible(false); // invisible by default
panel.putClientProperty("panelBuilder", selectedBuilder); selectedPanel.putClientProperty(PanelBuilder.class.getName(), selectedBuilder);
contentPane.add(selectedPanel);
contentPane.add(panel);
} }
// make visible, ignore action is visible already // make visible, ignore action is visible already
if (!panel.isVisible()) { if (!selectedPanel.isVisible()) {
headerPanel.setTitle(selectedBuilder.getName()); headerPanel.setTitle(selectedBuilder.getName());
panel.setVisible(true); selectedPanel.setVisible(true);
eventBus.register(selectedPanel);
} }
} }

View File

@ -1,19 +1,26 @@
package net.filebot.ui; package net.filebot.ui;
import javax.swing.Icon; import javax.swing.Icon;
import javax.swing.JComponent; 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 interface PanelBuilder {
public String getName(); public String getName();
public Icon getIcon(); public Icon getIcon();
public JComponent create(); 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 static net.filebot.Settings.*;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.datatransfer.Transferable;
import javax.swing.JComponent; import javax.swing.JComponent;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.border.EmptyBorder; import javax.swing.border.EmptyBorder;
import net.filebot.ui.transfer.TransferablePolicy; import com.google.common.eventbus.EventBus;
import net.filebot.ui.transfer.TransferablePolicy.TransferAction;
import net.filebot.util.ExceptionUtilities;
import net.miginfocom.swing.MigLayout; import net.miginfocom.swing.MigLayout;
public class SinglePanelFrame extends JFrame { public class SinglePanelFrame extends JFrame {
private final JComponent panel; public SinglePanelFrame(PanelBuilder builder, EventBus eventBus) {
public SinglePanelFrame(PanelBuilder builder) {
super(String.format("%s %s %s", getApplicationName(), builder.getName(), getApplicationVersion())); super(String.format("%s %s %s", getApplicationName(), builder.getName(), getApplicationVersion()));
panel = builder.create(); JComponent panel = builder.create();
JComponent c = (JComponent) getContentPane(); JComponent c = (JComponent) getContentPane();
c.setLayout(new MigLayout("insets 0, nogrid, fill", "fill", "fill")); c.setLayout(new MigLayout("insets 0, nogrid, fill", "fill", "fill"));
@ -35,20 +31,8 @@ public class SinglePanelFrame extends JFrame {
setSize(850, 600); setSize(850, 600);
setMinimumSize(new Dimension(800, 400)); setMinimumSize(new Dimension(800, 400));
}
public SinglePanelFrame publish(Transferable transferable) { eventBus.register(panel);
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;
} }
} }

View File

@ -1,8 +1,17 @@
package net.filebot.ui.analyze; 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.JComponent;
import javax.swing.JTabbedPane; 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; import net.miginfocom.swing.MigLayout;
public class AnalyzePanel extends JComponent { public class AnalyzePanel extends JComponent {
@ -15,8 +24,6 @@ public class AnalyzePanel extends JComponent {
add(fileTreePanel, "grow 1, w 300:pref:500"); add(fileTreePanel, "grow 1, w 300:pref:500");
add(toolsPanel, "grow 2"); add(toolsPanel, "grow 2");
putClientProperty("transferablePolicy", fileTreePanel.getTransferablePolicy());
fileTreePanel.addPropertyChangeListener("filetree", evt -> { fileTreePanel.addPropertyChangeListener("filetree", evt -> {
// stopped loading, refresh tools // stopped loading, refresh tools
for (int i = 0; i < toolsPanel.getTabCount(); i++) { for (int i = 0; i < toolsPanel.getTabCount(); i++) {
@ -30,4 +37,17 @@ public class AnalyzePanel extends JComponent {
toolsPanel.addTab(tool.getName(), tool); 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.Cursor;
import java.awt.Insets; import java.awt.Insets;
import java.awt.Window; import java.awt.Window;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeEvent;
@ -47,6 +47,13 @@ import javax.swing.SwingWorker;
import javax.swing.border.CompoundBorder; import javax.swing.border.CompoundBorder;
import javax.swing.border.TitledBorder; 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.History;
import net.filebot.HistorySpooler; import net.filebot.HistorySpooler;
import net.filebot.Language; 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.FormatDialog.Mode;
import net.filebot.ui.rename.RenameModel.FormattedFuture; import net.filebot.ui.rename.RenameModel.FormattedFuture;
import net.filebot.ui.transfer.BackgroundFileTransferablePolicy; 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.PreferencesMap.PreferencesEntry;
import net.filebot.util.ui.ActionPopup; import net.filebot.util.ui.ActionPopup;
import net.filebot.util.ui.LoadingOverlayPane; import net.filebot.util.ui.LoadingOverlayPane;
@ -77,12 +86,6 @@ import net.filebot.web.MovieIdentificationService;
import net.filebot.web.MusicIdentificationService; import net.filebot.web.MusicIdentificationService;
import net.filebot.web.SortOrder; import net.filebot.web.SortOrder;
import net.miginfocom.swing.MigLayout; 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 { public class RenamePanel extends JComponent {
@ -168,41 +171,37 @@ public class RenamePanel extends JComponent {
new ScrollPaneSynchronizer(namesList, filesList); new ScrollPaneSynchronizer(namesList, filesList);
// delete items from both lists // 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 if (evt.getSource() instanceof JButton) {
public void actionPerformed(ActionEvent e) { list = filesList;
RenameList list = null; deleteCell = isShiftOrAltDown(evt);
boolean deleteCell; } else {
list = ((RenameList) evt.getSource());
deleteCell = isShiftOrAltDown(evt);
}
if (e.getSource() instanceof JButton) { int index = list.getListComponent().getSelectedIndex();
list = filesList; if (index >= 0) {
deleteCell = isShiftOrAltDown(e); if (deleteCell) {
EventList eventList = list.getModel();
if (index < eventList.size()) {
list.getModel().remove(index);
}
} else { } else {
list = ((RenameList) e.getSource()); renameModel.matches().remove(index);
deleteCell = isShiftOrAltDown(e); }
int maxIndex = list.getModel().size() - 1;
if (index > maxIndex) {
index = maxIndex;
} }
int index = list.getListComponent().getSelectedIndex();
if (index >= 0) { if (index >= 0) {
if (deleteCell) { list.getListComponent().setSelectedIndex(index);
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);
}
} }
} }
}; });
namesList.setRemoveAction(removeAction); namesList.setRemoveAction(removeAction);
filesList.setRemoveAction(removeAction); filesList.setRemoveAction(removeAction);
@ -219,7 +218,7 @@ public class RenamePanel extends JComponent {
// create fetch popup // create fetch popup
ActionPopup fetchPopup = createFetchPopup(); 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); JButton fetchButton = new JButton(fetchPopupAction);
filesList.getListComponent().setComponentPopupMenu(fetchPopup); filesList.getListComponent().setComponentPopupMenu(fetchPopup);
namesList.getListComponent().setComponentPopupMenu(fetchPopup); namesList.getListComponent().setComponentPopupMenu(fetchPopup);
@ -248,14 +247,10 @@ public class RenamePanel extends JComponent {
JButton macrosButton = createImageButton(macrosAction); JButton macrosButton = createImageButton(macrosAction);
filesList.getButtonPanel().add(macrosButton, "gap 0"); filesList.getButtonPanel().add(macrosButton, "gap 0");
matchButton.addActionListener(new ActionListener() { // show popup on actionPerformed only when names list is empty
matchButton.addActionListener(evt -> {
@Override if (renameModel.names().isEmpty()) {
public void actionPerformed(ActionEvent e) { fetchPopupAction.actionPerformed(evt);
// show popup on actionPerformed only when names list is empty
if (renameModel.names().isEmpty()) {
fetchPopupAction.actionPerformed(e);
}
} }
}); });
@ -309,16 +304,11 @@ public class RenamePanel extends JComponent {
add(new LoadingOverlayPane(filesList, filesList, "37px", "30px"), "grow, sizegroupx list"); add(new LoadingOverlayPane(filesList, filesList, "37px", "30px"), "grow, sizegroupx list");
BackgroundFileTransferablePolicy<?> transferablePolicy = (BackgroundFileTransferablePolicy<?>) filesList.getTransferablePolicy(); BackgroundFileTransferablePolicy<?> transferablePolicy = (BackgroundFileTransferablePolicy<?>) filesList.getTransferablePolicy();
transferablePolicy.addPropertyChangeListener(new PropertyChangeListener() { transferablePolicy.addPropertyChangeListener(evt -> {
if (BackgroundFileTransferablePolicy.LOADING_PROPERTY.equals(evt.getPropertyName())) {
@Override filesList.firePropertyChange(LoadingOverlayPane.LOADING_PROPERTY, (boolean) evt.getOldValue(), (boolean) evt.getNewValue());
public void propertyChange(PropertyChangeEvent 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 // make buttons larger
matchButton.setMargin(new Insets(3, 14, 2, 14)); 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 { protected static class ShowPopupAction extends AbstractAction {
public ShowPopupAction(String name, Icon icon) { 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.sfv.ChecksumTableModel.*;
import static net.filebot.ui.transfer.BackgroundFileTransferablePolicy.*; import static net.filebot.ui.transfer.BackgroundFileTransferablePolicy.*;
import static net.filebot.util.FileUtilities.*; import static net.filebot.util.FileUtilities.*;
import static net.filebot.util.ui.SwingUI.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent; 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.SaveAction;
import net.filebot.ui.transfer.TransferablePolicy; import net.filebot.ui.transfer.TransferablePolicy;
import net.filebot.util.FileUtilities; import net.filebot.util.FileUtilities;
import net.filebot.util.ui.SwingUI;
import net.miginfocom.swing.MigLayout; import net.miginfocom.swing.MigLayout;
public class SfvPanel extends JComponent { public class SfvPanel extends JComponent {
@ -86,10 +86,8 @@ public class SfvPanel extends JComponent {
} }
}); });
putClientProperty("transferablePolicy", transferablePolicy);
// Shortcut DELETE // Shortcut DELETE
SwingUI.installAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), removeAction); installAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), removeAction);
} }
public TransferablePolicy getTransferablePolicy() { public TransferablePolicy getTransferablePolicy() {