From 4a482832f9130a7b77dfa6e4800e65ed20a1da92 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Tue, 30 Jun 2009 20:49:09 +0000 Subject: [PATCH] * allow user to copy subtitle files via clipboard * added "Save as..." action for subtitles * improved subtitle download tab layout behaviour * added junrar-custom and commons-logging to fatjar build --- build.xml | 8 ++ .../subtitle/MemoryFileListExportHandler.java | 11 ++- .../subtitle/SubtitleDownloadComponent.java | 96 +++++++++++-------- .../ui/panel/subtitle/SubtitleViewer.java | 2 +- 4 files changed, 73 insertions(+), 44 deletions(-) diff --git a/build.xml b/build.xml index db5dfc21..779c712f 100644 --- a/build.xml +++ b/build.xml @@ -97,6 +97,14 @@ + + + + + + + + diff --git a/source/net/sourceforge/filebot/ui/panel/subtitle/MemoryFileListExportHandler.java b/source/net/sourceforge/filebot/ui/panel/subtitle/MemoryFileListExportHandler.java index e7e41965..64fcdd4f 100644 --- a/source/net/sourceforge/filebot/ui/panel/subtitle/MemoryFileListExportHandler.java +++ b/source/net/sourceforge/filebot/ui/panel/subtitle/MemoryFileListExportHandler.java @@ -2,6 +2,7 @@ package net.sourceforge.filebot.ui.panel.subtitle; +import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.Transferable; import java.nio.ByteBuffer; import java.util.AbstractList; @@ -14,10 +15,11 @@ import javax.swing.JList; import javax.swing.TransferHandler; import net.sourceforge.filebot.ui.transfer.ByteBufferTransferable; +import net.sourceforge.filebot.ui.transfer.ClipboardHandler; import net.sourceforge.filebot.ui.transfer.TransferableExportHandler; -class MemoryFileListExportHandler implements TransferableExportHandler { +class MemoryFileListExportHandler implements TransferableExportHandler, ClipboardHandler { public boolean canExport(JComponent component) { JList list = (JList) component; @@ -68,8 +70,15 @@ class MemoryFileListExportHandler implements TransferableExportHandler { } + @Override + public void exportToClipboard(JComponent component, Clipboard clip, int action) { + clip.setContents(createTransferable(component), null); + } + + @Override public void exportDone(JComponent source, Transferable data, int action) { } + } diff --git a/source/net/sourceforge/filebot/ui/panel/subtitle/SubtitleDownloadComponent.java b/source/net/sourceforge/filebot/ui/panel/subtitle/SubtitleDownloadComponent.java index 0db89d9f..7d776c54 100644 --- a/source/net/sourceforge/filebot/ui/panel/subtitle/SubtitleDownloadComponent.java +++ b/source/net/sourceforge/filebot/ui/panel/subtitle/SubtitleDownloadComponent.java @@ -99,27 +99,26 @@ public class SubtitleDownloadComponent extends JComponent { } }; + // install dnd and clipboard export handler + MemoryFileListExportHandler memoryFileExportHandler = new MemoryFileListExportHandler(); + fileList.setTransferHandler(new DefaultTransferHandler(null, memoryFileExportHandler, memoryFileExportHandler)); + fileList.setDragEnabled(true); - fileList.setTransferHandler(new DefaultTransferHandler(null, new MemoryFileListExportHandler())); fileList.addMouseListener(fileListMouseHandler); JButton clearButton = new JButton(clearFilterAction); clearButton.setOpaque(false); - JButton exportButton = new JButton(exportToFolderAction); - exportButton.setOpaque(false); - - setLayout(new MigLayout("fill, nogrid", "[fill]", "[pref!][fill]")); + setLayout(new MigLayout("nogrid, fill", "[fill]", "[pref!][fill]")); add(new JLabel("Filter:"), "gap indent:push"); add(filterEditor, "wmin 120px, gap rel"); add(clearButton, "w 24px!, h 24px!"); - add(new JScrollPane(packageList), "newline"); + add(new JScrollPane(packageList), "newline, hmin 80px"); JScrollPane scrollPane = new JScrollPane(fileList); scrollPane.setViewportBorder(new LineBorder(fileList.getBackground())); - add(scrollPane, "newline"); - add(exportButton, "w pref!, h pref!"); + add(scrollPane, "newline, hmin max(80px, 30%)"); } @@ -228,35 +227,6 @@ public class SubtitleDownloadComponent extends JComponent { } }; - private final Action exportToFolderAction = new AbstractAction("Export") { - - @Override - public void actionPerformed(ActionEvent evt) { - JComponent source = (JComponent) evt.getSource(); - - JFileChooser fileChooser = new JFileChooser(); - fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); - - if (fileChooser.showSaveDialog(source) == JFileChooser.APPROVE_OPTION) { - File folder = fileChooser.getSelectedFile(); - - for (MemoryFile file : files) { - try { - FileChannel fileChannel = new FileOutputStream(new File(folder, file.getName())).getChannel(); - - try { - fileChannel.write(file.getData()); - } finally { - fileChannel.close(); - } - } catch (IOException e) { - Logger.getLogger("ui").log(Level.SEVERE, e.getMessage(), e); - } - } - } - } - }; - private final MouseListener packageListMouseHandler = new MouseAdapter() { @Override @@ -382,7 +352,7 @@ public class SubtitleDownloadComponent extends JComponent { @Override public void actionPerformed(ActionEvent evt) { - // save() + save(selection); } }); @@ -392,12 +362,12 @@ public class SubtitleDownloadComponent extends JComponent { private void open(Object[] selection) { - for (Object object : selection) { - try { + try { + for (Object object : selection) { open((MemoryFile) object); - } catch (IOException e) { - Logger.getLogger(getClass().getName()).log(Level.WARNING, e.getMessage()); } + } catch (Exception e) { + Logger.getLogger("ui").log(Level.WARNING, e.getMessage(), e); } } @@ -447,6 +417,48 @@ public class SubtitleDownloadComponent extends JComponent { throw new IOException("Cannot read subtitle format"); } + + private void save(Object[] selection) { + try { + if (selection.length == 1) { + // single file + MemoryFile file = (MemoryFile) selection[0]; + + JFileChooser fileChooser = new JFileChooser(); + fileChooser.setSelectedFile(new File(validateFileName(file.getName()))); + + if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) { + write(file, fileChooser.getSelectedFile()); + } + } else { + // multiple files + JFileChooser fileChooser = new JFileChooser(); + fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); + + if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) { + File folder = fileChooser.getSelectedFile(); + + for (Object object : selection) { + MemoryFile file = (MemoryFile) object; + write(file, new File(folder, validateFileName(file.getName()))); + } + } + } + } catch (IOException e) { + Logger.getLogger("ui").log(Level.WARNING, e.getMessage(), e); + } + } + + + private void write(MemoryFile source, File destination) throws IOException { + FileChannel fileChannel = new FileOutputStream(destination).getChannel(); + + try { + fileChannel.write(source.getData()); + } finally { + fileChannel.close(); + } + } }; } diff --git a/source/net/sourceforge/filebot/ui/panel/subtitle/SubtitleViewer.java b/source/net/sourceforge/filebot/ui/panel/subtitle/SubtitleViewer.java index 3a8f5141..7f1cf914 100644 --- a/source/net/sourceforge/filebot/ui/panel/subtitle/SubtitleViewer.java +++ b/source/net/sourceforge/filebot/ui/panel/subtitle/SubtitleViewer.java @@ -74,7 +74,7 @@ class SubtitleViewer extends JFrame { header.add(titleLabel, "wrap"); header.add(infoLabel, "gap indent*2, wrap paragraph:push"); - JPanel content = new JPanel(new MigLayout("fill, insets dialog, nogrid", "[grow]", "[pref!][grow]")); + JPanel content = new JPanel(new MigLayout("fill, insets dialog, nogrid", "[fill]", "[pref!][fill]")); content.add(new JLabel("Filter:"), "gap indent:push"); content.add(filterEditor, "wmin 120px, gap rel");