1 package be
.nikiroo
.fanfix
.reader
.ui
;
3 import java
.awt
.BorderLayout
;
4 import java
.awt
.Component
;
5 import java
.awt
.EventQueue
;
6 import java
.awt
.event
.ActionEvent
;
7 import java
.awt
.event
.ActionListener
;
8 import java
.lang
.reflect
.InvocationTargetException
;
9 import java
.util
.ArrayList
;
10 import java
.util
.List
;
12 import javax
.swing
.JComboBox
;
13 import javax
.swing
.JFrame
;
14 import javax
.swing
.JLabel
;
15 import javax
.swing
.JPanel
;
16 import javax
.swing
.JScrollPane
;
18 import be
.nikiroo
.fanfix
.Instance
;
19 import be
.nikiroo
.fanfix
.data
.MetaData
;
20 import be
.nikiroo
.fanfix
.reader
.ui
.GuiReaderBook
.BookActionListener
;
21 import be
.nikiroo
.fanfix
.searchable
.BasicSearchable
;
22 import be
.nikiroo
.fanfix
.searchable
.SearchableTag
;
23 import be
.nikiroo
.fanfix
.supported
.SupportType
;
26 * This frame will allow you to search through the supported websites for new
31 // JCombobox<E> not 1.6 compatible
32 @SuppressWarnings({ "unchecked", "rawtypes" })
33 public class GuiReaderSearchFrame
extends JFrame
{
34 private static final long serialVersionUID
= 1L;
36 private List
<SupportType
> supportTypes
;
37 private SupportType supportType
;
41 private JComboBox comboSupportTypes
;
42 private GuiReaderSearchByNamePanel searchPanel
;
44 private boolean seeWordcount
;
45 private GuiReaderGroup books
;
47 public GuiReaderSearchFrame(final GuiReader reader
) {
48 super("Browse stories");
49 setLayout(new BorderLayout());
55 supportTypes
= new ArrayList
<SupportType
>();
56 for (SupportType type
: SupportType
.values()) {
57 if (BasicSearchable
.getSearchable(type
) != null) {
58 supportTypes
.add(type
);
61 supportType
= supportTypes
.isEmpty() ?
null : supportTypes
.get(0);
63 comboSupportTypes
= new JComboBox(
64 supportTypes
.toArray(new SupportType
[] {}));
65 comboSupportTypes
.addActionListener(new ActionListener() {
67 public void actionPerformed(ActionEvent e
) {
68 setWaitingScreen(true);
70 (SupportType
) comboSupportTypes
.getSelectedItem(),
74 setWaitingScreen(false);
79 JPanel searchSites
= new JPanel(new BorderLayout());
80 searchSites
.add(comboSupportTypes
, BorderLayout
.CENTER
);
81 searchSites
.add(new JLabel(" " + "Website : "), BorderLayout
.WEST
);
83 searchPanel
= new GuiReaderSearchByNamePanel(supportType
,
87 setWaitingScreen(false);
91 searchPanel
.addActionListener(new ActionListener() {
93 public void actionPerformed(ActionEvent e
) {
94 updateMaxPage(searchPanel
.getMaxPage());
95 List
<GuiReaderBookInfo
> infos
= new ArrayList
<GuiReaderBookInfo
>();
96 for (MetaData meta
: searchPanel
.getStories()) {
97 infos
.add(GuiReaderBookInfo
.fromMeta(meta
));
103 int item
= searchPanel
.getStoryItem();
104 if (item
> 0 && item
<= books
.getBooksCount()) {
105 // TODO: "click" on item ITEM
110 JPanel top
= new JPanel(new BorderLayout());
111 top
.add(searchSites
, BorderLayout
.NORTH
);
112 top
.add(searchPanel
, BorderLayout
.CENTER
);
114 add(top
, BorderLayout
.NORTH
);
116 books
= new GuiReaderGroup(reader
, null, null);
117 books
.setActionListener(new BookActionListener() {
119 public void select(GuiReaderBook book
) {
123 public void popupRequested(GuiReaderBook book
, Component target
,
128 public void action(GuiReaderBook book
) {
129 new GuiReaderSearchAction(reader
.getLibrary(), book
.getInfo())
133 JScrollPane scroll
= new JScrollPane(books
);
134 scroll
.getVerticalScrollBar().setUnitIncrement(16);
135 add(scroll
, BorderLayout
.CENTER
);
137 setWaitingScreen(true);
140 private void updateSupportType(SupportType supportType
, Runnable inUi
) {
141 if (supportType
!= this.supportType
) {
142 this.supportType
= supportType
;
143 comboSupportTypes
.setSelectedItem(supportType
);
145 searchPanel
.setSupportType(supportType
, inUi
);
149 private void updatePage(final int page
) {
150 inUi(new Runnable() {
153 GuiReaderSearchFrame
.this.page
= page
;
156 System
.out
.println("page: " + page
);
161 private void updateMaxPage(final int maxPage
) {
162 inUi(new Runnable() {
165 GuiReaderSearchFrame
.this.maxPage
= maxPage
;
168 System
.out
.println("max page: " + maxPage
);
173 private void updateBooks(final List
<GuiReaderBookInfo
> infos
) {
174 setWaitingScreen(true);
175 inUi(new Runnable() {
178 books
.refreshBooks(infos
, seeWordcount
);
179 setWaitingScreen(false);
184 // item 0 = no selection, else = default selection
185 public void search(final SupportType searchOn
, final String keywords
,
186 final int page
, final int item
) {
188 setWaitingScreen(true);
190 searchPanel
.setSupportType(searchOn
, new Runnable() {
193 if (keywords
!= null) {
194 setWaitingScreen(true);
195 searchPanel
.search(keywords
, page
, item
, new Runnable() {
198 updateMaxPage(searchPanel
.getMaxPage());
200 setWaitingScreen(false);
208 // tag: null = base tags
209 public void searchTag(final SupportType searchOn
, final int page
,
210 final int item
, final SearchableTag tag
) {
212 setWaitingScreen(true);
214 searchPanel
.setSupportType(searchOn
, new Runnable() {
217 setWaitingScreen(true);
218 searchPanel
.searchTag(tag
, page
, item
, new Runnable() {
221 updateMaxPage(searchPanel
.getMaxPage());
223 setWaitingScreen(false);
231 * Process the given action in the main Swing UI thread.
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.
236 * Synchronous operation.
241 static void inUi(final Runnable run
) {
242 if (EventQueue
.isDispatchThread()) {
246 EventQueue
.invokeAndWait(run
);
247 } catch (InterruptedException e
) {
249 } catch (InvocationTargetException e
) {
255 static void error(Exception e
) {
256 Instance
.getTraceHandler().error(e
);
259 static void error(String e
) {
260 Instance
.getTraceHandler().error(e
);
263 private void setWaitingScreen(final boolean waiting
) {
264 inUi(new Runnable() {
267 GuiReaderSearchFrame
.this.setEnabled(!waiting
);
268 books
.setEnabled(!waiting
);
269 searchPanel
.setEnabled(!waiting
);