Make TUI optional (and update nikiroo-utils):
[fanfix.git] / src / be / nikiroo / fanfix / reader / LocalReaderGroup.java
... / ...
CommitLineData
1package be.nikiroo.fanfix.reader;
2
3import java.awt.BorderLayout;
4import java.awt.Color;
5import java.awt.event.ActionListener;
6import java.awt.event.MouseEvent;
7import java.util.ArrayList;
8import java.util.List;
9
10import javax.swing.JLabel;
11import javax.swing.JPanel;
12
13import be.nikiroo.fanfix.data.MetaData;
14import be.nikiroo.fanfix.reader.LocalReaderBook.BookActionListener;
15import be.nikiroo.utils.ui.WrapLayout;
16
17/**
18 * A group of {@link LocalReaderBook}s for display.
19 *
20 * @author niki
21 */
22public class LocalReaderGroup extends JPanel {
23 private static final long serialVersionUID = 1L;
24 private BookActionListener action;
25 private Color backgroundColor;
26 private LocalReader reader;
27 private List<MetaData> stories;
28 private List<LocalReaderBook> books;
29 private JPanel pane;
30 private boolean words; // words or authors (secondary info on books)
31
32 /**
33 * Create a new {@link LocalReaderGroup}.
34 *
35 * @param reader
36 * the {@link LocalReaderBook} used to probe some information
37 * about 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 LocalReaderGroup(LocalReader reader, String title,
44 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 LocalReaderBook} 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 LocalReaderBook}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<LocalReaderBook>();
98 invalidate();
99 pane.invalidate();
100 pane.removeAll();
101
102 if (stories != null) {
103 for (MetaData meta : stories) {
104 LocalReaderBook book = new LocalReaderBook(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 public void select(LocalReaderBook book) {
114 for (LocalReaderBook abook : books) {
115 abook.setSelected(abook == book);
116 }
117 }
118
119 public void popupRequested(LocalReaderBook book,
120 MouseEvent e) {
121 }
122
123 public void action(LocalReaderBook book) {
124 }
125 });
126
127 if (action != null) {
128 book.addActionListener(action);
129 }
130
131 pane.add(book);
132 }
133 }
134
135 pane.validate();
136 pane.repaint();
137 validate();
138 repaint();
139 }
140
141 /**
142 * Enables or disables this component, depending on the value of the
143 * parameter <code>b</code>. An enabled component can respond to user input
144 * and generate events. Components are enabled initially by default.
145 * <p>
146 * Disabling this component will also affect its children.
147 *
148 * @param b
149 * If <code>true</code>, this component is enabled; otherwise
150 * this component is disabled
151 */
152 @Override
153 public void setEnabled(boolean b) {
154 if (books != null) {
155 for (LocalReaderBook book : books) {
156 book.setEnabled(b);
157 book.repaint();
158 }
159 }
160
161 pane.setEnabled(b);
162 super.setEnabled(b);
163 repaint();
164 }
165}