some update/refresh fixes
[fanfix.git] / src / be / nikiroo / fanfix_swing / gui / book / BookBlock.java
1 package be.nikiroo.fanfix_swing.gui.book;
2
3 import java.awt.BorderLayout;
4 import java.awt.Dimension;
5 import java.awt.Graphics;
6 import java.awt.Image;
7
8 import javax.swing.JLabel;
9 import javax.swing.JPanel;
10
11 import be.nikiroo.fanfix.data.Story;
12 import be.nikiroo.fanfix.library.BasicLibrary;
13 import be.nikiroo.fanfix_swing.gui.BooksPanel;
14
15 /**
16 * A book item presented in a {@link BooksPanel}.
17 * <p>
18 * Can be a story, or a comic or... a group.
19 *
20 * @author niki
21 */
22 public class BookBlock extends BookLine {
23 static private final long serialVersionUID = 1L;
24 static private Image empty = BookCoverImager.generateCoverImage(null, (BookInfo) null);
25
26 private JLabel title;
27 private Image coverImage;
28
29 /**
30 * Create a new {@link BookBlock} item for the given {@link Story}.
31 *
32 * @param info the information about the story to represent
33 * @param seeWordCount TRUE to see word counts, FALSE to see authors
34 */
35 public BookBlock(BookInfo info, boolean seeWordCount) {
36 super(info, seeWordCount);
37 }
38
39 @Override
40 protected void init() {
41 coverImage = empty;
42 title = new JLabel();
43 updateMeta();
44
45 JPanel filler = new JPanel();
46 filler.setPreferredSize(new Dimension(BookCoverImager.getCoverWidth(), BookCoverImager.getCoverHeight()));
47 filler.setOpaque(false);
48
49 setLayout(new BorderLayout(10, 10));
50 add(filler, BorderLayout.CENTER);
51 add(title, BorderLayout.SOUTH);
52 }
53
54 /**
55 * the cover image to use a base (see
56 * {@link BookCoverImager#generateCoverImage(BasicLibrary, BookInfo)})
57 *
58 * @param coverImage the image
59 */
60 public void setCoverImage(Image coverImage) {
61 this.coverImage = coverImage;
62 }
63
64 @Override
65 public void paint(Graphics g) {
66 super.paint(g);
67 g.drawImage(coverImage, BookCoverImager.TEXT_WIDTH - BookCoverImager.COVER_WIDTH, 0, null);
68 BookCoverImager.paintOverlay(g, isEnabled(), isSelected(), isHovered(), getInfo().isCached());
69 }
70
71 @Override
72 protected void updateMeta() {
73 String main = getInfo().getMainInfo();
74 String optSecondary = getInfo().getSecondaryInfo(isSeeWordCount());
75 String color = String.format("#%X%X%X", AUTHOR_COLOR.getRed(), AUTHOR_COLOR.getGreen(), AUTHOR_COLOR.getBlue());
76 title.setText(String.format(
77 "<html>" + "<body style='width: %d px; height: %d px; text-align: center;'>" + "%s" + "<br>"
78 + "<span style='color: %s;'>" + "%s" + "</span>" + "</body>" + "</html>",
79 BookCoverImager.TEXT_WIDTH, BookCoverImager.TEXT_HEIGHT, main, color, optSecondary));
80
81 setBackground(BookCoverImager.getBackground(isEnabled(), isSelected(), isHovered()));
82 }
83
84 /**
85 * Generate a cover icon based upon the given {@link BookInfo}.
86 *
87 * @param lib the library the meta comes from
88 * @param info the {@link BookInfo}
89 *
90 * @return the image
91 */
92 static public java.awt.Image generateCoverImage(BasicLibrary lib, BookInfo info) {
93 return BookCoverImager.generateCoverImage(lib, info);
94 }
95 }