1 package be
.nikiroo
.fanfix_swing
.gui
;
3 import java
.awt
.BorderLayout
;
4 import java
.awt
.Component
;
5 import java
.awt
.Dimension
;
6 import java
.awt
.event
.ActionEvent
;
7 import java
.awt
.event
.ActionListener
;
10 import javax
.swing
.JButton
;
11 import javax
.swing
.JComponent
;
12 import javax
.swing
.JPanel
;
13 import javax
.swing
.JTabbedPane
;
14 import javax
.swing
.event
.ChangeEvent
;
15 import javax
.swing
.event
.ChangeListener
;
17 import be
.nikiroo
.fanfix
.Instance
;
18 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
19 import be
.nikiroo
.fanfix_swing
.gui
.book
.BookInfo
;
20 import be
.nikiroo
.fanfix_swing
.gui
.browser
.AuthorTab
;
21 import be
.nikiroo
.fanfix_swing
.gui
.browser
.BasicTab
;
22 import be
.nikiroo
.fanfix_swing
.gui
.browser
.SourceTab
;
23 import be
.nikiroo
.fanfix_swing
.gui
.browser
.TagsTab
;
24 import be
.nikiroo
.fanfix_swing
.gui
.utils
.UiHelper
;
27 * Panel dedicated to browse the stories through different means: by authors, by
32 public class BrowserPanel
extends JPanel
{
33 private static final long serialVersionUID
= 1L;
36 * The {@link ActionEvent} you receive from
37 * {@link BrowserPanel#addActionListener(ActionListener)} can return this as a
38 * command (see {@link ActionEvent#getActionCommand()}) if they were created in
39 * the scope of a source.
41 static public final String SOURCE_SELECTION
= "source_selection";
43 * The {@link ActionEvent} you receive from
44 * {@link BrowserPanel#addActionListener(ActionListener)} can return this as a
45 * command (see {@link ActionEvent#getActionCommand()}) if they were created in
46 * the scope of an author.
48 static public final String AUTHOR_SELECTION
= "author_selection";
50 * The {@link ActionEvent} you receive from
51 * {@link BrowserPanel#addActionListener(ActionListener)} can return this as a
52 * command (see {@link ActionEvent#getActionCommand()}) if they were created in
55 static public final String TAGS_SELECTION
= "tags_selection";
57 private JTabbedPane tabs
;
58 private SourceTab sourceTab
;
59 private AuthorTab authorTab
;
60 private TagsTab tagsTab
;
62 private boolean keepSelection
;
65 * Create a nesw {@link BrowserPanel}.
67 public BrowserPanel() {
68 this.setPreferredSize(new Dimension(200, 800));
70 this.setLayout(new BorderLayout());
71 tabs
= new JTabbedPane();
74 tabs
.add(sourceTab
= new SourceTab(index
++, SOURCE_SELECTION
));
75 tabs
.add(authorTab
= new AuthorTab(index
++, AUTHOR_SELECTION
));
76 tabs
.add(tagsTab
= new TagsTab(index
++, TAGS_SELECTION
));
78 setText(tabs
, sourceTab
, "Sources", "Tooltip for Sources");
79 setText(tabs
, authorTab
, "Authors", "Tooltip for Authors");
80 setText(tabs
, tagsTab
, "Tags", "Tooltip for Tags");
82 JPanel options
= new JPanel();
83 options
.setLayout(new BorderLayout());
85 final JButton keep
= new JButton("Keep selection");
86 UiHelper
.setButtonPressed(keep
, keepSelection
);
87 keep
.addActionListener(new ActionListener() {
89 public void actionPerformed(ActionEvent e
) {
90 keepSelection
= !keepSelection
;
91 UiHelper
.setButtonPressed(keep
, keepSelection
);
92 keep
.setSelected(keepSelection
);
99 options
.add(keep
, BorderLayout
.CENTER
);
101 add(tabs
, BorderLayout
.CENTER
);
102 add(options
, BorderLayout
.SOUTH
);
104 tabs
.addChangeListener(new ChangeListener() {
106 public void stateChanged(ChangeEvent e
) {
107 if (!keepSelection
) {
114 @SuppressWarnings("rawtypes")
115 private void unselect() {
116 for (int i
= 0; i
< tabs
.getTabCount(); i
++) {
117 if (i
== tabs
.getSelectedIndex())
120 BasicTab tab
= (BasicTab
) tabs
.getComponent(i
);
125 private void setText(JTabbedPane tabs
, @SuppressWarnings("rawtypes") BasicTab tab
, String name
, String tooltip
) {
126 tab
.setBaseTitle(name
);
127 tabs
.setTitleAt(tab
.getIndex(), tab
.getTitle());
128 tabs
.setToolTipTextAt(tab
.getIndex(), tooltip
);
129 listenTitleChange(tabs
, tab
);
132 private void listenTitleChange(final JTabbedPane tabs
, @SuppressWarnings("rawtypes") final BasicTab tab
) {
133 tab
.addActionListener(new ActionListener() {
135 public void actionPerformed(ActionEvent e
) {
136 tabs
.setTitleAt(tab
.getIndex(), tab
.getTitle());
142 * Get the {@link BookInfo} to highlight, even if more than one are selected.
144 * Return NULL when nothing is selected.
146 * @return the {@link BookInfo} to highlight, can be NULL
148 public BookInfo
getHighlight() {
149 String selected1
= null;
150 Component selectedTab
= tabs
.getSelectedComponent();
151 if (selectedTab
instanceof BasicTab
) {
152 @SuppressWarnings({ "unchecked", "rawtypes" })
153 List
<String
> selectedAll
= ((BasicTab
) selectedTab
).getSelectedElements();
154 if (!selectedAll
.isEmpty()) {
155 selected1
= selectedAll
.get(0);
159 if (selected1
!= null) {
160 BasicLibrary lib
= Instance
.getInstance().getLibrary();
161 if (tabs
.getSelectedComponent() == sourceTab
) {
162 return BookInfo
.fromSource(lib
, selected1
);
163 } else if (tabs
.getSelectedComponent() == authorTab
) {
164 return BookInfo
.fromAuthor(lib
, selected1
);
165 } else if (tabs
.getSelectedComponent() == tagsTab
) {
166 return BookInfo
.fromTag(lib
, selected1
);
174 * The currently selected sources, or an empty list.
176 * @return the sources (cannot be NULL)
178 public List
<String
> getSelectedSources() {
179 return sourceTab
.getSelectedElements();
183 * The currently selected authors, or an empty list.
185 * @return the sources (cannot be NULL)
187 public List
<String
> getSelectedAuthors() {
188 return authorTab
.getSelectedElements();
192 * The currently selected tags, or an empty list.
194 * @return the sources (cannot be NULL)
196 public List
<String
> getSelectedTags() {
197 return tagsTab
.getSelectedElements();
201 * Reload all the data from the 3 tabs.
203 public void reloadData() {
204 sourceTab
.reloadData();
205 authorTab
.reloadData();
206 tagsTab
.reloadData();
210 * Adds the specified action listener to receive action events from this
213 * @param listener the action listener to be added
215 public synchronized void addActionListener(ActionListener listener
) {
216 sourceTab
.addActionListener(listener
);
217 authorTab
.addActionListener(listener
);
218 tagsTab
.addActionListener(listener
);
222 * Removes the specified action listener so that it no longer receives action
223 * events from this {@link SearchBar}.
225 * @param listener the action listener to be removed
227 public synchronized void removeActionListener(ActionListener listener
) {
228 sourceTab
.removeActionListener(listener
);
229 authorTab
.removeActionListener(listener
);
230 tagsTab
.removeActionListener(listener
);