| 1 | package be.nikiroo.fanfix.reader.ui; |
| 2 | |
| 3 | import java.awt.BorderLayout; |
| 4 | import java.awt.event.ActionEvent; |
| 5 | import java.awt.event.ActionListener; |
| 6 | import java.awt.image.BufferedImage; |
| 7 | |
| 8 | import javax.swing.Icon; |
| 9 | import javax.swing.ImageIcon; |
| 10 | import javax.swing.JButton; |
| 11 | import javax.swing.JLabel; |
| 12 | import javax.swing.JPanel; |
| 13 | import javax.swing.JProgressBar; |
| 14 | import javax.swing.JScrollPane; |
| 15 | |
| 16 | import be.nikiroo.fanfix.Instance; |
| 17 | import be.nikiroo.fanfix.data.Chapter; |
| 18 | import be.nikiroo.fanfix.data.Story; |
| 19 | import be.nikiroo.utils.Image; |
| 20 | import be.nikiroo.utils.ui.ImageUtilsAwt; |
| 21 | |
| 22 | /** |
| 23 | * A {@link JPanel} that will show a {@link Story} chapter on screen. |
| 24 | * |
| 25 | * @author niki |
| 26 | */ |
| 27 | public class GuiReaderViewerPanel extends JPanel { |
| 28 | private static final long serialVersionUID = 1L; |
| 29 | |
| 30 | private boolean imageDocument; |
| 31 | private Chapter chap; |
| 32 | |
| 33 | private JLabel label; |
| 34 | private GuiReaderViewerTextOutput htmlOutput; |
| 35 | private JProgressBar imageProgress; |
| 36 | private int currentImage; |
| 37 | private JButton left; |
| 38 | private JButton right; |
| 39 | |
| 40 | /** |
| 41 | * Create a new viewer. |
| 42 | * |
| 43 | * @param story |
| 44 | * the {@link Story} to work on. |
| 45 | */ |
| 46 | public GuiReaderViewerPanel(Story story) { |
| 47 | super(new BorderLayout()); |
| 48 | |
| 49 | this.imageDocument = story.getMeta().isImageDocument(); |
| 50 | this.label = new JLabel(); |
| 51 | label.setAlignmentY(TOP_ALIGNMENT); |
| 52 | htmlOutput = new GuiReaderViewerTextOutput(); |
| 53 | |
| 54 | if (!imageDocument) { |
| 55 | // TODO: why is it broken? |
| 56 | // text.setPreferredSize(new Dimension(500, 500)); |
| 57 | |
| 58 | JScrollPane scroll = new JScrollPane(label); |
| 59 | scroll.getVerticalScrollBar().setUnitIncrement(16); |
| 60 | |
| 61 | add(scroll, BorderLayout.CENTER); |
| 62 | } else { |
| 63 | imageProgress = new JProgressBar(); |
| 64 | imageProgress.setStringPainted(true); |
| 65 | add(imageProgress, BorderLayout.SOUTH); |
| 66 | |
| 67 | JPanel main = new JPanel(new BorderLayout()); |
| 68 | main.add(label, BorderLayout.CENTER); |
| 69 | |
| 70 | left = new JButton(" < "); |
| 71 | left.addActionListener(new ActionListener() { |
| 72 | @Override |
| 73 | public void actionPerformed(ActionEvent e) { |
| 74 | setImage(--currentImage); |
| 75 | } |
| 76 | }); |
| 77 | main.add(left, BorderLayout.WEST); |
| 78 | |
| 79 | right = new JButton(" > "); |
| 80 | right.addActionListener(new ActionListener() { |
| 81 | @Override |
| 82 | public void actionPerformed(ActionEvent e) { |
| 83 | setImage(++currentImage); |
| 84 | } |
| 85 | }); |
| 86 | main.add(right, BorderLayout.EAST); |
| 87 | |
| 88 | add(main, BorderLayout.CENTER); |
| 89 | } |
| 90 | |
| 91 | setChapter(story.getMeta().getResume()); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Load the given chapter. |
| 96 | * <p> |
| 97 | * Will always be text for a non-image document. |
| 98 | * <p> |
| 99 | * Will be an image and left/right controls for an image-document, except |
| 100 | * for chapter 0 which will be text (chapter 0 = resume). |
| 101 | * |
| 102 | * @param chap |
| 103 | * the chapter to load |
| 104 | */ |
| 105 | public void setChapter(Chapter chap) { |
| 106 | this.chap = chap; |
| 107 | |
| 108 | if (!imageDocument) { |
| 109 | label.setText(htmlOutput.convert(chap)); |
| 110 | } else { |
| 111 | left.setVisible(chap.getNumber() > 0); |
| 112 | right.setVisible(chap.getNumber() > 0); |
| 113 | imageProgress.setVisible(chap.getNumber() > 0); |
| 114 | |
| 115 | imageProgress.setMinimum(0); |
| 116 | imageProgress.setMaximum(chap.getParagraphs().size() - 1); |
| 117 | |
| 118 | if (chap.getNumber() == 0) { |
| 119 | label.setText(htmlOutput.convert(chap)); |
| 120 | } else { |
| 121 | setImage(0); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Will set and display the current image, take care about the progression |
| 128 | * and update the left and right cursors' <tt>enabled</tt> property. |
| 129 | * |
| 130 | * @param i |
| 131 | * the image index to load |
| 132 | */ |
| 133 | private void setImage(int i) { |
| 134 | left.setEnabled(i > 0); |
| 135 | right.setEnabled(i + 1 < chap.getParagraphs().size()); |
| 136 | |
| 137 | if (i < 0 || i >= chap.getParagraphs().size()) { |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | imageProgress.setValue(i); |
| 142 | imageProgress.setString(String.format("Image %d / %d", i + 1, chap |
| 143 | .getParagraphs().size())); |
| 144 | |
| 145 | currentImage = i; |
| 146 | label.setText(""); |
| 147 | label.setIcon(null); |
| 148 | |
| 149 | Image img = chap.getParagraphs().get(i).getContentImage(); |
| 150 | if (img == null) { |
| 151 | label.setText("Error: cannot render image."); |
| 152 | } else { |
| 153 | try { |
| 154 | BufferedImage buffImg = ImageUtilsAwt.fromImage(img); |
| 155 | Icon icon = new ImageIcon(buffImg); |
| 156 | label.setIcon(icon); |
| 157 | } catch (Exception e) { |
| 158 | Instance.getTraceHandler().error( |
| 159 | new Exception("Failed to load image into label", e)); |
| 160 | label.setText("Error: cannot load image."); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | } |