step 2
authorNiki Roo <niki@nikiroo.be>
Tue, 26 Mar 2019 21:09:58 +0000 (22:09 +0100)
committerNiki Roo <niki@nikiroo.be>
Tue, 26 Mar 2019 21:09:58 +0000 (22:09 +0100)
src/be/nikiroo/fanfix/reader/ui/GuiReaderBook.java
src/be/nikiroo/fanfix/reader/ui/GuiReaderFrame.java
src/be/nikiroo/fanfix/reader/ui/GuiReaderGroup.java

index f0e1371eda728fa02504d5a602576f4244bedbbb..728964baa3faa82676f3b7a6fb6c875b6529bebf 100644 (file)
@@ -112,6 +112,9 @@ class GuiReaderBook extends JPanel {
 
        /**
         * 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
@@ -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.
         * 
index 4c65972a2d81e710eb636bdbd7a8894335020d17..131ab0509e570379be119e40c24c17a34764f029 100644 (file)
@@ -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);
index 78e1d06a473bc91b8cafcff9147bd3f2325d85ed..db21c49c8c0d9414c4be0d6e3b1d4f41c043ed52 100644 (file)
@@ -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 <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.
         * 
@@ -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);
                }
        }
 }