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;
-import javax.management.RuntimeErrorException;
import javax.swing.JLabel;
import javax.swing.JPanel;
-import be.nikiroo.fanfix.data.MetaData;
+import be.nikiroo.fanfix.bundles.StringIdGui;
import be.nikiroo.fanfix.reader.ui.GuiReaderBook.BookActionListener;
import be.nikiroo.utils.ui.WrapLayout;
private BookActionListener action;
private Color backgroundColor;
private GuiReader reader;
- private List<MetaData> stories;
+ private List<GuiReaderBookInfo> infos;
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}.
if (title != null) {
if (title.isEmpty()) {
- title = "[unknown]";
+ title = GuiReader.trans(StringIdGui.MENU_AUTHORS_UNKNOWN);
}
JLabel label = new JLabel();
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;
}
/**
*/
public void setActionListener(BookActionListener action) {
this.action = action;
- refreshBooks(stories, words);
+ refreshBooks(infos, words);
}
/**
* 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> 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(List<MetaData> stories, boolean seeWordcount) {
- this.stories = stories;
+ public void refreshBooks(boolean seeWordcount) {
this.words = seeWordcount;
books = new ArrayList<GuiReaderBook>();
pane.invalidate();
pane.removeAll();
- if (stories != null) {
- for (MetaData meta : stories) {
- GuiReaderBook book = new GuiReaderBook(reader, meta,
- reader.isCached(meta.getLuid()), seeWordcount);
+ 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,
+ words);
if (backgroundColor != null) {
book.setBackground(backgroundColor);
}
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);
+ }
+ }
}