gui: better refresh for changeSTA
[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
10import javax.swing.JLabel;
11import javax.swing.JPanel;
12
16a81ef7 13import be.nikiroo.fanfix.reader.ui.GuiReaderBook.BookActionListener;
4310bae9
NR
14import be.nikiroo.utils.ui.WrapLayout;
15
16/**
5dd985cf 17 * A group of {@link GuiReaderBook}s for display.
4310bae9
NR
18 *
19 * @author niki
20 */
5dd985cf 21public class GuiReaderGroup extends JPanel {
4310bae9
NR
22 private static final long serialVersionUID = 1L;
23 private BookActionListener action;
24 private Color backgroundColor;
5dd985cf 25 private GuiReader reader;
79a99506 26 private List<GuiReaderBookInfo> infos;
5dd985cf 27 private List<GuiReaderBook> books;
4310bae9 28 private JPanel pane;
793f1071 29 private boolean words; // words or authors (secondary info on books)
4310bae9
NR
30
31 /**
5dd985cf 32 * Create a new {@link GuiReaderGroup}.
4310bae9
NR
33 *
34 * @param reader
e42573a0
NR
35 * the {@link GuiReaderBook} used to probe some information about
36 * the stories
4310bae9
NR
37 * @param title
38 * the title of this group
39 * @param backgroundColor
40 * the background colour to use (or NULL for default)
41 */
e42573a0 42 public GuiReaderGroup(GuiReader reader, String title, Color backgroundColor) {
4310bae9
NR
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>"
71d72f34
NR
64 + "<body style='text-align: center; color: gray;'><br><b>"
65 + "%s" + "</b></body>" + "</html>", title));
4310bae9
NR
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
5dd985cf 73 * {@link GuiReaderBook} action.
4310bae9
NR
74 *
75 * @param action
76 * the action
77 */
78 public void setActionListener(BookActionListener action) {
79 this.action = action;
79a99506 80 refreshBooks(infos, words);
4310bae9
NR
81 }
82
83 /**
5dd985cf 84 * Refresh the list of {@link GuiReaderBook}s displayed in the control.
4310bae9 85 *
c349fd48
NR
86 * @param infos
87 * the new list of infos
793f1071
NR
88 * @param seeWordcount
89 * TRUE to see word counts, FALSE to see authors
4310bae9 90 */
fb1ffdd0
NR
91 public void refreshBooks(List<GuiReaderBookInfo> infos, boolean seeWordcount) {
92 this.infos = infos;
8590da19
NR
93 refreshBooks(seeWordcount);
94 }
95
96 /**
97 * Refresh the list of {@link GuiReaderBook}s displayed in the control.
98 * <p>
99 * Will not change the current stories.
100 *
101 * @param seeWordcount
102 * TRUE to see word counts, FALSE to see authors
103 */
104 public void refreshBooks(boolean seeWordcount) {
793f1071 105 this.words = seeWordcount;
4310bae9 106
5dd985cf 107 books = new ArrayList<GuiReaderBook>();
4310bae9
NR
108 invalidate();
109 pane.invalidate();
110 pane.removeAll();
111
fb1ffdd0
NR
112 if (infos != null) {
113 for (GuiReaderBookInfo info : infos) {
79a99506
NR
114 boolean isCached = false;
115 if (info.getMeta() != null) {
116 isCached = reader.isCached(info.getMeta().getLuid());
117 }
118
119 GuiReaderBook book = new GuiReaderBook(reader, info, isCached,
fb1ffdd0 120 words);
4310bae9
NR
121 if (backgroundColor != null) {
122 book.setBackground(backgroundColor);
123 }
124
125 books.add(book);
126
127 book.addActionListener(new BookActionListener() {
211f7ddb 128 @Override
5dd985cf
NR
129 public void select(GuiReaderBook book) {
130 for (GuiReaderBook abook : books) {
4310bae9
NR
131 abook.setSelected(abook == book);
132 }
133 }
134
211f7ddb 135 @Override
e42573a0 136 public void popupRequested(GuiReaderBook book, MouseEvent e) {
4310bae9
NR
137 }
138
211f7ddb 139 @Override
5dd985cf 140 public void action(GuiReaderBook book) {
4310bae9
NR
141 }
142 });
143
144 if (action != null) {
145 book.addActionListener(action);
146 }
147
148 pane.add(book);
149 }
150 }
151
152 pane.validate();
153 pane.repaint();
154 validate();
155 repaint();
156 }
157
158 /**
159 * Enables or disables this component, depending on the value of the
160 * parameter <code>b</code>. An enabled component can respond to user input
161 * and generate events. Components are enabled initially by default.
162 * <p>
163 * Disabling this component will also affect its children.
164 *
165 * @param b
166 * If <code>true</code>, this component is enabled; otherwise
167 * this component is disabled
168 */
169 @Override
170 public void setEnabled(boolean b) {
171 if (books != null) {
5dd985cf 172 for (GuiReaderBook book : books) {
4310bae9
NR
173 book.setEnabled(b);
174 book.repaint();
175 }
176 }
177
178 pane.setEnabled(b);
179 super.setEnabled(b);
180 repaint();
181 }
182}