From: Niki Roo Date: Fri, 19 Apr 2019 22:40:34 +0000 (+0200) Subject: GUI search: beta quality X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=cf032e2963976f6c25c42c6729330397f90b8e57;p=fanfix-jexer.git GUI search: beta quality --- diff --git a/src/be/nikiroo/fanfix/reader/ui/GuiReaderNavBar.java b/src/be/nikiroo/fanfix/reader/ui/GuiReaderNavBar.java index 0f3d8dc..099b3c8 100644 --- a/src/be/nikiroo/fanfix/reader/ui/GuiReaderNavBar.java +++ b/src/be/nikiroo/fanfix/reader/ui/GuiReaderNavBar.java @@ -36,17 +36,20 @@ public class GuiReaderNavBar extends JPanel { * Create a new navigation bar. *

* The minimum must be lower or equal to the maximum. + *

+ * Note than a max of "-1" means "infinite". * * @param min - * the minimum page number + * the minimum page number (cannot be negative) * @param max - * the maximum page number + * the maximum page number (cannot be lower than min, except if + * -1 (infinite)) * * @throws IndexOutOfBoundsException - * if min > max + * if min > max and max is not "-1" */ public GuiReaderNavBar(int min, int max) { - if (min > max) { + if (min > max && max != -1) { throw new IndexOutOfBoundsException(String.format( "min (%d) > max (%d)", min, max)); } @@ -60,24 +63,28 @@ public class GuiReaderNavBar extends JPanel { @Override public void actionPerformed(ActionEvent e) { setIndex(GuiReaderNavBar.this.min); + fireEvent(); } }); navButtons[1] = createNavButton(" < ", new ActionListener() { @Override public void actionPerformed(ActionEvent e) { setIndex(index - 1); + fireEvent(); } }); navButtons[2] = createNavButton(" > ", new ActionListener() { @Override public void actionPerformed(ActionEvent e) { setIndex(index + 1); + fireEvent(); } }); navButtons[3] = createNavButton(">>", new ActionListener() { @Override public void actionPerformed(ActionEvent e) { setIndex(GuiReaderNavBar.this.max); + fireEvent(); } }); @@ -114,15 +121,14 @@ public class GuiReaderNavBar extends JPanel { * @param index * the new index */ - private void setIndex(int index) { + public void setIndex(int index) { if (index != this.index) { - if (index < min || index > max) { + if (index < min || (index > max && max != -1)) { throw new IndexOutOfBoundsException(String.format( "Index %d but min/max is [%d/%d]", index, min, max)); } this.index = index; - fireEvent(); updateLabel(); } @@ -130,7 +136,7 @@ public class GuiReaderNavBar extends JPanel { } /** - * The minimun page number. + * The minimun page number. Cannot be negative. * * @return the min */ @@ -139,7 +145,7 @@ public class GuiReaderNavBar extends JPanel { } /** - * The minimum page number. + * The minimum page number. Cannot be negative. *

* May update the index if needed (if the index is < the new min). *

@@ -153,17 +159,15 @@ public class GuiReaderNavBar extends JPanel { this.min = min; if (index < min) { index = min; - updateEnabled(); - updateLabel(); - fireEvent(); - } else { - updateEnabled(); - updateLabel(); } + updateEnabled(); + updateLabel(); + } /** - * The maximum page number. + * The maximum page number. Cannot be lower than min, except if -1 + * (infinite). * * @return the max */ @@ -172,7 +176,8 @@ public class GuiReaderNavBar extends JPanel { } /** - * The maximum page number. + * The maximum page number. Cannot be lower than min, except if -1 + * (infinite). *

* May update the index if needed (if the index is > the new max). *

@@ -184,15 +189,11 @@ public class GuiReaderNavBar extends JPanel { */ public void setMax(int max) { this.max = max; - if (index > max) { + if (index > max && max != -1) { index = max; - updateEnabled(); - updateLabel(); - fireEvent(); - } else { - updateEnabled(); - updateLabel(); } + updateEnabled(); + updateLabel(); } /** @@ -246,9 +247,9 @@ public class GuiReaderNavBar extends JPanel { } /** - * Notify a chnge of page. + * Notify a change of page. */ - private void fireEvent() { + public void fireEvent() { for (ActionListener listener : listeners) { try { listener.actionPerformed(new ActionEvent(this, @@ -289,8 +290,8 @@ public class GuiReaderNavBar extends JPanel { private void updateEnabled() { navButtons[0].setEnabled(index > min); navButtons[1].setEnabled(index > min); - navButtons[2].setEnabled(index < max); - navButtons[3].setEnabled(index < max); + navButtons[2].setEnabled(index < max || max == -1); + navButtons[3].setEnabled(index < max || max == -1); } /** @@ -313,7 +314,12 @@ public class GuiReaderNavBar extends JPanel { protected String computeLabel(int index, @SuppressWarnings("unused") int min, int max) { - String base = "  Page %d / %d"; + String base = "  Page %d "; + if (max >= 0) { + base += "/ %d"; + } + base += ""; + String ifLabel = ": %s"; String display = base; @@ -324,6 +330,10 @@ public class GuiReaderNavBar extends JPanel { display = "" + display + ""; - return String.format(display, index, max, label); + if (max >= 0) { + return String.format(display, index, max, label); + } + + return String.format(display, index, label); } } diff --git a/src/be/nikiroo/fanfix/reader/ui/GuiReaderSearchByNamePanel.java b/src/be/nikiroo/fanfix/reader/ui/GuiReaderSearchByNamePanel.java index 51db24c..ebdb21a 100644 --- a/src/be/nikiroo/fanfix/reader/ui/GuiReaderSearchByNamePanel.java +++ b/src/be/nikiroo/fanfix/reader/ui/GuiReaderSearchByNamePanel.java @@ -90,7 +90,7 @@ public class GuiReaderSearchByNamePanel extends JPanel { */ public void setSearchable(BasicSearchable searchable) { this.searchable = searchable; - page = 1; + page = 0; maxPage = -1; storyItem = 0; stories = new ArrayList(); diff --git a/src/be/nikiroo/fanfix/reader/ui/GuiReaderSearchByTagPanel.java b/src/be/nikiroo/fanfix/reader/ui/GuiReaderSearchByTagPanel.java index a654d16..260fc48 100644 --- a/src/be/nikiroo/fanfix/reader/ui/GuiReaderSearchByTagPanel.java +++ b/src/be/nikiroo/fanfix/reader/ui/GuiReaderSearchByTagPanel.java @@ -49,7 +49,7 @@ public class GuiReaderSearchByTagPanel extends JPanel { this.waitable = waitable; combos = new ArrayList(); - page = 1; + page = 0; maxPage = -1; tagBars = new JPanel(); @@ -69,7 +69,7 @@ public class GuiReaderSearchByTagPanel extends JPanel { */ public void setSearchable(BasicSearchable searchable) { this.searchable = searchable; - page = 1; + page = 0; maxPage = -1; storyItem = 0; stories = new ArrayList(); @@ -298,6 +298,7 @@ public class GuiReaderSearchByTagPanel extends JPanel { page = 1; stories = searchable.search(tag, 1); maxPage = searchable.searchPages(tag); + currentTag = tag; } catch (IOException e) { GuiReaderSearchFrame.error(e); page = 0; diff --git a/src/be/nikiroo/fanfix/reader/ui/GuiReaderSearchFrame.java b/src/be/nikiroo/fanfix/reader/ui/GuiReaderSearchFrame.java index 11d45e4..5b99772 100644 --- a/src/be/nikiroo/fanfix/reader/ui/GuiReaderSearchFrame.java +++ b/src/be/nikiroo/fanfix/reader/ui/GuiReaderSearchFrame.java @@ -34,12 +34,11 @@ public class GuiReaderSearchFrame extends JFrame { private static final long serialVersionUID = 1L; private List supportTypes; - private int page; - private int maxPage; private JComboBox comboSupportTypes; private ActionListener comboSupportTypesListener; private GuiReaderSearchByPanel searchPanel; + private GuiReaderNavBar navbar; private boolean seeWordcount; private GuiReaderGroup books; @@ -49,9 +48,6 @@ public class GuiReaderSearchFrame extends JFrame { setLayout(new BorderLayout()); setSize(800, 600); - page = 1; - maxPage = -1; - supportTypes = new ArrayList(); supportTypes.add(null); for (SupportType type : SupportType.values()) { @@ -103,6 +99,16 @@ public class GuiReaderSearchFrame extends JFrame { infos.add(GuiReaderBookInfo.fromMeta(meta)); } + int page = searchPanel.getPage(); + if (page <= 0) { + navbar.setMin(1); + navbar.setMax(1); + } else { + int max = searchPanel.getMaxPage(); + navbar.setMin(1); + navbar.setMax(max); + navbar.setIndex(page); + } updateBooks(infos); // ! 1-based index ! @@ -139,6 +145,27 @@ public class GuiReaderSearchFrame extends JFrame { JScrollPane scroll = new JScrollPane(books); scroll.getVerticalScrollBar().setUnitIncrement(16); add(scroll, BorderLayout.CENTER); + + navbar = new GuiReaderNavBar(-1, -1) { + private static final long serialVersionUID = 1L; + + @Override + protected String computeLabel(int index, int min, int max) { + if (index <= 0) { + return ""; + } + return super.computeLabel(index, min, max); + } + }; + + navbar.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + searchPanel.setPage(navbar.getIndex()); + } + }); + + add(navbar, BorderLayout.SOUTH); } /** @@ -162,7 +189,6 @@ public class GuiReaderSearchFrame extends JFrame { .removeActionListener(comboSupportTypesListener); comboSupportTypes.setSelectedItem(supportType); comboSupportTypes.addActionListener(comboSupportTypesListener); - } }); @@ -185,12 +211,14 @@ public class GuiReaderSearchFrame extends JFrame { inUi(new Runnable() { @Override public void run() { - GuiReaderSearchFrame.this.page = page; - GuiReaderSearchFrame.this.maxPage = maxPage; - - // TODO: gui - System.out.println("page: " + page); - System.out.println("max page: " + maxPage); + if (maxPage >= 1) { + navbar.setMin(1); + navbar.setMax(maxPage); + navbar.setIndex(page); + } else { + navbar.setMin(-1); + navbar.setMax(-1); + } } }); }