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