1 package be
.nikiroo
.fanfix
.reader
.ui
;
3 import java
.awt
.BorderLayout
;
5 import java
.awt
.event
.ActionListener
;
6 import java
.awt
.event
.ComponentAdapter
;
7 import java
.awt
.event
.ComponentEvent
;
8 import java
.awt
.event
.FocusAdapter
;
9 import java
.awt
.event
.FocusEvent
;
10 import java
.awt
.event
.KeyAdapter
;
11 import java
.awt
.event
.KeyEvent
;
12 import java
.awt
.event
.MouseEvent
;
13 import java
.util
.ArrayList
;
14 import java
.util
.List
;
16 import javax
.swing
.JLabel
;
17 import javax
.swing
.JPanel
;
19 import be
.nikiroo
.fanfix
.bundles
.StringIdGui
;
20 import be
.nikiroo
.fanfix
.reader
.ui
.GuiReaderBook
.BookActionListener
;
21 import be
.nikiroo
.utils
.ui
.WrapLayout
;
24 * A group of {@link GuiReaderBook}s for display.
28 public class GuiReaderGroup
extends JPanel
{
29 private static final long serialVersionUID
= 1L;
30 private BookActionListener action
;
31 private Color backgroundColor
;
32 private GuiReader reader
;
33 private List
<GuiReaderBookInfo
> infos
;
34 private List
<GuiReaderBook
> books
;
36 private boolean words
; // words or authors (secondary info on books)
37 private int itemsPerLine
;
40 * Create a new {@link GuiReaderGroup}.
43 * the {@link GuiReaderBook} used to probe some information about
46 * the title of this group
47 * @param backgroundColor
48 * the background colour to use (or NULL for default)
50 public GuiReaderGroup(GuiReader reader
, String title
, Color backgroundColor
) {
52 this.backgroundColor
= backgroundColor
;
54 this.pane
= new JPanel();
56 pane
.setLayout(new WrapLayout(WrapLayout
.LEADING
, 5, 5));
57 if (backgroundColor
!= null) {
58 pane
.setBackground(backgroundColor
);
59 setBackground(backgroundColor
);
62 setLayout(new BorderLayout(0, 10));
69 add(pane
, BorderLayout
.CENTER
);
72 if (title
.isEmpty()) {
73 title
= GuiReader
.trans(StringIdGui
.MENU_AUTHORS_UNKNOWN
);
76 JLabel label
= new JLabel();
77 label
.setText(String
.format("<html>"
78 + "<body style='text-align: center; color: gray;'><br><b>"
79 + "%s" + "</b></body>" + "</html>", title
));
80 label
.setHorizontalAlignment(JLabel
.CENTER
);
81 add(label
, BorderLayout
.NORTH
);
84 // Compute the number of items per line at each resize
85 addComponentListener(new ComponentAdapter() {
87 public void componentResized(ComponentEvent e
) {
88 super.componentResized(e
);
89 computeItemsPerLine();
92 computeItemsPerLine();
94 addKeyListener(new KeyAdapter() {
96 public void keyPressed(KeyEvent e
) {
101 public void keyTyped(KeyEvent e
) {
106 addFocusListener(new FocusAdapter() {
108 public void focusGained(FocusEvent e
) {
109 if (getSelectedBookIndex() < 0) {
110 setSelectedBook(0, true);
115 public void focusLost(FocusEvent e
) {
117 setSelectedBook(-1, false);
123 * Compute how many items can fit in a line so UP and DOWN can be used to go
124 * up/down one line at a time.
126 private void computeItemsPerLine() {
132 * Set the {@link ActionListener} that will be fired on each
133 * {@link GuiReaderBook} action.
138 public void setActionListener(BookActionListener action
) {
139 this.action
= action
;
140 refreshBooks(infos
, words
);
144 * Refresh the list of {@link GuiReaderBook}s displayed in the control.
147 * the new list of infos
148 * @param seeWordcount
149 * TRUE to see word counts, FALSE to see authors
151 public void refreshBooks(List
<GuiReaderBookInfo
> infos
, boolean seeWordcount
) {
153 refreshBooks(seeWordcount
);
157 * Refresh the list of {@link GuiReaderBook}s displayed in the control.
159 * Will not change the current stories.
161 * @param seeWordcount
162 * TRUE to see word counts, FALSE to see authors
164 public void refreshBooks(boolean seeWordcount
) {
165 this.words
= seeWordcount
;
167 books
= new ArrayList
<GuiReaderBook
>();
173 for (GuiReaderBookInfo info
: infos
) {
174 boolean isCached
= false;
175 if (info
.getMeta() != null) {
176 isCached
= reader
.isCached(info
.getMeta().getLuid());
179 GuiReaderBook book
= new GuiReaderBook(reader
, info
, isCached
,
181 if (backgroundColor
!= null) {
182 book
.setBackground(backgroundColor
);
187 book
.addActionListener(new BookActionListener() {
189 public void select(GuiReaderBook book
) {
190 GuiReaderGroup
.this.requestFocusInWindow();
191 for (GuiReaderBook abook
: books
) {
192 abook
.setSelected(abook
== book
);
197 public void popupRequested(GuiReaderBook book
, MouseEvent e
) {
201 public void action(GuiReaderBook book
) {
205 if (action
!= null) {
206 book
.addActionListener(action
);
220 * Enables or disables this component, depending on the value of the
221 * parameter <code>b</code>. An enabled component can respond to user input
222 * and generate events. Components are enabled initially by default.
224 * Disabling this component will also affect its children.
227 * If <code>true</code>, this component is enabled; otherwise
228 * this component is disabled
231 public void setEnabled(boolean b
) {
233 for (GuiReaderBook book
: books
) {
245 * Return the index of the currently selected book if any, -1 if none.
247 * @return the index or -1
249 private int getSelectedBookIndex() {
251 for (int i
= 0; i
< books
.size(); i
++) {
252 if (books
.get(i
).isSelected()) {
261 * Select the given book, or unselect all items.
264 * the index of the book to select, can be outside the bounds
265 * (either all the items will be unselected or the first or last
266 * book will then be selected, see <tt>forceRange>/tt>)
268 * TRUE to constraint the index to the first/last element, FALSE
269 * to unselect when outside the range
271 private void setSelectedBook(int index
, boolean forceRange
) {
272 int previousIndex
= getSelectedBookIndex();
274 if (index
>= books
.size()) {
276 index
= books
.size() - 1;
282 if (index
< 0 && forceRange
) {
286 if (previousIndex
>= 0) {
287 books
.get(previousIndex
).setSelected(false);
291 books
.get(index
).setSelected(true);
296 * The action to execute when a key is typed.
301 private void onKeyTyped(KeyEvent e
) {
302 boolean consumed
= false;
303 if (e
.getKeyChar() == '\n') {
306 int index
= getSelectedBookIndex();
308 books
.get(index
).action();
318 * The action to execute when a key is pressed.
323 private void onKeyPressed(KeyEvent e
) {
324 boolean consumed
= false;
325 if (e
.isActionKey()) {
327 switch (e
.getKeyCode()) {
328 case KeyEvent
.VK_LEFT
:
331 case KeyEvent
.VK_RIGHT
:
335 offset
= -itemsPerLine
;
337 case KeyEvent
.VK_DOWN
:
338 offset
= itemsPerLine
;
345 int previousIndex
= getSelectedBookIndex();
346 if (previousIndex
>= 0) {
347 setSelectedBook(previousIndex
+ offset
, true);