X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Freader%2Fui%2FGuiReaderSearchByNamePanel.java;h=ebdb21ab2cf760562089447d48fffac6b873445f;hb=1387a30ab59dbf4071f2c5e5e0e08ca98c75b726;hp=94579fd4c7dbf58356b1554fcc964ef5a84b160b;hpb=9c59820764e52d69c4248a2c38a8f4a42059d647;p=nikiroo-utils.git diff --git a/src/be/nikiroo/fanfix/reader/ui/GuiReaderSearchByNamePanel.java b/src/be/nikiroo/fanfix/reader/ui/GuiReaderSearchByNamePanel.java index 94579fd..ebdb21a 100644 --- a/src/be/nikiroo/fanfix/reader/ui/GuiReaderSearchByNamePanel.java +++ b/src/be/nikiroo/fanfix/reader/ui/GuiReaderSearchByNamePanel.java @@ -14,6 +14,7 @@ import javax.swing.JPanel; import javax.swing.JTextField; import be.nikiroo.fanfix.data.MetaData; +import be.nikiroo.fanfix.reader.ui.GuiReaderSearchByPanel.Waitable; import be.nikiroo.fanfix.searchable.BasicSearchable; /** @@ -35,7 +36,7 @@ public class GuiReaderSearchByNamePanel extends JPanel { private List stories = new ArrayList(); private int storyItem; - public GuiReaderSearchByNamePanel(final Runnable fireEvent) { + public GuiReaderSearchByNamePanel(final Waitable waitable) { super(new BorderLayout()); keywordsField = new JTextField(); @@ -44,11 +45,25 @@ public class GuiReaderSearchByNamePanel extends JPanel { submitKeywords = new JButton("Search"); add(submitKeywords, BorderLayout.EAST); + // should be done out of UI + final Runnable go = new Runnable() { + @Override + public void run() { + waitable.setWaiting(true); + try { + search(keywordsField.getText(), 1, 0); + waitable.fireEvent(); + } finally { + waitable.setWaiting(false); + } + } + }; + keywordsField.addKeyListener(new KeyAdapter() { @Override public void keyReleased(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ENTER) { - search(keywordsField.getText(), 1, 0); + new Thread(go).start(); } else { super.keyReleased(e); } @@ -58,13 +73,7 @@ public class GuiReaderSearchByNamePanel extends JPanel { submitKeywords.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - new Thread(new Runnable() { - @Override - public void run() { - search(keywordsField.getText(), 1, 0); - fireEvent.run(); - } - }).start(); + new Thread(go).start(); } }); @@ -81,35 +90,73 @@ public class GuiReaderSearchByNamePanel extends JPanel { */ public void setSearchable(BasicSearchable searchable) { this.searchable = searchable; - page = 1; + page = 0; maxPage = -1; storyItem = 0; stories = new ArrayList(); updateKeywords(""); } + /** + * The currently displayed page of result for the current search (see the + * page parameter of + * {@link GuiReaderSearchByNamePanel#search(String, int, int)}). + * + * @return the currently displayed page of results + */ public int getPage() { return page; } + /** + * The number of pages of result for the current search (see the + * page parameter of + * {@link GuiReaderSearchByPanel#search(String, int, int)}). + *

+ * For an unknown number or when not applicable, -1 is returned. + * + * @return the number of pages of results or -1 + */ public int getMaxPage() { return maxPage; } + /** + * Return the keywords used for the current search. + * + * @return the keywords + */ public String getCurrentKeywords() { return keywordsField.getText(); } + /** + * The currently loaded stories (the result of the latest search). + * + * @return the stories + */ public List getStories() { return stories; } - // selected item or 0 if none ! one-based ! + /** + * Return the currently selected story (the item) if it was + * specified in the latest, or 0 if not. + *

+ * Note: this is thus a 1-based index, not a 0-based index. + * + * @return the item + */ public int getStoryItem() { return storyItem; } - // cannot be NULL + /** + * Update the keywords displayed on screen. + * + * @param keywords + * the keywords + */ private void updateKeywords(final String keywords) { if (!keywords.equals(keywordsField.getText())) { GuiReaderSearchFrame.inUi(new Runnable() { @@ -121,8 +168,20 @@ public class GuiReaderSearchByNamePanel extends JPanel { } } - // item 0 = no selection, else = default selection - // throw if page > max + /** + * Search for the given terms on the currently selected searchable. + *

+ * This operation can be long and should be run outside the UI thread. + * + * @param keywords + * the keywords to search for + * @param page + * the page of results to load + * @param item + * the item to select (or 0 for none by default) + * + * @throw IndexOutOfBoundsException if the page is out of bounds + */ public void search(String keywords, int page, int item) { List stories = new ArrayList(); int storyItem = 0; @@ -130,10 +189,12 @@ public class GuiReaderSearchByNamePanel extends JPanel { updateKeywords(keywords); int maxPage = -1; - try { - maxPage = searchable.searchPages(keywords); - } catch (IOException e) { - GuiReaderSearchFrame.error(e); + if (searchable != null) { + try { + maxPage = searchable.searchPages(keywords); + } catch (IOException e) { + GuiReaderSearchFrame.error(e); + } } if (page > 0) { @@ -142,11 +203,12 @@ public class GuiReaderSearchByNamePanel extends JPanel { + maxPage); } - try { - stories = searchable.search(keywords, page); - } catch (IOException e) { - GuiReaderSearchFrame.error(e); - stories = new ArrayList(); + if (searchable != null) { + try { + stories = searchable.search(keywords, page); + } catch (IOException e) { + GuiReaderSearchFrame.error(e); + } } if (item > 0 && item <= stories.size()) {