1
0
mirror of https://github.com/mitb-archive/filebot synced 2025-01-11 13:58:16 -05:00

Fix ListPanel "Copy selected item" behaviour

This commit is contained in:
Reinhard Pointner 2016-03-21 17:14:04 +00:00
parent 47ab6f6b96
commit 912ddbc2d8
7 changed files with 91 additions and 67 deletions

View File

@ -26,7 +26,7 @@ public class FileBotList<E> extends JComponent {
protected EventList<E> model = new BasicEventList<E>(); protected EventList<E> model = new BasicEventList<E>();
protected JList list = new JList(new DefaultEventListModel<E>(model)); protected JList<E> list = new JList<E>(new DefaultEventListModel<E>(model));
protected JScrollPane listScrollPane = new JScrollPane(list); protected JScrollPane listScrollPane = new JScrollPane(list);
@ -58,7 +58,7 @@ public class FileBotList<E> extends JComponent {
list.setModel(new DefaultEventListModel(model)); list.setModel(new DefaultEventListModel(model));
} }
public JList getListComponent() { public JList<E> getListComponent() {
return list; return list;
} }

View File

@ -2,16 +2,29 @@
package net.filebot.ui; package net.filebot.ui;
import java.awt.Cursor; import java.awt.Cursor;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.io.PrintWriter; 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; import net.filebot.ui.transfer.TextFileExportHandler;
public class FileBotListExportHandler<T> extends TextFileExportHandler { public class FileBotListExportHandler<T> extends TextFileExportHandler implements ClipboardHandler {
protected final FileBotList<T> list; protected final FileBotList<T> list;
protected final BiConsumer<T, PrintWriter> exportItem;
public FileBotListExportHandler(FileBotList<T> list) { public FileBotListExportHandler(FileBotList<T> list) {
this(list, (item, out) -> out.println(item));
}
public FileBotListExportHandler(FileBotList<T> list, BiConsumer<T, PrintWriter> exportItem) {
this.list = list; this.list = list;
this.exportItem = exportItem;
} }
@Override @Override
@ -24,20 +37,31 @@ public class FileBotListExportHandler<T> extends TextFileExportHandler {
try { try {
list.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); list.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
for (T item : list.getModel()) { for (T item : list.getModel()) {
export(item, out); exportItem.accept(item, out);
} }
} finally { } finally {
list.setCursor(Cursor.getDefaultCursor()); list.setCursor(Cursor.getDefaultCursor());
} }
} }
public void export(T item, PrintWriter out) {
out.println(item);
}
@Override @Override
public String getDefaultFileName() { public String getDefaultFileName() {
return list.getTitle() + ".txt"; 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);
}
} }

View File

@ -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<Episode> implements ClipboardHandler {
public EpisodeListExportHandler(FileBotList<Episode> 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<Episode> episodeData = export(list, false);
StringSelection stringSelection = new StringSelection(StringUtilities.join(episodeData.getArray(), System.lineSeparator()));
clipboard.setContents(new CompositeTranserable(episodeData, stringSelection), null);
}
public ArrayTransferable<Episode> 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<Episode>(selection);
}
}

View File

@ -6,9 +6,6 @@ import static net.filebot.web.EpisodeUtilities.*;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Toolkit; 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.ActionEvent;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeEvent;
@ -22,7 +19,6 @@ import java.util.Locale;
import javax.swing.AbstractAction; import javax.swing.AbstractAction;
import javax.swing.Icon; import javax.swing.Icon;
import javax.swing.JComboBox; import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JMenu; import javax.swing.JMenu;
import javax.swing.JPopupMenu; import javax.swing.JPopupMenu;
import javax.swing.JSpinner; import javax.swing.JSpinner;
@ -37,15 +33,10 @@ import net.filebot.media.MediaDetection;
import net.filebot.similarity.Normalization; import net.filebot.similarity.Normalization;
import net.filebot.ui.AbstractSearchPanel; import net.filebot.ui.AbstractSearchPanel;
import net.filebot.ui.FileBotList; import net.filebot.ui.FileBotList;
import net.filebot.ui.FileBotListExportHandler;
import net.filebot.ui.LanguageComboBox; import net.filebot.ui.LanguageComboBox;
import net.filebot.ui.PanelBuilder; import net.filebot.ui.PanelBuilder;
import net.filebot.ui.SelectDialog; 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.ui.transfer.SaveAction;
import net.filebot.util.StringUtilities;
import net.filebot.util.ui.LabelProvider; import net.filebot.util.ui.LabelProvider;
import net.filebot.util.ui.SelectButton; import net.filebot.util.ui.SelectButton;
import net.filebot.util.ui.SimpleLabelProvider; import net.filebot.util.ui.SimpleLabelProvider;
@ -279,37 +270,4 @@ public class EpisodeListPanel extends AbstractSearchPanel<EpisodeListProvider, E
} }
protected static class EpisodeListExportHandler extends FileBotListExportHandler<Episode> implements ClipboardHandler {
public EpisodeListExportHandler(FileBotList<Episode> 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<Episode> episodeData = export(list, false);
Transferable stringSelection = new StringSelection(StringUtilities.join(episodeData.getArray(), System.lineSeparator()));
clipboard.setContents(new CompositeTranserable(episodeData, stringSelection), null);
}
public ArrayTransferable<Episode> 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<Episode>(selection);
}
}
} }

View File

@ -9,7 +9,6 @@ import java.awt.BorderLayout;
import java.awt.Color; import java.awt.Color;
import java.awt.Font; import java.awt.Font;
import java.awt.datatransfer.Transferable; import java.awt.datatransfer.Transferable;
import java.io.PrintWriter;
import java.util.List; import java.util.List;
import java.util.ListIterator; import java.util.ListIterator;
import java.util.stream.IntStream; import java.util.stream.IntStream;
@ -71,13 +70,10 @@ public class ListPanel extends JComponent {
list.getRemoveAction().setEnabled(true); list.getRemoveAction().setEnabled(true);
list.setTransferablePolicy(new FileListTransferablePolicy(list::setTitle, editor::setText, this::createItemSequence)); list.setTransferablePolicy(new FileListTransferablePolicy(list::setTitle, editor::setText, this::createItemSequence));
list.setExportHandler(new FileBotListExportHandler<ListItem>(list) {
@Override FileBotListExportHandler<ListItem> exportHandler = new FileBotListExportHandler<ListItem>(list, (item, out) -> out.println(item.getFormattedValue()));
public void export(ListItem item, PrintWriter out) { list.setExportHandler(exportHandler);
out.println(item.getFormattedValue()); list.getTransferHandler().setClipboardHandler(exportHandler);
}
});
list.getListComponent().setCellRenderer(new DefaultFancyListCellRenderer() { list.getListComponent().setCellRenderer(new DefaultFancyListCellRenderer() {

View File

@ -34,10 +34,10 @@ public abstract class TextFileExportHandler implements TransferableExportHandler
@Override @Override
public Transferable createTransferable(JComponent c) { public Transferable createTransferable(JComponent c) {
// get transfer data
StringWriter buffer = new StringWriter(); StringWriter buffer = new StringWriter();
export(new PrintWriter(buffer)); try (PrintWriter out = new PrintWriter(buffer)) {
export(out);
}
return new TextFileTransferable(getDefaultFileName(), buffer.toString()); return new TextFileTransferable(getDefaultFileName(), buffer.toString());
} }

View File

@ -1,13 +1,13 @@
package net.filebot.ui.transfer; package net.filebot.ui.transfer;
import static java.nio.charset.StandardCharsets.*; import static java.nio.charset.StandardCharsets.*;
import static java.util.Collections.*;
import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.datatransfer.UnsupportedFlavorException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.AbstractMap; import java.util.AbstractMap;
import java.util.Collections;
import java.util.Set; import java.util.Set;
public class TextFileTransferable extends ByteBufferTransferable { public class TextFileTransferable extends ByteBufferTransferable {
@ -18,17 +18,13 @@ public class TextFileTransferable extends ByteBufferTransferable {
this(name, text, UTF_8); 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 // lazy data map for file transfer
super(new AbstractMap<String, ByteBuffer>() { super(new AbstractMap<String, ByteBuffer>() {
@Override @Override
public Set<Entry<String, ByteBuffer>> entrySet() { public Set<Entry<String, ByteBuffer>> entrySet() {
// encode text return singletonMap(name, charset.encode(text)).entrySet();
Entry<String, ByteBuffer> entry = new SimpleEntry<String, ByteBuffer>(name, charset.encode(text));
// return memory file entry
return Collections.singleton(entry);
} }
}); });