c8b171367749d778f595b8926f51430b8ead8cb9
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
.JPanel
;
12 import javax
.swing
.JTabbedPane
;
13 import javax
.swing
.event
.ChangeEvent
;
14 import javax
.swing
.event
.ChangeListener
;
16 import be
.nikiroo
.fanfix
.Instance
;
17 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
18 import be
.nikiroo
.fanfix_swing
.gui
.book
.BookInfo
;
19 import be
.nikiroo
.fanfix_swing
.gui
.browser
.AuthorTab
;
20 import be
.nikiroo
.fanfix_swing
.gui
.browser
.BasicTab
;
21 import be
.nikiroo
.fanfix_swing
.gui
.browser
.SourceTab
;
22 import be
.nikiroo
.fanfix_swing
.gui
.browser
.TagsTab
;
23 import be
.nikiroo
.fanfix_swing
.gui
.utils
.ListenerPanel
;
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 ListenerPanel
{
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 * The {@link ActionEvent} you receive from
58 * {@link BrowserPanel#addActionListener(ActionListener)} can return this as a
59 * command (see {@link ActionEvent#getActionCommand()}) if they were created in
60 * the scope of a tab change.
62 static public final String TAB_CHANGE
= "tab_change";
64 private JTabbedPane tabs
;
65 private SourceTab sourceTab
;
66 private AuthorTab authorTab
;
67 private TagsTab tagsTab
;
69 private boolean keepSelection
;
72 * Create a nesw {@link BrowserPanel}.
74 public BrowserPanel() {
75 this.setPreferredSize(new Dimension(200, 800));
77 this.setLayout(new BorderLayout());
78 tabs
= new JTabbedPane();
81 tabs
.add(sourceTab
= new SourceTab(index
++, SOURCE_SELECTION
));
82 tabs
.add(authorTab
= new AuthorTab(index
++, AUTHOR_SELECTION
));
83 tabs
.add(tagsTab
= new TagsTab(index
++, TAGS_SELECTION
));
85 configureTab(tabs
, sourceTab
, "Sources", "Tooltip for Sources");
86 configureTab(tabs
, authorTab
, "Authors", "Tooltip for Authors");
87 configureTab(tabs
, tagsTab
, "Tags", "Tooltip for Tags");
89 JPanel options
= new JPanel();
90 options
.setLayout(new BorderLayout());
92 final JButton keep
= new JButton("Keep selection");
93 UiHelper
.setButtonPressed(keep
, keepSelection
);
94 keep
.addActionListener(new ActionListener() {
96 public void actionPerformed(ActionEvent e
) {
97 keepSelection
= !keepSelection
;
98 UiHelper
.setButtonPressed(keep
, keepSelection
);
99 keep
.setSelected(keepSelection
);
100 if (!keepSelection
) {
106 options
.add(keep
, BorderLayout
.CENTER
);
108 add(tabs
, BorderLayout
.CENTER
);
109 add(options
, BorderLayout
.SOUTH
);
111 tabs
.addChangeListener(new ChangeListener() {
113 public void stateChanged(ChangeEvent e
) {
114 if (!keepSelection
) {
118 fireActionPerformed(TAB_CHANGE
);
123 @SuppressWarnings("rawtypes")
124 private void unselect() {
125 for (int i
= 0; i
< tabs
.getTabCount(); i
++) {
126 if (i
== tabs
.getSelectedIndex())
129 BasicTab tab
= (BasicTab
) tabs
.getComponent(i
);
134 private void configureTab(JTabbedPane tabs
, @SuppressWarnings("rawtypes") BasicTab tab
, String name
,
136 tab
.setBaseTitle(name
);
137 tabs
.setTitleAt(tab
.getIndex(), tab
.getTitle());
138 tabs
.setToolTipTextAt(tab
.getIndex(), tooltip
);
139 listenTabs(tabs
, tab
);
142 private void listenTabs(final JTabbedPane tabs
, @SuppressWarnings("rawtypes") final BasicTab tab
) {
143 tab
.addActionListener(new ActionListener() {
145 public void actionPerformed(ActionEvent e
) {
146 tabs
.setTitleAt(tab
.getIndex(), tab
.getTitle());
147 fireActionPerformed(e
.getActionCommand());
153 * Get the {@link BookInfo} to highlight, even if none or more than one are
156 * Return a special "all" {@link BookInfo} of the correct type when nothing is
159 * @return the {@link BookInfo} to highlight, can be NULL
161 public BookInfo
getHighlight() {
162 String selected1
= null;
163 Component selectedTab
= tabs
.getSelectedComponent();
164 if (selectedTab
instanceof BasicTab
) {
165 @SuppressWarnings({ "unchecked", "rawtypes" })
166 List
<String
> selectedAll
= ((BasicTab
) selectedTab
).getSelectedElements();
167 if (!selectedAll
.isEmpty()) {
168 selected1
= selectedAll
.get(0);
172 BasicLibrary lib
= Instance
.getInstance().getLibrary();
173 if (tabs
.getSelectedComponent() == sourceTab
) {
174 return BookInfo
.fromSource(lib
, selected1
);
175 } else if (tabs
.getSelectedComponent() == authorTab
) {
176 return BookInfo
.fromAuthor(lib
, selected1
);
177 } else if (tabs
.getSelectedComponent() == tagsTab
) {
178 return BookInfo
.fromTag(lib
, selected1
);
186 * The currently selected sources, or an empty list.
188 * @return the sources (cannot be NULL)
190 public List
<String
> getSelectedSources() {
191 return sourceTab
.getSelectedElements();
195 * The currently selected authors, or an empty list.
197 * @return the sources (cannot be NULL)
199 public List
<String
> getSelectedAuthors() {
200 return authorTab
.getSelectedElements();
204 * The currently selected tags, or an empty list.
206 * @return the sources (cannot be NULL)
208 public List
<String
> getSelectedTags() {
209 return tagsTab
.getSelectedElements();
213 * Reload all the data from the 3 tabs.
215 public void reloadData() {
216 sourceTab
.reloadData();
217 authorTab
.reloadData();
218 tagsTab
.reloadData();