1 package be
.nikiroo
.fanfix
.reader
.ui
;
3 import java
.awt
.BorderLayout
;
4 import java
.awt
.event
.ActionEvent
;
5 import java
.awt
.event
.ActionListener
;
6 import java
.awt
.image
.BufferedImage
;
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
;
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
;
23 * A {@link JPanel} that will show a {@link Story} chapter on screen.
27 public class GuiReaderViewerPanel
extends JPanel
{
28 private static final long serialVersionUID
= 1L;
30 private boolean imageDocument
;
34 private GuiReaderViewerTextOutput htmlOutput
;
35 private JProgressBar imageProgress
;
36 private int currentImage
;
38 private JButton right
;
41 * Create a new viewer.
44 * the {@link Story} to work on.
46 public GuiReaderViewerPanel(Story story
) {
47 super(new BorderLayout());
49 this.imageDocument
= story
.getMeta().isImageDocument();
50 this.label
= new JLabel();
51 label
.setAlignmentY(TOP_ALIGNMENT
);
52 htmlOutput
= new GuiReaderViewerTextOutput();
55 // TODO: why is it broken?
56 // text.setPreferredSize(new Dimension(500, 500));
58 JScrollPane scroll
= new JScrollPane(label
);
59 scroll
.getVerticalScrollBar().setUnitIncrement(16);
61 add(scroll
, BorderLayout
.CENTER
);
63 imageProgress
= new JProgressBar();
64 imageProgress
.setStringPainted(true);
65 add(imageProgress
, BorderLayout
.SOUTH
);
67 JPanel main
= new JPanel(new BorderLayout());
68 main
.add(label
, BorderLayout
.CENTER
);
70 left
= new JButton(" < ");
71 left
.addActionListener(new ActionListener() {
73 public void actionPerformed(ActionEvent e
) {
74 setImage(--currentImage
);
77 main
.add(left
, BorderLayout
.WEST
);
79 right
= new JButton(" > ");
80 right
.addActionListener(new ActionListener() {
82 public void actionPerformed(ActionEvent e
) {
83 setImage(++currentImage
);
86 main
.add(right
, BorderLayout
.EAST
);
88 add(main
, BorderLayout
.CENTER
);
91 setChapter(story
.getMeta().getResume());
95 * Load the given chapter.
97 * Will always be text for a non-image document.
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).
103 * the chapter to load
105 public void setChapter(Chapter chap
) {
108 if (!imageDocument
) {
109 label
.setText(htmlOutput
.convert(chap
));
111 left
.setVisible(chap
.getNumber() > 0);
112 right
.setVisible(chap
.getNumber() > 0);
113 imageProgress
.setVisible(chap
.getNumber() > 0);
115 imageProgress
.setMinimum(0);
116 imageProgress
.setMaximum(chap
.getParagraphs().size() - 1);
118 if (chap
.getNumber() == 0) {
119 label
.setText(htmlOutput
.convert(chap
));
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.
131 * the image index to load
133 private void setImage(int i
) {
134 left
.setEnabled(i
> 0);
135 right
.setEnabled(i
+ 1 < chap
.getParagraphs().size());
137 if (i
< 0 || i
>= chap
.getParagraphs().size()) {
141 imageProgress
.setValue(i
);
142 imageProgress
.setString(String
.format("Image %d / %d", i
+ 1, chap
143 .getParagraphs().size()));
149 Image img
= chap
.getParagraphs().get(i
).getContentImage();
151 label
.setText("Error: cannot render image.");
154 BufferedImage buffImg
= ImageUtilsAwt
.fromImage(img
);
155 Icon icon
= new ImageIcon(buffImg
);
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.");