search: cleanup
[nikiroo-utils.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderViewer.java
CommitLineData
47b1710c
NR
1package be.nikiroo.fanfix.reader.ui;
2
3import java.awt.BorderLayout;
4import java.awt.Color;
5import java.awt.Font;
6import java.awt.LayoutManager;
7import java.awt.event.ActionEvent;
8import java.awt.event.ActionListener;
9
10import javax.swing.BorderFactory;
11import javax.swing.BoxLayout;
12import javax.swing.JButton;
13import javax.swing.JFrame;
14import javax.swing.JLabel;
15import javax.swing.JPanel;
47b1710c
NR
16import javax.swing.SwingConstants;
17
5bc9573b 18import be.nikiroo.fanfix.bundles.StringIdGui;
47b1710c
NR
19import be.nikiroo.fanfix.data.Chapter;
20import be.nikiroo.fanfix.data.MetaData;
47b1710c
NR
21import be.nikiroo.fanfix.data.Story;
22import be.nikiroo.fanfix.library.BasicLibrary;
23
32ba91e9
NR
24/**
25 * An internal, Swing-based {@link Story} viewer.
26 * <p>
27 * Works on both text and image document (see {@link MetaData#isImageDocument()}
28 * ).
29 *
30 * @author niki
31 */
32public class GuiReaderViewer extends JFrame {
47b1710c
NR
33 private static final long serialVersionUID = 1L;
34
35 private Story story;
36 private MetaData meta;
37 private JLabel title;
47b1710c
NR
38 private JLabel chapterLabel;
39 private GuiReaderPropertiesPane descPane;
40 private int currentChapter = -42; // cover = -1
32ba91e9
NR
41 private GuiReaderViewerPanel mainPanel;
42 private JButton[] navButtons;
43
44 /**
45 * Create a new {@link Story} viewer.
46 *
47 * @param lib
48 * the {@link BasicLibrary} to load the cover from
49 * @param story
50 * the {@link Story} to display
51 */
52 public GuiReaderViewer(BasicLibrary lib, Story story) {
5bc9573b
NR
53 setTitle(GuiReader.trans(StringIdGui.TITLE_STORY, story.getMeta()
54 .getLuid(), story.getMeta().getTitle()));
47b1710c
NR
55
56 setSize(800, 600);
57
58 this.story = story;
59 this.meta = story.getMeta();
60
61 initGuiBase(lib);
62 initGuiNavButtons();
63
64 setChapter(-1);
65 }
66
32ba91e9
NR
67 /**
68 * Initialise the base panel with everything but the navigation buttons.
69 *
70 * @param lib
71 * the {@link BasicLibrary} to use to retrieve the cover image in
72 * the description panel
73 */
47b1710c
NR
74 private void initGuiBase(BasicLibrary lib) {
75 setLayout(new BorderLayout());
76
77 title = new JLabel();
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);
84
85 JPanel contentPane = new JPanel(new BorderLayout());
86 add(contentPane, BorderLayout.CENTER);
87
88 descPane = new GuiReaderPropertiesPane(lib, meta);
89 contentPane.add(descPane, BorderLayout.NORTH);
90
32ba91e9
NR
91 mainPanel = new GuiReaderViewerPanel(story);
92 contentPane.add(mainPanel, BorderLayout.CENTER);
47b1710c
NR
93 }
94
32ba91e9
NR
95 /**
96 * Create the 4 navigation buttons in {@link GuiReaderViewer#navButtons} and
97 * initialise them.
98 */
47b1710c 99 private void initGuiNavButtons() {
32ba91e9
NR
100 JPanel navButtonsPane = new JPanel();
101 LayoutManager layout = new BoxLayout(navButtonsPane, BoxLayout.X_AXIS);
102 navButtonsPane.setLayout(layout);
47b1710c 103
32ba91e9
NR
104 navButtons = new JButton[4];
105
106 navButtons[0] = createNavButton("<<", new ActionListener() {
47b1710c
NR
107 @Override
108 public void actionPerformed(ActionEvent e) {
109 setChapter(-1);
110 }
32ba91e9
NR
111 });
112 navButtons[1] = createNavButton(" < ", new ActionListener() {
47b1710c
NR
113 @Override
114 public void actionPerformed(ActionEvent e) {
115 setChapter(currentChapter - 1);
116 }
32ba91e9
NR
117 });
118 navButtons[2] = createNavButton(" > ", new ActionListener() {
47b1710c
NR
119 @Override
120 public void actionPerformed(ActionEvent e) {
121 setChapter(currentChapter + 1);
122 }
32ba91e9
NR
123 });
124 navButtons[3] = createNavButton(">>", new ActionListener() {
47b1710c
NR
125 @Override
126 public void actionPerformed(ActionEvent e) {
127 setChapter(story.getChapters().size() - 1);
128 }
32ba91e9 129 });
47b1710c 130
32ba91e9
NR
131 for (JButton navButton : navButtons) {
132 navButtonsPane.add(navButton);
133 }
134
135 add(navButtonsPane, BorderLayout.SOUTH);
47b1710c
NR
136
137 chapterLabel = new JLabel("");
32ba91e9 138 navButtonsPane.add(chapterLabel);
47b1710c
NR
139 }
140
32ba91e9
NR
141 /**
142 * Create a single navigation button.
143 *
144 * @param text
145 * the text to display
146 * @param action
147 * the action to take on click
148 * @return the button
149 */
47b1710c
NR
150 private JButton createNavButton(String text, ActionListener action) {
151 JButton navButton = new JButton(text);
152 navButton.addActionListener(action);
153 navButton.setForeground(Color.BLUE);
154 return navButton;
155 }
156
32ba91e9
NR
157 /**
158 * Set the current chapter, 0-based.
159 * <p>
160 * Chapter -1 is reserved for the description page.
161 *
162 * @param chapter
163 * the chapter number to set
164 */
47b1710c 165 private void setChapter(int chapter) {
32ba91e9
NR
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());
170
47b1710c
NR
171 if (chapter >= -1 && chapter < story.getChapters().size()
172 && chapter != currentChapter) {
173 currentChapter = chapter;
174
175 Chapter chap;
176 if (chapter == -1) {
177 chap = meta.getResume();
32ba91e9 178 descPane.setVisible(true);
47b1710c
NR
179 } else {
180 chap = story.getChapters().get(chapter);
181 descPane.setVisible(false);
182 }
183
5bc9573b
NR
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());
191 }
192
193 chapterLabel.setText("<HTML>" + chapterDisplay + "</HTML>");
47b1710c 194
32ba91e9 195 mainPanel.setChapter(chap);
47b1710c
NR
196 }
197 }
47b1710c 198}