+ /**
+ * Return the index of the currently selected book if any, -1 if none.
+ *
+ * @return the index or -1
+ */
+ private int getSelectedBookIndex() {
+ int index = -1;
+ for (int i = 0; i < books.size(); i++) {
+ if (books.get(i).isSelected()) {
+ index = i;
+ break;
+ }
+ }
+ return index;
+ }
+
+ /**
+ * Select the given book, or unselect all items.
+ *
+ * @param index
+ * the index of the book to select, can be outside the bounds
+ * (either all the items will be unselected or the first or last
+ * book will then be selected, see <tt>forceRange>/tt>)
+ * @param forceRange
+ * TRUE to constraint the index to the first/last element, FALSE
+ * to unselect when outside the range
+ */
+ private void setSelectedBook(int index, boolean forceRange) {
+ int previousIndex = getSelectedBookIndex();
+
+ if (index >= books.size()) {
+ if (forceRange) {
+ index = books.size() - 1;
+ } else {
+ index = -1;
+ }
+ }
+
+ if (index < 0 && forceRange) {
+ index = 0;
+ }
+
+ if (previousIndex >= 0) {
+ books.get(previousIndex).setSelected(false);
+ }
+
+ if (index >= 0 && !books.isEmpty()) {
+ books.get(index).setSelected(true);
+ }
+ }
+