| 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.JScrollPane; |
| 17 | import javax.swing.SwingConstants; |
| 18 | |
| 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; |
| 23 | |
| 24 | public class GuiReaderTextViewer extends JFrame { |
| 25 | private static final long serialVersionUID = 1L; |
| 26 | |
| 27 | private Story story; |
| 28 | private MetaData meta; |
| 29 | private JLabel title; |
| 30 | private JLabel text; |
| 31 | private JLabel chapterLabel; |
| 32 | private GuiReaderPropertiesPane descPane; |
| 33 | private int currentChapter = -42; // cover = -1 |
| 34 | private GuiReaderTextViewerOutput htmlOutput = new GuiReaderTextViewerOutput(); |
| 35 | |
| 36 | public GuiReaderTextViewer(BasicLibrary lib, Story story) { |
| 37 | super(story.getMeta().getLuid() + ": " + story.getMeta().getTitle()); |
| 38 | |
| 39 | setSize(800, 600); |
| 40 | |
| 41 | this.story = story; |
| 42 | this.meta = story.getMeta(); |
| 43 | |
| 44 | initGuiBase(lib); |
| 45 | initGuiNavButtons(); |
| 46 | |
| 47 | setChapter(-1); |
| 48 | } |
| 49 | |
| 50 | private void initGuiBase(BasicLibrary lib) { |
| 51 | setLayout(new BorderLayout()); |
| 52 | |
| 53 | title = new JLabel(); |
| 54 | title.setFont(new Font(Font.SERIF, Font.BOLD, |
| 55 | title.getFont().getSize() * 3)); |
| 56 | title.setText(meta.getTitle()); |
| 57 | title.setHorizontalAlignment(SwingConstants.CENTER); |
| 58 | title.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); |
| 59 | add(title, BorderLayout.NORTH); |
| 60 | |
| 61 | JPanel contentPane = new JPanel(new BorderLayout()); |
| 62 | add(contentPane, BorderLayout.CENTER); |
| 63 | |
| 64 | descPane = new GuiReaderPropertiesPane(lib, meta); |
| 65 | contentPane.add(descPane, BorderLayout.NORTH); |
| 66 | |
| 67 | text = new JLabel(); |
| 68 | text.setAlignmentY(TOP_ALIGNMENT); |
| 69 | |
| 70 | // TODO: why is it broken? |
| 71 | // text.setPreferredSize(new Dimension(500, 500)); |
| 72 | |
| 73 | JScrollPane scroll = new JScrollPane(text); |
| 74 | scroll.getVerticalScrollBar().setUnitIncrement(16); |
| 75 | contentPane.add(scroll, BorderLayout.CENTER); |
| 76 | } |
| 77 | |
| 78 | private void initGuiNavButtons() { |
| 79 | JPanel navButtons = new JPanel(); |
| 80 | LayoutManager layout = new BoxLayout(navButtons, BoxLayout.X_AXIS); |
| 81 | navButtons.setLayout(layout); |
| 82 | |
| 83 | navButtons.add(createNavButton("<<", new ActionListener() { |
| 84 | @Override |
| 85 | public void actionPerformed(ActionEvent e) { |
| 86 | setChapter(-1); |
| 87 | } |
| 88 | })); |
| 89 | navButtons.add(createNavButton(" < ", new ActionListener() { |
| 90 | @Override |
| 91 | public void actionPerformed(ActionEvent e) { |
| 92 | setChapter(currentChapter - 1); |
| 93 | } |
| 94 | })); |
| 95 | navButtons.add(createNavButton(" > ", new ActionListener() { |
| 96 | @Override |
| 97 | public void actionPerformed(ActionEvent e) { |
| 98 | setChapter(currentChapter + 1); |
| 99 | } |
| 100 | })); |
| 101 | navButtons.add(createNavButton(">>", new ActionListener() { |
| 102 | @Override |
| 103 | public void actionPerformed(ActionEvent e) { |
| 104 | setChapter(story.getChapters().size() - 1); |
| 105 | } |
| 106 | })); |
| 107 | |
| 108 | add(navButtons, BorderLayout.SOUTH); |
| 109 | |
| 110 | chapterLabel = new JLabel(""); |
| 111 | navButtons.add(chapterLabel); |
| 112 | } |
| 113 | |
| 114 | private JButton createNavButton(String text, ActionListener action) { |
| 115 | JButton navButton = new JButton(text); |
| 116 | navButton.addActionListener(action); |
| 117 | navButton.setForeground(Color.BLUE); |
| 118 | return navButton; |
| 119 | } |
| 120 | |
| 121 | private void setChapter(int chapter) { |
| 122 | if (chapter >= -1 && chapter < story.getChapters().size() |
| 123 | && chapter != currentChapter) { |
| 124 | currentChapter = chapter; |
| 125 | |
| 126 | Chapter chap; |
| 127 | if (chapter == -1) { |
| 128 | chap = meta.getResume(); |
| 129 | setCoverPage(); |
| 130 | } else { |
| 131 | chap = story.getChapters().get(chapter); |
| 132 | descPane.setVisible(false); |
| 133 | } |
| 134 | |
| 135 | chapterLabel.setText("<HTML> <B>Chapter " |
| 136 | + chap.getNumber() + "</B>: " + chap.getName() + "</HTML>"); |
| 137 | |
| 138 | text.setText(htmlOutput.convert(chap)); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | private void setCoverPage() { |
| 143 | descPane.setVisible(true); |
| 144 | } |
| 145 | } |