From: Niki Date: Thu, 18 Apr 2019 10:47:31 +0000 (+0200) Subject: GUI search: reorg, step 1 X-Git-Tag: fanfix-3.0.0~84 X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=2f1e78931b11c6372a535671105896b94d6b19ba;p=fanfix.git GUI search: reorg, step 1 --- diff --git a/src/be/nikiroo/fanfix/reader/ui/GuiReaderSearchByNamePanel.java b/src/be/nikiroo/fanfix/reader/ui/GuiReaderSearchByNamePanel.java index a648b72..625dc93 100644 --- a/src/be/nikiroo/fanfix/reader/ui/GuiReaderSearchByNamePanel.java +++ b/src/be/nikiroo/fanfix/reader/ui/GuiReaderSearchByNamePanel.java @@ -208,8 +208,8 @@ public class GuiReaderSearchByNamePanel extends JPanel { List rootTags = null; SearchableTag selectedRootTag = null; - selectedRootTag = parents.isEmpty() ? null : parents - .get(parents.size() - 1); + selectedRootTag = parents.isEmpty() ? null + : parents.get(parents.size() - 1); try { rootTags = searchable.getTags(); @@ -282,89 +282,87 @@ public class GuiReaderSearchByNamePanel extends JPanel { } }); - combo.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - final SearchableTag tag = (SearchableTag) combo - .getSelectedItem(); - if (tag != null) { - while (comboIndex + 1 < combos.size()) { - JComboBox combo = combos.remove(comboIndex + 1); - tagBars.remove(combo); - } - - addTagBar(tag, new Runnable() { - @Override - public void run() { - // TODO: slow ui - SearchableTag tag = ((SearchableTag) combo - .getSelectedItem()); - if (tag != null && tag.isLeaf()) { - BasicSearchable searchable = BasicSearchable - .getSearchable(supportType); - List metas = new ArrayList(); - try { - metas = searchable.search(tag, 1); - search(metas, 1, - searchable.searchPages(tag), 0); - } catch (IOException e) { - error(e); - } - } - - setWaitingScreen(false); - } - }); - } - } - }); + combo.addActionListener(createComboTagAction(comboIndex)); combos.add(combo); tagBars.add(combo); } - - // async, add children of tag, NULL = base tags - private void addTagBar(final SearchableTag tag, final Runnable inUi) { - new Thread(new Runnable() { + + private ActionListener createComboTagAction(final int comboIndex) { + return new ActionListener() { @Override - public void run() { - List children = new ArrayList(); - if (tag == null) { - try { - List baseTags = searchable.getTags(); - children = baseTags; - } catch (IOException e) { - error(e); - } - } else { - try { - searchable.fillTag(tag); - } catch (IOException e) { - error(e); - } - - if (!tag.isLeaf()) { - children = tag.getChildren(); - } else { - children = null; - } + public void actionPerformed(ActionEvent ae) { + List combos = GuiReaderSearchByNamePanel.this.combos; + if (combos == null || comboIndex < 0 || comboIndex >= combos.size()) { + return; + } + + // Tag can be NULL + final SearchableTag tag = (SearchableTag) combos.get(comboIndex) + .getSelectedItem(); + + while (comboIndex + 1 < combos.size()) { + JComboBox combo = combos.remove(comboIndex + 1); + tagBars.remove(combo); } - final List fchildren = children; - inUi(new Runnable() { + new Thread(new Runnable() { @Override public void run() { - if (fchildren != null) { - addTagBar(fchildren, tag); + final List children = getChildrenForTag(tag); + if (children != null) { + GuiReaderSearchFrame.inUi(new Runnable() { + @Override + public void run() { + addTagBar(children, tag); + } + }); } - - if (inUi != null) { - inUi.run(); + + if (tag != null && tag.isLeaf()) { + try { + GuiReaderSearchByNamePanel.this.page = 1; + stories = searchable.search(tag, 1); + } catch (IOException e) { + GuiReaderSearchFrame.error(e); + GuiReaderSearchByNamePanel.this.page = 0; + stories = new ArrayList(); + } + + fireAction(); } } - }); + }).start(); } - }).start(); + }; + } + + // sync, add children of tag, NULL = base tags + // return children of the tag or base tags or NULL + private List getChildrenForTag(final SearchableTag tag) { + List children = new ArrayList(); + if (tag == null) { + try { + List baseTags = searchable.getTags(); + children = baseTags; + } catch (IOException e) { + GuiReaderSearchFrame.error(e); + } + } else { + try { + searchable.fillTag(tag); + } catch (IOException e) { + GuiReaderSearchFrame.error(e); + } + + if (!tag.isLeaf()) { + children = tag.getChildren(); + } else { + children = null; + } + } + + return children; } // item 0 = no selection, else = default selection @@ -446,10 +444,9 @@ public class GuiReaderSearchByNamePanel extends JPanel { if (item > 0 && item <= stories.size()) { storyItem = item; } else if (item > 0) { - GuiReaderSearchFrame - .error(String - .format("Story item does not exist: Tag [%s], item %d", - tag.getFqName(), item)); + GuiReaderSearchFrame.error(String.format( + "Story item does not exist: Tag [%s], item %d", + tag.getFqName(), item)); } } catch (IOException e) { GuiReaderSearchFrame.error(e);