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 if (tabs
.getSelectedComponent() == sourceTab
) {
149 List
<String
> sel
= sourceTab
.getSelectedElements();
150 if (!sel
.isEmpty()) {
151 return BookInfo
.fromSource(lib
, sel
.get(0));
153 } else if (tabs
.getSelectedComponent() == authorTab
) {
154 List
<String
> sel
= authorTab
.getSelectedElements();
155 if (!sel
.isEmpty()) {
156 return BookInfo
.fromAuthor(lib
, sel
.get(0));
158 } else if (tabs
.getSelectedComponent() == tagsTab
) {
159 List
<String
> sel
= tagsTab
.getSelectedElements();
160 if (!sel
.isEmpty()) {
161 return BookInfo
.fromTag(lib
, sel
.get(0));
169 * The currently selected sources, or an empty list.
171 * @return the sources (cannot be NULL)
173 public List
<String
> getSelectedSources() {
174 return sourceTab
.getSelectedElements();
178 * The currently selected authors, or an empty list.
180 * @return the sources (cannot be NULL)
182 public List
<String
> getSelectedAuthors() {
183 return authorTab
.getSelectedElements();
187 * The currently selected tags, or an empty list.
189 * @return the sources (cannot be NULL)
191 public List
<String
> getSelectedTags() {
192 return tagsTab
.getSelectedElements();
196 * Adds the specified action listener to receive action events from this
199 * @param listener the action listener to be added
201 public synchronized void addActionListener(ActionListener listener
) {
202 sourceTab
.addActionListener(listener
);
203 authorTab
.addActionListener(listener
);
204 tagsTab
.addActionListener(listener
);
208 * Removes the specified action listener so that it no longer receives action
209 * events from this {@link SearchBar}.
211 * @param listener the action listener to be removed
213 public synchronized void removeActionListener(ActionListener listener
) {
214 sourceTab
.removeActionListener(listener
);
215 authorTab
.removeActionListener(listener
);
216 tagsTab
.removeActionListener(listener
);