keyboard: step 1
authorNiki Roo <niki@nikiroo.be>
Tue, 26 Mar 2019 18:53:53 +0000 (19:53 +0100)
committerNiki Roo <niki@nikiroo.be>
Tue, 26 Mar 2019 18:53:53 +0000 (19:53 +0100)
src/be/nikiroo/fanfix/reader/ui/GuiReaderGroup.java

index 3e09aeaf03efad3d2fac4ec00b1b8eced26549b6..78e1d06a473bc91b8cafcff9147bd3f2325d85ed 100644 (file)
@@ -3,6 +3,10 @@ package be.nikiroo.fanfix.reader.ui;
 import java.awt.BorderLayout;
 import java.awt.Color;
 import java.awt.event.ActionListener;
+import java.awt.event.ComponentAdapter;
+import java.awt.event.ComponentEvent;
+import java.awt.event.KeyAdapter;
+import java.awt.event.KeyEvent;
 import java.awt.event.MouseEvent;
 import java.util.ArrayList;
 import java.util.List;
@@ -28,6 +32,7 @@ public class GuiReaderGroup extends JPanel {
        private List<GuiReaderBook> books;
        private JPanel pane;
        private boolean words; // words or authors (secondary info on books)
+       private int itemsPerLine;
 
        /**
         * Create a new {@link GuiReaderGroup}.
@@ -67,6 +72,32 @@ public class GuiReaderGroup extends JPanel {
                        label.setHorizontalAlignment(JLabel.CENTER);
                        add(label, BorderLayout.NORTH);
                }
+
+               // Compute the number of items per line at each resize
+               addComponentListener(new ComponentAdapter() {
+                       @Override
+                       public void componentResized(ComponentEvent e) {
+                               super.componentResized(e);
+                               computeItemsPerLine();
+                       }
+               });
+               computeItemsPerLine();
+
+               addKeyListener(new KeyAdapter() {
+                       @Override
+                       public void keyTyped(KeyEvent e) {
+                               onKeyTyped(e);
+                       }
+               });
+       }
+
+       /**
+        * Compute how many items can fit in a line so UP and DOWN can be used to go
+        * up/down one line at a time.
+        */
+       private void computeItemsPerLine() {
+               // TODO
+               itemsPerLine = 5;
        }
 
        /**
@@ -180,4 +211,64 @@ public class GuiReaderGroup extends JPanel {
                super.setEnabled(b);
                repaint();
        }
+
+       /**
+        * The action to execute when a key is typed.
+        * 
+        * @param e
+        *            the key event
+        */
+       private void onKeyTyped(KeyEvent e) {
+               boolean consumed = false;
+               System.out.println(e);
+               if (e.isActionKey()) {
+                       int offset = 0;
+                       switch (e.getKeyCode()) {
+                       case KeyEvent.VK_LEFT:
+                               offset = -1;
+                               break;
+                       case KeyEvent.VK_RIGHT:
+                               offset = 1;
+                               break;
+                       case KeyEvent.VK_UP:
+                               offset = itemsPerLine;
+                               break;
+                       case KeyEvent.VK_DOWN:
+                               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);
+                                               }
+                                       }
+                               }
+                       }
+               }
+
+               if (consumed) {
+                       e.consume();
+               } else {
+                       super.processKeyEvent(e);
+               }
+       }
 }