From 824c29fd36843528a30b3b672c8890e308b53e96 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Fri, 21 Mar 2008 01:45:21 +0000 Subject: [PATCH] * improved dnd behaviour --- .../net/sourceforge/filebot/FileBotUtil.java | 76 +++++++++++++----- .../net/sourceforge/filebot/FileFormat.java | 23 ++++-- .../sourceforge/filebot/ui/FileBotList.java | 3 +- .../list/FileListTransferablePolicy.java | 78 +++++++++++++----- .../rename/FilesListTransferablePolicy.java | 32 +++++--- .../rename/NamesListTransferablePolicy.java | 79 ++++++++++++------- .../panel/rename/RenameListCellRenderer.java | 9 ++- .../filebot/ui/panel/sfv/SfvTable.java | 3 +- .../ui/panel/sfv/SfvTransferablePolicy.java | 14 +--- .../MultiTransferablePolicy.java | 15 +++- .../sourceforge/tuned/ui/SimpleListModel.java | 19 ++++- 11 files changed, 240 insertions(+), 111 deletions(-) diff --git a/source/net/sourceforge/filebot/FileBotUtil.java b/source/net/sourceforge/filebot/FileBotUtil.java index f6e7039f..9445c672 100644 --- a/source/net/sourceforge/filebot/FileBotUtil.java +++ b/source/net/sourceforge/filebot/FileBotUtil.java @@ -5,7 +5,8 @@ package net.sourceforge.filebot; import java.awt.Dimension; import java.awt.Point; import java.awt.Window; -import java.util.Iterator; +import java.io.File; +import java.util.List; import java.util.regex.Pattern; import javax.swing.Action; @@ -20,13 +21,6 @@ public class FileBotUtil { } - - public static void registerActionForKeystroke(JComponent component, KeyStroke keystroke, Action action) { - Integer key = action.hashCode(); - component.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(keystroke, key); - component.getActionMap().put(key, action); - } - /** * invalid characters: \, /, :, *, ?, ", <, > and | */ @@ -60,19 +54,63 @@ public class FileBotUtil { } - public static String join(Iterable list, String delim) { - StringBuilder sb = new StringBuilder(); - - Iterator it = list.iterator(); - - while (it.hasNext()) { - sb.append(it.next().toString()); - - if (it.hasNext()) - sb.append(delim); + public static boolean containsOnlyFolders(List files) { + for (File file : files) { + if (!file.isDirectory()) + return false; } - return sb.toString(); + return true; + } + + + public static boolean containsOnlyTorrentFiles(List files) { + for (File file : files) { + if (!file.isFile() || !FileFormat.getExtension(file).equalsIgnoreCase("torrent")) + return false; + } + + return true; + } + + + public static boolean containsOnlySfvFiles(List files) { + for (File file : files) { + if (!file.isFile() || !FileFormat.getExtension(file).equalsIgnoreCase("sfv")) + return false; + } + + return true; + } + + + public static boolean containsOnlyListFiles(List files) { + for (File file : files) { + if (!isListFile(file)) + return false; + } + + return true; + } + + + public static boolean isListFile(File file) { + if (!file.isFile()) + return false; + + String extension = FileFormat.getExtension(file).toLowerCase(); + + if (extension.equals("txt") || extension.equals("list") || extension.isEmpty()) + return true; + + return false; + } + + + public static void registerActionForKeystroke(JComponent component, KeyStroke keystroke, Action action) { + Integer key = action.hashCode(); + component.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(keystroke, key); + component.getActionMap().put(key, action); } diff --git a/source/net/sourceforge/filebot/FileFormat.java b/source/net/sourceforge/filebot/FileFormat.java index 061b3b70..c3e0f11a 100644 --- a/source/net/sourceforge/filebot/FileFormat.java +++ b/source/net/sourceforge/filebot/FileFormat.java @@ -31,10 +31,13 @@ public class FileFormat { } - public static String formatName(File f) { - String name = f.getName(); + public static String formatName(File file) { + if (file == null) + return ""; - if (f.isDirectory()) + String name = file.getName(); + + if (file.isDirectory()) return name; return getNameWithoutExtension(name); @@ -49,13 +52,16 @@ public class FileFormat { } - public static String getExtension(File f) { - return getExtension(f, false); + public static String getExtension(File file) { + return getExtension(file, false); } - public static String getExtension(File f, boolean includeDot) { - String name = f.getName(); + public static String getExtension(File file, boolean includeDot) { + if (!file.isFile()) + return null; + + String name = file.getName(); int dotIndex = name.lastIndexOf("."); // .config -> no extension @@ -90,6 +96,9 @@ public class FileFormat { public static String getName(File file) { + if (file == null) + return ""; + String name = file.getName(); if (!name.isEmpty()) diff --git a/source/net/sourceforge/filebot/ui/FileBotList.java b/source/net/sourceforge/filebot/ui/FileBotList.java index 9252d89d..654167bb 100644 --- a/source/net/sourceforge/filebot/ui/FileBotList.java +++ b/source/net/sourceforge/filebot/ui/FileBotList.java @@ -5,7 +5,6 @@ package net.sourceforge.filebot.ui; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.io.File; -import java.io.FileNotFoundException; import java.io.PrintStream; import java.util.List; import java.util.logging.Level; @@ -134,7 +133,7 @@ public class FileBotList extends JPanel implements Saveable, TransferablePolicyS } out.close(); - } catch (FileNotFoundException e) { + } catch (Exception e) { // should not happen Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.SEVERE, e.toString(), e); } diff --git a/source/net/sourceforge/filebot/ui/panel/list/FileListTransferablePolicy.java b/source/net/sourceforge/filebot/ui/panel/list/FileListTransferablePolicy.java index 061d37b3..e20ac162 100644 --- a/source/net/sourceforge/filebot/ui/panel/list/FileListTransferablePolicy.java +++ b/source/net/sourceforge/filebot/ui/panel/list/FileListTransferablePolicy.java @@ -4,9 +4,12 @@ package net.sourceforge.filebot.ui.panel.list; import java.io.File; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; +import net.sourceforge.filebot.FileBotUtil; import net.sourceforge.filebot.FileFormat; import net.sourceforge.filebot.torrent.Torrent; import net.sourceforge.filebot.ui.FileBotList; @@ -25,7 +28,7 @@ class FileListTransferablePolicy extends FileTransferablePolicy { @Override protected boolean accept(File file) { - return file.isDirectory() || FileFormat.getExtension(file).equalsIgnoreCase("torrent"); + return file.isFile() || file.isDirectory(); } @@ -36,34 +39,67 @@ class FileListTransferablePolicy extends FileTransferablePolicy { @Override - protected void load(File file) { - if (file.isDirectory()) { - list.setTitle(file.getName()); - - for (File f : file.listFiles()) { - list.getModel().add(FileFormat.formatName(f)); - } + protected void load(List files) { + if (files.size() > 1) { + list.setTitle(FileFormat.getName(files.get(0).getParentFile())); + } + + if (FileBotUtil.containsOnlyFolders(files)) { + loadFolderList(files); + } else if (FileBotUtil.containsOnlyTorrentFiles(files)) { + loadTorrentList(files); } else { - if (FileFormat.getExtension(file).equalsIgnoreCase("torrent")) { - try { - Torrent torrent = new Torrent(file); - list.setTitle(FileFormat.getNameWithoutExtension(torrent.getName())); - - for (Torrent.Entry entry : torrent.getFiles()) { - list.getModel().add(FileFormat.getNameWithoutExtension(entry.getName())); - } - } catch (IOException e) { - // should not happen - Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.SEVERE, e.toString(), e); - } + super.load(files); + } + } + + + private void loadFolderList(List folders) { + if (folders.size() == 1) { + list.setTitle(FileFormat.getName(folders.get(0))); + } + + for (File folder : folders) { + for (File file : folder.listFiles()) { + list.getModel().add(FileFormat.formatName(file)); } } } + private void loadTorrentList(List torrentFiles) { + try { + List torrents = new ArrayList(torrentFiles.size()); + + for (File file : torrentFiles) { + torrents.add(new Torrent(file)); + } + + if (torrentFiles.size() == 1) { + list.setTitle(FileFormat.getNameWithoutExtension(torrents.get(0).getName())); + } + + for (Torrent torrent : torrents) { + for (Torrent.Entry entry : torrent.getFiles()) { + list.getModel().add(FileFormat.getNameWithoutExtension(entry.getName())); + } + } + } catch (IOException e) { + // should not happen + Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.SEVERE, e.toString(), e); + } + } + + + @Override + protected void load(File file) { + list.getModel().add(FileFormat.formatName(file)); + } + + @Override public String getDescription() { - return "folders and torrents"; + return "files, folders and torrents"; } } diff --git a/source/net/sourceforge/filebot/ui/panel/rename/FilesListTransferablePolicy.java b/source/net/sourceforge/filebot/ui/panel/rename/FilesListTransferablePolicy.java index edd39431..712b06ba 100644 --- a/source/net/sourceforge/filebot/ui/panel/rename/FilesListTransferablePolicy.java +++ b/source/net/sourceforge/filebot/ui/panel/rename/FilesListTransferablePolicy.java @@ -4,7 +4,9 @@ package net.sourceforge.filebot.ui.panel.rename; import java.io.File; import java.util.Arrays; +import java.util.List; +import net.sourceforge.filebot.FileBotUtil; import net.sourceforge.filebot.ui.panel.rename.entry.FileEntry; import net.sourceforge.filebot.ui.transferablepolicies.FileTransferablePolicy; import net.sourceforge.tuned.ui.SimpleListModel; @@ -12,37 +14,41 @@ import net.sourceforge.tuned.ui.SimpleListModel; class FilesListTransferablePolicy extends FileTransferablePolicy { - private SimpleListModel listModel; + private final SimpleListModel model; public FilesListTransferablePolicy(SimpleListModel listModel) { - this.listModel = listModel; + this.model = listModel; } @Override protected boolean accept(File file) { - return file.isDirectory() || file.isFile(); + return file.isFile() || file.isDirectory(); } @Override protected void clear() { - listModel.clear(); + model.clear(); + } + + + @Override + protected void load(List files) { + if (FileBotUtil.containsOnlyFolders(files)) { + for (File folder : files) { + super.load(Arrays.asList(folder.listFiles())); + } + } else { + super.load(files); + } } @Override protected void load(File file) { - if (file.isDirectory()) { - File subfiles[] = file.listFiles(); - Arrays.sort(subfiles); - - for (File f : subfiles) - listModel.add(new FileEntry(f)); - } else { - listModel.add(new FileEntry(file)); - } + model.add(new FileEntry(file)); } diff --git a/source/net/sourceforge/filebot/ui/panel/rename/NamesListTransferablePolicy.java b/source/net/sourceforge/filebot/ui/panel/rename/NamesListTransferablePolicy.java index a6fed9c0..7f5140a6 100644 --- a/source/net/sourceforge/filebot/ui/panel/rename/NamesListTransferablePolicy.java +++ b/source/net/sourceforge/filebot/ui/panel/rename/NamesListTransferablePolicy.java @@ -5,6 +5,7 @@ package net.sourceforge.filebot.ui.panel.rename; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; +import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; @@ -14,19 +15,17 @@ import java.util.logging.Logger; import javax.swing.SwingUtilities; import net.sourceforge.filebot.FileBotUtil; -import net.sourceforge.filebot.FileFormat; import net.sourceforge.filebot.torrent.Torrent; import net.sourceforge.filebot.ui.panel.rename.entry.ListEntry; import net.sourceforge.filebot.ui.panel.rename.entry.StringEntry; import net.sourceforge.filebot.ui.panel.rename.entry.TorrentEntry; -import net.sourceforge.filebot.ui.transferablepolicies.FileTransferablePolicy; import net.sourceforge.filebot.ui.transferablepolicies.MultiTransferablePolicy; import net.sourceforge.filebot.ui.transferablepolicies.TextTransferablePolicy; class NamesListTransferablePolicy extends MultiTransferablePolicy { - private NamesRenameList list; + private final NamesRenameList list; public NamesListTransferablePolicy(NamesRenameList list) { @@ -63,45 +62,71 @@ class NamesListTransferablePolicy extends MultiTransferablePolicy { } - private class FilePolicy extends FileTransferablePolicy { + private class FilePolicy extends FilesListTransferablePolicy { - private long MAX_FILESIZE = 10 * FileFormat.MEGA; - - - @Override - protected boolean accept(File file) { - return file.isFile() && (file.length() < MAX_FILESIZE); + public FilePolicy() { + super(list.getModel()); } @Override - protected void load(File file) { + protected boolean accept(File file) { + return file.isFile() || file.isDirectory(); + } + + + @Override + protected void load(List files) { + + if (FileBotUtil.containsOnlyListFiles(files)) { + loadListFiles(files); + } else if (FileBotUtil.containsOnlyTorrentFiles(files)) { + loadTorrentFiles(files); + } else { + super.load(files); + } + } + + + private void loadListFiles(List files) { try { List> entries = new ArrayList>(); - if (FileFormat.getExtension(file).equalsIgnoreCase("torrent")) { + for (File file : files) { + BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file))); + + String line = null; + + while ((line = in.readLine()) != null) { + if (line.trim().length() > 0) { + entries.add(new StringEntry(line)); + } + } + + in.close(); + } + + submit(entries); + } catch (IOException e) { + Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.SEVERE, e.toString(), e); + } + } + + + private void loadTorrentFiles(List files) { + try { + List> entries = new ArrayList>(); + + for (File file : files) { Torrent torrent = new Torrent(file); for (Torrent.Entry entry : torrent.getFiles()) { entries.add(new TorrentEntry(entry)); } - } else { - BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file))); - - String line = null; - - while ((line = in.readLine()) != null) - if (line.trim().length() > 0) - entries.add(new StringEntry(line)); - - in.close(); } - if (!entries.isEmpty()) { - submit(entries); - } - } catch (Exception e) { - // should not happen + submit(entries); + } catch (IOException e) { Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.SEVERE, e.toString(), e); } } diff --git a/source/net/sourceforge/filebot/ui/panel/rename/RenameListCellRenderer.java b/source/net/sourceforge/filebot/ui/panel/rename/RenameListCellRenderer.java index e44989ae..3831e80a 100644 --- a/source/net/sourceforge/filebot/ui/panel/rename/RenameListCellRenderer.java +++ b/source/net/sourceforge/filebot/ui/panel/rename/RenameListCellRenderer.java @@ -5,6 +5,7 @@ package net.sourceforge.filebot.ui.panel.rename; import java.awt.Color; import java.awt.Component; +import javax.swing.JLabel; import javax.swing.JList; import javax.swing.ListModel; @@ -13,8 +14,10 @@ import net.sourceforge.tuned.ui.FancyListCellRenderer; class RenameListCellRenderer extends FancyListCellRenderer { - private ListModel names; - private ListModel files; + private final ListModel names; + private final ListModel files; + + private final JLabel extension = new JLabel(".png"); public RenameListCellRenderer(ListModel names, ListModel files) { @@ -22,6 +25,8 @@ class RenameListCellRenderer extends FancyListCellRenderer { this.files = files; setHighlightingEnabled(false); + + this.add(extension); } private Color noMatchGradientBeginColor = Color.decode("#B7B7B7"); diff --git a/source/net/sourceforge/filebot/ui/panel/sfv/SfvTable.java b/source/net/sourceforge/filebot/ui/panel/sfv/SfvTable.java index 60f37c8c..4b536f40 100644 --- a/source/net/sourceforge/filebot/ui/panel/sfv/SfvTable.java +++ b/source/net/sourceforge/filebot/ui/panel/sfv/SfvTable.java @@ -5,7 +5,6 @@ package net.sourceforge.filebot.ui.panel.sfv; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.io.File; -import java.io.FileNotFoundException; import java.io.PrintStream; import java.text.SimpleDateFormat; import java.util.Date; @@ -166,7 +165,7 @@ class SfvTable extends JTable implements TransferablePolicySupport, Saveable { } out.close(); - } catch (FileNotFoundException e) { + } catch (Exception e) { // should not happen Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.SEVERE, e.toString(), e); } diff --git a/source/net/sourceforge/filebot/ui/panel/sfv/SfvTransferablePolicy.java b/source/net/sourceforge/filebot/ui/panel/sfv/SfvTransferablePolicy.java index b40f9d94..f4eb5e50 100644 --- a/source/net/sourceforge/filebot/ui/panel/sfv/SfvTransferablePolicy.java +++ b/source/net/sourceforge/filebot/ui/panel/sfv/SfvTransferablePolicy.java @@ -13,7 +13,7 @@ import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; -import net.sourceforge.filebot.FileFormat; +import net.sourceforge.filebot.FileBotUtil; import net.sourceforge.filebot.ui.transferablepolicies.BackgroundFileTransferablePolicy; @@ -89,22 +89,12 @@ class SfvTransferablePolicy extends BackgroundFileTransferablePolicy files) { - for (File file : files) { - if (!FileFormat.getExtension(file).equalsIgnoreCase("sfv")) - return false; - } - - return true; - } - - @Override protected void load(List files) { synchronized (ChecksumComputationExecutor.getInstance()) { ChecksumComputationExecutor.getInstance().pause(); - if (isSfvFileList(files)) { + if (FileBotUtil.containsOnlySfvFiles(files)) { // one or more sfv files for (File file : files) { loadSfvFile(file); diff --git a/source/net/sourceforge/filebot/ui/transferablepolicies/MultiTransferablePolicy.java b/source/net/sourceforge/filebot/ui/transferablepolicies/MultiTransferablePolicy.java index 39a3fe49..634daf19 100644 --- a/source/net/sourceforge/filebot/ui/transferablepolicies/MultiTransferablePolicy.java +++ b/source/net/sourceforge/filebot/ui/transferablepolicies/MultiTransferablePolicy.java @@ -5,10 +5,9 @@ package net.sourceforge.filebot.ui.transferablepolicies; import java.awt.datatransfer.Transferable; import java.util.ArrayList; import java.util.Collections; +import java.util.Iterator; import java.util.List; -import net.sourceforge.filebot.FileBotUtil; - public class MultiTransferablePolicy implements TransferablePolicy { @@ -84,7 +83,17 @@ public class MultiTransferablePolicy implements TransferablePolicy { descriptions.add(desc); } - return FileBotUtil.join(descriptions, ", "); + StringBuilder sb = new StringBuilder(); + Iterator iterator = descriptions.iterator(); + + while (iterator.hasNext()) { + sb.append(iterator.next().toString()); + + if (iterator.hasNext()) + sb.append(", "); + } + + return sb.toString(); } } diff --git a/source/net/sourceforge/tuned/ui/SimpleListModel.java b/source/net/sourceforge/tuned/ui/SimpleListModel.java index d044b846..b0942212 100644 --- a/source/net/sourceforge/tuned/ui/SimpleListModel.java +++ b/source/net/sourceforge/tuned/ui/SimpleListModel.java @@ -101,7 +101,18 @@ public class SimpleListModel extends AbstractListModel { public void remove(Object object) { - remove(indexOf(object)); + synchronized (list) { + remove(indexOf(object)); + } + } + + + public void sort() { + synchronized (list) { + Collections.sort(list, null); + } + + fireContentsChanged(this, 0, list.size()); } @@ -115,8 +126,10 @@ public class SimpleListModel extends AbstractListModel { public void set(Collection c) { int end = Math.max(list.size(), c.size()) - 1; - list.clear(); - list.addAll(c); + synchronized (list) { + list.clear(); + list.addAll(c); + } if (end >= 0) { fireContentsChanged(this, 0, end);