fix see word count on source/author, step 1
[fanfix.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderGroup.java
1 package be.nikiroo.fanfix.reader.ui;
2
3 import java.awt.BorderLayout;
4 import java.awt.Color;
5 import java.awt.event.ActionListener;
6 import java.awt.event.MouseEvent;
7 import java.util.ArrayList;
8 import java.util.List;
9
10 import javax.swing.JLabel;
11 import javax.swing.JPanel;
12
13 import be.nikiroo.fanfix.reader.ui.GuiReaderBook.BookActionListener;
14 import be.nikiroo.utils.ui.WrapLayout;
15
16 /**
17 * A group of {@link GuiReaderBook}s for display.
18 *
19 * @author niki
20 */
21 public class GuiReaderGroup extends JPanel {
22 private static final long serialVersionUID = 1L;
23 private BookActionListener action;
24 private Color backgroundColor;
25 private GuiReader reader;
26 private List<GuiReaderBookInfo> infos;
27 private List<GuiReaderBook> books;
28 private JPanel pane;
29 private boolean words; // words or authors (secondary info on books)
30
31 /**
32 * Create a new {@link GuiReaderGroup}.
33 *
34 * @param reader
35 * the {@link GuiReaderBook} used to probe some information about
36 * the stories
37 * @param title
38 * the title of this group
39 * @param backgroundColor
40 * the background colour to use (or NULL for default)
41 */
42 public GuiReaderGroup(GuiReader reader, String title, Color backgroundColor) {
43 this.reader = reader;
44 this.backgroundColor = backgroundColor;
45
46 this.pane = new JPanel();
47
48 pane.setLayout(new WrapLayout(WrapLayout.LEADING, 5, 5));
49 if (backgroundColor != null) {
50 pane.setBackground(backgroundColor);
51 setBackground(backgroundColor);
52 }
53
54 setLayout(new BorderLayout(0, 10));
55 add(pane, BorderLayout.CENTER);
56
57 if (title != null) {
58 if (title.isEmpty()) {
59 title = "[unknown]";
60 }
61
62 JLabel label = new JLabel();
63 label.setText(String.format("<html>"
64 + "<body style='text-align: center; color: gray;'><br><b>"
65 + "%s" + "</b></body>" + "</html>", title));
66 label.setHorizontalAlignment(JLabel.CENTER);
67 add(label, BorderLayout.NORTH);
68 }
69 }
70
71 /**
72 * Set the {@link ActionListener} that will be fired on each
73 * {@link GuiReaderBook} action.
74 *
75 * @param action
76 * the action
77 */
78 public void setActionListener(BookActionListener action) {
79 this.action = action;
80 refreshBooks(infos, words);
81 }
82
83 /**
84 * Refresh the list of {@link GuiReaderBook}s displayed in the control.
85 *
86 * @param stories
87 * the stories
88 * @param seeWordcount
89 * TRUE to see word counts, FALSE to see authors
90 */
91 public void refreshBooks(List<GuiReaderBookInfo> infos, boolean seeWordcount) {
92 this.infos = infos;
93 this.words = seeWordcount;
94
95 books = new ArrayList<GuiReaderBook>();
96 invalidate();
97 pane.invalidate();
98 pane.removeAll();
99
100 if (infos != null) {
101 for (GuiReaderBookInfo info : infos) {
102 boolean isCached = false;
103 if (info.getMeta() != null) {
104 isCached = reader.isCached(info.getMeta().getLuid());
105 }
106
107 GuiReaderBook book = new GuiReaderBook(reader, info, isCached,
108 words);
109 if (backgroundColor != null) {
110 book.setBackground(backgroundColor);
111 }
112
113 books.add(book);
114
115 book.addActionListener(new BookActionListener() {
116 @Override
117 public void select(GuiReaderBook book) {
118 for (GuiReaderBook abook : books) {
119 abook.setSelected(abook == book);
120 }
121 }
122
123 @Override
124 public void popupRequested(GuiReaderBook book, MouseEvent e) {
125 }
126
127 @Override
128 public void action(GuiReaderBook book) {
129 }
130 });
131
132 if (action != null) {
133 book.addActionListener(action);
134 }
135
136 pane.add(book);
137 }
138 }
139
140 pane.validate();
141 pane.repaint();
142 validate();
143 repaint();
144 }
145
146 /**
147 * Enables or disables this component, depending on the value of the
148 * parameter <code>b</code>. An enabled component can respond to user input
149 * and generate events. Components are enabled initially by default.
150 * <p>
151 * Disabling this component will also affect its children.
152 *
153 * @param b
154 * If <code>true</code>, this component is enabled; otherwise
155 * this component is disabled
156 */
157 @Override
158 public void setEnabled(boolean b) {
159 if (books != null) {
160 for (GuiReaderBook book : books) {
161 book.setEnabled(b);
162 book.repaint();
163 }
164 }
165
166 pane.setEnabled(b);
167 super.setEnabled(b);
168 repaint();
169 }
170 }