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