X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Freader%2Fui%2FGuiReaderGroup.java;h=3e86fba9d0a7a3677a73323ea9d44c4e35a86629;hb=b31a0db030e164bc1d9a8620e71b595dad31adb6;hp=78e1d06a473bc91b8cafcff9147bd3f2325d85ed;hpb=07e0fc1e184a10bb85866b20527e28f6329e64b3;p=nikiroo-utils.git diff --git a/src/be/nikiroo/fanfix/reader/ui/GuiReaderGroup.java b/src/be/nikiroo/fanfix/reader/ui/GuiReaderGroup.java index 78e1d06..3e86fba 100644 --- a/src/be/nikiroo/fanfix/reader/ui/GuiReaderGroup.java +++ b/src/be/nikiroo/fanfix/reader/ui/GuiReaderGroup.java @@ -2,12 +2,14 @@ package be.nikiroo.fanfix.reader.ui; import java.awt.BorderLayout; import java.awt.Color; +import java.awt.Component; 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; import java.util.ArrayList; import java.util.List; @@ -31,6 +33,7 @@ public class GuiReaderGroup extends JPanel { private List infos; private List books; private JPanel pane; + private JLabel titleLabel; private boolean words; // words or authors (secondary info on books) private int itemsPerLine; @@ -41,7 +44,8 @@ public class GuiReaderGroup extends JPanel { * the {@link GuiReaderBook} used to probe some information about * the stories * @param title - * the title of this group + * the title of this group (can be NULL for "no title", an empty + * {@link String} will trigger a default title for empty groups) * @param backgroundColor * the background colour to use (or NULL for default) */ @@ -58,20 +62,18 @@ public class GuiReaderGroup extends JPanel { } setLayout(new BorderLayout(0, 10)); - add(pane, BorderLayout.CENTER); - if (title != null) { - if (title.isEmpty()) { - title = GuiReader.trans(StringIdGui.MENU_AUTHORS_UNKNOWN); - } + // Make it focusable: + setFocusable(true); + setEnabled(true); + setVisible(true); - JLabel label = new JLabel(); - label.setText(String.format("" - + "
" - + "%s" + "" + "", title)); - label.setHorizontalAlignment(JLabel.CENTER); - add(label, BorderLayout.NORTH); - } + add(pane, BorderLayout.CENTER); + + titleLabel = new JLabel(); + titleLabel.setHorizontalAlignment(JLabel.CENTER); + add(titleLabel, BorderLayout.NORTH); + setTitle(title); // Compute the number of items per line at each resize addComponentListener(new ComponentAdapter() { @@ -84,11 +86,53 @@ 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); + } + }); + } + + /** + * The title of this group (can be NULL for "no title", an empty + * {@link String} will trigger a default title for empty groups) + * + * @param title + * the title or NULL + */ + public void setTitle(String title) { + if (title != null) { + if (title.isEmpty()) { + title = GuiReader.trans(StringIdGui.MENU_AUTHORS_UNKNOWN); + } + + titleLabel.setText(String.format("" + + "
" + + "%s" + "" + "", title)); + titleLabel.setVisible(true); + } else { + titleLabel.setVisible(false); + } } /** @@ -144,7 +188,7 @@ public class GuiReaderGroup extends JPanel { if (infos != null) { for (GuiReaderBookInfo info : infos) { boolean isCached = false; - if (info.getMeta() != null) { + if (info.getMeta() != null && info.getMeta().getLuid() != null) { isCached = reader.isCached(info.getMeta().getLuid()); } @@ -159,13 +203,15 @@ 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); } } @Override - public void popupRequested(GuiReaderBook book, MouseEvent e) { + public void popupRequested(GuiReaderBook book, + Component target, int x, int y) { } @Override @@ -212,6 +258,66 @@ public class GuiReaderGroup extends JPanel { repaint(); } + /** + * The number of books in this group. + * + * @return the count + */ + public int getBooksCount() { + return books.size(); + } + + /** + * Return the index of the currently selected book if any, -1 if none. + * + * @return the index or -1 + */ + public 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 + */ + public 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.isEmpty()) { + books.get(index).setSelected(true); + } + } + /** * The action to execute when a key is typed. * @@ -220,7 +326,35 @@ public class GuiReaderGroup extends JPanel { */ private void onKeyTyped(KeyEvent e) { boolean consumed = false; - System.out.println(e); + boolean action = e.getKeyChar() == '\n'; + boolean popup = e.getKeyChar() == ' '; + if (action || popup) { + consumed = true; + + int index = getSelectedBookIndex(); + if (index >= 0) { + GuiReaderBook book = books.get(index); + if (action) { + book.action(); + } else if (popup) { + book.popup(book, book.getWidth() / 2, book.getHeight() / 2); + } + } + } + + 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 +365,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); } } }