/**
* The book current selection state.
+ * <p>
+ * Setting this value to true can cause a "select" action to occur if the
+ * previous state was "unselected".
*
* @param selected
* TRUE if it is selected
if (this.selected != selected) {
this.selected = selected;
repaint();
+
+ if (selected) {
+ select();
+ }
}
}
}
private void click(boolean doubleClick) {
- for (BookActionListener listener : listeners) {
- if (doubleClick) {
- listener.action(GuiReaderBook.this);
- } else {
- listener.select(GuiReaderBook.this);
- }
+ if (doubleClick) {
+ action();
+ } else {
+ select();
}
}
listeners.add(listener);
}
+ /**
+ * Cause an action to occur on this {@link GuiReaderBook}.
+ */
+ public void action() {
+ for (BookActionListener listener : listeners) {
+ listener.action(GuiReaderBook.this);
+ }
+ }
+
+ /**
+ * Cause a select event on this {@link GuiReaderBook}.
+ */
+ private void select() {
+ for (BookActionListener listener : listeners) {
+ listener.select(GuiReaderBook.this);
+ }
+ }
+
/**
* The information about the book represented by this item.
*
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
+import java.awt.event.FocusAdapter;
+import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
}
setLayout(new BorderLayout(0, 10));
+
+ // Make it focusable:
+ setFocusable(true);
+ setEnabled(true);
+ setVisible(true);
+
add(pane, BorderLayout.CENTER);
if (title != null) {
computeItemsPerLine();
addKeyListener(new KeyAdapter() {
+ @Override
+ public void keyPressed(KeyEvent e) {
+ onKeyPressed(e);
+ }
+
@Override
public void keyTyped(KeyEvent e) {
onKeyTyped(e);
}
});
+
+ addFocusListener(new FocusAdapter() {
+ @Override
+ public void focusGained(FocusEvent e) {
+ if (getSelectedBookIndex() < 0) {
+ setSelectedBook(0, true);
+ }
+ }
+
+ @Override
+ public void focusLost(FocusEvent e) {
+ setBackground(null);
+ setSelectedBook(-1, false);
+ }
+ });
}
/**
book.addActionListener(new BookActionListener() {
@Override
public void select(GuiReaderBook book) {
+ GuiReaderGroup.this.requestFocusInWindow();
for (GuiReaderBook abook : books) {
abook.setSelected(abook == book);
}
repaint();
}
+ /**
+ * 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.get(index).setSelected(true);
+ }
+ }
+
/**
* The action to execute when a key is typed.
*
*/
private void onKeyTyped(KeyEvent e) {
boolean consumed = false;
- System.out.println(e);
+ if (e.getKeyChar() == '\n') {
+ consumed = true;
+
+ int index = getSelectedBookIndex();
+ if (index >= 0) {
+ books.get(index).action();
+ }
+ }
+
+ if (consumed) {
+ e.consume();
+ }
+ }
+
+ /**
+ * The action to execute when a key is pressed.
+ *
+ * @param e
+ * the key event
+ */
+ private void onKeyPressed(KeyEvent e) {
+ boolean consumed = false;
if (e.isActionKey()) {
int offset = 0;
switch (e.getKeyCode()) {
offset = 1;
break;
case KeyEvent.VK_UP:
- offset = itemsPerLine;
+ offset = -itemsPerLine;
break;
case KeyEvent.VK_DOWN:
- offset = -itemsPerLine;
+ offset = itemsPerLine;
break;
}
if (offset != 0) {
consumed = true;
- int selected = -1;
- for (int i = 0; i < books.size(); i++) {
- if (books.get(i).isSelected()) {
- selected = i;
- break;
- }
- }
-
- if (selected >= 0) {
- int newSelect = selected + offset;
- if (newSelect >= books.size()) {
- newSelect = books.size() - 1;
- }
-
- if (selected != newSelect && newSelect >= 0) {
- if (selected >= 0) {
- books.get(selected).setSelected(false);
- books.get(newSelect).setSelected(true);
- }
- }
+ int previousIndex = getSelectedBookIndex();
+ if (previousIndex >= 0) {
+ setSelectedBook(previousIndex + offset, true);
}
}
}
if (consumed) {
e.consume();
- } else {
- super.processKeyEvent(e);
}
}
}