much quicker BooksPanel display, set max title size for BookBlocks
[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,
25 (BookInfo) null);
26
27 private JLabel title;
28 private Image coverImage;
29
30 /**
31 * Create a new {@link BookBlock} item for the given {@link Story}.
32 *
33 * @param info
34 * the information about the story to represent
35 * @param seeWordCount
36 * TRUE to see word counts, FALSE to see authors
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();
49 filler.setPreferredSize(new Dimension(BookCoverImager.getCoverWidth(),
50 BookCoverImager.getCoverHeight()));
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 *
62 * @param coverImage
63 * the image
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);
72 g.drawImage(coverImage,
73 BookCoverImager.TEXT_WIDTH - BookCoverImager.COVER_WIDTH, 0,
74 null);
75 BookCoverImager.paintOverlay(g, isEnabled(), isSelected(), isHovered(),
76 getInfo().isCached());
77 }
78
79 @Override
80 protected void updateMeta() {
81 String main = getMainInfoDisplay();
82 String optSecondary = getSecondaryInfoDisplay(isSeeWordCount());
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));
90
91 setBackground(BookCoverImager.getBackground(isEnabled(), isSelected(),
92 isHovered()));
93 }
94
95 /**
96 * Generate a cover icon based upon the given {@link BookInfo}.
97 *
98 * @param lib
99 * the library the meta comes from
100 * @param info
101 * the {@link BookInfo}
102 *
103 * @return the image
104 */
105 static public java.awt.Image generateCoverImage(BasicLibrary lib,
106 BookInfo info) {
107 return BookCoverImager.generateCoverImage(lib, info);
108 }
109 }