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