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.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
-import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
private List<GuiReaderBookInfo> infos;
private List<GuiReaderBook> books;
private JPanel pane;
+ private JLabel titleLabel;
private boolean words; // words or authors (secondary info on books)
private int itemsPerLine;
* 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)
*/
add(pane, BorderLayout.CENTER);
- if (title != null) {
- if (title.isEmpty()) {
- title = GuiReader.trans(StringIdGui.MENU_AUTHORS_UNKNOWN);
- }
-
- JLabel label = new JLabel();
- label.setText(String.format("<html>"
- + "<body style='text-align: center; color: gray;'><br><b>"
- + "%s" + "</b></body>" + "</html>", title));
- label.setHorizontalAlignment(JLabel.CENTER);
- add(label, BorderLayout.NORTH);
- }
+ 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() {
});
}
+ /**
+ * 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("<html>"
+ + "<body style='text-align: center; color: gray;'><br><b>"
+ + "%s" + "</b></body>" + "</html>", title));
+ titleLabel.setVisible(true);
+ } else {
+ titleLabel.setVisible(false);
+ }
+ }
+
/**
* 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.
}
@Override
- public void popupRequested(GuiReaderBook book, MouseEvent e) {
+ public void popupRequested(GuiReaderBook book,
+ Component target, int x, int y) {
}
@Override
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
*/
- private int getSelectedBookIndex() {
+ public int getSelectedBookIndex() {
int index = -1;
for (int i = 0; i < books.size(); i++) {
if (books.get(i).isSelected()) {
* TRUE to constraint the index to the first/last element, FALSE
* to unselect when outside the range
*/
- private void setSelectedBook(int index, boolean forceRange) {
+ public void setSelectedBook(int index, boolean forceRange) {
int previousIndex = getSelectedBookIndex();
if (index >= books.size()) {
books.get(previousIndex).setSelected(false);
}
- if (index >= 0) {
+ if (index >= 0 && !books.isEmpty()) {
books.get(index).setSelected(true);
}
}
*/
private void onKeyTyped(KeyEvent e) {
boolean consumed = false;
- if (e.getKeyChar() == '\n') {
+ boolean action = e.getKeyChar() == '\n';
+ boolean popup = e.getKeyChar() == ' ';
+ if (action || popup) {
consumed = true;
int index = getSelectedBookIndex();
if (index >= 0) {
- books.get(index).action();
+ GuiReaderBook book = books.get(index);
+ if (action) {
+ book.action();
+ } else if (popup) {
+ book.popup(book, book.getWidth() / 2, book.getHeight() / 2);
+ }
}
}