some update/refresh fixes
[fanfix.git] / src / be / nikiroo / fanfix_swing / gui / book / BookLine.java
CommitLineData
3cdf3fd8
NR
1package be.nikiroo.fanfix_swing.gui.book;
2
3import java.awt.BorderLayout;
4import java.awt.Color;
89f2c479 5import java.awt.Dimension;
3cdf3fd8
NR
6import java.awt.Graphics;
7
8import javax.swing.JLabel;
9import javax.swing.JPanel;
10
11import be.nikiroo.fanfix.data.Story;
12import be.nikiroo.fanfix_swing.gui.BooksPanel;
13
14/**
15 * A book item presented in a {@link BooksPanel}.
16 * <p>
17 * Can be a story, or a comic or... a group.
18 *
19 * @author niki
20 */
21public class BookLine extends JPanel {
22 private static final long serialVersionUID = 1L;
23
24 /** Colour used for the seconday item (author/word count). */
25 protected static final Color AUTHOR_COLOR = new Color(128, 128, 128);
26
27 private boolean selected;
28 private boolean hovered;
29
30 private BookInfo info;
31 private boolean seeWordCount;
32
33 private JLabel title;
34 private JLabel secondary;
35 private JLabel iconCached;
36 private JLabel iconNotCached;
37
38 /**
39 * Create a new {@link BookLine} item for the given {@link Story}.
40 *
41 * @param info the information about the story to represent
42 * @param seeWordCount TRUE to see word counts, FALSE to see authors
43 */
44 public BookLine(BookInfo info, boolean seeWordCount) {
45 this.info = info;
46 this.seeWordCount = seeWordCount;
47
48 init();
49 }
50
51 /**
52 * Initialise this {@link BookLine}.
53 */
54 protected void init() {
89f2c479 55 // TODO: image?
3cdf3fd8
NR
56 iconCached = new JLabel(" ");
57 iconNotCached = new JLabel(" * ");
58
89f2c479
NR
59 iconNotCached.setForeground(BookCoverImager.UNCACHED_ICON_COLOR);
60 iconCached.setPreferredSize(iconNotCached.getPreferredSize());
61
3cdf3fd8
NR
62 title = new JLabel();
63 secondary = new JLabel();
64 secondary.setForeground(AUTHOR_COLOR);
65
66 setLayout(new BorderLayout());
67 add(title, BorderLayout.CENTER);
68 add(secondary, BorderLayout.EAST);
69
70 updateMeta();
71 }
72
73 /**
74 * The book current selection state.
75 *
76 * @return the selection state
77 */
78 public boolean isSelected() {
79 return selected;
80 }
81
82 /**
83 * The book current selection state,
84 *
85 * @param selected TRUE if it is selected
86 */
87 public void setSelected(boolean selected) {
88 if (this.selected != selected) {
89 this.selected = selected;
90 repaint();
91 }
92 }
93
94 /**
95 * The item mouse-hover state.
96 *
97 * @return TRUE if it is mouse-hovered
98 */
99 public boolean isHovered() {
100 return this.hovered;
101 }
102
103 /**
104 * The item mouse-hover state.
105 *
106 * @param hovered TRUE if it is mouse-hovered
107 */
108 public void setHovered(boolean hovered) {
109 if (this.hovered != hovered) {
110 this.hovered = hovered;
111 repaint();
112 }
113 }
114
115 /**
116 * The secondary value content: word count or author.
117 *
118 * @return TRUE to see word counts, FALSE to see authors
119 */
120 public boolean isSeeWordCount() {
121 return seeWordCount;
122 }
123
124 /**
125 * The secondary value content: word count or author.
126 *
127 * @param seeWordCount TRUE to see word counts, FALSE to see authors
128 */
129 public void setSeeWordCount(boolean seeWordCount) {
130 if (this.seeWordCount != seeWordCount) {
131 this.seeWordCount = seeWordCount;
132 repaint();
133 }
134 }
135
136 /**
137 * The information about the book represented by this item.
138 *
139 * @return the meta
140 */
141 public BookInfo getInfo() {
142 return info;
143 }
144
145 /**
146 * Update the title, paint the item.
147 */
148 @Override
149 public void paint(Graphics g) {
150 updateMeta();
151 super.paint(g);
152 }
153
154 /**
155 * Update the title with the currently registered information.
156 */
157 protected void updateMeta() {
158 String main = info.getMainInfo();
159 String optSecondary = info.getSecondaryInfo(seeWordCount);
160
89f2c479 161 // TODO: max size limit?
3cdf3fd8
NR
162 title.setText(main);
163 secondary.setText(optSecondary + " ");
164
165 setBackground(BookCoverImager.getBackground(isEnabled(), isSelected(), isHovered()));
166
167 remove(iconCached);
168 remove(iconNotCached);
169 add(getInfo().isCached() ? iconCached : iconNotCached, BorderLayout.WEST);
170 validate();
171 }
172}