From 036eefc5b97d6d9fcfd9b8416c8f6180474077fb Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Fri, 15 Feb 2008 18:57:18 +0000 Subject: [PATCH] * drop to file works on ubuntu now :D --- .../sourceforge/filebot/ui/FileBotUtil.java | 21 ++-- .../filebot/ui/panel/analyze/FileTree.java | 4 +- .../ui/panel/sfv/SfvTransferablePolicy.java | 12 +-- .../filebot/ui/transfer/FileTransferable.java | 27 ++--- .../ui/transfer/SaveableExportHandler.java | 3 +- .../TransferablePolicyImportHandler.java | 4 +- .../BackgroundFileTransferablePolicy.java | 9 +- .../FileTransferablePolicy.java | 102 ++++++------------ .../MultiTransferablePolicy.java | 12 ++- .../NullTransferablePolicy.java | 6 +- .../TextTransferablePolicy.java | 7 +- .../TransferablePolicy.java | 2 +- 12 files changed, 90 insertions(+), 119 deletions(-) diff --git a/source/net/sourceforge/filebot/ui/FileBotUtil.java b/source/net/sourceforge/filebot/ui/FileBotUtil.java index 56d64e05..9bb1f408 100644 --- a/source/net/sourceforge/filebot/ui/FileBotUtil.java +++ b/source/net/sourceforge/filebot/ui/FileBotUtil.java @@ -2,6 +2,8 @@ package net.sourceforge.filebot.ui; +import java.awt.datatransfer.DataFlavor; + import javax.swing.Action; import javax.swing.JComponent; import javax.swing.KeyStroke; @@ -32,13 +34,18 @@ public class FileBotUtil { return filename.replaceAll("[\\\\/:*?\"<>|]", ""); } - - public static boolean isFileListFlavorSupportedByWindowManager() { - String os = System.getProperty("os.name"); + public static final DataFlavor uriListFlavor = createUriListFlavor(); + + + private static DataFlavor createUriListFlavor() { + try { + return new DataFlavor("text/uri-list;class=java.lang.String"); + } catch (ClassNotFoundException e) { + // will never happen + e.printStackTrace(); + } - if (os.toLowerCase().contains("windows")) - return true; - - return false; + return null; } + } diff --git a/source/net/sourceforge/filebot/ui/panel/analyze/FileTree.java b/source/net/sourceforge/filebot/ui/panel/analyze/FileTree.java index f0880b3c..32fb5132 100644 --- a/source/net/sourceforge/filebot/ui/panel/analyze/FileTree.java +++ b/source/net/sourceforge/filebot/ui/panel/analyze/FileTree.java @@ -100,15 +100,13 @@ public class FileTree extends FileBotTree { @Override - protected boolean load(List files) { + protected void load(List files) { DefaultMutableTreeNode root = (DefaultMutableTreeNode) getModel().getRoot(); File fileArray[] = new File[files.size()]; files.toArray(fileArray); addFiles(root, fileArray); - - return true; } diff --git a/source/net/sourceforge/filebot/ui/panel/sfv/SfvTransferablePolicy.java b/source/net/sourceforge/filebot/ui/panel/sfv/SfvTransferablePolicy.java index 7812c041..12268585 100644 --- a/source/net/sourceforge/filebot/ui/panel/sfv/SfvTransferablePolicy.java +++ b/source/net/sourceforge/filebot/ui/panel/sfv/SfvTransferablePolicy.java @@ -50,7 +50,7 @@ public class SfvTransferablePolicy extends MultiTransferablePolicy { @Override - protected boolean load(List files) { + protected void load(List files) { synchronized (ChecksumComputationExecutor.getInstance()) { ChecksumComputationExecutor.getInstance().pause(); @@ -60,8 +60,6 @@ public class SfvTransferablePolicy extends MultiTransferablePolicy { ChecksumComputationExecutor.getInstance().resume(); } - - return true; } @@ -134,16 +132,16 @@ public class SfvTransferablePolicy extends MultiTransferablePolicy { @Override - protected boolean load(List files) { + protected void load(List files) { if (files.isEmpty()) - return true; + return; synchronized (ChecksumComputationExecutor.getInstance()) { ChecksumComputationExecutor.getInstance().pause(); File firstFile = files.get(0); - if (files.size() == 1 && firstFile.isDirectory()) { + if ((files.size() == 1) && firstFile.isDirectory()) { for (File f : firstFile.listFiles()) { load(f, firstFile, ""); } @@ -155,8 +153,6 @@ public class SfvTransferablePolicy extends MultiTransferablePolicy { ChecksumComputationExecutor.getInstance().resume(); } - - return true; } diff --git a/source/net/sourceforge/filebot/ui/transfer/FileTransferable.java b/source/net/sourceforge/filebot/ui/transfer/FileTransferable.java index 94fd4959..e4050989 100644 --- a/source/net/sourceforge/filebot/ui/transfer/FileTransferable.java +++ b/source/net/sourceforge/filebot/ui/transfer/FileTransferable.java @@ -15,10 +15,10 @@ import net.sourceforge.filebot.ui.FileBotUtil; public class FileTransferable implements Transferable { - private static final boolean fileListFlavorSupported = FileBotUtil.isFileListFlavorSupportedByWindowManager(); - private List files; + private DataFlavor[] supportedFlavors = { DataFlavor.javaFileListFlavor, FileBotUtil.uriListFlavor }; + public FileTransferable(File... fileArray) { files = new ArrayList(fileArray.length); @@ -38,7 +38,7 @@ public class FileTransferable implements Transferable { public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException { if (flavor.isFlavorJavaFileListType()) return files; - else if (flavor.isFlavorTextType()) + else if (flavor.equals(FileBotUtil.uriListFlavor)) return getUriList(); else throw new UnsupportedFlavorException(flavor); @@ -54,7 +54,7 @@ public class FileTransferable implements Transferable { for (File file : files) { sb.append(file.toURI()); - sb.append("\n"); + sb.append("\r\n"); } return sb.toString(); @@ -62,21 +62,16 @@ public class FileTransferable implements Transferable { public DataFlavor[] getTransferDataFlavors() { - if (fileListFlavorSupported) { - DataFlavor[] flavours = { DataFlavor.javaFileListFlavor }; - return flavours; - } else { - DataFlavor[] flavours = { DataFlavor.javaFileListFlavor, DataFlavor.stringFlavor }; - return flavours; - } + return supportedFlavors; } public boolean isDataFlavorSupported(DataFlavor flavor) { - if (fileListFlavorSupported) - return flavor.isFlavorJavaFileListType(); - else - return flavor.isFlavorJavaFileListType() || flavor.isFlavorTextType(); + for (DataFlavor supportedFlavor : supportedFlavors) { + if (flavor.equals(supportedFlavor)) + return true; + } + + return false; } - } diff --git a/source/net/sourceforge/filebot/ui/transfer/SaveableExportHandler.java b/source/net/sourceforge/filebot/ui/transfer/SaveableExportHandler.java index 71fb8612..e5c266b4 100644 --- a/source/net/sourceforge/filebot/ui/transfer/SaveableExportHandler.java +++ b/source/net/sourceforge/filebot/ui/transfer/SaveableExportHandler.java @@ -33,7 +33,8 @@ public class SaveableExportHandler implements ExportHandler { List files = (List) data.getTransferData(DataFlavor.javaFileListFlavor); for (File file : files) { - file.deleteOnExit(); + if (file.exists()) + file.deleteOnExit(); } } catch (Exception e) { e.printStackTrace(); diff --git a/source/net/sourceforge/filebot/ui/transfer/TransferablePolicyImportHandler.java b/source/net/sourceforge/filebot/ui/transfer/TransferablePolicyImportHandler.java index 36bdf7f2..030188d1 100644 --- a/source/net/sourceforge/filebot/ui/transfer/TransferablePolicyImportHandler.java +++ b/source/net/sourceforge/filebot/ui/transfer/TransferablePolicyImportHandler.java @@ -49,7 +49,9 @@ public class TransferablePolicyImportHandler implements ImportHandler { Transferable t = support.getTransferable(); - return transferablePolicySupport.getTransferablePolicy().handleTransferable(t, add); + transferablePolicySupport.getTransferablePolicy().handleTransferable(t, add); + + return true; } } diff --git a/source/net/sourceforge/filebot/ui/transferablepolicies/BackgroundFileTransferablePolicy.java b/source/net/sourceforge/filebot/ui/transferablepolicies/BackgroundFileTransferablePolicy.java index 592808dd..e7fe32c1 100644 --- a/source/net/sourceforge/filebot/ui/transferablepolicies/BackgroundFileTransferablePolicy.java +++ b/source/net/sourceforge/filebot/ui/transferablepolicies/BackgroundFileTransferablePolicy.java @@ -20,11 +20,11 @@ public abstract class BackgroundFileTransferablePolicy extends FileTransferab @Override - public boolean handleTransferable(Transferable tr, boolean add) { + public void handleTransferable(Transferable tr, boolean add) { List files = getFilesFromTransferable(tr); if (files == null) - return false; + return; if (!add) clear(); @@ -32,8 +32,6 @@ public abstract class BackgroundFileTransferablePolicy extends FileTransferab backgroundWorker = new BackgroundWorker(files); backgroundWorker.addPropertyChangeListener(new BackgroundWorkerListener()); backgroundWorker.execute(); - - return true; } @@ -48,7 +46,8 @@ public abstract class BackgroundFileTransferablePolicy extends FileTransferab /** - * Receives data chunks from the publish method asynchronously on the Event Dispatch Thread. + * Receives data chunks from the publish method asynchronously on the Event Dispatch + * Thread. * * @param chunks */ diff --git a/source/net/sourceforge/filebot/ui/transferablepolicies/FileTransferablePolicy.java b/source/net/sourceforge/filebot/ui/transferablepolicies/FileTransferablePolicy.java index 69a2888b..4b992ae3 100644 --- a/source/net/sourceforge/filebot/ui/transferablepolicies/FileTransferablePolicy.java +++ b/source/net/sourceforge/filebot/ui/transferablepolicies/FileTransferablePolicy.java @@ -8,90 +8,59 @@ import java.awt.datatransfer.UnsupportedFlavorException; import java.io.File; import java.io.IOException; import java.net.URI; -import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; +import net.sourceforge.filebot.ui.FileBotUtil; + public abstract class FileTransferablePolicy extends TransferablePolicy { + @Override public boolean accept(Transferable tr) { if (!isEnabled()) return false; List files = getFilesFromTransferable(tr); - if (files == null || files.isEmpty()) + if ((files == null) || files.isEmpty()) return false; return accept(files); } + @SuppressWarnings("unchecked") protected List getFilesFromTransferable(Transferable tr) { - List files = getFilesFromFileTransferable(tr); - - // if there is no file transferable, look if there is a string transferable that - // contains file uris - if (files == null) - files = getFilesFromStringTransferable(tr); - - Collections.sort(files); - - return files; - } - - - protected List getFilesFromFileTransferable(Transferable tr) { - if (!tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) - return null; - try { - List list = (List) tr.getTransferData(DataFlavor.javaFileListFlavor); - - ArrayList files = new ArrayList(list.size()); - - for (Object object : list) - files.add((File) object); - - if (!files.isEmpty()) - return files; - } catch (UnsupportedFlavorException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - - return null; - } - - - protected List getFilesFromStringTransferable(Transferable tr) { - if (!tr.isDataFlavorSupported(DataFlavor.stringFlavor)) - return null; - - try { - String transferString = (String) tr.getTransferData(DataFlavor.stringFlavor); - - String lines[] = transferString.split("\r?\n"); - ArrayList files = new ArrayList(lines.length); - - for (String line : lines) { - try { - File file = new File(new URI(line)); + if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { + return (List) tr.getTransferData(DataFlavor.javaFileListFlavor); + } else if (tr.isDataFlavorSupported(FileBotUtil.uriListFlavor)) { + String transferString = (String) tr.getTransferData(DataFlavor.stringFlavor); + + String lines[] = transferString.split("\r?\n"); + ArrayList files = new ArrayList(lines.length); + + for (String line : lines) { + if (line.startsWith("#")) { + // the line is a comment (as per the RFC 2483) + continue; + } - if (file.exists()) - files.add(file); - } catch (URISyntaxException e) { - Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.WARNING, "Invalid file url: " + line, e); + try { + File file = new File(new URI(line)); + + if (file.exists()) + files.add(file); + } catch (Exception e) { + // URISyntaxException, IllegalArgumentException + Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.WARNING, "Invalid file url: " + line, e); + } } } - - if (!files.isEmpty()) - return files; } catch (UnsupportedFlavorException e) { e.printStackTrace(); } catch (IOException e) { @@ -102,16 +71,19 @@ public abstract class FileTransferablePolicy extends TransferablePolicy { } - public boolean handleTransferable(Transferable tr, boolean add) { + @Override + public void handleTransferable(Transferable tr, boolean add) { List files = getFilesFromTransferable(tr); if (files == null) - return false; + return; + + Collections.sort(files); if (!add) clear(); - return load(files); + load(files); } @@ -124,14 +96,10 @@ public abstract class FileTransferablePolicy extends TransferablePolicy { } - protected boolean load(List files) { - boolean success = false; - + protected void load(List files) { for (File file : files) { - success |= load(file); + load(file); } - - return success; } diff --git a/source/net/sourceforge/filebot/ui/transferablepolicies/MultiTransferablePolicy.java b/source/net/sourceforge/filebot/ui/transferablepolicies/MultiTransferablePolicy.java index 1f3a4288..64111012 100644 --- a/source/net/sourceforge/filebot/ui/transferablepolicies/MultiTransferablePolicy.java +++ b/source/net/sourceforge/filebot/ui/transferablepolicies/MultiTransferablePolicy.java @@ -31,6 +31,7 @@ public class MultiTransferablePolicy extends TransferablePolicy { } + @Override public boolean accept(Transferable tr) { if (!isEnabled()) return false; @@ -44,13 +45,14 @@ public class MultiTransferablePolicy extends TransferablePolicy { } - public boolean handleTransferable(Transferable tr, boolean add) { + @Override + public void handleTransferable(Transferable tr, boolean add) { for (TransferablePolicy policy : policies) { - if (policy.accept(tr)) - return policy.handleTransferable(tr, add); + if (policy.accept(tr)) { + policy.handleTransferable(tr, add); + return; + } } - - return true; } private final PropertyChangeListener relayListener = new PropertyChangeListener() { diff --git a/source/net/sourceforge/filebot/ui/transferablepolicies/NullTransferablePolicy.java b/source/net/sourceforge/filebot/ui/transferablepolicies/NullTransferablePolicy.java index 9e8ac844..affea9e2 100644 --- a/source/net/sourceforge/filebot/ui/transferablepolicies/NullTransferablePolicy.java +++ b/source/net/sourceforge/filebot/ui/transferablepolicies/NullTransferablePolicy.java @@ -7,13 +7,15 @@ import java.awt.datatransfer.Transferable; public class NullTransferablePolicy extends TransferablePolicy { + @Override public boolean accept(Transferable tr) { return false; } - public boolean handleTransferable(Transferable tr, boolean add) { - return false; + @Override + public void handleTransferable(Transferable tr, boolean add) { + } diff --git a/source/net/sourceforge/filebot/ui/transferablepolicies/TextTransferablePolicy.java b/source/net/sourceforge/filebot/ui/transferablepolicies/TextTransferablePolicy.java index 1f64f354..5402997d 100644 --- a/source/net/sourceforge/filebot/ui/transferablepolicies/TextTransferablePolicy.java +++ b/source/net/sourceforge/filebot/ui/transferablepolicies/TextTransferablePolicy.java @@ -8,6 +8,7 @@ import java.awt.datatransfer.Transferable; public abstract class TextTransferablePolicy extends TransferablePolicy { + @Override public boolean accept(Transferable tr) { if (!isEnabled()) return false; @@ -16,7 +17,8 @@ public abstract class TextTransferablePolicy extends TransferablePolicy { } - public boolean handleTransferable(Transferable tr, boolean add) { + @Override + public void handleTransferable(Transferable tr, boolean add) { try { String string = (String) tr.getTransferData(DataFlavor.stringFlavor); @@ -24,10 +26,9 @@ public abstract class TextTransferablePolicy extends TransferablePolicy { clear(); } - return load(string); + load(string); } catch (Exception e) { e.printStackTrace(); - return false; } } diff --git a/source/net/sourceforge/filebot/ui/transferablepolicies/TransferablePolicy.java b/source/net/sourceforge/filebot/ui/transferablepolicies/TransferablePolicy.java index e7905601..6d1d8b8e 100644 --- a/source/net/sourceforge/filebot/ui/transferablepolicies/TransferablePolicy.java +++ b/source/net/sourceforge/filebot/ui/transferablepolicies/TransferablePolicy.java @@ -13,7 +13,7 @@ public abstract class TransferablePolicy { public abstract boolean accept(Transferable tr); - public abstract boolean handleTransferable(Transferable tr, boolean add); + public abstract void handleTransferable(Transferable tr, boolean add); private boolean enabled = true;