* Create a new navigation bar.
* <p>
* The minimum must be lower or equal to the maximum.
+ * <p>
+ * 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));
}
@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();
}
});
* @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();
}
}
/**
- * The minimun page number.
+ * The minimun page number. Cannot be negative.
*
* @return the min
*/
}
/**
- * The minimum page number.
+ * The minimum page number. Cannot be negative.
* <p>
* May update the index if needed (if the index is < the new min).
* <p>
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
*/
}
/**
- * The maximum page number.
+ * The maximum page number. Cannot be lower than min, except if -1
+ * (infinite).
* <p>
* May update the index if needed (if the index is > the new max).
* <p>
*/
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();
}
/**
}
/**
- * 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,
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);
}
/**
protected String computeLabel(int index,
@SuppressWarnings("unused") int min, int max) {
- String base = " <B>Page <SPAN COLOR='#444466'>%d</SPAN> / %d</B>";
+ String base = " <B>Page <SPAN COLOR='#444466'>%d</SPAN> ";
+ if (max >= 0) {
+ base += "/ %d";
+ }
+ base += "</B>";
+
String ifLabel = ": %s";
String display = base;
display = "<HTML>" + display + "</HTML>";
- return String.format(display, index, max, label);
+ if (max >= 0) {
+ return String.format(display, index, max, label);
+ }
+
+ return String.format(display, index, label);
}
}
private static final long serialVersionUID = 1L;
private List<SupportType> 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;
setLayout(new BorderLayout());
setSize(800, 600);
- page = 1;
- maxPage = -1;
-
supportTypes = new ArrayList<SupportType>();
supportTypes.add(null);
for (SupportType type : SupportType.values()) {
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 !
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);
}
/**
.removeActionListener(comboSupportTypesListener);
comboSupportTypes.setSelectedItem(supportType);
comboSupportTypes.addActionListener(comboSupportTypesListener);
-
}
});
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);
+ }
}
});
}