1 package be
.nikiroo
.fanfix
.reader
.ui
;
3 import java
.awt
.BorderLayout
;
5 import java
.awt
.Component
;
6 import java
.awt
.event
.ActionListener
;
7 import java
.awt
.event
.ComponentAdapter
;
8 import java
.awt
.event
.ComponentEvent
;
9 import java
.awt
.event
.FocusAdapter
;
10 import java
.awt
.event
.FocusEvent
;
11 import java
.awt
.event
.KeyAdapter
;
12 import java
.awt
.event
.KeyEvent
;
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
,
198 Component target
, int x
, int y
) {
202 public void action(GuiReaderBook book
) {
206 if (action
!= null) {
207 book
.addActionListener(action
);
221 * Enables or disables this component, depending on the value of the
222 * parameter <code>b</code>. An enabled component can respond to user input
223 * and generate events. Components are enabled initially by default.
225 * Disabling this component will also affect its children.
228 * If <code>true</code>, this component is enabled; otherwise
229 * this component is disabled
232 public void setEnabled(boolean b
) {
234 for (GuiReaderBook book
: books
) {
246 * Return the index of the currently selected book if any, -1 if none.
248 * @return the index or -1
250 private int getSelectedBookIndex() {
252 for (int i
= 0; i
< books
.size(); i
++) {
253 if (books
.get(i
).isSelected()) {
262 * Select the given book, or unselect all items.
265 * the index of the book to select, can be outside the bounds
266 * (either all the items will be unselected or the first or last
267 * book will then be selected, see <tt>forceRange>/tt>)
269 * TRUE to constraint the index to the first/last element, FALSE
270 * to unselect when outside the range
272 private void setSelectedBook(int index
, boolean forceRange
) {
273 int previousIndex
= getSelectedBookIndex();
275 if (index
>= books
.size()) {
277 index
= books
.size() - 1;
283 if (index
< 0 && forceRange
) {
287 if (previousIndex
>= 0) {
288 books
.get(previousIndex
).setSelected(false);
291 if (index
>= 0 && !books
.isEmpty()) {
292 books
.get(index
).setSelected(true);
297 * The action to execute when a key is typed.
302 private void onKeyTyped(KeyEvent e
) {
303 boolean consumed
= false;
304 boolean action
= e
.getKeyChar() == '\n';
305 boolean popup
= e
.getKeyChar() == ' ';
306 if (action
|| popup
) {
309 int index
= getSelectedBookIndex();
311 GuiReaderBook book
= books
.get(index
);
315 book
.popup(book
, book
.getWidth() / 2, book
.getHeight() / 2);
326 * The action to execute when a key is pressed.
331 private void onKeyPressed(KeyEvent e
) {
332 boolean consumed
= false;
333 if (e
.isActionKey()) {
335 switch (e
.getKeyCode()) {
336 case KeyEvent
.VK_LEFT
:
339 case KeyEvent
.VK_RIGHT
:
343 offset
= -itemsPerLine
;
345 case KeyEvent
.VK_DOWN
:
346 offset
= itemsPerLine
;
353 int previousIndex
= getSelectedBookIndex();
354 if (previousIndex
>= 0) {
355 setSelectedBook(previousIndex
+ offset
, true);