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