| 1 | package be.nikiroo.fanfix.reader.ui; |
| 2 | |
| 3 | import java.awt.BorderLayout; |
| 4 | import java.awt.EventQueue; |
| 5 | import java.awt.Graphics2D; |
| 6 | import java.awt.event.ActionEvent; |
| 7 | import java.awt.event.ActionListener; |
| 8 | import java.awt.image.BufferedImage; |
| 9 | |
| 10 | import javax.swing.Icon; |
| 11 | import javax.swing.ImageIcon; |
| 12 | import javax.swing.JButton; |
| 13 | import javax.swing.JEditorPane; |
| 14 | import javax.swing.JLabel; |
| 15 | import javax.swing.JPanel; |
| 16 | import javax.swing.JProgressBar; |
| 17 | import javax.swing.JScrollPane; |
| 18 | import javax.swing.SwingConstants; |
| 19 | |
| 20 | import be.nikiroo.fanfix.Instance; |
| 21 | import be.nikiroo.fanfix.data.Chapter; |
| 22 | import be.nikiroo.fanfix.data.Story; |
| 23 | import be.nikiroo.utils.Image; |
| 24 | import be.nikiroo.utils.ui.ImageUtilsAwt; |
| 25 | |
| 26 | /** |
| 27 | * A {@link JPanel} that will show a {@link Story} chapter on screen. |
| 28 | * |
| 29 | * @author niki |
| 30 | */ |
| 31 | public class GuiReaderViewerPanel extends JPanel { |
| 32 | private static final long serialVersionUID = 1L; |
| 33 | |
| 34 | private boolean imageDocument; |
| 35 | private Chapter chap; |
| 36 | private JScrollPane scroll; |
| 37 | private GuiReaderViewerTextOutput htmlOutput; |
| 38 | |
| 39 | // text only: |
| 40 | private JEditorPane text; |
| 41 | |
| 42 | // image only: |
| 43 | private JLabel image; |
| 44 | private JProgressBar imageProgress; |
| 45 | private int currentImage; |
| 46 | private JButton left; |
| 47 | private JButton right; |
| 48 | |
| 49 | /** |
| 50 | * Create a new viewer. |
| 51 | * |
| 52 | * @param story |
| 53 | * the {@link Story} to work on. |
| 54 | */ |
| 55 | public GuiReaderViewerPanel(Story story) { |
| 56 | super(new BorderLayout()); |
| 57 | |
| 58 | this.imageDocument = story.getMeta().isImageDocument(); |
| 59 | |
| 60 | this.text = new JEditorPane("text/html", ""); |
| 61 | text.setAlignmentY(TOP_ALIGNMENT); |
| 62 | htmlOutput = new GuiReaderViewerTextOutput(); |
| 63 | |
| 64 | image = new JLabel(); |
| 65 | image.setHorizontalAlignment(SwingConstants.CENTER); |
| 66 | |
| 67 | scroll = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, |
| 68 | JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); |
| 69 | scroll.getVerticalScrollBar().setUnitIncrement(16); |
| 70 | |
| 71 | if (!imageDocument) { |
| 72 | add(scroll, BorderLayout.CENTER); |
| 73 | } else { |
| 74 | imageProgress = new JProgressBar(); |
| 75 | imageProgress.setStringPainted(true); |
| 76 | add(imageProgress, BorderLayout.SOUTH); |
| 77 | |
| 78 | JPanel main = new JPanel(new BorderLayout()); |
| 79 | main.add(scroll, BorderLayout.CENTER); |
| 80 | |
| 81 | left = new JButton("<HTML> < </HTML>"); |
| 82 | left.addActionListener(new ActionListener() { |
| 83 | @Override |
| 84 | public void actionPerformed(ActionEvent e) { |
| 85 | setImage(--currentImage); |
| 86 | } |
| 87 | }); |
| 88 | main.add(left, BorderLayout.WEST); |
| 89 | |
| 90 | right = new JButton("<HTML> > </HTML>"); |
| 91 | right.addActionListener(new ActionListener() { |
| 92 | @Override |
| 93 | public void actionPerformed(ActionEvent e) { |
| 94 | setImage(++currentImage); |
| 95 | } |
| 96 | }); |
| 97 | main.add(right, BorderLayout.EAST); |
| 98 | |
| 99 | add(main, BorderLayout.CENTER); |
| 100 | main.invalidate(); |
| 101 | } |
| 102 | |
| 103 | setChapter(story.getMeta().getResume()); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Load the given chapter. |
| 108 | * <p> |
| 109 | * Will always be text for a non-image document. |
| 110 | * <p> |
| 111 | * Will be an image and left/right controls for an image-document, except |
| 112 | * for chapter 0 which will be text (chapter 0 = resume). |
| 113 | * |
| 114 | * @param chap |
| 115 | * the chapter to load |
| 116 | */ |
| 117 | public void setChapter(Chapter chap) { |
| 118 | this.chap = chap; |
| 119 | |
| 120 | if (!imageDocument) { |
| 121 | setText(chap); |
| 122 | } else { |
| 123 | left.setVisible(chap.getNumber() > 0); |
| 124 | right.setVisible(chap.getNumber() > 0); |
| 125 | imageProgress.setVisible(chap.getNumber() > 0); |
| 126 | |
| 127 | imageProgress.setMinimum(0); |
| 128 | imageProgress.setMaximum(chap.getParagraphs().size() - 1); |
| 129 | |
| 130 | if (chap.getNumber() == 0) { |
| 131 | setText(chap); |
| 132 | } else { |
| 133 | setImage(0); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Will set and display the current chapter text. |
| 140 | * |
| 141 | * @param chap |
| 142 | * the chapter to display |
| 143 | */ |
| 144 | private void setText(final Chapter chap) { |
| 145 | new Thread(new Runnable() { |
| 146 | @Override |
| 147 | public void run() { |
| 148 | final String content = htmlOutput.convert(chap); |
| 149 | // Wait until size computations are correct |
| 150 | while (!scroll.isValid()) { |
| 151 | try { |
| 152 | Thread.sleep(1); |
| 153 | } catch (InterruptedException e) { |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | setText(content); |
| 158 | } |
| 159 | }).start(); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Actually set the text in the UI. |
| 164 | * <p> |
| 165 | * Do <b>NOT</b> use this method from the UI thread. |
| 166 | * |
| 167 | * @param content |
| 168 | * the text |
| 169 | */ |
| 170 | private void setText(final String content) { |
| 171 | EventQueue.invokeLater(new Runnable() { |
| 172 | @Override |
| 173 | public void run() { |
| 174 | text.setText(content); |
| 175 | text.setCaretPosition(0); |
| 176 | scroll.setViewportView(text); |
| 177 | } |
| 178 | }); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Will set and display the current image, take care about the progression |
| 183 | * and update the left and right cursors' <tt>enabled</tt> property. |
| 184 | * |
| 185 | * @param i |
| 186 | * the image index to load |
| 187 | */ |
| 188 | private void setImage(int i) { |
| 189 | left.setEnabled(i > 0); |
| 190 | right.setEnabled(i + 1 < chap.getParagraphs().size()); |
| 191 | |
| 192 | if (i < 0 || i >= chap.getParagraphs().size()) { |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | imageProgress.setValue(i); |
| 197 | imageProgress.setString(String.format("Image %d / %d", i + 1, chap |
| 198 | .getParagraphs().size())); |
| 199 | |
| 200 | currentImage = i; |
| 201 | |
| 202 | final Image img = chap.getParagraphs().get(i).getContentImage(); |
| 203 | |
| 204 | new Thread(new Runnable() { |
| 205 | @Override |
| 206 | public void run() { |
| 207 | // Wait until size computations are correct |
| 208 | while (!scroll.isValid()) { |
| 209 | try { |
| 210 | Thread.sleep(1); |
| 211 | } catch (InterruptedException e) { |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | if (img == null) { |
| 216 | setText("Error: cannot render image."); |
| 217 | } else { |
| 218 | setImage(img); |
| 219 | } |
| 220 | } |
| 221 | }).start(); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Actually set the image in the UI. |
| 226 | * <p> |
| 227 | * Do <b>NOT</b> use this method from the UI thread. |
| 228 | * |
| 229 | * @param img |
| 230 | * the image to set |
| 231 | */ |
| 232 | private void setImage(Image img) { |
| 233 | try { |
| 234 | int scrollWidth = scroll.getWidth() |
| 235 | - scroll.getVerticalScrollBar().getWidth(); |
| 236 | |
| 237 | BufferedImage buffImg = ImageUtilsAwt.fromImage(img); |
| 238 | |
| 239 | int iw = buffImg.getWidth(); |
| 240 | int ih = buffImg.getHeight(); |
| 241 | double ratio = ((double) ih) / iw; |
| 242 | |
| 243 | int w = scrollWidth; |
| 244 | int h = (int) (ratio * scrollWidth); |
| 245 | |
| 246 | BufferedImage resizedImage = new BufferedImage(w, h, |
| 247 | BufferedImage.TYPE_4BYTE_ABGR); |
| 248 | |
| 249 | Graphics2D g = resizedImage.createGraphics(); |
| 250 | try { |
| 251 | g.drawImage(buffImg, 0, 0, w, h, null); |
| 252 | } finally { |
| 253 | g.dispose(); |
| 254 | } |
| 255 | |
| 256 | final Icon icon = new ImageIcon(resizedImage); |
| 257 | EventQueue.invokeLater(new Runnable() { |
| 258 | @Override |
| 259 | public void run() { |
| 260 | image.setIcon(icon); |
| 261 | scroll.setViewportView(image); |
| 262 | } |
| 263 | }); |
| 264 | } catch (Exception e) { |
| 265 | Instance.getTraceHandler().error( |
| 266 | new Exception("Failed to load image into label", e)); |
| 267 | EventQueue.invokeLater(new Runnable() { |
| 268 | @Override |
| 269 | public void run() { |
| 270 | text.setText("Error: cannot load image."); |
| 271 | } |
| 272 | }); |
| 273 | } |
| 274 | } |
| 275 | } |