1 package be
.nikiroo
.fanfix
.reader
.ui
;
3 import java
.awt
.BorderLayout
;
6 import java
.awt
.LayoutManager
;
7 import java
.awt
.event
.ActionEvent
;
8 import java
.awt
.event
.ActionListener
;
10 import javax
.swing
.BorderFactory
;
11 import javax
.swing
.BoxLayout
;
12 import javax
.swing
.JButton
;
13 import javax
.swing
.JFrame
;
14 import javax
.swing
.JLabel
;
15 import javax
.swing
.JPanel
;
16 import javax
.swing
.SwingConstants
;
18 import be
.nikiroo
.fanfix
.data
.Chapter
;
19 import be
.nikiroo
.fanfix
.data
.MetaData
;
20 import be
.nikiroo
.fanfix
.data
.Story
;
21 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
24 * An internal, Swing-based {@link Story} viewer.
26 * Works on both text and image document (see {@link MetaData#isImageDocument()}
31 public class GuiReaderViewer
extends JFrame
{
32 private static final long serialVersionUID
= 1L;
35 private MetaData meta
;
37 private JLabel chapterLabel
;
38 private GuiReaderPropertiesPane descPane
;
39 private int currentChapter
= -42; // cover = -1
40 private GuiReaderViewerPanel mainPanel
;
41 private JButton
[] navButtons
;
44 * Create a new {@link Story} viewer.
47 * the {@link BasicLibrary} to load the cover from
49 * the {@link Story} to display
51 public GuiReaderViewer(BasicLibrary lib
, Story story
) {
52 super(story
.getMeta().getLuid() + ": " + story
.getMeta().getTitle());
57 this.meta
= story
.getMeta();
66 * Initialise the base panel with everything but the navigation buttons.
69 * the {@link BasicLibrary} to use to retrieve the cover image in
70 * the description panel
72 private void initGuiBase(BasicLibrary lib
) {
73 setLayout(new BorderLayout());
76 title
.setFont(new Font(Font
.SERIF
, Font
.BOLD
,
77 title
.getFont().getSize() * 3));
78 title
.setText(meta
.getTitle());
79 title
.setHorizontalAlignment(SwingConstants
.CENTER
);
80 title
.setBorder(BorderFactory
.createEmptyBorder(10, 10, 10, 10));
81 add(title
, BorderLayout
.NORTH
);
83 JPanel contentPane
= new JPanel(new BorderLayout());
84 add(contentPane
, BorderLayout
.CENTER
);
86 descPane
= new GuiReaderPropertiesPane(lib
, meta
);
87 contentPane
.add(descPane
, BorderLayout
.NORTH
);
89 mainPanel
= new GuiReaderViewerPanel(story
);
90 contentPane
.add(mainPanel
, BorderLayout
.CENTER
);
94 * Create the 4 navigation buttons in {@link GuiReaderViewer#navButtons} and
97 private void initGuiNavButtons() {
98 JPanel navButtonsPane
= new JPanel();
99 LayoutManager layout
= new BoxLayout(navButtonsPane
, BoxLayout
.X_AXIS
);
100 navButtonsPane
.setLayout(layout
);
102 navButtons
= new JButton
[4];
104 navButtons
[0] = createNavButton("<<", new ActionListener() {
106 public void actionPerformed(ActionEvent e
) {
110 navButtons
[1] = createNavButton(" < ", new ActionListener() {
112 public void actionPerformed(ActionEvent e
) {
113 setChapter(currentChapter
- 1);
116 navButtons
[2] = createNavButton(" > ", new ActionListener() {
118 public void actionPerformed(ActionEvent e
) {
119 setChapter(currentChapter
+ 1);
122 navButtons
[3] = createNavButton(">>", new ActionListener() {
124 public void actionPerformed(ActionEvent e
) {
125 setChapter(story
.getChapters().size() - 1);
129 for (JButton navButton
: navButtons
) {
130 navButtonsPane
.add(navButton
);
133 add(navButtonsPane
, BorderLayout
.SOUTH
);
135 chapterLabel
= new JLabel("");
136 navButtonsPane
.add(chapterLabel
);
140 * Create a single navigation button.
143 * the text to display
145 * the action to take on click
148 private JButton
createNavButton(String text
, ActionListener action
) {
149 JButton navButton
= new JButton(text
);
150 navButton
.addActionListener(action
);
151 navButton
.setForeground(Color
.BLUE
);
156 * Set the current chapter, 0-based.
158 * Chapter -1 is reserved for the description page.
161 * the chapter number to set
163 private void setChapter(int chapter
) {
164 navButtons
[0].setEnabled(chapter
>= 0);
165 navButtons
[1].setEnabled(chapter
>= 0);
166 navButtons
[2].setEnabled(chapter
+ 1 < story
.getChapters().size());
167 navButtons
[3].setEnabled(chapter
+ 1 < story
.getChapters().size());
169 if (chapter
>= -1 && chapter
< story
.getChapters().size()
170 && chapter
!= currentChapter
) {
171 currentChapter
= chapter
;
175 chap
= meta
.getResume();
176 descPane
.setVisible(true);
178 chap
= story
.getChapters().get(chapter
);
179 descPane
.setVisible(false);
182 chapterLabel
.setText("<HTML> <B>Chapter "
183 + chap
.getNumber() + "</B>: " + chap
.getName() + "</HTML>");
185 mainPanel
.setChapter(chap
);