25f7c9df334e34421a0d1772291539cc02c6a258
1 package be
.nikiroo
.fanfix_swing
.gui
;
3 import java
.awt
.BorderLayout
;
4 import java
.awt
.Dimension
;
5 import java
.awt
.event
.ActionEvent
;
6 import java
.awt
.event
.ActionListener
;
9 import javax
.swing
.JButton
;
10 import javax
.swing
.JPanel
;
11 import javax
.swing
.JTabbedPane
;
12 import javax
.swing
.event
.ChangeEvent
;
13 import javax
.swing
.event
.ChangeListener
;
15 import be
.nikiroo
.fanfix
.Instance
;
16 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
17 import be
.nikiroo
.fanfix_swing
.gui
.book
.BookInfo
;
18 import be
.nikiroo
.fanfix_swing
.gui
.browser
.AuthorTab
;
19 import be
.nikiroo
.fanfix_swing
.gui
.browser
.BasicTab
;
20 import be
.nikiroo
.fanfix_swing
.gui
.browser
.SourceTab
;
21 import be
.nikiroo
.fanfix_swing
.gui
.browser
.TagsTab
;
22 import be
.nikiroo
.fanfix_swing
.gui
.utils
.UiHelper
;
25 * Panel dedicated to browse the stories through different means: by authors, by
30 public class BrowserPanel
extends JPanel
{
31 private static final long serialVersionUID
= 1L;
34 * The {@link ActionEvent} you receive from
35 * {@link BrowserPanel#addActionListener(ActionListener)} can return this as a
36 * command (see {@link ActionEvent#getActionCommand()}) if they were created in
37 * the scope of a source.
39 static public final String SOURCE_SELECTION
= "source_selection";
41 * The {@link ActionEvent} you receive from
42 * {@link BrowserPanel#addActionListener(ActionListener)} can return this as a
43 * command (see {@link ActionEvent#getActionCommand()}) if they were created in
44 * the scope of an author.
46 static public final String AUTHOR_SELECTION
= "author_selection";
48 * The {@link ActionEvent} you receive from
49 * {@link BrowserPanel#addActionListener(ActionListener)} can return this as a
50 * command (see {@link ActionEvent#getActionCommand()}) if they were created in
53 static public final String TAGS_SELECTION
= "tags_selection";
55 private JTabbedPane tabs
;
56 private SourceTab sourceTab
;
57 private AuthorTab authorTab
;
58 private TagsTab tagsTab
;
60 private boolean keepSelection
;
63 * Create a nesw {@link BrowserPanel}.
65 public BrowserPanel() {
66 this.setPreferredSize(new Dimension(200, 800));
68 this.setLayout(new BorderLayout());
69 tabs
= new JTabbedPane();
72 tabs
.add(sourceTab
= new SourceTab(index
++, SOURCE_SELECTION
));
73 tabs
.add(authorTab
= new AuthorTab(index
++, AUTHOR_SELECTION
));
74 tabs
.add(tagsTab
= new TagsTab(index
++, TAGS_SELECTION
));
76 setText(tabs
, sourceTab
, "Sources", "Tooltip for Sources");
77 setText(tabs
, authorTab
, "Authors", "Tooltip for Authors");
78 setText(tabs
, tagsTab
, "Tags", "Tooltip for Tags");
80 JPanel options
= new JPanel();
81 options
.setLayout(new BorderLayout());
83 final JButton keep
= new JButton("Keep selection");
84 UiHelper
.setButtonPressed(keep
, keepSelection
);
85 keep
.addActionListener(new ActionListener() {
87 public void actionPerformed(ActionEvent e
) {
88 keepSelection
= !keepSelection
;
89 UiHelper
.setButtonPressed(keep
, keepSelection
);
90 keep
.setSelected(keepSelection
);
97 options
.add(keep
, BorderLayout
.CENTER
);
99 add(tabs
, BorderLayout
.CENTER
);
100 add(options
, BorderLayout
.SOUTH
);
102 tabs
.addChangeListener(new ChangeListener() {
104 public void stateChanged(ChangeEvent e
) {
105 if (!keepSelection
) {
112 @SuppressWarnings("rawtypes")
113 private void unselect() {
114 for (int i
= 0; i
< tabs
.getTabCount(); i
++) {
115 if (i
== tabs
.getSelectedIndex())
118 BasicTab tab
= (BasicTab
) tabs
.getComponent(i
);
123 private void setText(JTabbedPane tabs
, @SuppressWarnings("rawtypes") BasicTab tab
, String name
, String tooltip
) {
124 tab
.setBaseTitle(name
);
125 tabs
.setTitleAt(tab
.getIndex(), tab
.getTitle());
126 tabs
.setToolTipTextAt(tab
.getIndex(), tooltip
);
127 listenTitleChange(tabs
, tab
);
130 private void listenTitleChange(final JTabbedPane tabs
, @SuppressWarnings("rawtypes") final BasicTab tab
) {
131 tab
.addActionListener(new ActionListener() {
133 public void actionPerformed(ActionEvent e
) {
134 tabs
.setTitleAt(tab
.getIndex(), tab
.getTitle());
140 * Get the {@link BookInfo} to highlight, even if more than one are selected.
142 * Return NULL when nothing is selected.
144 * @return the {@link BookInfo} to highlight, can be NULL
146 public BookInfo
getHighlight() {
147 BasicLibrary lib
= Instance
.getInstance().getLibrary();
148 List
<String
> sel
= sourceTab
.getSelectedElements();
149 if (!sel
.isEmpty()) {
150 if (tabs
.getSelectedComponent() == sourceTab
) {
151 return BookInfo
.fromSource(lib
, sel
.get(0));
152 } else if (tabs
.getSelectedComponent() == authorTab
) {
153 return BookInfo
.fromAuthor(lib
, sel
.get(0));
154 } else if (tabs
.getSelectedComponent() == tagsTab
) {
155 return BookInfo
.fromTag(lib
, sel
.get(0));
163 * The currently selected sources, or an empty list.
165 * @return the sources (cannot be NULL)
167 public List
<String
> getSelectedSources() {
168 return sourceTab
.getSelectedElements();
172 * The currently selected authors, or an empty list.
174 * @return the sources (cannot be NULL)
176 public List
<String
> getSelectedAuthors() {
177 return authorTab
.getSelectedElements();
181 * The currently selected tags, or an empty list.
183 * @return the sources (cannot be NULL)
185 public List
<String
> getSelectedTags() {
186 return tagsTab
.getSelectedElements();
190 * Reload all the data from the 3 tabs.
192 public void reloadData() {
193 sourceTab
.reloadData();
194 authorTab
.reloadData();
195 tagsTab
.reloadData();
199 * Adds the specified action listener to receive action events from this
202 * @param listener the action listener to be added
204 public synchronized void addActionListener(ActionListener listener
) {
205 sourceTab
.addActionListener(listener
);
206 authorTab
.addActionListener(listener
);
207 tagsTab
.addActionListener(listener
);
211 * Removes the specified action listener so that it no longer receives action
212 * events from this {@link SearchBar}.
214 * @param listener the action listener to be removed
216 public synchronized void removeActionListener(ActionListener listener
) {
217 sourceTab
.removeActionListener(listener
);
218 authorTab
.removeActionListener(listener
);
219 tagsTab
.removeActionListener(listener
);