GUI search: reorg mostly OK
[fanfix.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderSearchFrame.java
CommitLineData
4357eb54
NR
1package be.nikiroo.fanfix.reader.ui;
2
3import java.awt.BorderLayout;
d16065ec 4import java.awt.Component;
4357eb54
NR
5import java.awt.EventQueue;
6import java.awt.event.ActionEvent;
7import java.awt.event.ActionListener;
4357eb54
NR
8import java.lang.reflect.InvocationTargetException;
9import java.util.ArrayList;
10import java.util.List;
11
4357eb54
NR
12import javax.swing.JComboBox;
13import javax.swing.JFrame;
08e2185a 14import javax.swing.JLabel;
4357eb54
NR
15import javax.swing.JPanel;
16import javax.swing.JScrollPane;
4357eb54
NR
17
18import be.nikiroo.fanfix.Instance;
19import be.nikiroo.fanfix.data.MetaData;
d16065ec 20import be.nikiroo.fanfix.reader.ui.GuiReaderBook.BookActionListener;
4357eb54
NR
21import be.nikiroo.fanfix.searchable.BasicSearchable;
22import be.nikiroo.fanfix.searchable.SearchableTag;
23import be.nikiroo.fanfix.supported.SupportType;
24
25/**
26 * This frame will allow you to search through the supported websites for new
27 * stories/comics.
28 *
29 * @author niki
30 */
6e950847
NR
31// JCombobox<E> not 1.6 compatible
32@SuppressWarnings({ "unchecked", "rawtypes" })
741e8467 33public class GuiReaderSearchFrame extends JFrame {
4357eb54
NR
34 private static final long serialVersionUID = 1L;
35
36 private List<SupportType> supportTypes;
37 private SupportType supportType;
4357eb54
NR
38 private int page;
39 private int maxPage;
40
415c7454 41 private JComboBox comboSupportTypes;
ce5a42e7 42 private GuiReaderSearchByNamePanel searchPanel;
4357eb54
NR
43
44 private boolean seeWordcount;
45 private GuiReaderGroup books;
46
741e8467 47 public GuiReaderSearchFrame(final GuiReader reader) {
4357eb54
NR
48 super("Browse stories");
49 setLayout(new BorderLayout());
50 setSize(800, 600);
51
7cc1e743 52 page = 1;
4357eb54
NR
53 maxPage = -1;
54
55 supportTypes = new ArrayList<SupportType>();
56 for (SupportType type : SupportType.values()) {
57 if (BasicSearchable.getSearchable(type) != null) {
58 supportTypes.add(type);
59 }
60 }
61 supportType = supportTypes.isEmpty() ? null : supportTypes.get(0);
62
415c7454 63 comboSupportTypes = new JComboBox(
4357eb54
NR
64 supportTypes.toArray(new SupportType[] {}));
65 comboSupportTypes.addActionListener(new ActionListener() {
66 @Override
67 public void actionPerformed(ActionEvent e) {
7cc1e743
NR
68 setWaitingScreen(true);
69 updateSupportType(
70 (SupportType) comboSupportTypes.getSelectedItem(),
71 new Runnable() {
72 @Override
73 public void run() {
74 setWaitingScreen(false);
75 }
76 });
4357eb54
NR
77 }
78 });
08e2185a
NR
79 JPanel searchSites = new JPanel(new BorderLayout());
80 searchSites.add(comboSupportTypes, BorderLayout.CENTER);
81 searchSites.add(new JLabel(" " + "Website : "), BorderLayout.WEST);
4357eb54 82
7cc1e743
NR
83 searchPanel = new GuiReaderSearchByNamePanel(supportType,
84 new Runnable() {
85 @Override
86 public void run() {
87 setWaitingScreen(false);
88 }
89 });
90
ce5a42e7
N
91 searchPanel.addActionListener(new ActionListener() {
92 @Override
93 public void actionPerformed(ActionEvent e) {
7cc1e743 94 updateMaxPage(searchPanel.getMaxPage());
ce5a42e7
N
95 List<GuiReaderBookInfo> infos = new ArrayList<GuiReaderBookInfo>();
96 for (MetaData meta : searchPanel.getStories()) {
97 infos.add(GuiReaderBookInfo.fromMeta(meta));
98 }
99
100 updateBooks(infos);
101
102 // ! 1-based index !
103 int item = searchPanel.getStoryItem();
104 if (item > 0 && item <= books.getBooksCount()) {
105 // TODO: "click" on item ITEM
106 }
107 }
108 });
4357eb54 109
08e2185a
NR
110 JPanel top = new JPanel(new BorderLayout());
111 top.add(searchSites, BorderLayout.NORTH);
ce5a42e7 112 top.add(searchPanel, BorderLayout.CENTER);
4357eb54
NR
113
114 add(top, BorderLayout.NORTH);
115
116 books = new GuiReaderGroup(reader, null, null);
d16065ec
NR
117 books.setActionListener(new BookActionListener() {
118 @Override
119 public void select(GuiReaderBook book) {
120 }
121
122 @Override
123 public void popupRequested(GuiReaderBook book, Component target,
124 int x, int y) {
125 }
126
127 @Override
128 public void action(GuiReaderBook book) {
129 new GuiReaderSearchAction(reader.getLibrary(), book.getInfo())
130 .setVisible(true);
131 }
132 });
4357eb54
NR
133 JScrollPane scroll = new JScrollPane(books);
134 scroll.getVerticalScrollBar().setUnitIncrement(16);
135 add(scroll, BorderLayout.CENTER);
7cc1e743
NR
136
137 setWaitingScreen(true);
4357eb54
NR
138 }
139
7cc1e743 140 private void updateSupportType(SupportType supportType, Runnable inUi) {
81acd363
NR
141 if (supportType != this.supportType) {
142 this.supportType = supportType;
143 comboSupportTypes.setSelectedItem(supportType);
a12b668f 144 books.clear();
7cc1e743 145 searchPanel.setSupportType(supportType, inUi);
81acd363
NR
146 }
147 }
148
7cc1e743 149 private void updatePage(final int page) {
81acd363
NR
150 inUi(new Runnable() {
151 @Override
152 public void run() {
741e8467 153 GuiReaderSearchFrame.this.page = page;
7cc1e743 154
81acd363
NR
155 // TODO: gui
156 System.out.println("page: " + page);
7cc1e743
NR
157 }
158 });
159 }
160
161 private void updateMaxPage(final int maxPage) {
162 inUi(new Runnable() {
163 @Override
164 public void run() {
165 GuiReaderSearchFrame.this.maxPage = maxPage;
166
167 // TODO: gui
81acd363
NR
168 System.out.println("max page: " + maxPage);
169 }
170 });
171 }
172
81acd363 173 private void updateBooks(final List<GuiReaderBookInfo> infos) {
c499d79f 174 setWaitingScreen(true);
81acd363
NR
175 inUi(new Runnable() {
176 @Override
177 public void run() {
178 books.refreshBooks(infos, seeWordcount);
c499d79f 179 setWaitingScreen(false);
81acd363
NR
180 }
181 });
182 }
183
184 // item 0 = no selection, else = default selection
4357eb54
NR
185 public void search(final SupportType searchOn, final String keywords,
186 final int page, final int item) {
81acd363 187
7cc1e743
NR
188 setWaitingScreen(true);
189
190 searchPanel.setSupportType(searchOn, new Runnable() {
191 @Override
192 public void run() {
193 if (keywords != null) {
194 setWaitingScreen(true);
195 searchPanel.search(keywords, page, item, new Runnable() {
196 @Override
197 public void run() {
198 updateMaxPage(searchPanel.getMaxPage());
199 updatePage(page);
200 setWaitingScreen(false);
201 }
202 });
4357eb54 203 }
7cc1e743
NR
204 }
205 });
4357eb54
NR
206 }
207
6e950847 208 // tag: null = base tags
c499d79f
NR
209 public void searchTag(final SupportType searchOn, final int page,
210 final int item, final SearchableTag tag) {
211
212 setWaitingScreen(true);
7cc1e743
NR
213
214 searchPanel.setSupportType(searchOn, new Runnable() {
c499d79f
NR
215 @Override
216 public void run() {
7cc1e743
NR
217 setWaitingScreen(true);
218 searchPanel.searchTag(tag, page, item, new Runnable() {
219 @Override
220 public void run() {
221 updateMaxPage(searchPanel.getMaxPage());
222 updatePage(page);
223 setWaitingScreen(false);
224 }
225 });
4357eb54 226 }
ce5a42e7 227 });
bf2b37b0
NR
228 }
229
4357eb54
NR
230 /**
231 * Process the given action in the main Swing UI thread.
232 * <p>
233 * The code will make sure the current thread is the main UI thread and, if
234 * not, will switch to it before executing the runnable.
235 * <p>
236 * Synchronous operation.
237 *
238 * @param run
239 * the action to run
240 */
741e8467 241 static void inUi(final Runnable run) {
4357eb54
NR
242 if (EventQueue.isDispatchThread()) {
243 run.run();
244 } else {
245 try {
246 EventQueue.invokeAndWait(run);
247 } catch (InterruptedException e) {
bf2b37b0 248 error(e);
4357eb54 249 } catch (InvocationTargetException e) {
bf2b37b0 250 error(e);
4357eb54
NR
251 }
252 }
253 }
c499d79f 254
741e8467 255 static void error(Exception e) {
bf2b37b0
NR
256 Instance.getTraceHandler().error(e);
257 }
258
741e8467
NR
259 static void error(String e) {
260 Instance.getTraceHandler().error(e);
261 }
7cc1e743 262
c499d79f
NR
263 private void setWaitingScreen(final boolean waiting) {
264 inUi(new Runnable() {
265 @Override
266 public void run() {
741e8467 267 GuiReaderSearchFrame.this.setEnabled(!waiting);
08e2185a 268 books.setEnabled(!waiting);
ce5a42e7 269 searchPanel.setEnabled(!waiting);
c499d79f
NR
270 }
271 });
272 }
4357eb54 273}