1 package be
.nikiroo
.fanfix_swing
.gui
.book
;
3 import java
.awt
.BorderLayout
;
5 import java
.awt
.Graphics
;
7 import javax
.swing
.JLabel
;
8 import javax
.swing
.JPanel
;
9 import javax
.swing
.SwingConstants
;
11 import be
.nikiroo
.fanfix
.data
.Story
;
12 import be
.nikiroo
.fanfix_swing
.gui
.BooksPanel
;
15 * A book item presented in a {@link BooksPanel}.
17 * Can be a story, or a comic or... a group.
21 public class BookLine
extends JPanel
{
22 private static final long serialVersionUID
= 1L;
24 private static final int MAX_DISPLAY_SIZE
= 40;
26 /** Colour used for the seconday item (author/word count). */
27 protected static final Color AUTHOR_COLOR
= new Color(128, 128, 128);
29 private boolean selected
;
30 private boolean hovered
;
32 private BookInfo info
;
33 private boolean seeWordCount
;
36 private JLabel secondary
;
37 private JLabel iconCached
;
38 private JLabel iconNotCached
;
41 * Create a new {@link BookLine} item for the given {@link Story}.
44 * the information about the story to represent
46 * TRUE to see word counts, FALSE to see authors
48 public BookLine(BookInfo info
, boolean seeWordCount
) {
50 this.seeWordCount
= seeWordCount
;
56 * Initialise this {@link BookLine}.
58 protected void init() {
59 iconCached
= new JLabel(" ◉ ");
60 iconNotCached
= new JLabel(" ○ ");
62 iconNotCached
.setForeground(BookCoverImager
.UNCACHED_ICON_COLOR
);
63 iconCached
.setForeground(BookCoverImager
.UNCACHED_ICON_COLOR
);
64 iconCached
.setPreferredSize(iconNotCached
.getPreferredSize());
67 secondary
= new JLabel();
68 secondary
.setForeground(AUTHOR_COLOR
);
71 if (info
.getMeta() != null) {
72 luid
= info
.getMeta().getLuid();
74 JLabel id
= new JLabel(luid
);
75 id
.setPreferredSize(new JLabel(" 999 ").getPreferredSize());
76 id
.setForeground(Color
.gray
);
77 id
.setHorizontalAlignment(SwingConstants
.CENTER
);
79 JPanel idTitle
= new JPanel(new BorderLayout());
80 idTitle
.setOpaque(false);
81 idTitle
.add(id
, BorderLayout
.WEST
);
82 idTitle
.add(title
, BorderLayout
.CENTER
);
84 setLayout(new BorderLayout());
85 add(idTitle
, BorderLayout
.CENTER
);
86 add(secondary
, BorderLayout
.EAST
);
92 * The book current selection state.
94 * @return the selection state
96 public boolean isSelected() {
101 * The book current selection state,
104 * TRUE if it is selected
106 public void setSelected(boolean selected
) {
107 if (this.selected
!= selected
) {
108 this.selected
= selected
;
114 * The item mouse-hover state.
116 * @return TRUE if it is mouse-hovered
118 public boolean isHovered() {
123 * The item mouse-hover state.
126 * TRUE if it is mouse-hovered
128 public void setHovered(boolean hovered
) {
129 if (this.hovered
!= hovered
) {
130 this.hovered
= hovered
;
136 * The secondary value content: word count or author.
138 * @return TRUE to see word counts, FALSE to see authors
140 public boolean isSeeWordCount() {
145 * The secondary value content: word count or author.
147 * @param seeWordCount
148 * TRUE to see word counts, FALSE to see authors
150 public void setSeeWordCount(boolean seeWordCount
) {
151 if (this.seeWordCount
!= seeWordCount
) {
152 this.seeWordCount
= seeWordCount
;
158 * The information about the book represented by this item.
162 public BookInfo
getInfo() {
167 * Update the title, paint the item.
170 public void paint(Graphics g
) {
176 * Return a display-ready version of {@link BookInfo#getMainInfo()}.
178 * @return the main info in a ready-to-display version
180 protected String
getMainInfoDisplay() {
181 return toDisplay(getInfo().getMainInfo());
185 * Return a display-ready version of
186 * {@link BookInfo#getSecondaryInfo(boolean)}.
189 * TRUE for word/image/story count, FALSE for author name
191 * @return the main info in a ready-to-display version
193 protected String
getSecondaryInfoDisplay(boolean seeCount
) {
194 return toDisplay(getInfo().getSecondaryInfo(seeCount
));
198 * Update the title with the currently registered information.
200 protected void updateMeta() {
201 String main
= getMainInfoDisplay();
202 String optSecondary
= getSecondaryInfoDisplay(isSeeWordCount());
205 secondary
.setText(optSecondary
+ " ");
207 setBackground(BookCoverImager
.getBackground(isEnabled(), isSelected(),
211 remove(iconNotCached
);
212 add(getInfo().isCached() ? iconCached
: iconNotCached
,
218 * Make the given {@link String} display-ready (i.e., shorten it if it is
224 * @return the display-ready value
226 private String
toDisplay(String value
) {
230 if (value
.length() > MAX_DISPLAY_SIZE
) {
231 value
= value
.substring(0, MAX_DISPLAY_SIZE
- 3) + "...";