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
.bundles
.StringIdGui
;
22 import be
.nikiroo
.fanfix
.data
.Chapter
;
23 import be
.nikiroo
.fanfix
.data
.Story
;
24 import be
.nikiroo
.utils
.Image
;
25 import be
.nikiroo
.utils
.ui
.ImageUtilsAwt
;
28 * A {@link JPanel} that will show a {@link Story} chapter on screen.
32 public class GuiReaderViewerPanel
extends JPanel
{
33 private static final long serialVersionUID
= 1L;
35 private boolean imageDocument
;
37 private JScrollPane scroll
;
38 private GuiReaderViewerTextOutput htmlOutput
;
41 private JEditorPane text
;
45 private JProgressBar imageProgress
;
46 private int currentImage
;
48 private JButton right
;
51 * Create a new viewer.
54 * the {@link Story} to work on.
56 public GuiReaderViewerPanel(Story story
) {
57 super(new BorderLayout());
59 this.imageDocument
= story
.getMeta().isImageDocument();
61 this.text
= new JEditorPane("text/html", "");
62 text
.setEditable(false);
63 text
.setAlignmentY(TOP_ALIGNMENT
);
64 htmlOutput
= new GuiReaderViewerTextOutput();
67 image
.setHorizontalAlignment(SwingConstants
.CENTER
);
69 scroll
= new JScrollPane(JScrollPane
.VERTICAL_SCROLLBAR_AS_NEEDED
,
70 JScrollPane
.HORIZONTAL_SCROLLBAR_NEVER
);
71 scroll
.getVerticalScrollBar().setUnitIncrement(16);
74 add(scroll
, BorderLayout
.CENTER
);
76 imageProgress
= new JProgressBar();
77 imageProgress
.setStringPainted(true);
78 add(imageProgress
, BorderLayout
.SOUTH
);
80 JPanel main
= new JPanel(new BorderLayout());
81 main
.add(scroll
, BorderLayout
.CENTER
);
83 left
= new JButton("<HTML> < </HTML>");
84 left
.addActionListener(new ActionListener() {
86 public void actionPerformed(ActionEvent e
) {
87 setImage(--currentImage
);
90 main
.add(left
, BorderLayout
.WEST
);
92 right
= new JButton("<HTML> > </HTML>");
93 right
.addActionListener(new ActionListener() {
95 public void actionPerformed(ActionEvent e
) {
96 setImage(++currentImage
);
99 main
.add(right
, BorderLayout
.EAST
);
101 add(main
, BorderLayout
.CENTER
);
105 setChapter(story
.getMeta().getResume());
109 * Load the given chapter.
111 * Will always be text for a non-image document.
113 * Will be an image and left/right controls for an image-document, except
114 * for chapter 0 which will be text (chapter 0 = resume).
117 * the chapter to load
119 public void setChapter(Chapter chap
) {
122 if (!imageDocument
) {
125 left
.setVisible(chap
.getNumber() > 0);
126 right
.setVisible(chap
.getNumber() > 0);
127 imageProgress
.setVisible(chap
.getNumber() > 0);
129 imageProgress
.setMinimum(0);
130 imageProgress
.setMaximum(chap
.getParagraphs().size() - 1);
132 if (chap
.getNumber() == 0) {
141 * Will set and display the current chapter text.
144 * the chapter to display
146 private void setText(final Chapter chap
) {
147 new Thread(new Runnable() {
150 final String content
= htmlOutput
.convert(chap
);
151 // Wait until size computations are correct
152 while (!scroll
.isValid()) {
155 } catch (InterruptedException e
) {
165 * Actually set the text in the UI.
167 * Do <b>NOT</b> use this method from the UI thread.
172 private void setText(final String content
) {
173 EventQueue
.invokeLater(new Runnable() {
176 text
.setText(content
);
177 text
.setCaretPosition(0);
178 scroll
.setViewportView(text
);
184 * Will set and display the current image, take care about the progression
185 * and update the left and right cursors' <tt>enabled</tt> property.
188 * the image index to load
190 private void setImage(int i
) {
191 left
.setEnabled(i
> 0);
192 right
.setEnabled(i
+ 1 < chap
.getParagraphs().size());
194 if (i
< 0 || i
>= chap
.getParagraphs().size()) {
198 imageProgress
.setValue(i
);
199 imageProgress
.setString(GuiReader
.trans(StringIdGui
.IMAGE_PROGRESSION
,
200 i
+ 1, chap
.getParagraphs().size()));
204 final Image img
= chap
.getParagraphs().get(i
).getContentImage();
206 // prepare the viewport to get the right sizes later on
208 scroll
.setViewportView(image
);
210 new Thread(new Runnable() {
213 // Wait until size computations are correct
214 while (!scroll
.isValid()) {
217 } catch (InterruptedException e
) {
222 setText("Error: cannot render image.");
231 * Actually set the image in the UI.
233 * Do <b>NOT</b> use this method from the UI thread.
238 private void setImage(Image img
) {
240 int scrollWidth
= scroll
.getWidth()
241 - scroll
.getVerticalScrollBar().getWidth();
243 BufferedImage buffImg
= ImageUtilsAwt
.fromImage(img
);
245 int iw
= buffImg
.getWidth();
246 int ih
= buffImg
.getHeight();
247 double ratio
= ((double) ih
) / iw
;
250 int h
= (int) (ratio
* scrollWidth
);
252 BufferedImage resizedImage
= new BufferedImage(w
, h
,
253 BufferedImage
.TYPE_4BYTE_ABGR
);
255 Graphics2D g
= resizedImage
.createGraphics();
257 g
.drawImage(buffImg
, 0, 0, w
, h
, null);
262 final Icon icon
= new ImageIcon(resizedImage
);
263 EventQueue
.invokeLater(new Runnable() {
267 scroll
.setViewportView(image
);
270 } catch (Exception e
) {
271 Instance
.getTraceHandler().error(
272 new Exception("Failed to load image into label", e
));
273 EventQueue
.invokeLater(new Runnable() {
276 text
.setText("Error: cannot load image.");