test: improve flag files
[nikiroo-utils.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderViewer.java
1 package be.nikiroo.fanfix.reader.ui;
2
3 import java.awt.BorderLayout;
4 import java.awt.Font;
5 import java.awt.LayoutManager;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8
9 import javax.swing.BorderFactory;
10 import javax.swing.BoxLayout;
11 import javax.swing.JFrame;
12 import javax.swing.JLabel;
13 import javax.swing.JPanel;
14 import javax.swing.SwingConstants;
15
16 import be.nikiroo.fanfix.bundles.StringIdGui;
17 import be.nikiroo.fanfix.data.Chapter;
18 import be.nikiroo.fanfix.data.MetaData;
19 import be.nikiroo.fanfix.data.Story;
20 import be.nikiroo.fanfix.library.BasicLibrary;
21
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 */
30 public class GuiReaderViewer extends JFrame {
31 private static final long serialVersionUID = 1L;
32
33 private Story story;
34 private MetaData meta;
35 private JLabel title;
36 private GuiReaderPropertiesPane descPane;
37 private GuiReaderViewerPanel mainPanel;
38 private GuiReaderNavBar navbar;
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) {
49 setTitle(GuiReader.trans(StringIdGui.TITLE_STORY, story.getMeta()
50 .getLuid(), story.getMeta().getTitle()));
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
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 */
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
87 mainPanel = new GuiReaderViewerPanel(story);
88 contentPane.add(mainPanel, BorderLayout.CENTER);
89 }
90
91 /**
92 * Create the 4 navigation buttons in {@link GuiReaderViewer#navButtons} and
93 * initialise them.
94 */
95 private void initGuiNavButtons() {
96 navbar = new GuiReaderNavBar(-1, story.getChapters().size() - 1) {
97 private static final long serialVersionUID = 1L;
98
99 @Override
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>";
121 }
122 };
123
124 navbar.addActionListener(new ActionListener() {
125 @Override
126 public void actionPerformed(ActionEvent e) {
127 setChapter(navbar.getIndex());
128 }
129 });
130
131 JPanel navButtonsPane = new JPanel();
132 LayoutManager layout = new BoxLayout(navButtonsPane, BoxLayout.X_AXIS);
133 navButtonsPane.setLayout(layout);
134
135 add(navbar, BorderLayout.SOUTH);
136 }
137
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 */
146 private void setChapter(int chapter) {
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);
154 }
155
156 mainPanel.setChapter(chap);
157 }
158 }