From a9f5570dbebd73e4d43533c40c1fdba7d6348e63 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sun, 22 Feb 2009 11:55:21 +0000 Subject: [PATCH] * improved automatic search result selection and allow manual selection in RenamePanel auto-matching --- .../filebot/ui/AbstractSearchPanel.java | 31 ++++++----- .../rename/AutoFetchEpisodeListMatcher.java | 19 +++++-- .../filebot/ui/panel/rename/RenamePanel.java | 51 ++++++++++++++++++- .../ui/panel/rename/ValidateNamesDialog.java | 1 + 4 files changed, 81 insertions(+), 21 deletions(-) diff --git a/source/net/sourceforge/filebot/ui/AbstractSearchPanel.java b/source/net/sourceforge/filebot/ui/AbstractSearchPanel.java index 33c0468d..9d66b72c 100644 --- a/source/net/sourceforge/filebot/ui/AbstractSearchPanel.java +++ b/source/net/sourceforge/filebot/ui/AbstractSearchPanel.java @@ -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 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 completionList = new CompositeList(); - - completionList.addMemberList((EventList) searchHistory); - completionList.addMemberList(fetchHistory); - */ - searchTextField.getEditor().setAction(searchAction); searchTextField.getSelectButton().setModel(createSearchEngines()); @@ -158,7 +150,7 @@ public abstract class AbstractSearchPanel 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 extends FileBotPanel { return; try { - // check if exception occurred + // check if an exception occurred Collection elements = get(); requestProcessor.process(elements); @@ -321,7 +313,7 @@ public abstract class AbstractSearchPanel extends FileBotPanel { } - protected SearchResult chooseSearchResult(Collection searchResults, Window window) throws Exception { + protected SearchResult selectSearchResult(Collection searchResults, Window window) throws Exception { switch (searchResults.size()) { case 0: @@ -331,10 +323,17 @@ public abstract class AbstractSearchPanel 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 exactMatches = new LinkedList(); + + // 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 diff --git a/source/net/sourceforge/filebot/ui/panel/rename/AutoFetchEpisodeListMatcher.java b/source/net/sourceforge/filebot/ui/panel/rename/AutoFetchEpisodeListMatcher.java index 6e9d52cd..9a702631 100644 --- a/source/net/sourceforge/filebot/ui/panel/rename/AutoFetchEpisodeListMatcher.java +++ b/source/net/sourceforge/filebot/ui/panel/rename/AutoFetchEpisodeListMatcher.java @@ -65,6 +65,12 @@ class AutoFetchEpisodeListMatcher extends SwingWorker> } + protected SearchResult selectSearchResult(String query, Collection searchResults) throws Exception { + // select first by default + return searchResults.iterator().next(); + } + + protected List fetchEpisodeList(Collection seriesNames) throws Exception { List>> tasks = new ArrayList>>(); @@ -74,12 +80,17 @@ class AutoFetchEpisodeListMatcher extends SwingWorker> @Override public Collection call() throws Exception { - Collection searchResults = client.search(seriesName); + Collection 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(); } }); } diff --git a/source/net/sourceforge/filebot/ui/panel/rename/RenamePanel.java b/source/net/sourceforge/filebot/ui/panel/rename/RenamePanel.java index 2b38595c..673faa45 100644 --- a/source/net/sourceforge/filebot/ui/panel/rename/RenamePanel.java +++ b/source/net/sourceforge/filebot/ui/panel/rename/RenamePanel.java @@ -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 searchResults) throws Exception { + if (searchResults.size() == 1) { + return searchResults.iterator().next(); + } + + List exactMatches = new LinkedList(); + + // 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 showSelectDialog = new FutureTask(new Callable() { + + @Override + public SearchResult call() throws Exception { + // multiple results have been found, user must select one + SelectDialog selectDialog = new SelectDialog(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(); } - } diff --git a/source/net/sourceforge/filebot/ui/panel/rename/ValidateNamesDialog.java b/source/net/sourceforge/filebot/ui/panel/rename/ValidateNamesDialog.java index ec419ec9..1c2cd2a7 100644 --- a/source/net/sourceforge/filebot/ui/panel/rename/ValidateNamesDialog.java +++ b/source/net/sourceforge/filebot/ui/panel/rename/ValidateNamesDialog.java @@ -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); }