From 0d34ea813966d76fbc97d7c2b0e626fe025f41e8 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Fri, 29 Apr 2016 00:55:49 +0800 Subject: [PATCH] + context menu for List panel (i.e. Send name list to Rename New Names) --- source/net/filebot/ui/PanelBuilder.java | 4 +++ source/net/filebot/ui/list/ListPanel.java | 27 ++++++++++++++++++- .../ui/transfer/TextFileExportHandler.java | 14 ++++++---- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/source/net/filebot/ui/PanelBuilder.java b/source/net/filebot/ui/PanelBuilder.java index 872ad2d8..9059f6a1 100644 --- a/source/net/filebot/ui/PanelBuilder.java +++ b/source/net/filebot/ui/PanelBuilder.java @@ -31,4 +31,8 @@ public interface PanelBuilder { return new PanelBuilder[] { new RenamePanelBuilder(), new SfvPanelBuilder(), new ListPanelBuilder() }; } + public static PanelBuilder[] textHandlerSequence() { + return new PanelBuilder[] { new RenamePanelBuilder() }; + } + } diff --git a/source/net/filebot/ui/list/ListPanel.java b/source/net/filebot/ui/list/ListPanel.java index b879fdc8..b710b1af 100644 --- a/source/net/filebot/ui/list/ListPanel.java +++ b/source/net/filebot/ui/list/ListPanel.java @@ -8,6 +8,8 @@ import static net.filebot.util.ui.SwingUI.*; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Font; +import java.awt.Toolkit; +import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; import java.util.List; import java.util.ListIterator; @@ -18,12 +20,15 @@ import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.JList; +import javax.swing.JMenu; import javax.swing.JPanel; +import javax.swing.JPopupMenu; import javax.swing.JSpinner; import javax.swing.JSpinner.NumberEditor; import javax.swing.JTextField; import javax.swing.SpinnerNumberModel; import javax.swing.SwingUtilities; +import javax.swing.TransferHandler; import javax.swing.event.DocumentEvent; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; @@ -39,6 +44,7 @@ import net.filebot.ResourceManager; import net.filebot.format.ExpressionFormat; import net.filebot.ui.FileBotList; import net.filebot.ui.FileBotListExportHandler; +import net.filebot.ui.PanelBuilder; import net.filebot.ui.transfer.LoadAction; import net.filebot.ui.transfer.SaveAction; import net.filebot.ui.transfer.TransferablePolicy; @@ -46,6 +52,7 @@ import net.filebot.ui.transfer.TransferablePolicy.TransferAction; import net.filebot.util.ui.DefaultFancyListCellRenderer; import net.filebot.util.ui.LazyDocumentListener; import net.filebot.util.ui.PrototypeCellValueUpdater; +import net.filebot.util.ui.SwingEventBus; import net.miginfocom.swing.MigLayout; public class ListPanel extends JComponent { @@ -68,13 +75,31 @@ public class ListPanel extends JComponent { list.getListComponent().getModel().addListDataListener(new PrototypeCellValueUpdater(list.getListComponent(), "")); list.getRemoveAction().setEnabled(true); - list.setTransferablePolicy(new FileListTransferablePolicy(list::setTitle, this::setFormatTemplate, this::createItemSequence)); FileBotListExportHandler exportHandler = new FileBotListExportHandler(list, (item, out) -> out.println(item.getFormattedValue())); list.setExportHandler(exportHandler); list.getTransferHandler().setClipboardHandler(exportHandler); + // context menu + JPopupMenu popup = new JPopupMenu("List"); + JMenu menu = new JMenu("Send to"); + for (PanelBuilder panel : PanelBuilder.textHandlerSequence()) { + menu.add(newAction(panel.getName(), panel.getIcon(), evt -> { + String text = list.getExportHandler().export(); + SwingEventBus.getInstance().post(panel); + invokeLater(200, () -> SwingEventBus.getInstance().post(new StringSelection(text))); + })); + } + popup.add(menu); + popup.addSeparator(); + popup.add(newAction("Copy", ResourceManager.getIcon("rename.action.copy"), evt -> { + list.getTransferHandler().getClipboardHandler().exportToClipboard(this, Toolkit.getDefaultToolkit().getSystemClipboard(), TransferHandler.COPY); + })); + popup.add(new SaveAction(list.getExportHandler())); + list.getListComponent().setComponentPopupMenu(popup); + + // cell renderer list.getListComponent().setCellRenderer(new DefaultFancyListCellRenderer() { @Override diff --git a/source/net/filebot/ui/transfer/TextFileExportHandler.java b/source/net/filebot/ui/transfer/TextFileExportHandler.java index c88503f5..a7f7a915 100644 --- a/source/net/filebot/ui/transfer/TextFileExportHandler.java +++ b/source/net/filebot/ui/transfer/TextFileExportHandler.java @@ -27,6 +27,14 @@ public abstract class TextFileExportHandler implements TransferableExportHandler } } + public String export() { + StringWriter buffer = new StringWriter(); + try (PrintWriter out = new PrintWriter(buffer)) { + export(out); + } + return buffer.toString(); + } + @Override public int getSourceActions(JComponent c) { return canExport() ? TransferHandler.COPY_OR_MOVE : TransferHandler.NONE; @@ -34,11 +42,7 @@ public abstract class TextFileExportHandler implements TransferableExportHandler @Override public Transferable createTransferable(JComponent c) { - StringWriter buffer = new StringWriter(); - try (PrintWriter out = new PrintWriter(buffer)) { - export(out); - } - return new TextFileTransferable(getDefaultFileName(), buffer.toString()); + return new TextFileTransferable(getDefaultFileName(), export()); } @Override