keyboard: step 1
[fanfix.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderGroup.java
index 8d766f22c0a5366028753e5f92382d76ed11c3e4..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;
@@ -10,6 +14,7 @@ import java.util.List;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 
+import be.nikiroo.fanfix.bundles.StringIdGui;
 import be.nikiroo.fanfix.reader.ui.GuiReaderBook.BookActionListener;
 import be.nikiroo.utils.ui.WrapLayout;
 
@@ -27,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}.
@@ -56,7 +62,7 @@ public class GuiReaderGroup extends JPanel {
 
                if (title != null) {
                        if (title.isEmpty()) {
-                               title = "[unknown]";
+                               title = GuiReader.trans(StringIdGui.MENU_AUTHORS_UNKNOWN);
                        }
 
                        JLabel label = new JLabel();
@@ -66,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;
        }
 
        /**
@@ -83,14 +115,25 @@ public class GuiReaderGroup extends JPanel {
        /**
         * Refresh the list of {@link GuiReaderBook}s displayed in the control.
         * 
-        * @param stories
-        *            the stories
+        * @param infos
+        *            the new list of infos
         * @param seeWordcount
         *            TRUE to see word counts, FALSE to see authors
         */
-       public void refreshBooks(List<GuiReaderBookInfo> stories,
-                       boolean seeWordcount) {
-               this.infos = stories;
+       public void refreshBooks(List<GuiReaderBookInfo> infos, boolean seeWordcount) {
+               this.infos = infos;
+               refreshBooks(seeWordcount);
+       }
+
+       /**
+        * Refresh the list of {@link GuiReaderBook}s displayed in the control.
+        * <p>
+        * Will not change the current stories.
+        * 
+        * @param seeWordcount
+        *            TRUE to see word counts, FALSE to see authors
+        */
+       public void refreshBooks(boolean seeWordcount) {
                this.words = seeWordcount;
 
                books = new ArrayList<GuiReaderBook>();
@@ -98,15 +141,15 @@ public class GuiReaderGroup extends JPanel {
                pane.invalidate();
                pane.removeAll();
 
-               if (stories != null) {
-                       for (GuiReaderBookInfo info : stories) {
+               if (infos != null) {
+                       for (GuiReaderBookInfo info : infos) {
                                boolean isCached = false;
                                if (info.getMeta() != null) {
                                        isCached = reader.isCached(info.getMeta().getLuid());
                                }
 
                                GuiReaderBook book = new GuiReaderBook(reader, info, isCached,
-                                               seeWordcount);
+                                               words);
                                if (backgroundColor != null) {
                                        book.setBackground(backgroundColor);
                                }
@@ -168,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);
+               }
+       }
 }