1 package be
.nikiroo
.fanfix
.reader
.ui
;
3 import java
.awt
.BorderLayout
;
4 import java
.awt
.event
.ActionEvent
;
5 import java
.awt
.event
.ActionListener
;
6 import java
.util
.ArrayList
;
9 import javax
.swing
.JPanel
;
10 import javax
.swing
.JTabbedPane
;
11 import javax
.swing
.event
.ChangeEvent
;
12 import javax
.swing
.event
.ChangeListener
;
14 import be
.nikiroo
.fanfix
.data
.MetaData
;
15 import be
.nikiroo
.fanfix
.searchable
.BasicSearchable
;
16 import be
.nikiroo
.fanfix
.searchable
.SearchableTag
;
17 import be
.nikiroo
.fanfix
.supported
.SupportType
;
20 * This panel represents a search panel that works for keywords and tags based
25 public class GuiReaderSearchByPanel
extends JPanel
{
26 private static final long serialVersionUID
= 1L;
28 private int actionEventId
= ActionEvent
.ACTION_FIRST
;
30 private Waitable waitable
;
32 private boolean searchByTags
;
34 private JTabbedPane searchTabs
;
35 private GuiReaderSearchByNamePanel byName
;
36 private GuiReaderSearchByTagPanel byTag
;
38 private List
<ActionListener
> actions
= new ArrayList
<ActionListener
>();
40 public interface Waitable
{
41 public void setWaiting(boolean waiting
);
44 // will throw illegalArgEx if bad support type, NULL allowed
45 public GuiReaderSearchByPanel(final SupportType supportType
,
47 setLayout(new BorderLayout());
49 this.waitable
= waitable
;
52 Runnable fireEvent
= new Runnable() {
59 byName
= new GuiReaderSearchByNamePanel(fireEvent
);
60 byTag
= new GuiReaderSearchByTagPanel(fireEvent
);
62 searchTabs
= new JTabbedPane();
63 searchTabs
.addTab("By name", byName
);
64 searchTabs
.addTab("By tags", byTag
);
65 searchTabs
.addChangeListener(new ChangeListener() {
67 public void stateChanged(ChangeEvent e
) {
68 searchByTags
= (searchTabs
.getSelectedComponent() == byTag
);
72 add(searchTabs
, BorderLayout
.CENTER
);
73 updateSearchBy(searchByTags
);
74 setSupportType(supportType
);
77 public void setSupportType(SupportType supportType
) {
78 BasicSearchable searchable
= BasicSearchable
.getSearchable(supportType
);
79 if (searchable
== null && supportType
!= null) {
80 throw new java
.lang
.IllegalArgumentException(
81 "Unupported support type: " + supportType
);
84 byName
.setSearchable(searchable
);
85 byTag
.setSearchable(searchable
);
88 public int getPage() {
90 return byName
.getPage();
93 return byTag
.getPage();
96 public int getMaxPage() {
98 return byName
.getMaxPage();
101 return byTag
.getMaxPage();
104 // throw outOfBounds if needed
105 public void setPage(int page
) {
107 searchTag(byTag
.getCurrentTag(), page
, 0);
109 search(byName
.getCurrentKeywords(), page
, 0);
113 // actions will be fired in UIthread
114 public void addActionListener(ActionListener action
) {
118 public boolean removeActionListener(ActionListener action
) {
119 return actions
.remove(action
);
122 public List
<MetaData
> getStories() {
124 return byName
.getStories();
127 return byTag
.getStories();
130 // selected item or 0 if none ! one-based !
131 public int getStoryItem() {
133 return byName
.getStoryItem();
136 return byTag
.getStoryItem();
139 private void fireAction() {
140 GuiReaderSearchFrame
.inUi(new Runnable() {
143 ActionEvent ae
= new ActionEvent(GuiReaderSearchByPanel
.this,
144 actionEventId
, "stories found");
147 if (actionEventId
> ActionEvent
.ACTION_LAST
) {
148 actionEventId
= ActionEvent
.ACTION_FIRST
;
151 for (ActionListener action
: actions
) {
153 action
.actionPerformed(ae
);
154 } catch (Exception e
) {
155 GuiReaderSearchFrame
.error(e
);
162 private void updateSearchBy(final boolean byTag
) {
163 GuiReaderSearchFrame
.inUi(new Runnable() {
167 searchTabs
.setSelectedIndex(0);
169 searchTabs
.setSelectedIndex(1);
175 // slow, start in UI mode
176 public void search(final String keywords
, final int page
, final int item
) {
177 waitable
.setWaiting(true);
178 updateSearchBy(false);
179 new Thread(new Runnable() {
183 byName
.search(keywords
, page
, item
);
186 waitable
.setWaiting(false);
192 // slow, start in UI mode
193 // tag: null = base tags
194 public void searchTag(final SearchableTag tag
, final int page
,
196 waitable
.setWaiting(true);
197 updateSearchBy(true);
198 new Thread(new Runnable() {
203 byTag
.searchTag(tag
, page
, item
);
206 waitable
.setWaiting(false);
213 * Enables or disables this component, depending on the value of the
214 * parameter <code>b</code>. An enabled component can respond to user input
215 * and generate events. Components are enabled initially by default.
217 * Disabling this component will also affect its children.
220 * If <code>true</code>, this component is enabled; otherwise
221 * this component is disabled
224 public void setEnabled(boolean b
) {
226 searchTabs
.setEnabled(b
);
227 byName
.setEnabled(b
);