filebot/source/net/filebot/ui/subtitle/MemoryFileListExportHandler...

86 lines
1.9 KiB
Java
Raw Normal View History

2014-04-19 02:30:29 -04:00
package net.filebot.ui.subtitle;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.Transferable;
import java.nio.ByteBuffer;
import java.util.AbstractList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.TransferHandler;
2014-04-19 02:30:29 -04:00
import net.filebot.ui.transfer.ByteBufferTransferable;
import net.filebot.ui.transfer.ClipboardHandler;
import net.filebot.ui.transfer.TransferableExportHandler;
import net.filebot.vfs.MemoryFile;
class MemoryFileListExportHandler implements TransferableExportHandler, ClipboardHandler {
2015-07-25 18:47:19 -04:00
public boolean canExport(JComponent component) {
JList list = (JList) component;
2015-07-25 18:47:19 -04:00
// can't export anything, if nothing is selected
return !list.isSelectionEmpty();
}
2015-07-25 18:47:19 -04:00
public List<MemoryFile> export(JComponent component) {
JList list = (JList) component;
2015-07-25 18:47:19 -04:00
// get selected values
final Object[] selection = list.getSelectedValues();
2015-07-25 18:47:19 -04:00
// as file list
return new AbstractList<MemoryFile>() {
2015-07-25 18:47:19 -04:00
@Override
public MemoryFile get(int index) {
return (MemoryFile) selection[index];
}
2015-07-25 18:47:19 -04:00
@Override
public int size() {
return selection.length;
}
};
}
2015-07-25 18:47:19 -04:00
@Override
public int getSourceActions(JComponent component) {
return canExport(component) ? TransferHandler.COPY_OR_MOVE : TransferHandler.NONE;
}
2015-07-25 18:47:19 -04:00
@Override
public Transferable createTransferable(JComponent component) {
Map<String, ByteBuffer> vfs = new HashMap<String, ByteBuffer>();
2015-07-25 18:47:19 -04:00
for (MemoryFile file : export(component)) {
vfs.put(file.getName(), file.getData());
}
2015-07-25 18:47:19 -04:00
return new ByteBufferTransferable(vfs);
}
2015-07-25 18:47:19 -04:00
@Override
public void exportToClipboard(JComponent component, Clipboard clip, int action) {
clip.setContents(createTransferable(component), null);
}
2015-07-25 18:47:19 -04:00
@Override
public void exportDone(JComponent source, Transferable data, int action) {
2015-07-25 18:47:19 -04:00
}
2015-07-25 18:47:19 -04:00
}