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