* improved dnd behaviour

This commit is contained in:
Reinhard Pointner 2008-03-21 01:45:21 +00:00
parent 05eecdedd5
commit 824c29fd36
11 changed files with 240 additions and 111 deletions

View File

@ -5,7 +5,8 @@ package net.sourceforge.filebot;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Point; import java.awt.Point;
import java.awt.Window; import java.awt.Window;
import java.util.Iterator; import java.io.File;
import java.util.List;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import javax.swing.Action; 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 | * invalid characters: \, /, :, *, ?, ", <, > and |
*/ */
@ -60,19 +54,63 @@ public class FileBotUtil {
} }
public static String join(Iterable<?> list, String delim) { public static boolean containsOnlyFolders(List<File> files) {
StringBuilder sb = new StringBuilder(); for (File file : files) {
if (!file.isDirectory())
Iterator<?> it = list.iterator(); return false;
while (it.hasNext()) {
sb.append(it.next().toString());
if (it.hasNext())
sb.append(delim);
} }
return sb.toString(); return true;
}
public static boolean containsOnlyTorrentFiles(List<File> files) {
for (File file : files) {
if (!file.isFile() || !FileFormat.getExtension(file).equalsIgnoreCase("torrent"))
return false;
}
return true;
}
public static boolean containsOnlySfvFiles(List<File> files) {
for (File file : files) {
if (!file.isFile() || !FileFormat.getExtension(file).equalsIgnoreCase("sfv"))
return false;
}
return true;
}
public static boolean containsOnlyListFiles(List<File> 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);
} }

View File

@ -31,10 +31,13 @@ public class FileFormat {
} }
public static String formatName(File f) { public static String formatName(File file) {
String name = f.getName(); if (file == null)
return "";
if (f.isDirectory()) String name = file.getName();
if (file.isDirectory())
return name; return name;
return getNameWithoutExtension(name); return getNameWithoutExtension(name);
@ -49,13 +52,16 @@ public class FileFormat {
} }
public static String getExtension(File f) { public static String getExtension(File file) {
return getExtension(f, false); return getExtension(file, false);
} }
public static String getExtension(File f, boolean includeDot) { public static String getExtension(File file, boolean includeDot) {
String name = f.getName(); if (!file.isFile())
return null;
String name = file.getName();
int dotIndex = name.lastIndexOf("."); int dotIndex = name.lastIndexOf(".");
// .config -> no extension // .config -> no extension
@ -90,6 +96,9 @@ public class FileFormat {
public static String getName(File file) { public static String getName(File file) {
if (file == null)
return "";
String name = file.getName(); String name = file.getName();
if (!name.isEmpty()) if (!name.isEmpty())

View File

@ -5,7 +5,6 @@ package net.sourceforge.filebot.ui;
import java.awt.BorderLayout; import java.awt.BorderLayout;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.List; import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
@ -134,7 +133,7 @@ public class FileBotList extends JPanel implements Saveable, TransferablePolicyS
} }
out.close(); out.close();
} catch (FileNotFoundException e) { } catch (Exception e) {
// should not happen // should not happen
Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.SEVERE, e.toString(), e); Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.SEVERE, e.toString(), e);
} }

View File

@ -4,9 +4,12 @@ package net.sourceforge.filebot.ui.panel.list;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import net.sourceforge.filebot.FileBotUtil;
import net.sourceforge.filebot.FileFormat; import net.sourceforge.filebot.FileFormat;
import net.sourceforge.filebot.torrent.Torrent; import net.sourceforge.filebot.torrent.Torrent;
import net.sourceforge.filebot.ui.FileBotList; import net.sourceforge.filebot.ui.FileBotList;
@ -25,7 +28,7 @@ class FileListTransferablePolicy extends FileTransferablePolicy {
@Override @Override
protected boolean accept(File file) { 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 @Override
protected void load(File file) { protected void load(List<File> files) {
if (file.isDirectory()) { if (files.size() > 1) {
list.setTitle(file.getName()); list.setTitle(FileFormat.getName(files.get(0).getParentFile()));
}
for (File f : file.listFiles()) {
list.getModel().add(FileFormat.formatName(f)); if (FileBotUtil.containsOnlyFolders(files)) {
} loadFolderList(files);
} else if (FileBotUtil.containsOnlyTorrentFiles(files)) {
loadTorrentList(files);
} else { } else {
if (FileFormat.getExtension(file).equalsIgnoreCase("torrent")) { super.load(files);
try { }
Torrent torrent = new Torrent(file); }
list.setTitle(FileFormat.getNameWithoutExtension(torrent.getName()));
for (Torrent.Entry entry : torrent.getFiles()) { private void loadFolderList(List<File> folders) {
list.getModel().add(FileFormat.getNameWithoutExtension(entry.getName())); if (folders.size() == 1) {
} list.setTitle(FileFormat.getName(folders.get(0)));
} catch (IOException e) { }
// should not happen
Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.SEVERE, e.toString(), e); for (File folder : folders) {
} for (File file : folder.listFiles()) {
list.getModel().add(FileFormat.formatName(file));
} }
} }
} }
private void loadTorrentList(List<File> torrentFiles) {
try {
List<Torrent> torrents = new ArrayList<Torrent>(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 @Override
public String getDescription() { public String getDescription() {
return "folders and torrents"; return "files, folders and torrents";
} }
} }

View File

@ -4,7 +4,9 @@ package net.sourceforge.filebot.ui.panel.rename;
import java.io.File; import java.io.File;
import java.util.Arrays; 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.panel.rename.entry.FileEntry;
import net.sourceforge.filebot.ui.transferablepolicies.FileTransferablePolicy; import net.sourceforge.filebot.ui.transferablepolicies.FileTransferablePolicy;
import net.sourceforge.tuned.ui.SimpleListModel; import net.sourceforge.tuned.ui.SimpleListModel;
@ -12,37 +14,41 @@ import net.sourceforge.tuned.ui.SimpleListModel;
class FilesListTransferablePolicy extends FileTransferablePolicy { class FilesListTransferablePolicy extends FileTransferablePolicy {
private SimpleListModel listModel; private final SimpleListModel model;
public FilesListTransferablePolicy(SimpleListModel listModel) { public FilesListTransferablePolicy(SimpleListModel listModel) {
this.listModel = listModel; this.model = listModel;
} }
@Override @Override
protected boolean accept(File file) { protected boolean accept(File file) {
return file.isDirectory() || file.isFile(); return file.isFile() || file.isDirectory();
} }
@Override @Override
protected void clear() { protected void clear() {
listModel.clear(); model.clear();
}
@Override
protected void load(List<File> files) {
if (FileBotUtil.containsOnlyFolders(files)) {
for (File folder : files) {
super.load(Arrays.asList(folder.listFiles()));
}
} else {
super.load(files);
}
} }
@Override @Override
protected void load(File file) { protected void load(File file) {
if (file.isDirectory()) { model.add(new FileEntry(file));
File subfiles[] = file.listFiles();
Arrays.sort(subfiles);
for (File f : subfiles)
listModel.add(new FileEntry(f));
} else {
listModel.add(new FileEntry(file));
}
} }

View File

@ -5,6 +5,7 @@ package net.sourceforge.filebot.ui.panel.rename;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -14,19 +15,17 @@ import java.util.logging.Logger;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
import net.sourceforge.filebot.FileBotUtil; import net.sourceforge.filebot.FileBotUtil;
import net.sourceforge.filebot.FileFormat;
import net.sourceforge.filebot.torrent.Torrent; import net.sourceforge.filebot.torrent.Torrent;
import net.sourceforge.filebot.ui.panel.rename.entry.ListEntry; 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.StringEntry;
import net.sourceforge.filebot.ui.panel.rename.entry.TorrentEntry; 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.MultiTransferablePolicy;
import net.sourceforge.filebot.ui.transferablepolicies.TextTransferablePolicy; import net.sourceforge.filebot.ui.transferablepolicies.TextTransferablePolicy;
class NamesListTransferablePolicy extends MultiTransferablePolicy { class NamesListTransferablePolicy extends MultiTransferablePolicy {
private NamesRenameList list; private final NamesRenameList list;
public NamesListTransferablePolicy(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; public FilePolicy() {
super(list.getModel());
@Override
protected boolean accept(File file) {
return file.isFile() && (file.length() < MAX_FILESIZE);
} }
@Override @Override
protected void load(File file) { protected boolean accept(File file) {
return file.isFile() || file.isDirectory();
}
@Override
protected void load(List<File> files) {
if (FileBotUtil.containsOnlyListFiles(files)) {
loadListFiles(files);
} else if (FileBotUtil.containsOnlyTorrentFiles(files)) {
loadTorrentFiles(files);
} else {
super.load(files);
}
}
private void loadListFiles(List<File> files) {
try { try {
List<ListEntry<?>> entries = new ArrayList<ListEntry<?>>(); List<ListEntry<?>> entries = new ArrayList<ListEntry<?>>();
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<File> files) {
try {
List<ListEntry<?>> entries = new ArrayList<ListEntry<?>>();
for (File file : files) {
Torrent torrent = new Torrent(file); Torrent torrent = new Torrent(file);
for (Torrent.Entry entry : torrent.getFiles()) { for (Torrent.Entry entry : torrent.getFiles()) {
entries.add(new TorrentEntry(entry)); 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);
submit(entries); } catch (IOException e) {
}
} catch (Exception e) {
// should not happen
Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.SEVERE, e.toString(), e); Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.SEVERE, e.toString(), e);
} }
} }

View File

@ -5,6 +5,7 @@ package net.sourceforge.filebot.ui.panel.rename;
import java.awt.Color; import java.awt.Color;
import java.awt.Component; import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JList; import javax.swing.JList;
import javax.swing.ListModel; import javax.swing.ListModel;
@ -13,8 +14,10 @@ import net.sourceforge.tuned.ui.FancyListCellRenderer;
class RenameListCellRenderer extends FancyListCellRenderer { class RenameListCellRenderer extends FancyListCellRenderer {
private ListModel names; private final ListModel names;
private ListModel files; private final ListModel files;
private final JLabel extension = new JLabel(".png");
public RenameListCellRenderer(ListModel names, ListModel files) { public RenameListCellRenderer(ListModel names, ListModel files) {
@ -22,6 +25,8 @@ class RenameListCellRenderer extends FancyListCellRenderer {
this.files = files; this.files = files;
setHighlightingEnabled(false); setHighlightingEnabled(false);
this.add(extension);
} }
private Color noMatchGradientBeginColor = Color.decode("#B7B7B7"); private Color noMatchGradientBeginColor = Color.decode("#B7B7B7");

View File

@ -5,7 +5,6 @@ package net.sourceforge.filebot.ui.panel.sfv;
import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener; import java.beans.PropertyChangeListener;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream; import java.io.PrintStream;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
@ -166,7 +165,7 @@ class SfvTable extends JTable implements TransferablePolicySupport, Saveable {
} }
out.close(); out.close();
} catch (FileNotFoundException e) { } catch (Exception e) {
// should not happen // should not happen
Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.SEVERE, e.toString(), e); Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.SEVERE, e.toString(), e);
} }

View File

@ -13,7 +13,7 @@ import java.util.logging.Logger;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import net.sourceforge.filebot.FileFormat; import net.sourceforge.filebot.FileBotUtil;
import net.sourceforge.filebot.ui.transferablepolicies.BackgroundFileTransferablePolicy; import net.sourceforge.filebot.ui.transferablepolicies.BackgroundFileTransferablePolicy;
@ -89,22 +89,12 @@ class SfvTransferablePolicy extends BackgroundFileTransferablePolicy<SfvTableMod
} }
private boolean isSfvFileList(List<File> files) {
for (File file : files) {
if (!FileFormat.getExtension(file).equalsIgnoreCase("sfv"))
return false;
}
return true;
}
@Override @Override
protected void load(List<File> files) { protected void load(List<File> files) {
synchronized (ChecksumComputationExecutor.getInstance()) { synchronized (ChecksumComputationExecutor.getInstance()) {
ChecksumComputationExecutor.getInstance().pause(); ChecksumComputationExecutor.getInstance().pause();
if (isSfvFileList(files)) { if (FileBotUtil.containsOnlySfvFiles(files)) {
// one or more sfv files // one or more sfv files
for (File file : files) { for (File file : files) {
loadSfvFile(file); loadSfvFile(file);

View File

@ -5,10 +5,9 @@ package net.sourceforge.filebot.ui.transferablepolicies;
import java.awt.datatransfer.Transferable; import java.awt.datatransfer.Transferable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator;
import java.util.List; import java.util.List;
import net.sourceforge.filebot.FileBotUtil;
public class MultiTransferablePolicy implements TransferablePolicy { public class MultiTransferablePolicy implements TransferablePolicy {
@ -84,7 +83,17 @@ public class MultiTransferablePolicy implements TransferablePolicy {
descriptions.add(desc); descriptions.add(desc);
} }
return FileBotUtil.join(descriptions, ", "); StringBuilder sb = new StringBuilder();
Iterator<String> iterator = descriptions.iterator();
while (iterator.hasNext()) {
sb.append(iterator.next().toString());
if (iterator.hasNext())
sb.append(", ");
}
return sb.toString();
} }
} }

View File

@ -101,7 +101,18 @@ public class SimpleListModel extends AbstractListModel {
public void remove(Object object) { 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<? extends Object> c) { public void set(Collection<? extends Object> c) {
int end = Math.max(list.size(), c.size()) - 1; int end = Math.max(list.size(), c.size()) - 1;
list.clear(); synchronized (list) {
list.addAll(c); list.clear();
list.addAll(c);
}
if (end >= 0) { if (end >= 0) {
fireContentsChanged(this, 0, end); fireContentsChanged(this, 0, end);