mirror of
https://github.com/mitb-archive/filebot
synced 2024-12-23 16:28:51 -05:00
* refactoring
This commit is contained in:
parent
d1f12dd628
commit
857d73bdc0
@ -4,6 +4,7 @@ package net.sourceforge.filebot.ui.panel.rename;
|
||||
|
||||
import static java.awt.datatransfer.DataFlavor.*;
|
||||
import static net.sourceforge.filebot.FileBotUtilities.*;
|
||||
import static net.sourceforge.filebot.ui.transfer.FileTransferable.*;
|
||||
import static net.sourceforge.tuned.FileUtilities.*;
|
||||
|
||||
import java.awt.datatransfer.DataFlavor;
|
||||
@ -46,7 +47,7 @@ class NamesListTransferablePolicy extends FileTransferablePolicy {
|
||||
|
||||
@Override
|
||||
public boolean accept(Transferable tr) throws Exception {
|
||||
return tr.isDataFlavorSupported(stringFlavor) || super.accept(tr);
|
||||
return tr.isDataFlavorSupported(stringFlavor) || hasFileListFlavor(tr);
|
||||
}
|
||||
|
||||
|
||||
@ -65,7 +66,7 @@ class NamesListTransferablePolicy extends FileTransferablePolicy {
|
||||
if (tr.isDataFlavorSupported(episodeArrayFlavor)) {
|
||||
// episode array transferable
|
||||
model.addAll(Arrays.asList((Episode[]) tr.getTransferData((episodeArrayFlavor))));
|
||||
} else if (super.accept(tr)) {
|
||||
} else if (hasFileListFlavor(tr)) {
|
||||
// file transferable
|
||||
load(getFilesFromTransferable(tr));
|
||||
} else if (tr.isDataFlavorSupported(stringFlavor)) {
|
||||
|
@ -8,7 +8,7 @@ import java.util.ResourceBundle;
|
||||
import java.util.Set;
|
||||
|
||||
|
||||
class Language {
|
||||
public class Language {
|
||||
|
||||
private final String code;
|
||||
private final String name;
|
||||
|
@ -85,6 +85,13 @@ class SubtitleDownloadComponent extends JComponent {
|
||||
// file list view
|
||||
final JList fileList = new ListView(createFileListModel()) {
|
||||
|
||||
@Override
|
||||
protected String convertValueToText(Object value) {
|
||||
MemoryFile file = (MemoryFile) value;
|
||||
return file.getName();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected Icon convertValueToIcon(Object value) {
|
||||
if (SUBTITLE_FILES.accept(value.toString()))
|
||||
@ -280,7 +287,7 @@ class SubtitleDownloadComponent extends JComponent {
|
||||
fileChooser.setSelectedFile(new File(validateFileName(file.getName())));
|
||||
|
||||
if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
|
||||
write(file, fileChooser.getSelectedFile());
|
||||
write(file.getData(), fileChooser.getSelectedFile());
|
||||
}
|
||||
} else {
|
||||
// multiple files
|
||||
@ -292,7 +299,7 @@ class SubtitleDownloadComponent extends JComponent {
|
||||
|
||||
for (Object object : selection) {
|
||||
MemoryFile file = (MemoryFile) object;
|
||||
write(file, new File(folder, validateFileName(file.getName())));
|
||||
write(file.getData(), new File(folder, validateFileName(file.getName())));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ import net.sourceforge.filebot.web.SubtitleDescriptor;
|
||||
import net.sourceforge.tuned.FileUtilities;
|
||||
|
||||
|
||||
class SubtitlePackage {
|
||||
public class SubtitlePackage {
|
||||
|
||||
private final SubtitleDescriptor subtitle;
|
||||
|
||||
|
@ -3,10 +3,12 @@ package net.sourceforge.filebot.ui.panel.subtitle;
|
||||
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
@ -60,11 +62,31 @@ final class SubtitleUtilities {
|
||||
}
|
||||
|
||||
|
||||
public static void write(MemoryFile source, File destination) throws IOException {
|
||||
public static byte[] read(File source) throws IOException {
|
||||
InputStream in = new FileInputStream(source);
|
||||
|
||||
try {
|
||||
byte[] data = new byte[(int) source.length()];
|
||||
|
||||
int position = 0;
|
||||
int read = 0;
|
||||
|
||||
while (position < data.length && (read = in.read(data, position, data.length - position)) >= 0) {
|
||||
position += read;
|
||||
}
|
||||
|
||||
return data;
|
||||
} finally {
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void write(ByteBuffer data, File destination) throws IOException {
|
||||
FileChannel fileChannel = new FileOutputStream(destination).getChannel();
|
||||
|
||||
try {
|
||||
fileChannel.write(source.getData());
|
||||
fileChannel.write(data);
|
||||
} finally {
|
||||
fileChannel.close();
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ import net.sourceforge.tuned.ui.notification.SeparatorBorder;
|
||||
import net.sourceforge.tuned.ui.notification.SeparatorBorder.Position;
|
||||
|
||||
|
||||
class SubtitleViewer extends JFrame {
|
||||
public class SubtitleViewer extends JFrame {
|
||||
|
||||
private final JLabel titleLabel = new JLabel();
|
||||
|
||||
|
@ -2,6 +2,8 @@
|
||||
package net.sourceforge.filebot.ui.transfer;
|
||||
|
||||
|
||||
import static net.sourceforge.filebot.ui.transfer.FileTransferable.*;
|
||||
|
||||
import java.awt.datatransfer.Transferable;
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.beans.PropertyChangeSupport;
|
||||
@ -166,6 +168,7 @@ public abstract class BackgroundFileTransferablePolicy<V> extends FileTransferab
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected final PropertyChangeSupport swingPropertyChangeSupport = new SwingPropertyChangeSupport(this, true);
|
||||
|
||||
|
||||
|
@ -6,9 +6,17 @@ import java.awt.datatransfer.DataFlavor;
|
||||
import java.awt.datatransfer.Transferable;
|
||||
import java.awt.datatransfer.UnsupportedFlavorException;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.nio.CharBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
|
||||
public class FileTransferable implements Transferable {
|
||||
@ -82,4 +90,50 @@ public class FileTransferable implements Transferable {
|
||||
return flavor.isFlavorJavaFileListType() || flavor.equals(uriListFlavor);
|
||||
}
|
||||
|
||||
|
||||
public static boolean hasFileListFlavor(Transferable tr) {
|
||||
return tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor) || tr.isDataFlavorSupported(FileTransferable.uriListFlavor);
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static List<File> getFilesFromTransferable(Transferable tr) throws IOException, UnsupportedFlavorException {
|
||||
if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
|
||||
// file list flavor
|
||||
return (List<File>) tr.getTransferData(DataFlavor.javaFileListFlavor);
|
||||
} else if (tr.isDataFlavorSupported(FileTransferable.uriListFlavor)) {
|
||||
// file URI list flavor
|
||||
Readable transferData = (Readable) tr.getTransferData(FileTransferable.uriListFlavor);
|
||||
|
||||
Scanner scanner = new Scanner(transferData);
|
||||
|
||||
List<File> files = new ArrayList<File>();
|
||||
|
||||
while (scanner.hasNextLine()) {
|
||||
String uri = scanner.nextLine();
|
||||
|
||||
if (uri.startsWith("#")) {
|
||||
// the line is a comment (as per RFC 2483)
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
File file = new File(new URI(uri));
|
||||
|
||||
if (!file.exists())
|
||||
throw new FileNotFoundException(file.toString());
|
||||
|
||||
files.add(file);
|
||||
} catch (Exception e) {
|
||||
// URISyntaxException, IllegalArgumentException, FileNotFoundException
|
||||
Logger.getLogger(FileTransferable.class.getName()).log(Level.WARNING, "Invalid file uri: " + uri);
|
||||
}
|
||||
}
|
||||
|
||||
return files;
|
||||
}
|
||||
|
||||
// cannot get files from transferable
|
||||
throw new UnsupportedFlavorException(DataFlavor.javaFileListFlavor);
|
||||
}
|
||||
}
|
||||
|
@ -2,73 +2,27 @@
|
||||
package net.sourceforge.filebot.ui.transfer;
|
||||
|
||||
|
||||
import java.awt.datatransfer.DataFlavor;
|
||||
import static net.sourceforge.filebot.ui.transfer.FileTransferable.*;
|
||||
|
||||
import java.awt.datatransfer.Transferable;
|
||||
import java.awt.datatransfer.UnsupportedFlavorException;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
|
||||
public abstract class FileTransferablePolicy extends TransferablePolicy {
|
||||
|
||||
@Override
|
||||
public boolean accept(Transferable tr) throws Exception {
|
||||
List<File> files = getFilesFromTransferable(tr);
|
||||
|
||||
if (files.isEmpty())
|
||||
return false;
|
||||
|
||||
return accept(files);
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected List<File> getFilesFromTransferable(Transferable tr) throws Exception {
|
||||
if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
|
||||
// file list flavor
|
||||
return (List<File>) tr.getTransferData(DataFlavor.javaFileListFlavor);
|
||||
} else if (tr.isDataFlavorSupported(FileTransferable.uriListFlavor)) {
|
||||
// file URI list flavor
|
||||
Readable transferData = (Readable) tr.getTransferData(FileTransferable.uriListFlavor);
|
||||
|
||||
Scanner scanner = new Scanner(transferData);
|
||||
|
||||
List<File> files = new ArrayList<File>();
|
||||
|
||||
while (scanner.hasNextLine()) {
|
||||
String uri = scanner.nextLine();
|
||||
|
||||
if (uri.startsWith("#")) {
|
||||
// the line is a comment (as per RFC 2483)
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
File file = new File(new URI(uri));
|
||||
|
||||
if (!file.exists())
|
||||
throw new FileNotFoundException(file.toString());
|
||||
|
||||
files.add(file);
|
||||
} catch (Exception e) {
|
||||
// URISyntaxException, IllegalArgumentException, FileNotFoundException
|
||||
Logger.getLogger(getClass().getName()).log(Level.WARNING, "Invalid file uri: " + uri);
|
||||
return accept(getFilesFromTransferable(tr));
|
||||
} catch (UnsupportedFlavorException e) {
|
||||
// no file list flavor
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return files;
|
||||
}
|
||||
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void handleTransferable(Transferable tr, TransferAction action) throws Exception {
|
||||
@ -88,9 +42,7 @@ public abstract class FileTransferablePolicy extends TransferablePolicy {
|
||||
protected abstract void load(List<File> files) throws IOException;
|
||||
|
||||
|
||||
protected void clear() {
|
||||
// do nothing
|
||||
}
|
||||
protected abstract void clear();
|
||||
|
||||
|
||||
public String getFileFilterDescription() {
|
||||
|
@ -6,7 +6,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public final class EpisodeListUtilities {
|
||||
final class EpisodeListUtilities {
|
||||
|
||||
public static List<Episode> filterBySeason(Iterable<Episode> episodes, int season) {
|
||||
|
||||
|
@ -93,6 +93,17 @@ public class ByteBufferOutputStream extends OutputStream {
|
||||
}
|
||||
|
||||
|
||||
public byte[] getByteArray() {
|
||||
ByteBuffer data = getByteBuffer();
|
||||
|
||||
// copy data to byte array
|
||||
byte[] bytes = new byte[data.remaining()];
|
||||
data.get(bytes);
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
|
||||
public int transferFrom(ReadableByteChannel channel) throws IOException {
|
||||
// make sure buffer is not at its limit
|
||||
ensureCapacity(buffer.position() + 1);
|
||||
|
Loading…
Reference in New Issue
Block a user