export page navigation to own class
[fanfix.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderViewer.java
CommitLineData
47b1710c
NR
1package be.nikiroo.fanfix.reader.ui;
2
3import java.awt.BorderLayout;
47b1710c
NR
4import java.awt.Font;
5import java.awt.LayoutManager;
6import java.awt.event.ActionEvent;
7import java.awt.event.ActionListener;
8
9import javax.swing.BorderFactory;
10import javax.swing.BoxLayout;
47b1710c
NR
11import javax.swing.JFrame;
12import javax.swing.JLabel;
13import javax.swing.JPanel;
47b1710c
NR
14import javax.swing.SwingConstants;
15
5bc9573b 16import be.nikiroo.fanfix.bundles.StringIdGui;
47b1710c
NR
17import be.nikiroo.fanfix.data.Chapter;
18import be.nikiroo.fanfix.data.MetaData;
47b1710c
NR
19import be.nikiroo.fanfix.data.Story;
20import be.nikiroo.fanfix.library.BasicLibrary;
21
32ba91e9
NR
22/**
23 * An internal, Swing-based {@link Story} viewer.
24 * <p>
25 * Works on both text and image document (see {@link MetaData#isImageDocument()}
26 * ).
27 *
28 * @author niki
29 */
30public class GuiReaderViewer extends JFrame {
47b1710c
NR
31 private static final long serialVersionUID = 1L;
32
33 private Story story;
34 private MetaData meta;
35 private JLabel title;
47b1710c 36 private GuiReaderPropertiesPane descPane;
32ba91e9 37 private GuiReaderViewerPanel mainPanel;
cfb18ec5 38 private GuiReaderNavBar navbar;
32ba91e9
NR
39
40 /**
41 * Create a new {@link Story} viewer.
42 *
43 * @param lib
44 * the {@link BasicLibrary} to load the cover from
45 * @param story
46 * the {@link Story} to display
47 */
48 public GuiReaderViewer(BasicLibrary lib, Story story) {
5bc9573b
NR
49 setTitle(GuiReader.trans(StringIdGui.TITLE_STORY, story.getMeta()
50 .getLuid(), story.getMeta().getTitle()));
47b1710c
NR
51
52 setSize(800, 600);
53
54 this.story = story;
55 this.meta = story.getMeta();
56
57 initGuiBase(lib);
58 initGuiNavButtons();
59
60 setChapter(-1);
61 }
62
32ba91e9
NR
63 /**
64 * Initialise the base panel with everything but the navigation buttons.
65 *
66 * @param lib
67 * the {@link BasicLibrary} to use to retrieve the cover image in
68 * the description panel
69 */
47b1710c
NR
70 private void initGuiBase(BasicLibrary lib) {
71 setLayout(new BorderLayout());
72
73 title = new JLabel();
74 title.setFont(new Font(Font.SERIF, Font.BOLD,
75 title.getFont().getSize() * 3));
76 title.setText(meta.getTitle());
77 title.setHorizontalAlignment(SwingConstants.CENTER);
78 title.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
79 add(title, BorderLayout.NORTH);
80
81 JPanel contentPane = new JPanel(new BorderLayout());
82 add(contentPane, BorderLayout.CENTER);
83
84 descPane = new GuiReaderPropertiesPane(lib, meta);
85 contentPane.add(descPane, BorderLayout.NORTH);
86
32ba91e9
NR
87 mainPanel = new GuiReaderViewerPanel(story);
88 contentPane.add(mainPanel, BorderLayout.CENTER);
47b1710c
NR
89 }
90
32ba91e9
NR
91 /**
92 * Create the 4 navigation buttons in {@link GuiReaderViewer#navButtons} and
93 * initialise them.
94 */
47b1710c 95 private void initGuiNavButtons() {
cfb18ec5
NR
96 navbar = new GuiReaderNavBar(-1, story.getChapters().size() - 1) {
97 private static final long serialVersionUID = 1L;
32ba91e9 98
47b1710c 99 @Override
cfb18ec5
NR
100 protected String computeLabel(int index, int min, int max) {
101 int chapter = index;
102 Chapter chap;
103 if (chapter < 0) {
104 chap = meta.getResume();
105 descPane.setVisible(true);
106 } else {
107 chap = story.getChapters().get(chapter);
108 descPane.setVisible(false);
109 }
110
111 String chapterDisplay = GuiReader.trans(
112 StringIdGui.CHAPTER_HTML_UNNAMED, chap.getNumber(),
113 story.getChapters().size());
114 if (chap.getName() != null && !chap.getName().trim().isEmpty()) {
115 chapterDisplay = GuiReader.trans(
116 StringIdGui.CHAPTER_HTML_NAMED, chap.getNumber(),
117 story.getChapters().size(), chap.getName());
118 }
119
120 return "<HTML>" + chapterDisplay + "</HTML>";
47b1710c 121 }
cfb18ec5
NR
122 };
123
124 navbar.addActionListener(new ActionListener() {
47b1710c
NR
125 @Override
126 public void actionPerformed(ActionEvent e) {
cfb18ec5 127 setChapter(navbar.getIndex());
47b1710c 128 }
32ba91e9 129 });
47b1710c 130
cfb18ec5
NR
131 JPanel navButtonsPane = new JPanel();
132 LayoutManager layout = new BoxLayout(navButtonsPane, BoxLayout.X_AXIS);
133 navButtonsPane.setLayout(layout);
47b1710c 134
cfb18ec5 135 add(navbar, BorderLayout.SOUTH);
47b1710c
NR
136 }
137
32ba91e9
NR
138 /**
139 * Set the current chapter, 0-based.
140 * <p>
141 * Chapter -1 is reserved for the description page.
142 *
143 * @param chapter
144 * the chapter number to set
145 */
47b1710c 146 private void setChapter(int chapter) {
cfb18ec5
NR
147 Chapter chap;
148 if (chapter < 0) {
149 chap = meta.getResume();
150 descPane.setVisible(true);
151 } else {
152 chap = story.getChapters().get(chapter);
153 descPane.setVisible(false);
47b1710c 154 }
cfb18ec5
NR
155
156 mainPanel.setChapter(chap);
47b1710c 157 }
47b1710c 158}