Merge branch 'master' of github.com:nikiroo/fanfix
[fanfix.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderGroup.java
CommitLineData
16a81ef7 1package be.nikiroo.fanfix.reader.ui;
4310bae9
NR
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
e604986c 10import javax.management.RuntimeErrorException;
4310bae9
NR
11import javax.swing.JLabel;
12import javax.swing.JPanel;
13
14import be.nikiroo.fanfix.data.MetaData;
16a81ef7 15import be.nikiroo.fanfix.reader.ui.GuiReaderBook.BookActionListener;
4310bae9
NR
16import be.nikiroo.utils.ui.WrapLayout;
17
18/**
5dd985cf 19 * A group of {@link GuiReaderBook}s for display.
4310bae9
NR
20 *
21 * @author niki
22 */
5dd985cf 23public class GuiReaderGroup extends JPanel {
4310bae9
NR
24 private static final long serialVersionUID = 1L;
25 private BookActionListener action;
26 private Color backgroundColor;
5dd985cf 27 private GuiReader reader;
4310bae9 28 private List<MetaData> stories;
5dd985cf 29 private List<GuiReaderBook> books;
4310bae9 30 private JPanel pane;
793f1071 31 private boolean words; // words or authors (secondary info on books)
4310bae9
NR
32
33 /**
5dd985cf 34 * Create a new {@link GuiReaderGroup}.
4310bae9
NR
35 *
36 * @param reader
e42573a0
NR
37 * the {@link GuiReaderBook} used to probe some information about
38 * the stories
4310bae9
NR
39 * @param title
40 * the title of this group
41 * @param backgroundColor
42 * the background colour to use (or NULL for default)
43 */
e42573a0 44 public GuiReaderGroup(GuiReader reader, String title, Color backgroundColor) {
4310bae9
NR
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>"
71d72f34
NR
66 + "<body style='text-align: center; color: gray;'><br><b>"
67 + "%s" + "</b></body>" + "</html>", title));
4310bae9
NR
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
5dd985cf 75 * {@link GuiReaderBook} action.
4310bae9
NR
76 *
77 * @param action
78 * the action
79 */
80 public void setActionListener(BookActionListener action) {
81 this.action = action;
793f1071 82 refreshBooks(stories, words);
4310bae9
NR
83 }
84
85 /**
5dd985cf 86 * Refresh the list of {@link GuiReaderBook}s displayed in the control.
4310bae9
NR
87 *
88 * @param stories
89 * the stories
793f1071
NR
90 * @param seeWordcount
91 * TRUE to see word counts, FALSE to see authors
4310bae9 92 */
793f1071 93 public void refreshBooks(List<MetaData> stories, boolean seeWordcount) {
4310bae9 94 this.stories = stories;
793f1071 95 this.words = seeWordcount;
4310bae9 96
5dd985cf 97 books = new ArrayList<GuiReaderBook>();
4310bae9
NR
98 invalidate();
99 pane.invalidate();
100 pane.removeAll();
101
102 if (stories != null) {
103 for (MetaData meta : stories) {
e42573a0 104 GuiReaderBook book = new GuiReaderBook(reader, meta,
793f1071 105 reader.isCached(meta.getLuid()), seeWordcount);
4310bae9
NR
106 if (backgroundColor != null) {
107 book.setBackground(backgroundColor);
108 }
109
110 books.add(book);
111
112 book.addActionListener(new BookActionListener() {
211f7ddb 113 @Override
5dd985cf
NR
114 public void select(GuiReaderBook book) {
115 for (GuiReaderBook abook : books) {
4310bae9
NR
116 abook.setSelected(abook == book);
117 }
118 }
119
211f7ddb 120 @Override
e42573a0 121 public void popupRequested(GuiReaderBook book, MouseEvent e) {
4310bae9
NR
122 }
123
211f7ddb 124 @Override
5dd985cf 125 public void action(GuiReaderBook book) {
4310bae9
NR
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) {
5dd985cf 157 for (GuiReaderBook book : books) {
4310bae9
NR
158 book.setEnabled(b);
159 book.repaint();
160 }
161 }
162
163 pane.setEnabled(b);
164 super.setEnabled(b);
165 repaint();
166 }
167}