From 912ddbc2d864f48c1949d43f5ed7c6c44e626a83 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Mon, 21 Mar 2016 17:14:04 +0000 Subject: [PATCH] Fix ListPanel "Copy selected item" behaviour --- source/net/filebot/ui/FileBotList.java | 4 +- .../filebot/ui/FileBotListExportHandler.java | 36 ++++++++++--- .../episodelist/EpisodeListExportHandler.java | 50 +++++++++++++++++++ .../ui/episodelist/EpisodeListPanel.java | 42 ---------------- source/net/filebot/ui/list/ListPanel.java | 10 ++-- .../ui/transfer/TextFileExportHandler.java | 6 +-- .../ui/transfer/TextFileTransferable.java | 10 ++-- 7 files changed, 91 insertions(+), 67 deletions(-) create mode 100644 source/net/filebot/ui/episodelist/EpisodeListExportHandler.java diff --git a/source/net/filebot/ui/FileBotList.java b/source/net/filebot/ui/FileBotList.java index be6e334a..be60b77f 100644 --- a/source/net/filebot/ui/FileBotList.java +++ b/source/net/filebot/ui/FileBotList.java @@ -26,7 +26,7 @@ public class FileBotList extends JComponent { protected EventList model = new BasicEventList(); - protected JList list = new JList(new DefaultEventListModel(model)); + protected JList list = new JList(new DefaultEventListModel(model)); protected JScrollPane listScrollPane = new JScrollPane(list); @@ -58,7 +58,7 @@ public class FileBotList extends JComponent { list.setModel(new DefaultEventListModel(model)); } - public JList getListComponent() { + public JList getListComponent() { return list; } diff --git a/source/net/filebot/ui/FileBotListExportHandler.java b/source/net/filebot/ui/FileBotListExportHandler.java index 5230f5db..e1d39fc0 100644 --- a/source/net/filebot/ui/FileBotListExportHandler.java +++ b/source/net/filebot/ui/FileBotListExportHandler.java @@ -2,16 +2,29 @@ package net.filebot.ui; import java.awt.Cursor; +import java.awt.datatransfer.Clipboard; +import java.awt.datatransfer.StringSelection; import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.function.BiConsumer; +import javax.swing.JComponent; + +import net.filebot.ui.transfer.ClipboardHandler; import net.filebot.ui.transfer.TextFileExportHandler; -public class FileBotListExportHandler extends TextFileExportHandler { +public class FileBotListExportHandler extends TextFileExportHandler implements ClipboardHandler { protected final FileBotList list; + protected final BiConsumer exportItem; public FileBotListExportHandler(FileBotList list) { + this(list, (item, out) -> out.println(item)); + } + + public FileBotListExportHandler(FileBotList list, BiConsumer exportItem) { this.list = list; + this.exportItem = exportItem; } @Override @@ -24,20 +37,31 @@ public class FileBotListExportHandler extends TextFileExportHandler { try { list.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); for (T item : list.getModel()) { - export(item, out); + exportItem.accept(item, out); } } finally { list.setCursor(Cursor.getDefaultCursor()); } } - public void export(T item, PrintWriter out) { - out.println(item); - } - @Override public String getDefaultFileName() { return list.getTitle() + ".txt"; } + @Override + public void exportToClipboard(JComponent c, Clipboard clip, int action) throws IllegalStateException { + StringWriter buffer = new StringWriter(); + try (PrintWriter out = new PrintWriter(buffer)) { + list.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); + for (T item : list.getListComponent().getSelectedValuesList()) { + exportItem.accept(item, out); + } + } finally { + list.setCursor(Cursor.getDefaultCursor()); + } + + clip.setContents(new StringSelection(buffer.toString()), null); + } + } diff --git a/source/net/filebot/ui/episodelist/EpisodeListExportHandler.java b/source/net/filebot/ui/episodelist/EpisodeListExportHandler.java new file mode 100644 index 00000000..6cfcfc04 --- /dev/null +++ b/source/net/filebot/ui/episodelist/EpisodeListExportHandler.java @@ -0,0 +1,50 @@ +package net.filebot.ui.episodelist; + +import java.awt.datatransfer.Clipboard; +import java.awt.datatransfer.StringSelection; +import java.awt.datatransfer.Transferable; +import java.util.List; + +import javax.swing.JComponent; + +import net.filebot.ui.FileBotList; +import net.filebot.ui.FileBotListExportHandler; +import net.filebot.ui.transfer.ArrayTransferable; +import net.filebot.ui.transfer.ClipboardHandler; +import net.filebot.ui.transfer.CompositeTranserable; +import net.filebot.util.StringUtilities; +import net.filebot.web.Episode; + +class EpisodeListExportHandler extends FileBotListExportHandler implements ClipboardHandler { + + public EpisodeListExportHandler(FileBotList list) { + super(list); + } + + @Override + public Transferable createTransferable(JComponent c) { + Transferable episodeArray = export(list, true); + Transferable textFile = super.createTransferable(c); + + return new CompositeTranserable(episodeArray, textFile); + } + + @Override + public void exportToClipboard(JComponent c, Clipboard clipboard, int action) throws IllegalStateException { + ArrayTransferable episodeData = export(list, false); + StringSelection stringSelection = new StringSelection(StringUtilities.join(episodeData.getArray(), System.lineSeparator())); + + clipboard.setContents(new CompositeTranserable(episodeData, stringSelection), null); + } + + public ArrayTransferable export(FileBotList list, boolean forceAll) { + Episode[] selection = ((List) list.getListComponent().getSelectedValuesList()).stream().map(Episode.class::cast).toArray(Episode[]::new); + + if (forceAll || selection.length == 0) { + selection = list.getModel().stream().map(Episode.class::cast).toArray(Episode[]::new); + } + + return new ArrayTransferable(selection); + } + +} diff --git a/source/net/filebot/ui/episodelist/EpisodeListPanel.java b/source/net/filebot/ui/episodelist/EpisodeListPanel.java index c5d33c57..6aa2e981 100644 --- a/source/net/filebot/ui/episodelist/EpisodeListPanel.java +++ b/source/net/filebot/ui/episodelist/EpisodeListPanel.java @@ -6,9 +6,6 @@ import static net.filebot.web.EpisodeUtilities.*; import java.awt.Dimension; import java.awt.Toolkit; -import java.awt.datatransfer.Clipboard; -import java.awt.datatransfer.StringSelection; -import java.awt.datatransfer.Transferable; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.beans.PropertyChangeEvent; @@ -22,7 +19,6 @@ import java.util.Locale; import javax.swing.AbstractAction; import javax.swing.Icon; import javax.swing.JComboBox; -import javax.swing.JComponent; import javax.swing.JMenu; import javax.swing.JPopupMenu; import javax.swing.JSpinner; @@ -37,15 +33,10 @@ import net.filebot.media.MediaDetection; import net.filebot.similarity.Normalization; import net.filebot.ui.AbstractSearchPanel; import net.filebot.ui.FileBotList; -import net.filebot.ui.FileBotListExportHandler; import net.filebot.ui.LanguageComboBox; import net.filebot.ui.PanelBuilder; import net.filebot.ui.SelectDialog; -import net.filebot.ui.transfer.ArrayTransferable; -import net.filebot.ui.transfer.ClipboardHandler; -import net.filebot.ui.transfer.CompositeTranserable; import net.filebot.ui.transfer.SaveAction; -import net.filebot.util.StringUtilities; import net.filebot.util.ui.LabelProvider; import net.filebot.util.ui.SelectButton; import net.filebot.util.ui.SimpleLabelProvider; @@ -279,37 +270,4 @@ public class EpisodeListPanel extends AbstractSearchPanel implements ClipboardHandler { - - public EpisodeListExportHandler(FileBotList list) { - super(list); - } - - @Override - public Transferable createTransferable(JComponent c) { - Transferable episodeArray = export(list, true); - Transferable textFile = super.createTransferable(c); - - return new CompositeTranserable(episodeArray, textFile); - } - - @Override - public void exportToClipboard(JComponent c, Clipboard clipboard, int action) throws IllegalStateException { - ArrayTransferable episodeData = export(list, false); - Transferable stringSelection = new StringSelection(StringUtilities.join(episodeData.getArray(), System.lineSeparator())); - - clipboard.setContents(new CompositeTranserable(episodeData, stringSelection), null); - } - - public ArrayTransferable export(FileBotList list, boolean forceAll) { - Episode[] selection = ((List) list.getListComponent().getSelectedValuesList()).stream().map(Episode.class::cast).toArray(Episode[]::new); - - if (forceAll || selection.length == 0) { - selection = list.getModel().stream().map(Episode.class::cast).toArray(Episode[]::new); - } - - return new ArrayTransferable(selection); - } - } - } diff --git a/source/net/filebot/ui/list/ListPanel.java b/source/net/filebot/ui/list/ListPanel.java index 1f17599d..1af7406e 100644 --- a/source/net/filebot/ui/list/ListPanel.java +++ b/source/net/filebot/ui/list/ListPanel.java @@ -9,7 +9,6 @@ import java.awt.BorderLayout; import java.awt.Color; import java.awt.Font; import java.awt.datatransfer.Transferable; -import java.io.PrintWriter; import java.util.List; import java.util.ListIterator; import java.util.stream.IntStream; @@ -71,13 +70,10 @@ public class ListPanel extends JComponent { list.getRemoveAction().setEnabled(true); list.setTransferablePolicy(new FileListTransferablePolicy(list::setTitle, editor::setText, this::createItemSequence)); - list.setExportHandler(new FileBotListExportHandler(list) { - @Override - public void export(ListItem item, PrintWriter out) { - out.println(item.getFormattedValue()); - } - }); + FileBotListExportHandler exportHandler = new FileBotListExportHandler(list, (item, out) -> out.println(item.getFormattedValue())); + list.setExportHandler(exportHandler); + list.getTransferHandler().setClipboardHandler(exportHandler); list.getListComponent().setCellRenderer(new DefaultFancyListCellRenderer() { diff --git a/source/net/filebot/ui/transfer/TextFileExportHandler.java b/source/net/filebot/ui/transfer/TextFileExportHandler.java index 21c4178e..c88503f5 100644 --- a/source/net/filebot/ui/transfer/TextFileExportHandler.java +++ b/source/net/filebot/ui/transfer/TextFileExportHandler.java @@ -34,10 +34,10 @@ public abstract class TextFileExportHandler implements TransferableExportHandler @Override public Transferable createTransferable(JComponent c) { - // get transfer data StringWriter buffer = new StringWriter(); - export(new PrintWriter(buffer)); - + try (PrintWriter out = new PrintWriter(buffer)) { + export(out); + } return new TextFileTransferable(getDefaultFileName(), buffer.toString()); } diff --git a/source/net/filebot/ui/transfer/TextFileTransferable.java b/source/net/filebot/ui/transfer/TextFileTransferable.java index ffc0a7cd..7ce21a09 100644 --- a/source/net/filebot/ui/transfer/TextFileTransferable.java +++ b/source/net/filebot/ui/transfer/TextFileTransferable.java @@ -1,13 +1,13 @@ package net.filebot.ui.transfer; import static java.nio.charset.StandardCharsets.*; +import static java.util.Collections.*; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.UnsupportedFlavorException; import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.util.AbstractMap; -import java.util.Collections; import java.util.Set; public class TextFileTransferable extends ByteBufferTransferable { @@ -18,17 +18,13 @@ public class TextFileTransferable extends ByteBufferTransferable { this(name, text, UTF_8); } - public TextFileTransferable(final String name, final String text, final Charset charset) { + public TextFileTransferable(String name, String text, Charset charset) { // lazy data map for file transfer super(new AbstractMap() { @Override public Set> entrySet() { - // encode text - Entry entry = new SimpleEntry(name, charset.encode(text)); - - // return memory file entry - return Collections.singleton(entry); + return singletonMap(name, charset.encode(text)).entrySet(); } });