From 17fafa56087ec04ff386c6e56d38c51c98d71511 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Tue, 26 Mar 2019 22:09:58 +0100 Subject: [PATCH] step 2 --- .../fanfix/reader/ui/GuiReaderBook.java | 35 ++++- .../fanfix/reader/ui/GuiReaderFrame.java | 3 +- .../fanfix/reader/ui/GuiReaderGroup.java | 132 ++++++++++++++---- 3 files changed, 138 insertions(+), 32 deletions(-) diff --git a/src/be/nikiroo/fanfix/reader/ui/GuiReaderBook.java b/src/be/nikiroo/fanfix/reader/ui/GuiReaderBook.java index f0e1371..728964b 100644 --- a/src/be/nikiroo/fanfix/reader/ui/GuiReaderBook.java +++ b/src/be/nikiroo/fanfix/reader/ui/GuiReaderBook.java @@ -112,6 +112,9 @@ class GuiReaderBook extends JPanel { /** * The book current selection state. + *

+ * 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 @@ -120,6 +123,10 @@ class GuiReaderBook extends JPanel { if (this.selected != selected) { this.selected = selected; repaint(); + + if (selected) { + select(); + } } } @@ -193,12 +200,10 @@ class GuiReaderBook extends JPanel { } 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(); } } @@ -223,6 +228,24 @@ class GuiReaderBook extends JPanel { 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. * diff --git a/src/be/nikiroo/fanfix/reader/ui/GuiReaderFrame.java b/src/be/nikiroo/fanfix/reader/ui/GuiReaderFrame.java index 4c65972..131ab05 100644 --- a/src/be/nikiroo/fanfix/reader/ui/GuiReaderFrame.java +++ b/src/be/nikiroo/fanfix/reader/ui/GuiReaderFrame.java @@ -4,6 +4,7 @@ import java.awt.BorderLayout; import java.awt.Frame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowEvent; import java.io.File; @@ -78,7 +79,7 @@ class GuiReaderFrame extends JFrame implements FrameHelper { */ public GuiReaderFrame(GuiReader reader, String type) { super(getAppTitle(reader.getLibrary().getLibraryName())); - + this.reader = reader; mainPanel = new GuiReaderMainPanel(this, type); diff --git a/src/be/nikiroo/fanfix/reader/ui/GuiReaderGroup.java b/src/be/nikiroo/fanfix/reader/ui/GuiReaderGroup.java index 78e1d06..db21c49 100644 --- a/src/be/nikiroo/fanfix/reader/ui/GuiReaderGroup.java +++ b/src/be/nikiroo/fanfix/reader/ui/GuiReaderGroup.java @@ -5,6 +5,8 @@ import java.awt.Color; 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; @@ -58,6 +60,12 @@ public class GuiReaderGroup extends JPanel { } setLayout(new BorderLayout(0, 10)); + + // Make it focusable: + setFocusable(true); + setEnabled(true); + setVisible(true); + add(pane, BorderLayout.CENTER); if (title != null) { @@ -84,11 +92,31 @@ public class GuiReaderGroup extends JPanel { 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); + } + }); } /** @@ -159,6 +187,7 @@ public class GuiReaderGroup extends JPanel { book.addActionListener(new BookActionListener() { @Override public void select(GuiReaderBook book) { + GuiReaderGroup.this.requestFocusInWindow(); for (GuiReaderBook abook : books) { abook.setSelected(abook == book); } @@ -212,6 +241,57 @@ public class GuiReaderGroup extends JPanel { 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 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. * @@ -220,7 +300,28 @@ public class GuiReaderGroup extends JPanel { */ 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()) { @@ -231,44 +332,25 @@ public class GuiReaderGroup extends JPanel { 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); } } } -- 2.27.0