* improved automatic search result selection and allow manual selection in RenamePanel auto-matching

This commit is contained in:
Reinhard Pointner 2009-02-22 11:55:21 +00:00
parent 214399f13c
commit a9f5570dbe
4 changed files with 81 additions and 21 deletions

View File

@ -8,6 +8,7 @@ import java.awt.Window;
import java.awt.event.ActionEvent;
import java.net.URI;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -66,15 +67,6 @@ public abstract class AbstractSearchPanel<S, E> extends FileBotPanel {
add(new JButton(searchAction), "gap 18px, wrap 10px");
add(tabbedPaneGroup, "grow");
/*
* TODO: fetchHistory
// no need to care about thread-safety, history-lists are only accessed from the EDT
CompositeList<Object> completionList = new CompositeList<Object>();
completionList.addMemberList((EventList) searchHistory);
completionList.addMemberList(fetchHistory);
*/
searchTextField.getEditor().setAction(searchAction);
searchTextField.getSelectButton().setModel(createSearchEngines());
@ -158,7 +150,7 @@ public abstract class AbstractSearchPanel<S, E> extends FileBotPanel {
try {
// choose search result
requestProcessor.setSearchResult(requestProcessor.chooseSearchResult(get(), SwingUtilities.getWindowAncestor(AbstractSearchPanel.this)));
requestProcessor.setSearchResult(requestProcessor.selectSearchResult(get(), SwingUtilities.getWindowAncestor(AbstractSearchPanel.this)));
if (requestProcessor.getSearchResult() == null) {
tab.close();
@ -215,7 +207,7 @@ public abstract class AbstractSearchPanel<S, E> extends FileBotPanel {
return;
try {
// check if exception occurred
// check if an exception occurred
Collection<E> elements = get();
requestProcessor.process(elements);
@ -321,7 +313,7 @@ public abstract class AbstractSearchPanel<S, E> extends FileBotPanel {
}
protected SearchResult chooseSearchResult(Collection<? extends SearchResult> searchResults, Window window) throws Exception {
protected SearchResult selectSearchResult(Collection<? extends SearchResult> searchResults, Window window) throws Exception {
switch (searchResults.size()) {
case 0:
@ -331,10 +323,17 @@ public abstract class AbstractSearchPanel<S, E> extends FileBotPanel {
return searchResults.iterator().next();
}
// check if an exact match has been found
for (SearchResult searchResult : searchResults) {
if (request.getSearchText().equalsIgnoreCase(searchResult.getName()))
return searchResult;
List<SearchResult> exactMatches = new LinkedList<SearchResult>();
// find exact matches
for (SearchResult result : searchResults) {
if (result.getName().toLowerCase().startsWith(request.getSearchText().toLowerCase())) {
exactMatches.add(result);
}
}
if (exactMatches.size() == 1) {
return exactMatches.get(0);
}
// multiple results have been found, user must select one

View File

@ -65,6 +65,12 @@ class AutoFetchEpisodeListMatcher extends SwingWorker<List<Match<File, Episode>>
}
protected SearchResult selectSearchResult(String query, Collection<SearchResult> searchResults) throws Exception {
// select first by default
return searchResults.iterator().next();
}
protected List<Episode> fetchEpisodeList(Collection<String> seriesNames) throws Exception {
List<Callable<Collection<Episode>>> tasks = new ArrayList<Callable<Collection<Episode>>>();
@ -74,12 +80,17 @@ class AutoFetchEpisodeListMatcher extends SwingWorker<List<Match<File, Episode>>
@Override
public Collection<Episode> call() throws Exception {
Collection<SearchResult> searchResults = client.search(seriesName);
Collection<SearchResult> results = client.search(seriesName);
if (searchResults.isEmpty())
return Collections.emptyList();
if (results.size() > 0) {
SearchResult selectedSearchResult = selectSearchResult(seriesName, results);
if (selectedSearchResult != null) {
return formatEpisodeNumbers(client.getEpisodeList(selectedSearchResult), 2);
}
}
return formatEpisodeNumbers(client.getEpisodeList(searchResults.iterator().next()), 2);
return Collections.emptyList();
}
});
}

View File

@ -12,7 +12,12 @@ import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.concurrent.RunnableFuture;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -23,15 +28,18 @@ import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import net.miginfocom.swing.MigLayout;
import net.sourceforge.filebot.ResourceManager;
import net.sourceforge.filebot.Settings;
import net.sourceforge.filebot.similarity.Match;
import net.sourceforge.filebot.ui.FileBotPanel;
import net.sourceforge.filebot.ui.SelectDialog;
import net.sourceforge.filebot.web.AnidbClient;
import net.sourceforge.filebot.web.Episode;
import net.sourceforge.filebot.web.EpisodeListClient;
import net.sourceforge.filebot.web.SearchResult;
import net.sourceforge.filebot.web.TVRageClient;
import net.sourceforge.filebot.web.TheTVDBClient;
import net.sourceforge.tuned.ExceptionUtilities;
@ -216,11 +224,52 @@ public class RenamePanel extends FileBotPanel {
// auto-match finished
namesList.firePropertyChange(LOADING_PROPERTY, true, false);
}
@Override
protected SearchResult selectSearchResult(String query, final Collection<SearchResult> searchResults) throws Exception {
if (searchResults.size() == 1) {
return searchResults.iterator().next();
}
List<SearchResult> exactMatches = new LinkedList<SearchResult>();
// find exact matches
for (SearchResult result : searchResults) {
if (result.getName().toLowerCase().startsWith(query.toLowerCase())) {
exactMatches.add(result);
}
}
if (exactMatches.size() == 1) {
return exactMatches.get(0);
}
// show selection dialog on EDT
final RunnableFuture<SearchResult> showSelectDialog = new FutureTask<SearchResult>(new Callable<SearchResult>() {
@Override
public SearchResult call() throws Exception {
// multiple results have been found, user must select one
SelectDialog<SearchResult> selectDialog = new SelectDialog<SearchResult>(SwingUtilities.getWindowAncestor(RenamePanel.this), searchResults);
selectDialog.setVisible(true);
// selected value or null if the dialog was canceled by the user
return selectDialog.getSelectedValue();
}
});
// run on EDT
SwingUtilities.invokeAndWait(showSelectDialog);
// selected value or null
return showSelectDialog.get();
}
};
worker.execute();
}
}

View File

@ -66,6 +66,7 @@ class ValidateNamesDialog extends JDialog {
c.add(new JButton(cancelAction), "gap 12mm");
setSize(365, 280);
setLocation(TunedUtilities.getPreferredLocation(this));
TunedUtilities.putActionForKeystroke(c, KeyStroke.getKeyStroke("released ESCAPE"), cancelAction);
}