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