From ce5a42e7154962bd11539d3bd62821881b734821 Mon Sep 17 00:00:00 2001 From: Niki Date: Thu, 18 Apr 2019 15:36:36 +0200 Subject: [PATCH] GUI search: reorg, step 2 --- .../reader/ui/GuiReaderSearchByNamePanel.java | 27 +- .../reader/ui/GuiReaderSearchFrame.java | 409 ++---------------- 2 files changed, 57 insertions(+), 379 deletions(-) diff --git a/src/be/nikiroo/fanfix/reader/ui/GuiReaderSearchByNamePanel.java b/src/be/nikiroo/fanfix/reader/ui/GuiReaderSearchByNamePanel.java index 625dc93e..a627d3a9 100644 --- a/src/be/nikiroo/fanfix/reader/ui/GuiReaderSearchByNamePanel.java +++ b/src/be/nikiroo/fanfix/reader/ui/GuiReaderSearchByNamePanel.java @@ -54,10 +54,12 @@ public class GuiReaderSearchByNamePanel extends JPanel { private List stories = new ArrayList(); private int storyItem; - // will throw illegalArgEx if bad support type + // will throw illegalArgEx if bad support type, NULL allowed public GuiReaderSearchByNamePanel(SupportType supportType) { setLayout(new BorderLayout()); + // TODO: check if null really is OK for supportType (must be) + setSupportType(supportType); page = 1; searchByTags = false; @@ -83,7 +85,7 @@ public class GuiReaderSearchByNamePanel extends JPanel { submitKeywords.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - search(keywordsField.getText(), 0); + search(keywordsField.getText(), 0, null); } }); @@ -107,11 +109,14 @@ public class GuiReaderSearchByNamePanel extends JPanel { public void setSupportType(SupportType supportType) { BasicSearchable searchable = BasicSearchable.getSearchable(supportType); - if (searchable == null) { + if (searchable == null && supportType != null) { throw new java.lang.IllegalArgumentException( "Unupported support type: " + supportType); } + // TODO: if <>, reset all + // if new, set the base tags + this.supportType = supportType; this.searchable = searchable; } @@ -144,7 +149,7 @@ public class GuiReaderSearchByNamePanel extends JPanel { return storyItem; } - private void fireAction() { + private void fireAction(final Runnable inUi) { GuiReaderSearchFrame.inUi(new Runnable() { @Override public void run() { @@ -164,6 +169,10 @@ public class GuiReaderSearchByNamePanel extends JPanel { GuiReaderSearchFrame.error(e); } } + + if (inUi != null) { + inUi.run(); + } } }); } @@ -329,7 +338,7 @@ public class GuiReaderSearchByNamePanel extends JPanel { stories = new ArrayList(); } - fireAction(); + fireAction(null); } } }).start(); @@ -367,7 +376,7 @@ public class GuiReaderSearchByNamePanel extends JPanel { // item 0 = no selection, else = default selection // return: maxpage - public int search(String keywords, int item) { + public int search(String keywords, int item, Runnable inUi) { List stories = new ArrayList(); int storyItem = 0; @@ -400,14 +409,14 @@ public class GuiReaderSearchByNamePanel extends JPanel { this.stories = stories; this.storyItem = storyItem; - fireAction(); + fireAction(inUi); return maxPage; } // tag: null = base tags // return: max pages - public int searchTag(SearchableTag tag, int item) { + public int searchTag(SearchableTag tag, int item, Runnable inUi) { List stories = new ArrayList(); int storyItem = 0; @@ -461,7 +470,7 @@ public class GuiReaderSearchByNamePanel extends JPanel { this.stories = stories; this.storyItem = storyItem; - fireAction(); + fireAction(inUi); return maxPage; } diff --git a/src/be/nikiroo/fanfix/reader/ui/GuiReaderSearchFrame.java b/src/be/nikiroo/fanfix/reader/ui/GuiReaderSearchFrame.java index dcbf9274..19e9ef45 100644 --- a/src/be/nikiroo/fanfix/reader/ui/GuiReaderSearchFrame.java +++ b/src/be/nikiroo/fanfix/reader/ui/GuiReaderSearchFrame.java @@ -1,27 +1,19 @@ package be.nikiroo.fanfix.reader.ui; import java.awt.BorderLayout; -import java.awt.Color; import java.awt.Component; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; -import javax.swing.BoxLayout; -import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; -import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; -import javax.swing.JTabbedPane; -import javax.swing.JTextField; -import javax.swing.ListCellRenderer; import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.data.MetaData; @@ -43,18 +35,11 @@ public class GuiReaderSearchFrame extends JFrame { private List supportTypes; private SupportType supportType; - private boolean searchByTags; - private String keywords; private int page; private int maxPage; - private JPanel tagBars; - private List combos; - private JComboBox comboSupportTypes; - private JTabbedPane searchTabs; - private JTextField keywordsField; - private JButton submitKeywords; + private GuiReaderSearchByNamePanel searchPanel; private boolean seeWordcount; private GuiReaderGroup books; @@ -66,7 +51,6 @@ public class GuiReaderSearchFrame extends JFrame { page = 1; // TODO maxPage = -1; - searchByTags = false; supportTypes = new ArrayList(); for (SupportType type : SupportType.values()) { @@ -89,13 +73,29 @@ public class GuiReaderSearchFrame extends JFrame { searchSites.add(comboSupportTypes, BorderLayout.CENTER); searchSites.add(new JLabel(" " + "Website : "), BorderLayout.WEST); - searchTabs = new JTabbedPane(); - searchTabs.addTab("By name", createByNameSearchPanel()); - searchTabs.addTab("By tags", createByTagSearchPanel()); + searchPanel = new GuiReaderSearchByNamePanel(supportType); + searchPanel.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + updatePages(0, maxPage); + List infos = new ArrayList(); + for (MetaData meta : searchPanel.getStories()) { + infos.add(GuiReaderBookInfo.fromMeta(meta)); + } + + updateBooks(infos); + + // ! 1-based index ! + int item = searchPanel.getStoryItem(); + if (item > 0 && item <= books.getBooksCount()) { + // TODO: "click" on item ITEM + } + } + }); JPanel top = new JPanel(new BorderLayout()); top.add(searchSites, BorderLayout.NORTH); - top.add(searchTabs, BorderLayout.CENTER); + top.add(searchPanel, BorderLayout.CENTER); add(top, BorderLayout.NORTH); @@ -119,40 +119,6 @@ public class GuiReaderSearchFrame extends JFrame { JScrollPane scroll = new JScrollPane(books); scroll.getVerticalScrollBar().setUnitIncrement(16); add(scroll, BorderLayout.CENTER); - - updateTags(null); - } - - private JPanel createByNameSearchPanel() { - JPanel byName = new JPanel(new BorderLayout()); - - keywordsField = new JTextField(); - byName.add(keywordsField, BorderLayout.CENTER); - - submitKeywords = new JButton("Search"); - byName.add(submitKeywords, BorderLayout.EAST); - - // TODO: ENTER -> search - - submitKeywords.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - search(supportType, keywordsField.getText(), page, 0); - } - }); - - return byName; - } - - private JPanel createByTagSearchPanel() { - combos = new ArrayList(); - - JPanel byTag = new JPanel(); - tagBars = new JPanel(); - tagBars.setLayout(new BoxLayout(tagBars, BoxLayout.Y_AXIS)); - byTag.add(tagBars, BorderLayout.NORTH); - - return byTag; } private void updateSupportType(SupportType supportType) { @@ -160,22 +126,7 @@ public class GuiReaderSearchFrame extends JFrame { this.supportType = supportType; comboSupportTypes.setSelectedItem(supportType); books.clear(); - updateTags(null); - } - } - - private void updateSearchBy(final boolean byTag) { - if (byTag != this.searchByTags) { - inUi(new Runnable() { - @Override - public void run() { - if (!byTag) { - searchTabs.setSelectedIndex(0); - } else { - searchTabs.setSelectedIndex(1); - } - } - }); + searchPanel.setSupportType(supportType); } } @@ -185,6 +136,9 @@ public class GuiReaderSearchFrame extends JFrame { public void run() { GuiReaderSearchFrame.this.page = page; GuiReaderSearchFrame.this.maxPage = maxPage; + + searchPanel.setPage(page); + // TODO: gui System.out.println("page: " + page); System.out.println("max page: " + maxPage); @@ -192,60 +146,6 @@ public class GuiReaderSearchFrame extends JFrame { }); } - // cannot be NULL - private void updateKeywords(final String keywords) { - if (!keywords.equals(this.keywords)) { - inUi(new Runnable() { - @Override - public void run() { - GuiReaderSearchFrame.this.keywords = keywords; - keywordsField.setText(keywords); - } - }); - } - } - - // update and reset the tagsbar - // can be NULL, for base tags - private void updateTags(final SearchableTag tag) { - final List parents = new ArrayList(); - SearchableTag parent = (tag == null) ? null : tag; - while (parent != null) { - parents.add(parent); - parent = parent.getParent(); - } - - inUi(new Runnable() { - @Override - public void run() { - tagBars.invalidate(); - tagBars.removeAll(); - - // TODO: Slow UI - // TODO: select the right one - try { - SearchableTag selectedChild = parents.isEmpty() ? null - : parents.get(parents.size() - 1); - addTagBar(BasicSearchable.getSearchable(supportType) - .getTags(), selectedChild); - } catch (IOException e) { - error(e); - } - - for (int i = parents.size() - 1; i >= 0; i--) { - SearchableTag selectedChild = null; - if (i > 0) { - selectedChild = parents.get(i - 1); - } - SearchableTag parent = parents.get(i); - addTagBar(parent.getChildren(), selectedChild); - } - - tagBars.validate(); - } - }); - } - private void updateBooks(final List infos) { setWaitingScreen(true); inUi(new Runnable() { @@ -257,179 +157,22 @@ public class GuiReaderSearchFrame extends JFrame { }); } - private void addTagBar(List tags, - final SearchableTag selected) { - tags.add(0, null); - - final int comboIndex = combos.size(); - - final JComboBox combo = new JComboBox( - tags.toArray(new SearchableTag[] {})); - combo.setSelectedItem(selected); - - final ListCellRenderer basic = combo.getRenderer(); - - combo.setRenderer(new ListCellRenderer() { - @Override - public Component getListCellRendererComponent(JList list, - Object value, int index, boolean isSelected, - boolean cellHasFocus) { - - Object displayValue = value; - if (value instanceof SearchableTag) { - displayValue = ((SearchableTag) value).getName(); - } else { - displayValue = "Select a tag..."; - cellHasFocus = false; - isSelected = false; - } - - Component rep = basic.getListCellRendererComponent(list, - displayValue, index, isSelected, cellHasFocus); - - if (value == null) { - rep.setForeground(Color.GRAY); - } - - return rep; - } - }); - - 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); - } - }); - } - } - }); - - 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() { - @Override - public void run() { - BasicSearchable searchable = BasicSearchable - .getSearchable(supportType); - - 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; - } - } - - final List fchildren = children; - inUi(new Runnable() { - @Override - public void run() { - if (fchildren != null) { - addTagBar(fchildren, tag); - } - - if (inUi != null) { - inUi.run(); - } - } - }); - } - }).start(); - } - // item 0 = no selection, else = default selection public void search(final SupportType searchOn, final String keywords, final int page, final int item) { - setWaitingScreen(true); - - updateSupportType(searchOn); - updateSearchBy(false); - updateKeywords(keywords); updatePages(page, maxPage); - - new Thread(new Runnable() { - @Override - public void run() { - BasicSearchable search = BasicSearchable - .getSearchable(searchOn); - - int maxPage = -1; - try { - maxPage = search.searchPages(keywords); - } catch (IOException e) { - error(e); - } - - if (page <= 0) { - updateBooks(new ArrayList()); - updatePages(0, maxPage); - } else { - List results; - try { - results = search.search(keywords, page); - } catch (IOException e) { - error(e); - results = new ArrayList(); - } - - search(results, page, maxPage, item); - - // ! 1-based index ! - if (item > 0 && item <= books.getBooksCount()) { - // TODO: "click" on item ITEM - } + searchPanel.setSupportType(searchOn); + + if (keywords != null) { + setWaitingScreen(true); + searchPanel.search(keywords, item, new Runnable() { + @Override + public void run() { + setWaitingScreen(false); } - - setWaitingScreen(false); - } - }).start(); + }); + } } // tag: null = base tags @@ -437,88 +180,14 @@ public class GuiReaderSearchFrame extends JFrame { final int item, final SearchableTag tag) { setWaitingScreen(true); - - updateSupportType(searchOn); - updateSearchBy(true); - updateTags(tag); updatePages(page, maxPage); - - new Thread(new Runnable() { + searchPanel.setSupportType(searchOn); + searchPanel.searchTag(tag, item, new Runnable() { @Override public void run() { - BasicSearchable search = BasicSearchable - .getSearchable(searchOn); - - if (tag != null) { - try { - search.fillTag(tag); - } catch (IOException e) { - error(e); - } - - int maxPage = 0; - try { - maxPage = search.searchPages(tag); - } catch (IOException e) { - error(e); - } - - updatePages(page, maxPage); - - if (page > 0) { - List metas = new ArrayList(); - - if (tag.isLeaf()) { - try { - metas = search.search(tag, page); - } catch (IOException e) { - error(e); - } - } else { - List subtags = tag.getChildren(); - if (item > 0 && item <= subtags.size()) { - SearchableTag subtag = subtags.get(item - 1); - try { - metas = search.search(subtag, page); - maxPage = subtag.getPages(); - } catch (IOException e) { - error(e); - } - } - } - - updatePages(page, maxPage); - search(metas, page, maxPage, item); - } - } - setWaitingScreen(false); } - }).start(); - } - - // item 0 = no selection, else = default selection - public void search(final List results, final int page, - final int maxPage, final int item) { - - updatePages(page, maxPage); - - if (page <= 0) { - updateBooks(new ArrayList()); - updatePages(0, maxPage); - } else { - List infos = new ArrayList(); - for (MetaData meta : results) { - infos.add(GuiReaderBookInfo.fromMeta(meta)); - } - - updateBooks(infos); - - // ! 1-based index ! - if (item > 0 && item <= books.getBooksCount()) { - // TODO: "click" on item ITEM - } - } + }); } /** @@ -560,7 +229,7 @@ public class GuiReaderSearchFrame extends JFrame { public void run() { GuiReaderSearchFrame.this.setEnabled(!waiting); books.setEnabled(!waiting); - submitKeywords.setEnabled(!waiting); + searchPanel.setEnabled(!waiting); } }); } -- 2.27.0