| 1 | package be.nikiroo.fanfix.reader; |
| 2 | |
| 3 | import java.awt.BorderLayout; |
| 4 | import java.awt.Color; |
| 5 | import java.awt.Graphics; |
| 6 | import java.awt.Graphics2D; |
| 7 | import java.awt.Polygon; |
| 8 | import java.awt.Rectangle; |
| 9 | import java.awt.event.MouseEvent; |
| 10 | import java.awt.event.MouseListener; |
| 11 | import java.awt.image.BufferedImage; |
| 12 | import java.io.ByteArrayInputStream; |
| 13 | import java.io.ByteArrayOutputStream; |
| 14 | import java.io.IOException; |
| 15 | import java.io.InputStream; |
| 16 | import java.net.MalformedURLException; |
| 17 | import java.util.ArrayList; |
| 18 | import java.util.Date; |
| 19 | import java.util.EventListener; |
| 20 | import java.util.List; |
| 21 | |
| 22 | import javax.imageio.ImageIO; |
| 23 | import javax.swing.ImageIcon; |
| 24 | import javax.swing.JLabel; |
| 25 | import javax.swing.JPanel; |
| 26 | |
| 27 | import be.nikiroo.fanfix.Instance; |
| 28 | import be.nikiroo.fanfix.data.MetaData; |
| 29 | import be.nikiroo.fanfix.data.Story; |
| 30 | import be.nikiroo.utils.ImageUtils; |
| 31 | import be.nikiroo.utils.ui.UIUtils; |
| 32 | |
| 33 | /** |
| 34 | * A book item presented in a {@link GuiReaderFrame}. |
| 35 | * |
| 36 | * @author niki |
| 37 | */ |
| 38 | class GuiReaderBook extends JPanel { |
| 39 | /** |
| 40 | * Action on a book item. |
| 41 | * |
| 42 | * @author niki |
| 43 | */ |
| 44 | interface BookActionListener extends EventListener { |
| 45 | /** |
| 46 | * The book was selected (single click). |
| 47 | * |
| 48 | * @param book |
| 49 | * the {@link GuiReaderBook} itself |
| 50 | */ |
| 51 | public void select(GuiReaderBook book); |
| 52 | |
| 53 | /** |
| 54 | * The book was double-clicked. |
| 55 | * |
| 56 | * @param book |
| 57 | * the {@link GuiReaderBook} itself |
| 58 | */ |
| 59 | public void action(GuiReaderBook book); |
| 60 | |
| 61 | /** |
| 62 | * A popup menu was requested for this {@link GuiReaderBook}. |
| 63 | * |
| 64 | * @param book |
| 65 | * the {@link GuiReaderBook} itself |
| 66 | * @param e |
| 67 | * the {@link MouseEvent} that generated this call |
| 68 | */ |
| 69 | public void popupRequested(GuiReaderBook book, MouseEvent e); |
| 70 | } |
| 71 | |
| 72 | private static final long serialVersionUID = 1L; |
| 73 | |
| 74 | // TODO: export some of the configuration options? |
| 75 | private static final int COVER_WIDTH = 100; |
| 76 | private static final int COVER_HEIGHT = 150; |
| 77 | private static final int SPINE_WIDTH = 5; |
| 78 | private static final int SPINE_HEIGHT = 5; |
| 79 | private static final int HOFFSET = 20; |
| 80 | private static final Color SPINE_COLOR_BOTTOM = new Color(180, 180, 180); |
| 81 | private static final Color SPINE_COLOR_RIGHT = new Color(100, 100, 100); |
| 82 | private static final int TEXT_WIDTH = COVER_WIDTH + 40; |
| 83 | private static final int TEXT_HEIGHT = 50; |
| 84 | private static final String AUTHOR_COLOR = "#888888"; |
| 85 | private static final Color BORDER = Color.black; |
| 86 | private static final long doubleClickDelay = 200; // in ms |
| 87 | // |
| 88 | |
| 89 | private JLabel icon; |
| 90 | private JLabel title; |
| 91 | private boolean selected; |
| 92 | private boolean hovered; |
| 93 | private Date lastClick; |
| 94 | |
| 95 | private List<BookActionListener> listeners; |
| 96 | private Reader reader; |
| 97 | private MetaData meta; |
| 98 | private boolean cached; |
| 99 | |
| 100 | /** |
| 101 | * Create a new {@link GuiReaderBook} item for the given {@link Story}. |
| 102 | * |
| 103 | * @param reader |
| 104 | * the associated reader |
| 105 | * @param meta |
| 106 | * the story {@link MetaData} or source (if no LUID) |
| 107 | * @param cached |
| 108 | * TRUE if it is locally cached |
| 109 | * @param seeWordCount |
| 110 | * TRUE to see word counts, FALSE to see authors |
| 111 | */ |
| 112 | public GuiReaderBook(Reader reader, MetaData meta, boolean cached, |
| 113 | boolean seeWordCount) { |
| 114 | this.reader = reader; |
| 115 | this.cached = cached; |
| 116 | this.meta = meta; |
| 117 | |
| 118 | String optSecondary = meta.getAuthor(); |
| 119 | if (seeWordCount) { |
| 120 | if (meta.getWords() >= 4000) { |
| 121 | optSecondary = "" + (meta.getWords() / 1000) + "k"; |
| 122 | } else if (meta.getWords() > 0) { |
| 123 | optSecondary = "" + meta.getWords(); |
| 124 | } else { |
| 125 | optSecondary = ""; |
| 126 | } |
| 127 | |
| 128 | if (!optSecondary.isEmpty()) { |
| 129 | if (meta.isImageDocument()) { |
| 130 | optSecondary += " images"; |
| 131 | } else { |
| 132 | optSecondary += " words"; |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | if (optSecondary != null && !optSecondary.isEmpty()) { |
| 138 | optSecondary = "(" + optSecondary + ")"; |
| 139 | } else { |
| 140 | optSecondary = ""; |
| 141 | } |
| 142 | |
| 143 | icon = new JLabel(generateCoverIcon()); |
| 144 | title = new JLabel( |
| 145 | String.format( |
| 146 | "<html>" |
| 147 | + "<body style='width: %d px; height: %d px; text-align: center'>" |
| 148 | + "%s" + "<br>" + "<span style='color: %s;'>" |
| 149 | + "%s" + "</span>" + "</body>" + "</html>", |
| 150 | TEXT_WIDTH, TEXT_HEIGHT, meta.getTitle(), AUTHOR_COLOR, |
| 151 | optSecondary)); |
| 152 | |
| 153 | setLayout(new BorderLayout(10, 10)); |
| 154 | add(icon, BorderLayout.CENTER); |
| 155 | add(title, BorderLayout.SOUTH); |
| 156 | |
| 157 | setupListeners(); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * The book current selection state. |
| 162 | * |
| 163 | * @return the selection state |
| 164 | */ |
| 165 | public boolean isSelected() { |
| 166 | return selected; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * The book current selection state. |
| 171 | * |
| 172 | * @param selected |
| 173 | * TRUE if it is selected |
| 174 | */ |
| 175 | public void setSelected(boolean selected) { |
| 176 | if (this.selected != selected) { |
| 177 | this.selected = selected; |
| 178 | repaint(); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * The item mouse-hover state. |
| 184 | * |
| 185 | * @param hovered |
| 186 | * TRUE if it is mouse-hovered |
| 187 | */ |
| 188 | private void setHovered(boolean hovered) { |
| 189 | if (this.hovered != hovered) { |
| 190 | this.hovered = hovered; |
| 191 | repaint(); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Setup the mouse listener that will activate {@link BookActionListener} |
| 197 | * events. |
| 198 | */ |
| 199 | private void setupListeners() { |
| 200 | listeners = new ArrayList<GuiReaderBook.BookActionListener>(); |
| 201 | addMouseListener(new MouseListener() { |
| 202 | @Override |
| 203 | public void mouseReleased(MouseEvent e) { |
| 204 | if (e.isPopupTrigger()) { |
| 205 | popup(e); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public void mousePressed(MouseEvent e) { |
| 211 | if (e.isPopupTrigger()) { |
| 212 | popup(e); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public void mouseExited(MouseEvent e) { |
| 218 | setHovered(false); |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public void mouseEntered(MouseEvent e) { |
| 223 | setHovered(true); |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public void mouseClicked(MouseEvent e) { |
| 228 | if (isEnabled()) { |
| 229 | Date now = new Date(); |
| 230 | if (lastClick != null |
| 231 | && now.getTime() - lastClick.getTime() < doubleClickDelay) { |
| 232 | click(true); |
| 233 | } else { |
| 234 | click(false); |
| 235 | } |
| 236 | |
| 237 | lastClick = now; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | private void click(boolean doubleClick) { |
| 242 | for (BookActionListener listener : listeners) { |
| 243 | if (doubleClick) { |
| 244 | listener.action(GuiReaderBook.this); |
| 245 | } else { |
| 246 | listener.select(GuiReaderBook.this); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | private void popup(MouseEvent e) { |
| 252 | for (BookActionListener listener : listeners) { |
| 253 | listener.select((GuiReaderBook.this)); |
| 254 | listener.popupRequested(GuiReaderBook.this, e); |
| 255 | } |
| 256 | } |
| 257 | }); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Add a new {@link BookActionListener} on this item. |
| 262 | * |
| 263 | * @param listener |
| 264 | * the listener |
| 265 | */ |
| 266 | public void addActionListener(BookActionListener listener) { |
| 267 | listeners.add(listener); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * The Library {@link MetaData} of the book represented by this item. |
| 272 | * |
| 273 | * @return the meta |
| 274 | */ |
| 275 | public MetaData getMeta() { |
| 276 | return meta; |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * This item {@link GuiReader} library cache state. |
| 281 | * |
| 282 | * @return TRUE if it is present in the {@link GuiReader} cache |
| 283 | */ |
| 284 | public boolean isCached() { |
| 285 | return cached; |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * This item {@link GuiReader} library cache state. |
| 290 | * |
| 291 | * @param cached |
| 292 | * TRUE if it is present in the {@link GuiReader} cache |
| 293 | */ |
| 294 | public void setCached(boolean cached) { |
| 295 | if (this.cached != cached) { |
| 296 | this.cached = cached; |
| 297 | repaint(); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Paint the item, then call {@link GuiReaderBook#paintOverlay(Graphics)}. |
| 303 | */ |
| 304 | @Override |
| 305 | public void paint(Graphics g) { |
| 306 | super.paint(g); |
| 307 | paintOverlay(g); |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Draw a partially transparent overlay if needed depending upon the |
| 312 | * selection and mouse-hover states on top of the normal component, as well |
| 313 | * as a possible "cached" icon if the item is cached. |
| 314 | * |
| 315 | * @param g |
| 316 | * the {@link Graphics} to paint onto |
| 317 | */ |
| 318 | public void paintOverlay(Graphics g) { |
| 319 | Rectangle clip = g.getClipBounds(); |
| 320 | if (clip.getWidth() <= 0 || clip.getHeight() <= 0) { |
| 321 | return; |
| 322 | } |
| 323 | |
| 324 | int h = COVER_HEIGHT; |
| 325 | int w = COVER_WIDTH; |
| 326 | int xOffset = (TEXT_WIDTH - COVER_WIDTH) - 1; |
| 327 | int yOffset = HOFFSET; |
| 328 | |
| 329 | if (BORDER != null) { |
| 330 | if (BORDER != null) { |
| 331 | g.setColor(BORDER); |
| 332 | g.drawRect(xOffset, yOffset, COVER_WIDTH, COVER_HEIGHT); |
| 333 | } |
| 334 | |
| 335 | xOffset++; |
| 336 | yOffset++; |
| 337 | } |
| 338 | |
| 339 | int[] xs = new int[] { xOffset, xOffset + SPINE_WIDTH, |
| 340 | xOffset + w + SPINE_WIDTH, xOffset + w }; |
| 341 | int[] ys = new int[] { yOffset + h, yOffset + h + SPINE_HEIGHT, |
| 342 | yOffset + h + SPINE_HEIGHT, yOffset + h }; |
| 343 | g.setColor(SPINE_COLOR_BOTTOM); |
| 344 | g.fillPolygon(new Polygon(xs, ys, xs.length)); |
| 345 | xs = new int[] { xOffset + w, xOffset + w + SPINE_WIDTH, |
| 346 | xOffset + w + SPINE_WIDTH, xOffset + w }; |
| 347 | ys = new int[] { yOffset, yOffset + SPINE_HEIGHT, |
| 348 | yOffset + h + SPINE_HEIGHT, yOffset + h }; |
| 349 | g.setColor(SPINE_COLOR_RIGHT); |
| 350 | g.fillPolygon(new Polygon(xs, ys, xs.length)); |
| 351 | |
| 352 | Color color = new Color(255, 255, 255, 0); |
| 353 | if (!isEnabled()) { |
| 354 | } else if (selected && !hovered) { |
| 355 | color = new Color(80, 80, 100, 40); |
| 356 | } else if (!selected && hovered) { |
| 357 | color = new Color(230, 230, 255, 100); |
| 358 | } else if (selected && hovered) { |
| 359 | color = new Color(200, 200, 255, 100); |
| 360 | } |
| 361 | |
| 362 | g.setColor(color); |
| 363 | g.fillRect(clip.x, clip.y, clip.width, clip.height); |
| 364 | |
| 365 | if (cached) { |
| 366 | UIUtils.drawEllipse3D(g, Color.green.darker(), COVER_WIDTH |
| 367 | + HOFFSET + 30, 10, 20, 20); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Generate a cover icon based upon the given {@link MetaData}. |
| 373 | * |
| 374 | * @return the icon |
| 375 | */ |
| 376 | private ImageIcon generateCoverIcon() { |
| 377 | BufferedImage resizedImage = null; |
| 378 | String id = getIconId(meta); |
| 379 | |
| 380 | InputStream in = Instance.getCache().getFromCache(id); |
| 381 | if (in != null) { |
| 382 | try { |
| 383 | resizedImage = ImageUtils.fromStream(in); |
| 384 | in.close(); |
| 385 | in = null; |
| 386 | } catch (IOException e) { |
| 387 | Instance.getTraceHandler().error(e); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | if (resizedImage == null) { |
| 392 | try { |
| 393 | BufferedImage cover = null; |
| 394 | if (meta.getLuid() == null) { |
| 395 | cover = reader.getLibrary() |
| 396 | .getSourceCover(meta.getSource()); |
| 397 | } else { |
| 398 | cover = reader.getLibrary().getCover(meta.getLuid()); |
| 399 | } |
| 400 | |
| 401 | resizedImage = new BufferedImage(SPINE_WIDTH + COVER_WIDTH, |
| 402 | SPINE_HEIGHT + COVER_HEIGHT + HOFFSET, |
| 403 | BufferedImage.TYPE_4BYTE_ABGR); |
| 404 | Graphics2D g = resizedImage.createGraphics(); |
| 405 | g.setColor(Color.white); |
| 406 | g.fillRect(0, HOFFSET, COVER_WIDTH, COVER_HEIGHT); |
| 407 | if (cover != null) { |
| 408 | g.drawImage(cover, 0, HOFFSET, COVER_WIDTH, COVER_HEIGHT, |
| 409 | null); |
| 410 | } else { |
| 411 | g.setColor(Color.black); |
| 412 | g.drawLine(0, HOFFSET, COVER_WIDTH, HOFFSET + COVER_HEIGHT); |
| 413 | g.drawLine(COVER_WIDTH, HOFFSET, 0, HOFFSET + COVER_HEIGHT); |
| 414 | } |
| 415 | g.dispose(); |
| 416 | |
| 417 | if (id != null) { |
| 418 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 419 | ImageIO.write(resizedImage, "png", out); |
| 420 | byte[] imageBytes = out.toByteArray(); |
| 421 | in = new ByteArrayInputStream(imageBytes); |
| 422 | Instance.getCache().addToCache(in, id); |
| 423 | in.close(); |
| 424 | in = null; |
| 425 | } |
| 426 | } catch (MalformedURLException e) { |
| 427 | Instance.getTraceHandler().error(e); |
| 428 | } catch (IOException e) { |
| 429 | Instance.getTraceHandler().error(e); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | return new ImageIcon(resizedImage); |
| 434 | } |
| 435 | |
| 436 | /** |
| 437 | * Manually clear the icon set for this item. |
| 438 | * |
| 439 | * @param meta |
| 440 | * the meta of the story or source (if luid is null) |
| 441 | */ |
| 442 | public static void clearIcon(MetaData meta) { |
| 443 | String id = getIconId(meta); |
| 444 | Instance.getCache().removeFromCache(id); |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * Get a unique ID from this meta (note that if the luid is null, it is |
| 449 | * considered a source and not a {@link Story}). |
| 450 | * |
| 451 | * @param meta |
| 452 | * the meta |
| 453 | * @return the unique ID |
| 454 | */ |
| 455 | private static String getIconId(MetaData meta) { |
| 456 | String id = null; |
| 457 | |
| 458 | String key = meta.getUuid(); |
| 459 | if (meta.getLuid() == null) { |
| 460 | // a fake meta (== a source) |
| 461 | key = "source_" + meta.getSource(); |
| 462 | } |
| 463 | |
| 464 | id = key + ".thumb_" + SPINE_WIDTH + "x" + COVER_WIDTH + "+" |
| 465 | + SPINE_HEIGHT + "+" + COVER_HEIGHT + "@" + HOFFSET; |
| 466 | |
| 467 | return id; |
| 468 | } |
| 469 | } |