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
.bundles
.StringIdGui
;
19 import be
.nikiroo
.fanfix
.data
.Chapter
;
20 import be
.nikiroo
.fanfix
.data
.MetaData
;
21 import be
.nikiroo
.fanfix
.data
.Story
;
22 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
25 * An internal, Swing-based {@link Story} viewer.
27 * Works on both text and image document (see {@link MetaData#isImageDocument()}
32 public class GuiReaderViewer
extends JFrame
{
33 private static final long serialVersionUID
= 1L;
36 private MetaData meta
;
38 private JLabel chapterLabel
;
39 private GuiReaderPropertiesPane descPane
;
40 private int currentChapter
= -42; // cover = -1
41 private GuiReaderViewerPanel mainPanel
;
42 private JButton
[] navButtons
;
45 * Create a new {@link Story} viewer.
48 * the {@link BasicLibrary} to load the cover from
50 * the {@link Story} to display
52 public GuiReaderViewer(BasicLibrary lib
, Story story
) {
53 setTitle(GuiReader
.trans(StringIdGui
.TITLE_STORY
, story
.getMeta()
54 .getLuid(), story
.getMeta().getTitle()));
59 this.meta
= story
.getMeta();
68 * Initialise the base panel with everything but the navigation buttons.
71 * the {@link BasicLibrary} to use to retrieve the cover image in
72 * the description panel
74 private void initGuiBase(BasicLibrary lib
) {
75 setLayout(new BorderLayout());
78 title
.setFont(new Font(Font
.SERIF
, Font
.BOLD
,
79 title
.getFont().getSize() * 3));
80 title
.setText(meta
.getTitle());
81 title
.setHorizontalAlignment(SwingConstants
.CENTER
);
82 title
.setBorder(BorderFactory
.createEmptyBorder(10, 10, 10, 10));
83 add(title
, BorderLayout
.NORTH
);
85 JPanel contentPane
= new JPanel(new BorderLayout());
86 add(contentPane
, BorderLayout
.CENTER
);
88 descPane
= new GuiReaderPropertiesPane(lib
, meta
);
89 contentPane
.add(descPane
, BorderLayout
.NORTH
);
91 mainPanel
= new GuiReaderViewerPanel(story
);
92 contentPane
.add(mainPanel
, BorderLayout
.CENTER
);
96 * Create the 4 navigation buttons in {@link GuiReaderViewer#navButtons} and
99 private void initGuiNavButtons() {
100 JPanel navButtonsPane
= new JPanel();
101 LayoutManager layout
= new BoxLayout(navButtonsPane
, BoxLayout
.X_AXIS
);
102 navButtonsPane
.setLayout(layout
);
104 navButtons
= new JButton
[4];
106 navButtons
[0] = createNavButton("<<", new ActionListener() {
108 public void actionPerformed(ActionEvent e
) {
112 navButtons
[1] = createNavButton(" < ", new ActionListener() {
114 public void actionPerformed(ActionEvent e
) {
115 setChapter(currentChapter
- 1);
118 navButtons
[2] = createNavButton(" > ", new ActionListener() {
120 public void actionPerformed(ActionEvent e
) {
121 setChapter(currentChapter
+ 1);
124 navButtons
[3] = createNavButton(">>", new ActionListener() {
126 public void actionPerformed(ActionEvent e
) {
127 setChapter(story
.getChapters().size() - 1);
131 for (JButton navButton
: navButtons
) {
132 navButtonsPane
.add(navButton
);
135 add(navButtonsPane
, BorderLayout
.SOUTH
);
137 chapterLabel
= new JLabel("");
138 navButtonsPane
.add(chapterLabel
);
142 * Create a single navigation button.
145 * the text to display
147 * the action to take on click
150 private JButton
createNavButton(String text
, ActionListener action
) {
151 JButton navButton
= new JButton(text
);
152 navButton
.addActionListener(action
);
153 navButton
.setForeground(Color
.BLUE
);
158 * Set the current chapter, 0-based.
160 * Chapter -1 is reserved for the description page.
163 * the chapter number to set
165 private void setChapter(int chapter
) {
166 navButtons
[0].setEnabled(chapter
>= 0);
167 navButtons
[1].setEnabled(chapter
>= 0);
168 navButtons
[2].setEnabled(chapter
+ 1 < story
.getChapters().size());
169 navButtons
[3].setEnabled(chapter
+ 1 < story
.getChapters().size());
171 if (chapter
>= -1 && chapter
< story
.getChapters().size()
172 && chapter
!= currentChapter
) {
173 currentChapter
= chapter
;
177 chap
= meta
.getResume();
178 descPane
.setVisible(true);
180 chap
= story
.getChapters().get(chapter
);
181 descPane
.setVisible(false);
184 String chapterDisplay
= GuiReader
.trans(
185 StringIdGui
.CHAPTER_HTML_UNNAMED
, chap
.getNumber(), story
186 .getChapters().size());
187 if (chap
.getName() != null && !chap
.getName().trim().isEmpty()) {
188 chapterDisplay
= GuiReader
.trans(
189 StringIdGui
.CHAPTER_HTML_NAMED
, chap
.getNumber(), story
190 .getChapters().size(), chap
.getName());
193 chapterLabel
.setText("<HTML>" + chapterDisplay
+ "</HTML>");
195 mainPanel
.setChapter(chap
);