X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Freader%2Fui%2FGuiReaderNavBar.java;h=43c1a993153028314fcaf2a96b8a2acb759f221a;hb=1387a30ab59dbf4071f2c5e5e0e08ca98c75b726;hp=0f3d8dc67ba6296629262e0b79399a0601a33670;hpb=fb4753428d43f0f4ca06523efbeab887164b5c91;p=fanfix.git diff --git a/src/be/nikiroo/fanfix/reader/ui/GuiReaderNavBar.java b/src/be/nikiroo/fanfix/reader/ui/GuiReaderNavBar.java index 0f3d8dc..43c1a99 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)); } @@ -54,30 +57,38 @@ public class GuiReaderNavBar extends JPanel { LayoutManager layout = new BoxLayout(this, BoxLayout.X_AXIS); setLayout(layout); + // TODO: + // JButton up = new BasicArrowButton(BasicArrowButton.NORTH); + // JButton down = new BasicArrowButton(BasicArrowButton.SOUTH); + navButtons = new JButton[4]; navButtons[0] = createNavButton("<<", new ActionListener() { @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 +125,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 +140,7 @@ public class GuiReaderNavBar extends JPanel { } /** - * The minimun page number. + * The minimun page number. Cannot be negative. * * @return the min */ @@ -139,7 +149,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 +163,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 +180,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 +193,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,15 +251,15 @@ 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, ActionEvent.ACTION_FIRST, "page changed")); } catch (Exception e) { - Instance.getTraceHandler().error(e); + Instance.getInstance().getTraceHandler().error(e); } } } @@ -289,8 +294,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 +318,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 +334,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); } }