Commit | Line | Data |
---|---|---|
333f0e7b NR |
1 | package be.nikiroo.fanfix.reader; |
2 | ||
3 | import java.awt.BorderLayout; | |
4 | import java.awt.Color; | |
d3c84ac3 | 5 | import java.awt.Graphics; |
333f0e7b | 6 | import java.awt.Graphics2D; |
b4dc6ab5 | 7 | import java.awt.Polygon; |
d3c84ac3 | 8 | import java.awt.Rectangle; |
333f0e7b NR |
9 | import java.awt.event.MouseEvent; |
10 | import java.awt.event.MouseListener; | |
11 | import java.awt.image.BufferedImage; | |
12 | import java.util.ArrayList; | |
13 | import java.util.Date; | |
14 | import java.util.EventListener; | |
15 | import java.util.List; | |
16 | ||
17 | import javax.swing.ImageIcon; | |
18 | import javax.swing.JLabel; | |
19 | import javax.swing.JPanel; | |
20 | ||
21 | import be.nikiroo.fanfix.data.MetaData; | |
22 | ||
23 | /** | |
24 | * A book item presented in a {@link LocalReaderFrame}. | |
25 | * | |
26 | * @author niki | |
27 | */ | |
28 | class LocalReaderBook extends JPanel { | |
29 | /** | |
30 | * Action on a book item. | |
31 | * | |
32 | * @author niki | |
33 | */ | |
92fb0719 | 34 | interface BookActionListener extends EventListener { |
333f0e7b NR |
35 | /** |
36 | * The book was selected (single click). | |
37 | * | |
38 | * @param book | |
39 | * the {@link LocalReaderBook} itself | |
40 | */ | |
41 | public void select(LocalReaderBook book); | |
42 | ||
43 | /** | |
44 | * The book was double-clicked. | |
45 | * | |
46 | * @param book | |
47 | * the {@link LocalReaderBook} itself | |
48 | */ | |
49 | public void action(LocalReaderBook book); | |
50 | } | |
51 | ||
b4dc6ab5 NR |
52 | private static final int COVER_WIDTH = 100; |
53 | private static final int COVER_HEIGHT = 150; | |
54 | private static final int SPINE_WIDTH = 5; | |
55 | private static final int SPINE_HEIGHT = 5; | |
56 | private static final int HOFFSET = 20; | |
57 | private static final Color SPINE_COLOR_BOTTOM = new Color(180, 180, 180); | |
58 | private static final Color SPINE_COLOR_RIGHT = new Color(100, 100, 100); | |
59 | private static final int TEXT_WIDTH = COVER_WIDTH + 40; | |
60 | private static final int TEXT_HEIGHT = 50; | |
61 | private static final String AUTHOR_COLOR = "#888888"; | |
62 | ||
333f0e7b NR |
63 | private static final long serialVersionUID = 1L; |
64 | private JLabel icon; | |
b4dc6ab5 | 65 | private JLabel tt; |
333f0e7b NR |
66 | private boolean selected; |
67 | private boolean hovered; | |
68 | private Date lastClick; | |
69 | private long doubleClickDelay = 200; // in ms | |
92fb0719 | 70 | private List<BookActionListener> listeners; |
333f0e7b NR |
71 | |
72 | public LocalReaderBook(MetaData meta) { | |
73 | if (meta.getCover() != null) { | |
b4dc6ab5 NR |
74 | BufferedImage resizedImage = new BufferedImage(SPINE_WIDTH |
75 | + COVER_WIDTH, SPINE_HEIGHT + COVER_HEIGHT + HOFFSET, | |
333f0e7b NR |
76 | BufferedImage.TYPE_4BYTE_ABGR); |
77 | Graphics2D g = resizedImage.createGraphics(); | |
b4dc6ab5 NR |
78 | g.setColor(Color.white); |
79 | g.fillRect(0, HOFFSET, COVER_WIDTH, COVER_HEIGHT); | |
80 | g.drawImage(meta.getCover(), 0, HOFFSET, COVER_WIDTH, COVER_HEIGHT, | |
81 | null); | |
333f0e7b NR |
82 | g.dispose(); |
83 | ||
84 | icon = new JLabel(new ImageIcon(resizedImage)); | |
85 | } else { | |
b4dc6ab5 | 86 | // TODO: a big black "X" ? |
333f0e7b NR |
87 | icon = new JLabel(" [ no cover ] "); |
88 | } | |
89 | ||
b4dc6ab5 NR |
90 | String optAuthor = meta.getAuthor(); |
91 | if (optAuthor != null && !optAuthor.isEmpty()) { | |
92 | optAuthor = "(" + optAuthor + ")"; | |
93 | } | |
94 | tt = new JLabel( | |
95 | String.format( | |
96 | "<html>" | |
97 | + "<body style='width: %d px; height: %d px; text-align: center'>" | |
98 | + "%s" + "<br>" + "<span style='color: %s;'>" | |
99 | + "%s" + "</span>" + "</body>" + "</html>", | |
100 | TEXT_WIDTH, TEXT_HEIGHT, meta.getTitle(), AUTHOR_COLOR, | |
101 | optAuthor)); | |
102 | ||
103 | this.setLayout(new BorderLayout(10, 10)); | |
333f0e7b | 104 | this.add(icon, BorderLayout.CENTER); |
b4dc6ab5 | 105 | this.add(tt, BorderLayout.SOUTH); |
333f0e7b NR |
106 | |
107 | setupListeners(); | |
108 | setSelected(false); | |
109 | } | |
110 | ||
111 | /** | |
112 | * The book current selection state. | |
113 | * | |
114 | * @return the selected | |
115 | */ | |
116 | public boolean isSelected() { | |
117 | return selected; | |
118 | } | |
119 | ||
120 | /** | |
121 | * The book current selection state. | |
122 | * | |
123 | * @param selected | |
124 | * the selected to set | |
125 | */ | |
126 | public void setSelected(boolean selected) { | |
127 | this.selected = selected; | |
d3c84ac3 | 128 | repaint(); |
333f0e7b NR |
129 | } |
130 | ||
131 | private void setHovered(boolean hovered) { | |
132 | this.hovered = hovered; | |
d3c84ac3 | 133 | repaint(); |
333f0e7b NR |
134 | } |
135 | ||
136 | private void setupListeners() { | |
92fb0719 | 137 | listeners = new ArrayList<LocalReaderBook.BookActionListener>(); |
333f0e7b NR |
138 | addMouseListener(new MouseListener() { |
139 | public void mouseReleased(MouseEvent e) { | |
140 | } | |
141 | ||
142 | public void mousePressed(MouseEvent e) { | |
143 | } | |
144 | ||
145 | public void mouseExited(MouseEvent e) { | |
146 | setHovered(false); | |
147 | } | |
148 | ||
149 | public void mouseEntered(MouseEvent e) { | |
150 | setHovered(true); | |
151 | } | |
152 | ||
153 | public void mouseClicked(MouseEvent e) { | |
154 | Date now = new Date(); | |
155 | if (lastClick != null | |
156 | && now.getTime() - lastClick.getTime() < doubleClickDelay) { | |
157 | click(true); | |
158 | } else { | |
159 | click(false); | |
160 | } | |
161 | lastClick = now; | |
162 | } | |
163 | }); | |
164 | } | |
165 | ||
166 | private void click(boolean doubleClick) { | |
92fb0719 | 167 | for (BookActionListener listener : listeners) { |
333f0e7b NR |
168 | if (doubleClick) { |
169 | listener.action(this); | |
170 | } else { | |
171 | listener.select(this); | |
172 | } | |
173 | } | |
174 | } | |
175 | ||
92fb0719 | 176 | public void addActionListener(BookActionListener listener) { |
333f0e7b NR |
177 | listeners.add(listener); |
178 | } | |
d3c84ac3 NR |
179 | |
180 | @Override | |
181 | public void paint(Graphics g) { | |
182 | super.paint(g); | |
183 | ||
b4dc6ab5 NR |
184 | int h = COVER_HEIGHT; |
185 | int w = COVER_WIDTH; | |
186 | int xOffset = (TEXT_WIDTH - COVER_WIDTH) - 4; | |
187 | ||
188 | int[] xs = new int[] { xOffset, xOffset + SPINE_WIDTH, | |
189 | xOffset + w + SPINE_WIDTH, xOffset + w }; | |
190 | int[] ys = new int[] { HOFFSET + h, HOFFSET + h + SPINE_HEIGHT, | |
191 | HOFFSET + h + SPINE_HEIGHT, HOFFSET + h }; | |
192 | g.setColor(SPINE_COLOR_BOTTOM); | |
193 | g.fillPolygon(new Polygon(xs, ys, xs.length)); | |
194 | xs = new int[] { xOffset + w, xOffset + w + SPINE_WIDTH, | |
195 | xOffset + w + SPINE_WIDTH, xOffset + w }; | |
196 | ys = new int[] { HOFFSET, HOFFSET + SPINE_HEIGHT, | |
197 | HOFFSET + h + SPINE_HEIGHT, HOFFSET + h }; | |
198 | g.setColor(SPINE_COLOR_RIGHT); | |
199 | g.fillPolygon(new Polygon(xs, ys, xs.length)); | |
200 | ||
d3c84ac3 NR |
201 | Color color = new Color(255, 255, 255, 0); |
202 | if (selected && !hovered) { | |
203 | color = new Color(80, 80, 100, 40); | |
204 | } else if (!selected && hovered) { | |
205 | color = new Color(230, 230, 255, 100); | |
206 | } else if (selected && hovered) { | |
207 | color = new Color(200, 200, 255, 100); | |
208 | } | |
209 | ||
210 | Rectangle clip = g.getClipBounds(); | |
211 | g.setColor(color); | |
212 | g.fillRect(clip.x, clip.y, clip.width, clip.height); | |
213 | } | |
333f0e7b | 214 | } |