1 package be
.nikiroo
.fanfix
.reader
.ui
;
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
;
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
;
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
;
27 * A {@link JPanel} that will show a {@link Story} chapter on screen.
31 public class GuiReaderViewerPanel
extends JPanel
{
32 private static final long serialVersionUID
= 1L;
34 private boolean imageDocument
;
36 private JScrollPane scroll
;
37 private GuiReaderViewerTextOutput htmlOutput
;
40 private JEditorPane text
;
44 private JProgressBar imageProgress
;
45 private int currentImage
;
47 private JButton right
;
50 * Create a new viewer.
53 * the {@link Story} to work on.
55 public GuiReaderViewerPanel(Story story
) {
56 super(new BorderLayout());
58 this.imageDocument
= story
.getMeta().isImageDocument();
60 this.text
= new JEditorPane("text/html", "");
61 text
.setAlignmentY(TOP_ALIGNMENT
);
62 htmlOutput
= new GuiReaderViewerTextOutput();
65 image
.setHorizontalAlignment(SwingConstants
.CENTER
);
67 scroll
= new JScrollPane(JScrollPane
.VERTICAL_SCROLLBAR_AS_NEEDED
,
68 JScrollPane
.HORIZONTAL_SCROLLBAR_NEVER
);
69 scroll
.getVerticalScrollBar().setUnitIncrement(16);
72 add(scroll
, BorderLayout
.CENTER
);
74 imageProgress
= new JProgressBar();
75 imageProgress
.setStringPainted(true);
76 add(imageProgress
, BorderLayout
.SOUTH
);
78 JPanel main
= new JPanel(new BorderLayout());
79 main
.add(scroll
, BorderLayout
.CENTER
);
81 left
= new JButton("<HTML> < </HTML>");
82 left
.addActionListener(new ActionListener() {
84 public void actionPerformed(ActionEvent e
) {
85 setImage(--currentImage
);
88 main
.add(left
, BorderLayout
.WEST
);
90 right
= new JButton("<HTML> > </HTML>");
91 right
.addActionListener(new ActionListener() {
93 public void actionPerformed(ActionEvent e
) {
94 setImage(++currentImage
);
97 main
.add(right
, BorderLayout
.EAST
);
99 add(main
, BorderLayout
.CENTER
);
103 setChapter(story
.getMeta().getResume());
107 * Load the given chapter.
109 * Will always be text for a non-image document.
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).
115 * the chapter to load
117 public void setChapter(Chapter chap
) {
120 if (!imageDocument
) {
123 left
.setVisible(chap
.getNumber() > 0);
124 right
.setVisible(chap
.getNumber() > 0);
125 imageProgress
.setVisible(chap
.getNumber() > 0);
127 imageProgress
.setMinimum(0);
128 imageProgress
.setMaximum(chap
.getParagraphs().size() - 1);
130 if (chap
.getNumber() == 0) {
139 * Will set and display the current chapter text.
142 * the chapter to display
144 private void setText(final Chapter chap
) {
145 new Thread(new Runnable() {
148 final String content
= htmlOutput
.convert(chap
);
149 // Wait until size computations are correct
150 while (!scroll
.isValid()) {
153 } catch (InterruptedException e
) {
163 * Actually set the text in the UI.
165 * Do <b>NOT</b> use this method from the UI thread.
170 private void setText(final String content
) {
171 EventQueue
.invokeLater(new Runnable() {
174 text
.setText(content
);
175 text
.setCaretPosition(0);
176 scroll
.setViewportView(text
);
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.
186 * the image index to load
188 private void setImage(int i
) {
189 left
.setEnabled(i
> 0);
190 right
.setEnabled(i
+ 1 < chap
.getParagraphs().size());
192 if (i
< 0 || i
>= chap
.getParagraphs().size()) {
196 imageProgress
.setValue(i
);
197 imageProgress
.setString(String
.format("Image %d / %d", i
+ 1, chap
198 .getParagraphs().size()));
202 final Image img
= chap
.getParagraphs().get(i
).getContentImage();
204 // prepare the viewport to get the right sizes later on
206 scroll
.setViewportView(image
);
208 new Thread(new Runnable() {
211 // Wait until size computations are correct
212 while (!scroll
.isValid()) {
215 } catch (InterruptedException e
) {
220 setText("Error: cannot render image.");
229 * Actually set the image in the UI.
231 * Do <b>NOT</b> use this method from the UI thread.
236 private void setImage(Image img
) {
238 int scrollWidth
= scroll
.getWidth()
239 - scroll
.getVerticalScrollBar().getWidth();
241 BufferedImage buffImg
= ImageUtilsAwt
.fromImage(img
);
243 int iw
= buffImg
.getWidth();
244 int ih
= buffImg
.getHeight();
245 double ratio
= ((double) ih
) / iw
;
248 int h
= (int) (ratio
* scrollWidth
);
250 BufferedImage resizedImage
= new BufferedImage(w
, h
,
251 BufferedImage
.TYPE_4BYTE_ABGR
);
253 Graphics2D g
= resizedImage
.createGraphics();
255 g
.drawImage(buffImg
, 0, 0, w
, h
, null);
260 final Icon icon
= new ImageIcon(resizedImage
);
261 EventQueue
.invokeLater(new Runnable() {
265 scroll
.setViewportView(image
);
268 } catch (Exception e
) {
269 Instance
.getTraceHandler().error(
270 new Exception("Failed to load image into label", e
));
271 EventQueue
.invokeLater(new Runnable() {
274 text
.setText("Error: cannot load image.");