* minor improvements

* refactoring
This commit is contained in:
Reinhard Pointner 2009-07-05 12:38:52 +00:00
parent d0faaad55a
commit 955c83bfd9
7 changed files with 140 additions and 82 deletions

View File

@ -49,6 +49,7 @@ public class MediaInfoPane extends JTabbedPane {
public void addTableTab(String title, Map<String, String> data) {
JTable table = new JTable(new ParameterTableModel(data));
table.setFillsViewportHeight(true);
// allow sorting
table.setAutoCreateRowSorter(true);
@ -69,6 +70,7 @@ public class MediaInfoPane extends JTabbedPane {
@Override
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
dialog.dispose();
}
};
@ -81,12 +83,12 @@ public class MediaInfoPane extends JTabbedPane {
dialog.setVisible(true);
}
private static class ParameterTableModel extends AbstractTableModel {
private final List<Entry<String, String>> data;
public ParameterTableModel(Map<String, String> data) {
this.data = new ArrayList<Entry<String, String>>(data.entrySet());
}

View File

@ -3,6 +3,7 @@ package net.sourceforge.filebot.ui.panel.subtitle;
import static net.sourceforge.filebot.FileBotUtilities.*;
import static net.sourceforge.filebot.ui.panel.subtitle.SubtitleUtilities.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
@ -11,15 +12,8 @@ import java.awt.event.MouseListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.channels.FileChannel;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Deque;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.logging.Level;
@ -33,6 +27,7 @@ import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
@ -56,18 +51,14 @@ import ca.odell.glazedlists.swing.TextComponentMatcherEditor;
import net.miginfocom.swing.MigLayout;
import net.sourceforge.filebot.ResourceManager;
import net.sourceforge.filebot.subtitle.SubtitleElement;
import net.sourceforge.filebot.subtitle.SubtitleFormat;
import net.sourceforge.filebot.subtitle.SubtitleReader;
import net.sourceforge.filebot.ui.panel.subtitle.SubtitlePackage.Download.Phase;
import net.sourceforge.filebot.ui.transfer.DefaultTransferHandler;
import net.sourceforge.tuned.ByteBufferInputStream;
import net.sourceforge.tuned.ExceptionUtilities;
import net.sourceforge.tuned.ui.ListView;
import net.sourceforge.tuned.ui.TunedUtilities;
public class SubtitleDownloadComponent extends JComponent {
class SubtitleDownloadComponent extends JComponent {
private EventList<SubtitlePackage> packages = new BasicEventList<SubtitlePackage>();
@ -129,7 +120,7 @@ public class SubtitleDownloadComponent extends JComponent {
@Override
public void actionPerformed(ActionEvent e) {
fetchAll(packageList.getSelectedValues());
fetch(packageList.getSelectedValues());
}
});
@ -212,7 +203,7 @@ public class SubtitleDownloadComponent extends JComponent {
}
private void fetchAll(Object[] selection) {
private void fetch(Object[] selection) {
for (Object value : selection) {
fetch((SubtitlePackage) value);
}
@ -270,46 +261,12 @@ public class SubtitleDownloadComponent extends JComponent {
private void open(MemoryFile file) throws IOException {
Deque<SubtitleFormat> priorityList = new ArrayDeque<SubtitleFormat>();
SubtitleViewer viewer = new SubtitleViewer(file.getName());
viewer.getTitleLabel().setText("Subtitle Viewer");
viewer.getInfoLabel().setText(file.getPath());
// gather all formats, put likely formats first
for (SubtitleFormat format : SubtitleFormat.values()) {
if (format.filter().accept(file.getName())) {
priorityList.addFirst(format);
} else {
priorityList.addLast(format);
}
}
// decode subtitle file with the first reader that seems to work
for (SubtitleFormat format : priorityList) {
InputStream data = new ByteBufferInputStream(file.getData());
SubtitleReader reader = format.newReader(new InputStreamReader(data, "UTF-8"));
try {
if (reader.hasNext()) {
// correct format found
List<SubtitleElement> list = new ArrayList<SubtitleElement>(500);
// read subtitle file
while (reader.hasNext()) {
list.add(reader.next());
}
SubtitleViewer viewer = new SubtitleViewer(file.getName());
viewer.getTitleLabel().setText("Subtitle Viewer");
viewer.getInfoLabel().setText(file.getPath());
viewer.setData(list);
viewer.setVisible(true);
// we're done
return;
}
} finally {
reader.close();
}
}
viewer.setData(decode(file));
viewer.setVisible(true);
}
@ -345,17 +302,6 @@ public class SubtitleDownloadComponent extends JComponent {
}
private void write(MemoryFile source, File destination) throws IOException {
FileChannel fileChannel = new FileOutputStream(destination).getChannel();
try {
fileChannel.write(source.getData());
} finally {
fileChannel.close();
}
}
private final Action clearFilterAction = new AbstractAction(null, ResourceManager.getIcon("edit.clear")) {
@Override
@ -372,7 +318,7 @@ public class SubtitleDownloadComponent extends JComponent {
if (SwingUtilities.isLeftMouseButton(e) && (e.getClickCount() == 2)) {
JList list = (JList) e.getSource();
fetchAll(list.getSelectedValues());
fetch(list.getSelectedValues());
}
}
@ -402,20 +348,22 @@ public class SubtitleDownloadComponent extends JComponent {
final Object[] selection = list.getSelectedValues();
Action downloadAction = new AbstractAction("Download", ResourceManager.getIcon("package.fetch")) {
if (selection.length > 0) {
JPopupMenu contextMenu = new JPopupMenu();
@Override
public void actionPerformed(ActionEvent e) {
fetchAll(selection);
}
};
downloadAction.setEnabled(isPending(selection));
JPopupMenu contextMenu = new JPopupMenu();
contextMenu.add(downloadAction);
contextMenu.show(e.getComponent(), e.getX(), e.getY());
JMenuItem item = contextMenu.add(new AbstractAction("Download", ResourceManager.getIcon("package.fetch")) {
@Override
public void actionPerformed(ActionEvent e) {
fetch(selection);
}
});
// disable menu item if all selected elements have been fetched already
item.setEnabled(isPending(selection));
contextMenu.show(e.getComponent(), e.getX(), e.getY());
}
}
}

View File

@ -24,7 +24,7 @@ import net.sourceforge.filebot.web.SubtitleDescriptor;
import net.sourceforge.tuned.FileUtilities;
public class SubtitlePackage {
class SubtitlePackage {
private final SubtitleDescriptor subtitle;

View File

@ -17,7 +17,7 @@ import net.sourceforge.filebot.ResourceManager;
import net.sourceforge.tuned.ui.AbstractFancyListCellRenderer;
public class SubtitlePackageCellRenderer extends AbstractFancyListCellRenderer {
class SubtitlePackageCellRenderer extends AbstractFancyListCellRenderer {
private final JLabel titleLabel = new JLabel();
private final JLabel languageLabel = new JLabel();

View File

@ -0,0 +1,81 @@
package net.sourceforge.filebot.ui.panel.subtitle;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.channels.FileChannel;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import net.sourceforge.filebot.subtitle.SubtitleElement;
import net.sourceforge.filebot.subtitle.SubtitleFormat;
import net.sourceforge.filebot.subtitle.SubtitleReader;
import net.sourceforge.tuned.ByteBufferInputStream;
final class SubtitleUtilities {
public static List<SubtitleElement> decode(MemoryFile file) throws IOException {
Deque<SubtitleFormat> priorityList = new ArrayDeque<SubtitleFormat>();
// gather all formats, put likely formats first
for (SubtitleFormat format : SubtitleFormat.values()) {
if (format.filter().accept(file.getName())) {
priorityList.addFirst(format);
} else {
priorityList.addLast(format);
}
}
// decode subtitle file with the first reader that seems to work
for (SubtitleFormat format : priorityList) {
InputStream data = new ByteBufferInputStream(file.getData());
SubtitleReader reader = format.newReader(new InputStreamReader(data, "UTF-8"));
try {
if (reader.hasNext()) {
// correct format found
List<SubtitleElement> list = new ArrayList<SubtitleElement>(500);
// read subtitle file
while (reader.hasNext()) {
list.add(reader.next());
}
return list;
}
} finally {
reader.close();
}
}
// unsupported subtitle format
throw new IOException("Cannot read subtitle format");
}
public static void write(MemoryFile source, File destination) throws IOException {
FileChannel fileChannel = new FileOutputStream(destination).getChannel();
try {
fileChannel.write(source.getData());
} finally {
fileChannel.close();
}
}
/**
* Dummy constructor to prevent instantiation.
*/
private SubtitleUtilities() {
throw new UnsupportedOperationException();
}
}

View File

@ -130,6 +130,9 @@ public class OpenSubtitlesClient implements SubtitleProvider {
// map id by name
subLanguageCache.put(entry.getValue(), entry.getKey());
}
// some additional special handling
subLanguageCache.put("Brazilian", "pob");
}
}

View File

@ -5,8 +5,11 @@ package net.sourceforge.filebot.web;
import static java.util.Collections.*;
import static net.sourceforge.tuned.StringUtilities.*;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -19,6 +22,7 @@ import java.util.regex.Pattern;
import redstone.xmlrpc.XmlRpcClient;
import redstone.xmlrpc.XmlRpcException;
import redstone.xmlrpc.XmlRpcFault;
import redstone.xmlrpc.util.Base64;
import net.sourceforge.filebot.web.OpenSubtitlesSubtitleDescriptor.Property;
@ -169,6 +173,26 @@ public class OpenSubtitlesXmlRpc {
}
@SuppressWarnings("unchecked")
public Collection<String> detectLanguage(String text) throws XmlRpcFault {
try {
// base64 encoded text
String parameter = new String(Base64.encode(text.getBytes("utf-8")));
Map<String, Map<String, String>> response = (Map<String, Map<String, String>>) invoke("DetectLanguage", token, new Object[] { parameter });
if (response.containsKey("data")) {
return response.get("data").values();
}
return Collections.emptySet();
} catch (UnsupportedEncodingException e) {
// will not happen
throw new RuntimeException(e);
}
}
public Map<String, String> getSubLanguages() throws XmlRpcFault {
return getSubLanguages("en");
}