f7926a0212d3dd36db60bf13c9c61b6e0c6308b3
[fanfix.git] / src / be / nikiroo / fanfix_swing / gui / book / BookLine.java
1 package be.nikiroo.fanfix_swing.gui.book;
2
3 import java.awt.BorderLayout;
4 import java.awt.Color;
5 import java.awt.Graphics;
6
7 import javax.swing.JLabel;
8 import javax.swing.JPanel;
9 import javax.swing.SwingConstants;
10
11 import be.nikiroo.fanfix.data.Story;
12 import 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 */
21 public 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
42 * the information about the story to represent
43 * @param seeWordCount
44 * TRUE to see word counts, FALSE to see authors
45 */
46 public BookLine(BookInfo info, boolean seeWordCount) {
47 this.info = info;
48 this.seeWordCount = seeWordCount;
49
50 init();
51 }
52
53 /**
54 * Initialise this {@link BookLine}.
55 */
56 protected void init() {
57 iconCached = new JLabel(" ◉ ");
58 iconNotCached = new JLabel(" ○ ");
59
60 iconNotCached.setForeground(BookCoverImager.UNCACHED_ICON_COLOR);
61 iconCached.setForeground(BookCoverImager.UNCACHED_ICON_COLOR);
62 iconCached.setPreferredSize(iconNotCached.getPreferredSize());
63
64 title = new JLabel();
65 secondary = new JLabel();
66 secondary.setForeground(AUTHOR_COLOR);
67
68 JLabel id = new JLabel(info.getMeta().getLuid());
69 id.setPreferredSize(new JLabel(" 999 ").getPreferredSize());
70 id.setForeground(Color.gray);
71 id.setHorizontalAlignment(SwingConstants.CENTER);
72
73 JPanel idTitle = new JPanel(new BorderLayout());
74 idTitle.setOpaque(false);
75 idTitle.add(id, BorderLayout.WEST);
76 idTitle.add(title, BorderLayout.CENTER);
77
78 setLayout(new BorderLayout());
79 add(idTitle, BorderLayout.CENTER);
80 add(secondary, BorderLayout.EAST);
81
82 updateMeta();
83 }
84
85 /**
86 * The book current selection state.
87 *
88 * @return the selection state
89 */
90 public boolean isSelected() {
91 return selected;
92 }
93
94 /**
95 * The book current selection state,
96 *
97 * @param selected
98 * TRUE if it is selected
99 */
100 public void setSelected(boolean selected) {
101 if (this.selected != selected) {
102 this.selected = selected;
103 repaint();
104 }
105 }
106
107 /**
108 * The item mouse-hover state.
109 *
110 * @return TRUE if it is mouse-hovered
111 */
112 public boolean isHovered() {
113 return this.hovered;
114 }
115
116 /**
117 * The item mouse-hover state.
118 *
119 * @param hovered
120 * TRUE if it is mouse-hovered
121 */
122 public void setHovered(boolean hovered) {
123 if (this.hovered != hovered) {
124 this.hovered = hovered;
125 repaint();
126 }
127 }
128
129 /**
130 * The secondary value content: word count or author.
131 *
132 * @return TRUE to see word counts, FALSE to see authors
133 */
134 public boolean isSeeWordCount() {
135 return seeWordCount;
136 }
137
138 /**
139 * The secondary value content: word count or author.
140 *
141 * @param seeWordCount
142 * TRUE to see word counts, FALSE to see authors
143 */
144 public void setSeeWordCount(boolean seeWordCount) {
145 if (this.seeWordCount != seeWordCount) {
146 this.seeWordCount = seeWordCount;
147 repaint();
148 }
149 }
150
151 /**
152 * The information about the book represented by this item.
153 *
154 * @return the meta
155 */
156 public BookInfo getInfo() {
157 return info;
158 }
159
160 /**
161 * Update the title, paint the item.
162 */
163 @Override
164 public void paint(Graphics g) {
165 updateMeta();
166 super.paint(g);
167 }
168
169 /**
170 * Update the title with the currently registered information.
171 */
172 protected void updateMeta() {
173 String main = info.getMainInfo();
174 String optSecondary = info.getSecondaryInfo(seeWordCount);
175
176 // TODO: max size limit?
177 title.setText(main);
178 secondary.setText(optSecondary + " ");
179
180 setBackground(BookCoverImager.getBackground(isEnabled(), isSelected(),
181 isHovered()));
182
183 remove(iconCached);
184 remove(iconNotCached);
185 add(getInfo().isCached() ? iconCached : iconNotCached,
186 BorderLayout.WEST);
187 validate();
188 }
189 }