* install Format/Validate view between namesList and model.names()
* refactored Preferences wrapper and unit tests
This commit is contained in:
parent
68968d84e6
commit
deb15a6e15
|
@ -4,6 +4,7 @@ package net.sourceforge.filebot;
|
|||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.prefs.BackingStoreException;
|
||||
import java.util.prefs.Preferences;
|
||||
|
||||
|
@ -11,6 +12,8 @@ import net.sourceforge.tuned.ExceptionUtilities;
|
|||
import net.sourceforge.tuned.PreferencesList;
|
||||
import net.sourceforge.tuned.PreferencesMap;
|
||||
import net.sourceforge.tuned.PreferencesMap.Adapter;
|
||||
import net.sourceforge.tuned.PreferencesMap.PreferencesEntry;
|
||||
import net.sourceforge.tuned.PreferencesMap.StringAdapter;
|
||||
|
||||
|
||||
public final class Settings {
|
||||
|
@ -66,8 +69,13 @@ public final class Settings {
|
|||
}
|
||||
|
||||
|
||||
public <T> Map<String, T> asMap(Class<T> type) {
|
||||
return PreferencesMap.map(prefs, type);
|
||||
public Entry<String, String> entry(String key) {
|
||||
return new PreferencesEntry<String>(prefs, key, new StringAdapter());
|
||||
}
|
||||
|
||||
|
||||
public Map<String, String> asMap() {
|
||||
return PreferencesMap.map(prefs);
|
||||
}
|
||||
|
||||
|
||||
|
@ -76,8 +84,8 @@ public final class Settings {
|
|||
}
|
||||
|
||||
|
||||
public <T> List<T> asList(Class<T> type) {
|
||||
return PreferencesList.map(prefs, type);
|
||||
public List<String> asList() {
|
||||
return PreferencesList.map(prefs);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -19,8 +19,8 @@ public class SeasonEpisodeMatcher {
|
|||
// match patterns like S01E01, s01e02, ... [s01]_[e02], s01.e02, ...
|
||||
patterns[0] = new SeasonEpisodePattern("(?<!\\p{Alnum})[Ss](\\d{1,2})[^\\p{Alnum}]{0,3}[Ee](\\d{1,3})(?!\\p{Digit})");
|
||||
|
||||
// match patterns like 1x01, 1x02, ... 10x01, 10x02, ...
|
||||
patterns[1] = new SeasonEpisodePattern("(?<!\\p{Alnum})(\\d{1,2})x(\\d{1,3})(?!\\p{Digit})");
|
||||
// match patterns like 1x01, 1.02, ... 10x01, 10.02, ...
|
||||
patterns[1] = new SeasonEpisodePattern("(?<!\\p{Alnum})(\\d{1,2})[x\\.](\\d{1,3})(?!\\p{Digit})");
|
||||
|
||||
// match patterns like 01, 102, 1003 (enclosed in separators)
|
||||
patterns[2] = new SeasonEpisodePattern("(?<=^|[\\._ ])([0-1]?\\d?)(\\d{2})(?=[\\._ ]|$)");
|
||||
|
|
|
@ -3,8 +3,6 @@ package net.sourceforge.filebot.ui.panel.episodelist;
|
|||
|
||||
|
||||
import static net.sourceforge.filebot.ui.panel.episodelist.SeasonSpinnerModel.ALL_SEASONS;
|
||||
import static net.sourceforge.filebot.web.Episode.formatEpisodeNumbers;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.beans.PropertyChangeEvent;
|
||||
import java.beans.PropertyChangeListener;
|
||||
|
@ -21,9 +19,6 @@ import javax.swing.JButton;
|
|||
import javax.swing.JSpinner;
|
||||
import javax.swing.KeyStroke;
|
||||
|
||||
import ca.odell.glazedlists.BasicEventList;
|
||||
import ca.odell.glazedlists.EventList;
|
||||
|
||||
import net.sourceforge.filebot.ResourceManager;
|
||||
import net.sourceforge.filebot.Settings;
|
||||
import net.sourceforge.filebot.ui.AbstractSearchPanel;
|
||||
|
@ -45,6 +40,8 @@ import net.sourceforge.tuned.ui.LabelProvider;
|
|||
import net.sourceforge.tuned.ui.SelectButton;
|
||||
import net.sourceforge.tuned.ui.SimpleLabelProvider;
|
||||
import net.sourceforge.tuned.ui.TunedUtilities;
|
||||
import ca.odell.glazedlists.BasicEventList;
|
||||
import ca.odell.glazedlists.EventList;
|
||||
|
||||
|
||||
public class EpisodeListPanel extends AbstractSearchPanel<EpisodeListClient, Episode> {
|
||||
|
@ -102,7 +99,7 @@ public class EpisodeListPanel extends AbstractSearchPanel<EpisodeListClient, Epi
|
|||
|
||||
// get the preferences node that contains the history entries
|
||||
// and get a StringList that read and writes directly from and to the preferences
|
||||
List<String> persistentSearchHistory = Settings.userRoot().node("episodelist/history").asList(String.class);
|
||||
List<String> persistentSearchHistory = Settings.userRoot().node("episodelist/history").asList();
|
||||
|
||||
// add history from the preferences to the current in-memory history (for completion)
|
||||
searchHistory.addAll(persistentSearchHistory);
|
||||
|
@ -233,15 +230,10 @@ public class EpisodeListPanel extends AbstractSearchPanel<EpisodeListClient, Epi
|
|||
|
||||
@Override
|
||||
public Collection<Episode> fetch() throws Exception {
|
||||
Collection<Episode> episodes;
|
||||
|
||||
if (request.getSeason() != ALL_SEASONS) {
|
||||
episodes = request.getClient().getEpisodeList(getSearchResult(), request.getSeason());
|
||||
} else {
|
||||
episodes = request.getClient().getEpisodeList(getSearchResult());
|
||||
}
|
||||
|
||||
return formatEpisodeNumbers(episodes, 2);
|
||||
if (request.getSeason() != ALL_SEASONS)
|
||||
return request.getClient().getEpisodeList(getSearchResult(), request.getSeason());
|
||||
else
|
||||
return request.getClient().getEpisodeList(getSearchResult());
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -4,7 +4,6 @@ package net.sourceforge.filebot.ui.panel.rename;
|
|||
|
||||
import static net.sourceforge.filebot.FileBotUtilities.MOVIE_FILES;
|
||||
import static net.sourceforge.filebot.FileBotUtilities.SUBTITLE_FILES;
|
||||
import static net.sourceforge.filebot.web.Episode.formatEpisodeNumbers;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
|
@ -86,7 +85,7 @@ class AutoFetchEpisodeListMatcher extends SwingWorker<List<Match<File, Episode>>
|
|||
SearchResult selectedSearchResult = selectSearchResult(seriesName, results);
|
||||
|
||||
if (selectedSearchResult != null) {
|
||||
return formatEpisodeNumbers(client.getEpisodeList(selectedSearchResult), 2);
|
||||
return client.getEpisodeList(selectedSearchResult);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -5,14 +5,13 @@ package net.sourceforge.filebot.ui.panel.rename;
|
|||
import static java.awt.datatransfer.DataFlavor.stringFlavor;
|
||||
import static net.sourceforge.filebot.FileBotUtilities.LIST_FILES;
|
||||
import static net.sourceforge.filebot.FileBotUtilities.TORRENT_FILES;
|
||||
import static net.sourceforge.filebot.FileBotUtilities.isInvalidFileName;
|
||||
import static net.sourceforge.tuned.FileUtilities.FOLDERS;
|
||||
import static net.sourceforge.tuned.FileUtilities.containsOnly;
|
||||
import static net.sourceforge.tuned.FileUtilities.getNameWithoutExtension;
|
||||
|
||||
import java.awt.datatransfer.Transferable;
|
||||
import java.awt.datatransfer.UnsupportedFlavorException;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
@ -22,7 +21,7 @@ import java.util.Scanner;
|
|||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.swing.SwingUtilities;
|
||||
import ca.odell.glazedlists.EventList;
|
||||
|
||||
import net.sourceforge.filebot.torrent.Torrent;
|
||||
import net.sourceforge.filebot.ui.transfer.FileTransferablePolicy;
|
||||
|
@ -31,17 +30,17 @@ import net.sourceforge.tuned.FileUtilities;
|
|||
|
||||
class NamesListTransferablePolicy extends FileTransferablePolicy {
|
||||
|
||||
private final RenameList<Object> list;
|
||||
private final EventList<Object> model;
|
||||
|
||||
|
||||
public NamesListTransferablePolicy(RenameList<Object> list) {
|
||||
this.list = list;
|
||||
public NamesListTransferablePolicy(EventList<Object> model) {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void clear() {
|
||||
list.getModel().clear();
|
||||
model.clear();
|
||||
}
|
||||
|
||||
|
||||
|
@ -65,15 +64,7 @@ class NamesListTransferablePolicy extends FileTransferablePolicy {
|
|||
|
||||
if (tr.isDataFlavorSupported(stringFlavor)) {
|
||||
// string transferable
|
||||
try {
|
||||
load((String) tr.getTransferData(stringFlavor));
|
||||
} catch (UnsupportedFlavorException e) {
|
||||
// should not happen
|
||||
throw new RuntimeException(e);
|
||||
} catch (IOException e) {
|
||||
// should not happen
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
load((String) tr.getTransferData(stringFlavor));
|
||||
} else if (super.accept(tr)) {
|
||||
// file transferable
|
||||
load(getFilesFromTransferable(tr));
|
||||
|
@ -81,29 +72,6 @@ class NamesListTransferablePolicy extends FileTransferablePolicy {
|
|||
}
|
||||
|
||||
|
||||
protected void submit(List<MutableString> entries) {
|
||||
List<MutableString> invalidEntries = new ArrayList<MutableString>();
|
||||
|
||||
for (MutableString entry : entries) {
|
||||
if (isInvalidFileName(entry.toString())) {
|
||||
invalidEntries.add(entry);
|
||||
}
|
||||
}
|
||||
|
||||
if (!invalidEntries.isEmpty()) {
|
||||
ValidateNamesDialog dialog = new ValidateNamesDialog(SwingUtilities.getWindowAncestor(list), invalidEntries);
|
||||
dialog.setVisible(true);
|
||||
|
||||
if (dialog.isCancelled()) {
|
||||
// return immediately, don't add items to list
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
list.getModel().addAll(entries);
|
||||
}
|
||||
|
||||
|
||||
protected void load(String string) {
|
||||
List<MutableString> entries = new ArrayList<MutableString>();
|
||||
|
||||
|
@ -117,7 +85,7 @@ class NamesListTransferablePolicy extends FileTransferablePolicy {
|
|||
}
|
||||
}
|
||||
|
||||
submit(entries);
|
||||
model.addAll(entries);
|
||||
}
|
||||
|
||||
|
||||
|
@ -140,29 +108,30 @@ class NamesListTransferablePolicy extends FileTransferablePolicy {
|
|||
|
||||
protected void loadFiles(List<File> files) {
|
||||
for (File file : files) {
|
||||
list.getModel().add(new AbstractFileEntry(FileUtilities.getName(file), file.length()));
|
||||
model.add(new AbstractFileEntry(FileUtilities.getName(file), file.length()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected void loadListFiles(List<File> files) throws FileNotFoundException {
|
||||
List<MutableString> entries = new ArrayList<MutableString>();
|
||||
List<String> values = new ArrayList<String>();
|
||||
|
||||
for (File file : files) {
|
||||
Scanner scanner = new Scanner(file, "UTF-8").useDelimiter(LINE_SEPARATOR);
|
||||
// don't use new Scanner(File) because of BUG 6368019 (http://bugs.sun.com/view_bug.do?bug_id=6368019)
|
||||
Scanner scanner = new Scanner(new FileInputStream(file), "UTF-8").useDelimiter(LINE_SEPARATOR);
|
||||
|
||||
while (scanner.hasNext()) {
|
||||
String line = scanner.next();
|
||||
|
||||
if (line.trim().length() > 0) {
|
||||
entries.add(new MutableString(line));
|
||||
values.add(line);
|
||||
}
|
||||
}
|
||||
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
submit(entries);
|
||||
model.addAll(values);
|
||||
}
|
||||
|
||||
|
||||
|
@ -179,7 +148,7 @@ class NamesListTransferablePolicy extends FileTransferablePolicy {
|
|||
}
|
||||
|
||||
// add torrent entries directly without checking file names for invalid characters
|
||||
list.getModel().addAll(entries);
|
||||
model.addAll(entries);
|
||||
} catch (IOException e) {
|
||||
Logger.getLogger("global").log(Level.SEVERE, e.toString(), e);
|
||||
}
|
||||
|
|
|
@ -2,8 +2,6 @@
|
|||
package net.sourceforge.filebot.ui.panel.rename;
|
||||
|
||||
|
||||
import static javax.swing.SwingUtilities.getWindowAncestor;
|
||||
import static net.sourceforge.filebot.FileBotUtilities.isInvalidFileName;
|
||||
import static net.sourceforge.tuned.ui.LoadingOverlayPane.LOADING_PROPERTY;
|
||||
|
||||
import java.awt.Insets;
|
||||
|
@ -55,7 +53,7 @@ public class RenamePanel extends FileBotPanel {
|
|||
|
||||
private RenameModel model = new RenameModel();
|
||||
|
||||
private RenameList<Object> namesList = new RenameList<Object>(model.names());
|
||||
private RenameList<String> namesList = new RenameList<String>(new NamesViewEventList(this, model.names()));
|
||||
|
||||
private RenameList<File> filesList = new RenameList<File>(model.files());
|
||||
|
||||
|
@ -70,7 +68,7 @@ public class RenamePanel extends FileBotPanel {
|
|||
super("Rename", ResourceManager.getIcon("panel.rename"));
|
||||
|
||||
namesList.setTitle("Proposed");
|
||||
namesList.setTransferablePolicy(new NamesListTransferablePolicy(namesList));
|
||||
namesList.setTransferablePolicy(new NamesListTransferablePolicy(model.names()));
|
||||
|
||||
filesList.setTitle("Current");
|
||||
filesList.setTransferablePolicy(new FilesListTransferablePolicy(filesList.getModel()));
|
||||
|
@ -186,35 +184,17 @@ public class RenamePanel extends FileBotPanel {
|
|||
@Override
|
||||
protected void done() {
|
||||
try {
|
||||
List<MutableString> names = new ArrayList<MutableString>();
|
||||
List<Episode> episodes = new ArrayList<Episode>();
|
||||
List<File> files = new ArrayList<File>();
|
||||
|
||||
List<MutableString> invalidNames = new ArrayList<MutableString>();
|
||||
|
||||
for (Match<File, Episode> match : get()) {
|
||||
MutableString name = new MutableString(match.getCandidate());
|
||||
|
||||
if (isInvalidFileName(name.toString())) {
|
||||
invalidNames.add(name);
|
||||
}
|
||||
|
||||
names.add(name);
|
||||
episodes.add(match.getCandidate());
|
||||
files.add(match.getValue());
|
||||
}
|
||||
|
||||
if (!invalidNames.isEmpty()) {
|
||||
ValidateNamesDialog dialog = new ValidateNamesDialog(getWindowAncestor(RenamePanel.this), invalidNames);
|
||||
dialog.setVisible(true);
|
||||
|
||||
if (dialog.isCancelled()) {
|
||||
// don't touch model
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
model.clear();
|
||||
|
||||
model.names().addAll(names);
|
||||
model.names().addAll(episodes);
|
||||
model.files().addAll(files);
|
||||
|
||||
// add remaining file entries
|
||||
|
|
|
@ -7,11 +7,12 @@ import static net.sourceforge.filebot.FileBotUtilities.validateFileName;
|
|||
|
||||
import java.awt.AlphaComposite;
|
||||
import java.awt.Color;
|
||||
import java.awt.Component;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Window;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import javax.swing.AbstractAction;
|
||||
import javax.swing.Action;
|
||||
|
@ -22,6 +23,7 @@ import javax.swing.JLabel;
|
|||
import javax.swing.JList;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.KeyStroke;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
import net.miginfocom.swing.MigLayout;
|
||||
import net.sourceforge.filebot.ResourceManager;
|
||||
|
@ -31,23 +33,26 @@ import net.sourceforge.tuned.ui.TunedUtilities;
|
|||
|
||||
class ValidateNamesDialog extends JDialog {
|
||||
|
||||
private final Collection<MutableString> entries;
|
||||
private final List<String> source;
|
||||
private String[] validatedValues;
|
||||
|
||||
private boolean cancelled = true;
|
||||
|
||||
protected final JList list;
|
||||
|
||||
protected final Action validateAction = new ValidateAction();
|
||||
protected final Action continueAction = new ContinueAction();
|
||||
protected final Action cancelAction = new CancelAction();
|
||||
|
||||
|
||||
public ValidateNamesDialog(Window owner, Collection<MutableString> entries) {
|
||||
public ValidateNamesDialog(Window owner, List<String> source) {
|
||||
super(owner, "Invalid Names", ModalityType.DOCUMENT_MODAL);
|
||||
|
||||
this.entries = entries;
|
||||
this.source = source;
|
||||
|
||||
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
|
||||
|
||||
JList list = new JList(new ArrayListModel(entries));
|
||||
list = new JList(new ArrayListModel(source));
|
||||
list.setEnabled(false);
|
||||
|
||||
list.setCellRenderer(new HighlightListCellRenderer(INVALID_CHARACTERS_PATTERN, new CharacterHighlightPainter(new Color(0xFF4200), new Color(0xFF1200)), 4));
|
||||
|
@ -82,6 +87,13 @@ class ValidateNamesDialog extends JDialog {
|
|||
|
||||
setVisible(false);
|
||||
dispose();
|
||||
|
||||
if (validatedValues != null && !cancelled) {
|
||||
// update source list
|
||||
for (int i = 0; i < validatedValues.length; i++) {
|
||||
source.set(i, validatedValues[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -95,8 +107,10 @@ class ValidateNamesDialog extends JDialog {
|
|||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
for (MutableString entry : entries) {
|
||||
entry.set(validateFileName(entry.toString()));
|
||||
validatedValues = new String[source.size()];
|
||||
|
||||
for (int i = 0; i < validatedValues.length; i++) {
|
||||
validatedValues[i] = validateFileName(source.get(i));
|
||||
}
|
||||
|
||||
setEnabled(false);
|
||||
|
@ -104,8 +118,8 @@ class ValidateNamesDialog extends JDialog {
|
|||
continueAction.putValue(SMALL_ICON, getValue(SMALL_ICON));
|
||||
continueAction.putValue(ContinueAction.ALPHA, 1.0f);
|
||||
|
||||
// render list entries again to display changes
|
||||
repaint();
|
||||
// update displayed values
|
||||
list.setModel(new ArrayListModel(validatedValues));
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -180,4 +194,12 @@ class ValidateNamesDialog extends JDialog {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
public static boolean showDialog(Component parent, List<String> source) {
|
||||
ValidateNamesDialog dialog = new ValidateNamesDialog(parent != null ? SwingUtilities.getWindowAncestor(parent) : null, source);
|
||||
|
||||
dialog.setVisible(true);
|
||||
|
||||
return !dialog.isCancelled();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -65,7 +65,7 @@ public class SubtitlePanel extends AbstractSearchPanel<SubtitleClient, SubtitleP
|
|||
|
||||
// get the preferences node that contains the history entries
|
||||
// and get a StringList that read and writes directly from and to the preferences
|
||||
List<String> persistentHistory = Settings.userRoot().node("subtitles/history").asList(String.class);
|
||||
List<String> persistentHistory = Settings.userRoot().node("subtitles/history").asList();
|
||||
|
||||
// add history from the preferences to the current in-memory history (for completion)
|
||||
history.addAll(persistentHistory);
|
||||
|
|
|
@ -103,8 +103,8 @@ public class PreferencesList<T> extends AbstractList<T> {
|
|||
}
|
||||
|
||||
|
||||
public static <T> PreferencesList<T> map(Preferences prefs, Class<T> type) {
|
||||
return new PreferencesList<T>(PreferencesMap.map(prefs, type));
|
||||
public static PreferencesList<String> map(Preferences prefs) {
|
||||
return new PreferencesList<String>(PreferencesMap.map(prefs));
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -47,13 +47,10 @@ public class PreferencesMap<T> implements Map<String, T> {
|
|||
public T put(String key, T value) {
|
||||
adapter.put(prefs, key, value);
|
||||
|
||||
return value;
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return always null
|
||||
*/
|
||||
@Override
|
||||
public T remove(Object key) {
|
||||
if (key instanceof String) {
|
||||
|
@ -107,7 +104,7 @@ public class PreferencesMap<T> implements Map<String, T> {
|
|||
Set<Map.Entry<String, T>> entries = new LinkedHashSet<Map.Entry<String, T>>();
|
||||
|
||||
for (String key : keys()) {
|
||||
entries.add(new Entry(key));
|
||||
entries.add(new PreferencesEntry<T>(prefs, key, adapter));
|
||||
}
|
||||
|
||||
return entries;
|
||||
|
@ -151,49 +148,9 @@ public class PreferencesMap<T> implements Map<String, T> {
|
|||
return values;
|
||||
}
|
||||
|
||||
|
||||
private class Entry implements Map.Entry<String, T> {
|
||||
|
||||
private final String key;
|
||||
|
||||
|
||||
public Entry(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public T getValue() {
|
||||
return get(key);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public T setValue(T value) {
|
||||
return put(key, value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> PreferencesMap<T> map(Preferences prefs, Class<T> type) {
|
||||
Adapter<T> adapter;
|
||||
|
||||
if (type == String.class) {
|
||||
// prefer StringAdapter, because SimpleAdapter would use the copy constructor of String, instead of returning the values directly
|
||||
adapter = (Adapter<T>) new StringAdapter();
|
||||
} else {
|
||||
adapter = new SimpleAdapter(type);
|
||||
}
|
||||
|
||||
return map(prefs, adapter);
|
||||
public static PreferencesMap<String> map(Preferences prefs) {
|
||||
return map(prefs, new StringAdapter());
|
||||
}
|
||||
|
||||
|
||||
|
@ -294,6 +251,11 @@ public class PreferencesMap<T> implements Map<String, T> {
|
|||
prefs.put(key, value.toString());
|
||||
}
|
||||
|
||||
|
||||
public static <T> SimpleAdapter<T> forClass(Class<T> type) {
|
||||
return new SimpleAdapter<T>(type);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -335,4 +297,42 @@ public class PreferencesMap<T> implements Map<String, T> {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
public static class PreferencesEntry<T> implements Entry<String, T> {
|
||||
|
||||
private final String key;
|
||||
|
||||
private final Preferences prefs;
|
||||
|
||||
private final Adapter<T> adapter;
|
||||
|
||||
|
||||
public PreferencesEntry(Preferences prefs, String key, Adapter<T> adapter) {
|
||||
this.key = key;
|
||||
this.prefs = prefs;
|
||||
this.adapter = adapter;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public T getValue() {
|
||||
return adapter.get(prefs, key);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public T setValue(T value) {
|
||||
adapter.put(prefs, key, value);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
package net.sourceforge.tuned.ui;
|
||||
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.swing.ListModel;
|
||||
|
@ -18,6 +19,11 @@ public class ArrayListModel implements ListModel {
|
|||
}
|
||||
|
||||
|
||||
public ArrayListModel(Object[] data) {
|
||||
this.data = Arrays.copyOf(data, data.length);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Object getElementAt(int index) {
|
||||
return data[index];
|
||||
|
|
|
@ -27,7 +27,7 @@ public class SeasonEpisodeMatcherTest {
|
|||
assertEquals("1x01", matcher.match("1x01").get(0).toString());
|
||||
|
||||
// test multiple matches
|
||||
assertEquals("1x02", matcher.match("Test - 1x01 and 1x02 - Multiple MatchCollection").get(1).toString());
|
||||
assertEquals("1x02", matcher.match("Test - 1x01 and 1.02 - Multiple MatchCollection").get(1).toString());
|
||||
|
||||
// test high values
|
||||
assertEquals("12x345", matcher.match("Test - 12x345 - High Values").get(0).toString());
|
||||
|
|
|
@ -27,12 +27,4 @@ public class SeasonEpisodeSimilarityMetricTest {
|
|||
assertEquals(0.0, metric.getSimilarity("abc", "xyz"), 0);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void fallbackMetric() {
|
||||
assertEquals(1.0, metric.getSimilarity("1x01", "sno=1, eno=1"), 0);
|
||||
|
||||
assertEquals(1.0, metric.getSimilarity("1x02", "Dexter - Staffel 1 Episode 2"), 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -9,6 +9,8 @@ import java.util.ArrayList;
|
|||
import java.util.List;
|
||||
import java.util.prefs.Preferences;
|
||||
|
||||
import net.sourceforge.tuned.PreferencesMap.SimpleAdapter;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
@ -51,7 +53,7 @@ public class PreferencesListTest {
|
|||
|
||||
@Test
|
||||
public void get() {
|
||||
List<String> list = PreferencesList.map(strings, String.class);
|
||||
List<String> list = PreferencesList.map(strings);
|
||||
|
||||
assertEquals("Rei", list.get(0));
|
||||
assertEquals("Roswell", list.get(2));
|
||||
|
@ -61,7 +63,7 @@ public class PreferencesListTest {
|
|||
|
||||
@Test
|
||||
public void add() {
|
||||
List<Integer> list = PreferencesList.map(numbers, Integer.class);
|
||||
List<Integer> list = PreferencesList.map(numbers, SimpleAdapter.forClass(Integer.class));
|
||||
|
||||
list.add(3);
|
||||
|
||||
|
@ -80,7 +82,7 @@ public class PreferencesListTest {
|
|||
compareValues.add("Gladiator 4");
|
||||
compareValues.add("Gladiator 5");
|
||||
|
||||
List<String> prefs = PreferencesList.map(temp, String.class);
|
||||
List<String> prefs = PreferencesList.map(temp);
|
||||
prefs.addAll(compareValues);
|
||||
|
||||
for (int index : new int[] { 4, 0, 1 }) {
|
||||
|
@ -95,7 +97,7 @@ public class PreferencesListTest {
|
|||
|
||||
@Test
|
||||
public void setEntry() {
|
||||
List<String> list = PreferencesList.map(strings, String.class);
|
||||
List<String> list = PreferencesList.map(strings);
|
||||
|
||||
list.set(3, "Buffy");
|
||||
|
||||
|
@ -105,7 +107,7 @@ public class PreferencesListTest {
|
|||
|
||||
@Test
|
||||
public void toArray() throws Exception {
|
||||
List<String> list = PreferencesList.map(strings, String.class);
|
||||
List<String> list = PreferencesList.map(strings);
|
||||
|
||||
assertArrayEquals(list.subList(0, 3).toArray(), new Object[] { "Rei", "Firefly", "Roswell" });
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@ import java.util.prefs.Preferences;
|
|||
|
||||
import net.sourceforge.filebot.web.MovieDescriptor;
|
||||
import net.sourceforge.tuned.PreferencesMap.SerializableAdapter;
|
||||
import net.sourceforge.tuned.PreferencesMap.SimpleAdapter;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
|
@ -62,7 +63,7 @@ public class PreferencesMapTest {
|
|||
|
||||
@Test
|
||||
public void get() {
|
||||
Map<String, String> stringMap = PreferencesMap.map(strings, String.class);
|
||||
Map<String, String> stringMap = PreferencesMap.map(strings);
|
||||
|
||||
assertEquals("Firefly", stringMap.get("1"));
|
||||
}
|
||||
|
@ -70,7 +71,7 @@ public class PreferencesMapTest {
|
|||
|
||||
@Test
|
||||
public void put() {
|
||||
Map<String, String> stringMap = PreferencesMap.map(temp, String.class);
|
||||
Map<String, String> stringMap = PreferencesMap.map(temp);
|
||||
|
||||
stringMap.put("key", "snake");
|
||||
|
||||
|
@ -80,7 +81,7 @@ public class PreferencesMapTest {
|
|||
|
||||
@Test
|
||||
public void remove() throws Exception {
|
||||
Map<String, Integer> map = PreferencesMap.map(numbers, Integer.class);
|
||||
Map<String, Integer> map = PreferencesMap.map(numbers, SimpleAdapter.forClass(Integer.class));
|
||||
|
||||
map.remove("A");
|
||||
|
||||
|
@ -90,7 +91,7 @@ public class PreferencesMapTest {
|
|||
|
||||
@Test
|
||||
public void clear() throws Exception {
|
||||
Map<String, Integer> map = PreferencesMap.map(temp, Integer.class);
|
||||
Map<String, Integer> map = PreferencesMap.map(temp, SimpleAdapter.forClass(Integer.class));
|
||||
|
||||
map.put("X", 42);
|
||||
|
||||
|
@ -104,7 +105,7 @@ public class PreferencesMapTest {
|
|||
public void containsKey() {
|
||||
temp.put("name", "kaya");
|
||||
|
||||
Map<String, String> map = PreferencesMap.map(temp, String.class);
|
||||
Map<String, String> map = PreferencesMap.map(temp);
|
||||
|
||||
assertTrue(map.containsKey("name"));
|
||||
}
|
||||
|
@ -113,7 +114,7 @@ public class PreferencesMapTest {
|
|||
@Test
|
||||
public void values() {
|
||||
|
||||
Map<String, Integer> map = PreferencesMap.map(sequence, Integer.class);
|
||||
Map<String, Integer> map = PreferencesMap.map(sequence, SimpleAdapter.forClass(Integer.class));
|
||||
|
||||
Collection<Integer> list = map.values();
|
||||
|
||||
|
@ -125,7 +126,7 @@ public class PreferencesMapTest {
|
|||
|
||||
@Test
|
||||
public void containsValue() {
|
||||
Map<String, String> map = PreferencesMap.map(strings, String.class);
|
||||
Map<String, String> map = PreferencesMap.map(strings);
|
||||
|
||||
assertTrue(map.containsValue("Firefly"));
|
||||
}
|
||||
|
@ -133,7 +134,7 @@ public class PreferencesMapTest {
|
|||
|
||||
@Test
|
||||
public void entrySet() {
|
||||
Map<String, Integer> map = PreferencesMap.map(numbers, Integer.class);
|
||||
Map<String, Integer> map = PreferencesMap.map(numbers, SimpleAdapter.forClass(Integer.class));
|
||||
|
||||
for (Entry<String, Integer> entry : map.entrySet()) {
|
||||
Integer v = entry.getValue();
|
||||
|
@ -146,20 +147,20 @@ public class PreferencesMapTest {
|
|||
|
||||
@Test(expected = NumberFormatException.class)
|
||||
public void adapterException() {
|
||||
PreferencesMap.map(strings, Integer.class).values();
|
||||
PreferencesMap.map(strings, SimpleAdapter.forClass(Integer.class)).values();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void containsKeyWithObjectKey() throws Exception {
|
||||
Map<String, String> map = PreferencesMap.map(strings, String.class);
|
||||
Map<String, String> map = PreferencesMap.map(strings);
|
||||
|
||||
assertFalse(map.containsKey(new Object()));
|
||||
}
|
||||
|
||||
|
||||
public void getWithObjectKey() throws Exception {
|
||||
Map<String, String> map = PreferencesMap.map(strings, String.class);
|
||||
Map<String, String> map = PreferencesMap.map(strings);
|
||||
|
||||
assertEquals(null, map.get(new Object()));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue