Commit | Line | Data |
---|---|---|
4357eb54 NR |
1 | package be.nikiroo.fanfix.reader.ui; |
2 | ||
3 | import java.awt.BorderLayout; | |
d16065ec | 4 | import java.awt.Component; |
4357eb54 NR |
5 | import java.awt.EventQueue; |
6 | import java.awt.event.ActionEvent; | |
7 | import java.awt.event.ActionListener; | |
4357eb54 NR |
8 | import java.lang.reflect.InvocationTargetException; |
9 | import java.util.ArrayList; | |
10 | import java.util.List; | |
11 | ||
4357eb54 NR |
12 | import javax.swing.JComboBox; |
13 | import javax.swing.JFrame; | |
08e2185a | 14 | import javax.swing.JLabel; |
4357eb54 NR |
15 | import javax.swing.JPanel; |
16 | import javax.swing.JScrollPane; | |
4357eb54 NR |
17 | |
18 | import be.nikiroo.fanfix.Instance; | |
19 | import be.nikiroo.fanfix.data.MetaData; | |
d16065ec | 20 | import be.nikiroo.fanfix.reader.ui.GuiReaderBook.BookActionListener; |
4357eb54 NR |
21 | import be.nikiroo.fanfix.searchable.BasicSearchable; |
22 | import be.nikiroo.fanfix.searchable.SearchableTag; | |
23 | import 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 | 33 | public 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 | ||
4357eb54 NR |
52 | page = 1; // TODO |
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) { | |
81acd363 | 68 | updateSupportType((SupportType) comboSupportTypes |
4357eb54 NR |
69 | .getSelectedItem()); |
70 | } | |
71 | }); | |
08e2185a NR |
72 | JPanel searchSites = new JPanel(new BorderLayout()); |
73 | searchSites.add(comboSupportTypes, BorderLayout.CENTER); | |
74 | searchSites.add(new JLabel(" " + "Website : "), BorderLayout.WEST); | |
4357eb54 | 75 | |
ce5a42e7 N |
76 | searchPanel = new GuiReaderSearchByNamePanel(supportType); |
77 | searchPanel.addActionListener(new ActionListener() { | |
78 | @Override | |
79 | public void actionPerformed(ActionEvent e) { | |
80 | updatePages(0, maxPage); | |
81 | List<GuiReaderBookInfo> infos = new ArrayList<GuiReaderBookInfo>(); | |
82 | for (MetaData meta : searchPanel.getStories()) { | |
83 | infos.add(GuiReaderBookInfo.fromMeta(meta)); | |
84 | } | |
85 | ||
86 | updateBooks(infos); | |
87 | ||
88 | // ! 1-based index ! | |
89 | int item = searchPanel.getStoryItem(); | |
90 | if (item > 0 && item <= books.getBooksCount()) { | |
91 | // TODO: "click" on item ITEM | |
92 | } | |
93 | } | |
94 | }); | |
4357eb54 | 95 | |
08e2185a NR |
96 | JPanel top = new JPanel(new BorderLayout()); |
97 | top.add(searchSites, BorderLayout.NORTH); | |
ce5a42e7 | 98 | top.add(searchPanel, BorderLayout.CENTER); |
4357eb54 NR |
99 | |
100 | add(top, BorderLayout.NORTH); | |
101 | ||
102 | books = new GuiReaderGroup(reader, null, null); | |
d16065ec NR |
103 | books.setActionListener(new BookActionListener() { |
104 | @Override | |
105 | public void select(GuiReaderBook book) { | |
106 | } | |
107 | ||
108 | @Override | |
109 | public void popupRequested(GuiReaderBook book, Component target, | |
110 | int x, int y) { | |
111 | } | |
112 | ||
113 | @Override | |
114 | public void action(GuiReaderBook book) { | |
115 | new GuiReaderSearchAction(reader.getLibrary(), book.getInfo()) | |
116 | .setVisible(true); | |
117 | } | |
118 | }); | |
4357eb54 NR |
119 | JScrollPane scroll = new JScrollPane(books); |
120 | scroll.getVerticalScrollBar().setUnitIncrement(16); | |
121 | add(scroll, BorderLayout.CENTER); | |
4357eb54 NR |
122 | } |
123 | ||
81acd363 NR |
124 | private void updateSupportType(SupportType supportType) { |
125 | if (supportType != this.supportType) { | |
126 | this.supportType = supportType; | |
127 | comboSupportTypes.setSelectedItem(supportType); | |
a12b668f | 128 | books.clear(); |
ce5a42e7 | 129 | searchPanel.setSupportType(supportType); |
81acd363 NR |
130 | } |
131 | } | |
132 | ||
133 | private void updatePages(final int page, final Integer maxPage) { | |
134 | inUi(new Runnable() { | |
135 | @Override | |
136 | public void run() { | |
741e8467 NR |
137 | GuiReaderSearchFrame.this.page = page; |
138 | GuiReaderSearchFrame.this.maxPage = maxPage; | |
ce5a42e7 N |
139 | |
140 | searchPanel.setPage(page); | |
141 | ||
81acd363 NR |
142 | // TODO: gui |
143 | System.out.println("page: " + page); | |
144 | System.out.println("max page: " + maxPage); | |
145 | } | |
146 | }); | |
147 | } | |
148 | ||
81acd363 | 149 | private void updateBooks(final List<GuiReaderBookInfo> infos) { |
c499d79f | 150 | setWaitingScreen(true); |
81acd363 NR |
151 | inUi(new Runnable() { |
152 | @Override | |
153 | public void run() { | |
154 | books.refreshBooks(infos, seeWordcount); | |
c499d79f | 155 | setWaitingScreen(false); |
81acd363 NR |
156 | } |
157 | }); | |
158 | } | |
159 | ||
160 | // item 0 = no selection, else = default selection | |
4357eb54 NR |
161 | public void search(final SupportType searchOn, final String keywords, |
162 | final int page, final int item) { | |
81acd363 | 163 | |
81acd363 | 164 | updatePages(page, maxPage); |
ce5a42e7 N |
165 | searchPanel.setSupportType(searchOn); |
166 | ||
167 | if (keywords != null) { | |
168 | setWaitingScreen(true); | |
169 | searchPanel.search(keywords, item, new Runnable() { | |
170 | @Override | |
171 | public void run() { | |
172 | setWaitingScreen(false); | |
4357eb54 | 173 | } |
ce5a42e7 N |
174 | }); |
175 | } | |
4357eb54 NR |
176 | } |
177 | ||
6e950847 | 178 | // tag: null = base tags |
c499d79f NR |
179 | public void searchTag(final SupportType searchOn, final int page, |
180 | final int item, final SearchableTag tag) { | |
181 | ||
182 | setWaitingScreen(true); | |
81acd363 | 183 | updatePages(page, maxPage); |
ce5a42e7 N |
184 | searchPanel.setSupportType(searchOn); |
185 | searchPanel.searchTag(tag, item, new Runnable() { | |
c499d79f NR |
186 | @Override |
187 | public void run() { | |
c499d79f | 188 | setWaitingScreen(false); |
4357eb54 | 189 | } |
ce5a42e7 | 190 | }); |
bf2b37b0 NR |
191 | } |
192 | ||
4357eb54 NR |
193 | /** |
194 | * Process the given action in the main Swing UI thread. | |
195 | * <p> | |
196 | * The code will make sure the current thread is the main UI thread and, if | |
197 | * not, will switch to it before executing the runnable. | |
198 | * <p> | |
199 | * Synchronous operation. | |
200 | * | |
201 | * @param run | |
202 | * the action to run | |
203 | */ | |
741e8467 | 204 | static void inUi(final Runnable run) { |
4357eb54 NR |
205 | if (EventQueue.isDispatchThread()) { |
206 | run.run(); | |
207 | } else { | |
208 | try { | |
209 | EventQueue.invokeAndWait(run); | |
210 | } catch (InterruptedException e) { | |
bf2b37b0 | 211 | error(e); |
4357eb54 | 212 | } catch (InvocationTargetException e) { |
bf2b37b0 | 213 | error(e); |
4357eb54 NR |
214 | } |
215 | } | |
216 | } | |
c499d79f | 217 | |
741e8467 | 218 | static void error(Exception e) { |
bf2b37b0 NR |
219 | Instance.getTraceHandler().error(e); |
220 | } | |
221 | ||
741e8467 NR |
222 | static void error(String e) { |
223 | Instance.getTraceHandler().error(e); | |
224 | } | |
225 | ||
c499d79f NR |
226 | private void setWaitingScreen(final boolean waiting) { |
227 | inUi(new Runnable() { | |
228 | @Override | |
229 | public void run() { | |
741e8467 | 230 | GuiReaderSearchFrame.this.setEnabled(!waiting); |
08e2185a | 231 | books.setEnabled(!waiting); |
ce5a42e7 | 232 | searchPanel.setEnabled(!waiting); |
c499d79f NR |
233 | } |
234 | }); | |
235 | } | |
4357eb54 | 236 | } |