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
.setAlignmentY(TOP_ALIGNMENT
);
63 htmlOutput
= new GuiReaderViewerTextOutput();
66 image
.setHorizontalAlignment(SwingConstants
.CENTER
);
68 scroll
= new JScrollPane(JScrollPane
.VERTICAL_SCROLLBAR_AS_NEEDED
,
69 JScrollPane
.HORIZONTAL_SCROLLBAR_NEVER
);
70 scroll
.getVerticalScrollBar().setUnitIncrement(16);
73 add(scroll
, BorderLayout
.CENTER
);
75 imageProgress
= new JProgressBar();
76 imageProgress
.setStringPainted(true);
77 add(imageProgress
, BorderLayout
.SOUTH
);
79 JPanel main
= new JPanel(new BorderLayout());
80 main
.add(scroll
, BorderLayout
.CENTER
);
82 left
= new JButton("<HTML> < </HTML>");
83 left
.addActionListener(new ActionListener() {
85 public void actionPerformed(ActionEvent e
) {
86 setImage(--currentImage
);
89 main
.add(left
, BorderLayout
.WEST
);
91 right
= new JButton("<HTML> > </HTML>");
92 right
.addActionListener(new ActionListener() {
94 public void actionPerformed(ActionEvent e
) {
95 setImage(++currentImage
);
98 main
.add(right
, BorderLayout
.EAST
);
100 add(main
, BorderLayout
.CENTER
);
104 setChapter(story
.getMeta().getResume());
108 * Load the given chapter.
110 * Will always be text for a non-image document.
112 * Will be an image and left/right controls for an image-document, except
113 * for chapter 0 which will be text (chapter 0 = resume).
116 * the chapter to load
118 public void setChapter(Chapter chap
) {
121 if (!imageDocument
) {
124 left
.setVisible(chap
.getNumber() > 0);
125 right
.setVisible(chap
.getNumber() > 0);
126 imageProgress
.setVisible(chap
.getNumber() > 0);
128 imageProgress
.setMinimum(0);
129 imageProgress
.setMaximum(chap
.getParagraphs().size() - 1);
131 if (chap
.getNumber() == 0) {
140 * Will set and display the current chapter text.
143 * the chapter to display
145 private void setText(final Chapter chap
) {
146 new Thread(new Runnable() {
149 final String content
= htmlOutput
.convert(chap
);
150 // Wait until size computations are correct
151 while (!scroll
.isValid()) {
154 } catch (InterruptedException e
) {
164 * Actually set the text in the UI.
166 * Do <b>NOT</b> use this method from the UI thread.
171 private void setText(final String content
) {
172 EventQueue
.invokeLater(new Runnable() {
175 text
.setText(content
);
176 text
.setCaretPosition(0);
177 scroll
.setViewportView(text
);
183 * Will set and display the current image, take care about the progression
184 * and update the left and right cursors' <tt>enabled</tt> property.
187 * the image index to load
189 private void setImage(int i
) {
190 left
.setEnabled(i
> 0);
191 right
.setEnabled(i
+ 1 < chap
.getParagraphs().size());
193 if (i
< 0 || i
>= chap
.getParagraphs().size()) {
197 imageProgress
.setValue(i
);
198 imageProgress
.setString(GuiReader
.trans(StringIdGui
.IMAGE_PROGRESSION
,
199 i
+ 1, chap
.getParagraphs().size()));
203 final Image img
= chap
.getParagraphs().get(i
).getContentImage();
205 // prepare the viewport to get the right sizes later on
207 scroll
.setViewportView(image
);
209 new Thread(new Runnable() {
212 // Wait until size computations are correct
213 while (!scroll
.isValid()) {
216 } catch (InterruptedException e
) {
221 setText("Error: cannot render image.");
230 * Actually set the image in the UI.
232 * Do <b>NOT</b> use this method from the UI thread.
237 private void setImage(Image img
) {
239 int scrollWidth
= scroll
.getWidth()
240 - scroll
.getVerticalScrollBar().getWidth();
242 BufferedImage buffImg
= ImageUtilsAwt
.fromImage(img
);
244 int iw
= buffImg
.getWidth();
245 int ih
= buffImg
.getHeight();
246 double ratio
= ((double) ih
) / iw
;
249 int h
= (int) (ratio
* scrollWidth
);
251 BufferedImage resizedImage
= new BufferedImage(w
, h
,
252 BufferedImage
.TYPE_4BYTE_ABGR
);
254 Graphics2D g
= resizedImage
.createGraphics();
256 g
.drawImage(buffImg
, 0, 0, w
, h
, null);
261 final Icon icon
= new ImageIcon(resizedImage
);
262 EventQueue
.invokeLater(new Runnable() {
266 scroll
.setViewportView(image
);
269 } catch (Exception e
) {
270 Instance
.getTraceHandler().error(
271 new Exception("Failed to load image into label", e
));
272 EventQueue
.invokeLater(new Runnable() {
275 text
.setText("Error: cannot load image.");