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
.KeyAdapter
;
9 import java
.awt
.event
.KeyEvent
;
10 import java
.awt
.event
.MouseEvent
;
11 import java
.util
.ArrayList
;
12 import java
.util
.List
;
14 import javax
.swing
.JLabel
;
15 import javax
.swing
.JPanel
;
17 import be
.nikiroo
.fanfix
.bundles
.StringIdGui
;
18 import be
.nikiroo
.fanfix
.reader
.ui
.GuiReaderBook
.BookActionListener
;
19 import be
.nikiroo
.utils
.ui
.WrapLayout
;
22 * A group of {@link GuiReaderBook}s for display.
26 public class GuiReaderGroup
extends JPanel
{
27 private static final long serialVersionUID
= 1L;
28 private BookActionListener action
;
29 private Color backgroundColor
;
30 private GuiReader reader
;
31 private List
<GuiReaderBookInfo
> infos
;
32 private List
<GuiReaderBook
> books
;
34 private boolean words
; // words or authors (secondary info on books)
35 private int itemsPerLine
;
38 * Create a new {@link GuiReaderGroup}.
41 * the {@link GuiReaderBook} used to probe some information about
44 * the title of this group
45 * @param backgroundColor
46 * the background colour to use (or NULL for default)
48 public GuiReaderGroup(GuiReader reader
, String title
, Color backgroundColor
) {
50 this.backgroundColor
= backgroundColor
;
52 this.pane
= new JPanel();
54 pane
.setLayout(new WrapLayout(WrapLayout
.LEADING
, 5, 5));
55 if (backgroundColor
!= null) {
56 pane
.setBackground(backgroundColor
);
57 setBackground(backgroundColor
);
60 setLayout(new BorderLayout(0, 10));
61 add(pane
, BorderLayout
.CENTER
);
64 if (title
.isEmpty()) {
65 title
= GuiReader
.trans(StringIdGui
.MENU_AUTHORS_UNKNOWN
);
68 JLabel label
= new JLabel();
69 label
.setText(String
.format("<html>"
70 + "<body style='text-align: center; color: gray;'><br><b>"
71 + "%s" + "</b></body>" + "</html>", title
));
72 label
.setHorizontalAlignment(JLabel
.CENTER
);
73 add(label
, BorderLayout
.NORTH
);
76 // Compute the number of items per line at each resize
77 addComponentListener(new ComponentAdapter() {
79 public void componentResized(ComponentEvent e
) {
80 super.componentResized(e
);
81 computeItemsPerLine();
84 computeItemsPerLine();
86 addKeyListener(new KeyAdapter() {
88 public void keyTyped(KeyEvent e
) {
95 * Compute how many items can fit in a line so UP and DOWN can be used to go
96 * up/down one line at a time.
98 private void computeItemsPerLine() {
104 * Set the {@link ActionListener} that will be fired on each
105 * {@link GuiReaderBook} action.
110 public void setActionListener(BookActionListener action
) {
111 this.action
= action
;
112 refreshBooks(infos
, words
);
116 * Refresh the list of {@link GuiReaderBook}s displayed in the control.
119 * the new list of infos
120 * @param seeWordcount
121 * TRUE to see word counts, FALSE to see authors
123 public void refreshBooks(List
<GuiReaderBookInfo
> infos
, boolean seeWordcount
) {
125 refreshBooks(seeWordcount
);
129 * Refresh the list of {@link GuiReaderBook}s displayed in the control.
131 * Will not change the current stories.
133 * @param seeWordcount
134 * TRUE to see word counts, FALSE to see authors
136 public void refreshBooks(boolean seeWordcount
) {
137 this.words
= seeWordcount
;
139 books
= new ArrayList
<GuiReaderBook
>();
145 for (GuiReaderBookInfo info
: infos
) {
146 boolean isCached
= false;
147 if (info
.getMeta() != null) {
148 isCached
= reader
.isCached(info
.getMeta().getLuid());
151 GuiReaderBook book
= new GuiReaderBook(reader
, info
, isCached
,
153 if (backgroundColor
!= null) {
154 book
.setBackground(backgroundColor
);
159 book
.addActionListener(new BookActionListener() {
161 public void select(GuiReaderBook book
) {
162 for (GuiReaderBook abook
: books
) {
163 abook
.setSelected(abook
== book
);
168 public void popupRequested(GuiReaderBook book
, MouseEvent e
) {
172 public void action(GuiReaderBook book
) {
176 if (action
!= null) {
177 book
.addActionListener(action
);
191 * Enables or disables this component, depending on the value of the
192 * parameter <code>b</code>. An enabled component can respond to user input
193 * and generate events. Components are enabled initially by default.
195 * Disabling this component will also affect its children.
198 * If <code>true</code>, this component is enabled; otherwise
199 * this component is disabled
202 public void setEnabled(boolean b
) {
204 for (GuiReaderBook book
: books
) {
216 * The action to execute when a key is typed.
221 private void onKeyTyped(KeyEvent e
) {
222 boolean consumed
= false;
223 System
.out
.println(e
);
224 if (e
.isActionKey()) {
226 switch (e
.getKeyCode()) {
227 case KeyEvent
.VK_LEFT
:
230 case KeyEvent
.VK_RIGHT
:
234 offset
= itemsPerLine
;
236 case KeyEvent
.VK_DOWN
:
237 offset
= -itemsPerLine
;
245 for (int i
= 0; i
< books
.size(); i
++) {
246 if (books
.get(i
).isSelected()) {
253 int newSelect
= selected
+ offset
;
254 if (newSelect
>= books
.size()) {
255 newSelect
= books
.size() - 1;
258 if (selected
!= newSelect
&& newSelect
>= 0) {
260 books
.get(selected
).setSelected(false);
261 books
.get(newSelect
).setSelected(true);
271 super.processKeyEvent(e
);