X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Freader%2Fui%2FGuiReaderBook.java;h=73ccdaa0c1b34427a6c10f64046de8199062d329;hb=1387a30ab59dbf4071f2c5e5e0e08ca98c75b726;hp=dc275950c6297ae561504b18131a1853dfd1a2c5;hpb=eeadd228f3069eed696802e25216b5580bca4303;p=nikiroo-utils.git diff --git a/src/be/nikiroo/fanfix/reader/ui/GuiReaderBook.java b/src/be/nikiroo/fanfix/reader/ui/GuiReaderBook.java index dc27595..73ccdaa 100644 --- a/src/be/nikiroo/fanfix/reader/ui/GuiReaderBook.java +++ b/src/be/nikiroo/fanfix/reader/ui/GuiReaderBook.java @@ -1,39 +1,25 @@ package be.nikiroo.fanfix.reader.ui; import java.awt.BorderLayout; -import java.awt.Color; +import java.awt.Component; import java.awt.Graphics; -import java.awt.Graphics2D; -import java.awt.Polygon; -import java.awt.Rectangle; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; -import java.awt.image.BufferedImage; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; import java.util.ArrayList; import java.util.Date; import java.util.EventListener; import java.util.List; -import javax.imageio.ImageIO; -import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JPanel; -import be.nikiroo.fanfix.Instance; -import be.nikiroo.fanfix.data.MetaData; import be.nikiroo.fanfix.data.Story; import be.nikiroo.fanfix.reader.Reader; -import be.nikiroo.utils.Image; -import be.nikiroo.utils.ui.ImageUtilsAwt; -import be.nikiroo.utils.ui.UIUtils; /** * A book item presented in a {@link GuiReaderFrame}. + *

+ * Can be a story, or a comic or... a group. * * @author niki */ @@ -65,28 +51,25 @@ class GuiReaderBook extends JPanel { * * @param book * the {@link GuiReaderBook} itself - * @param e - * the {@link MouseEvent} that generated this call + * @param target + * the target component for the popup + * @param x + * the X position of the click/request (in case of popup + * request from the keyboard, the center of the target is + * selected as point of reference) + * @param y + * the Y position of the click/request (in case of popup + * request from the keyboard, the center of the target is + * selected as point of reference) */ - public void popupRequested(GuiReaderBook book, MouseEvent e); + public void popupRequested(GuiReaderBook book, Component target, int x, + int y); } private static final long serialVersionUID = 1L; - // TODO: export some of the configuration options? - private static final int COVER_WIDTH = 100; - private static final int COVER_HEIGHT = 150; - private static final int SPINE_WIDTH = 5; - private static final int SPINE_HEIGHT = 5; - private static final int HOFFSET = 20; - private static final Color SPINE_COLOR_BOTTOM = new Color(180, 180, 180); - private static final Color SPINE_COLOR_RIGHT = new Color(100, 100, 100); - private static final int TEXT_WIDTH = COVER_WIDTH + 40; - private static final int TEXT_HEIGHT = 50; private static final String AUTHOR_COLOR = "#888888"; - private static final Color BORDER = Color.black; private static final long doubleClickDelay = 200; // in ms - // private JLabel icon; private JLabel title; @@ -95,62 +78,33 @@ class GuiReaderBook extends JPanel { private Date lastClick; private List listeners; - private Reader reader; - private MetaData meta; + private GuiReaderBookInfo info; private boolean cached; + private boolean seeWordCount; /** * Create a new {@link GuiReaderBook} item for the given {@link Story}. * * @param reader * the associated reader - * @param meta - * the story {@link MetaData} or source (if no LUID) + * @param info + * the information about the story to represent * @param cached * TRUE if it is locally cached * @param seeWordCount * TRUE to see word counts, FALSE to see authors */ - public GuiReaderBook(Reader reader, MetaData meta, boolean cached, + public GuiReaderBook(Reader reader, GuiReaderBookInfo info, boolean cached, boolean seeWordCount) { - this.reader = reader; + this.info = info; this.cached = cached; - this.meta = meta; - - String optSecondary = meta.getAuthor(); - if (seeWordCount) { - if (meta.getWords() >= 4000) { - optSecondary = "" + (meta.getWords() / 1000) + "k"; - } else if (meta.getWords() > 0) { - optSecondary = "" + meta.getWords(); - } else { - optSecondary = ""; - } - - if (!optSecondary.isEmpty()) { - if (meta.isImageDocument()) { - optSecondary += " images"; - } else { - optSecondary += " words"; - } - } - } + this.seeWordCount = seeWordCount; - if (optSecondary != null && !optSecondary.isEmpty()) { - optSecondary = "(" + optSecondary + ")"; - } else { - optSecondary = ""; - } + icon = new JLabel(GuiReaderCoverImager.generateCoverIcon( + reader.getLibrary(), info)); - icon = new JLabel(generateCoverIcon()); - title = new JLabel( - String.format( - "" - + "" - + "%s" + "
" + "" - + "%s" + "" + "" + "", - TEXT_WIDTH, TEXT_HEIGHT, meta.getTitle(), AUTHOR_COLOR, - optSecondary)); + title = new JLabel(); + updateTitle(); setLayout(new BorderLayout(10, 10)); add(icon, BorderLayout.CENTER); @@ -170,6 +124,9 @@ class GuiReaderBook extends JPanel { /** * The book current selection state. + *

+ * Setting this value to true can cause a "select" action to occur if the + * previous state was "unselected". * * @param selected * TRUE if it is selected @@ -178,16 +135,29 @@ class GuiReaderBook extends JPanel { if (this.selected != selected) { this.selected = selected; repaint(); + + if (selected) { + select(); + } } } + /** + * The item mouse-hover state. + * + * @return TRUE if it is mouse-hovered + */ + public boolean isHovered() { + return this.hovered; + } + /** * The item mouse-hover state. * * @param hovered * TRUE if it is mouse-hovered */ - private void setHovered(boolean hovered) { + public void setHovered(boolean hovered) { if (this.hovered != hovered) { this.hovered = hovered; repaint(); @@ -203,14 +173,14 @@ class GuiReaderBook extends JPanel { addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { - if (e.isPopupTrigger()) { + if (isEnabled() && e.isPopupTrigger()) { popup(e); } } @Override public void mousePressed(MouseEvent e) { - if (e.isPopupTrigger()) { + if (isEnabled() && e.isPopupTrigger()) { popup(e); } } @@ -237,24 +207,22 @@ class GuiReaderBook extends JPanel { } lastClick = now; + e.consume(); } } private void click(boolean doubleClick) { - for (BookActionListener listener : listeners) { - if (doubleClick) { - listener.action(GuiReaderBook.this); - } else { - listener.select(GuiReaderBook.this); - } + if (doubleClick) { + action(); + } else { + select(); } } private void popup(MouseEvent e) { - for (BookActionListener listener : listeners) { - listener.select((GuiReaderBook.this)); - listener.popupRequested(GuiReaderBook.this, e); - } + GuiReaderBook.this + .popup(GuiReaderBook.this, e.getX(), e.getY()); + e.consume(); } }); } @@ -270,12 +238,53 @@ class GuiReaderBook extends JPanel { } /** - * The Library {@link MetaData} of the book represented by this item. + * Cause an action to occur on this {@link GuiReaderBook}. + */ + public void action() { + for (BookActionListener listener : listeners) { + listener.action(GuiReaderBook.this); + } + } + + /** + * Cause a select event on this {@link GuiReaderBook}. + *

+ * Have a look at {@link GuiReaderBook#setSelected(boolean)}. + */ + private void select() { + for (BookActionListener listener : listeners) { + listener.select(GuiReaderBook.this); + } + } + + /** + * Request a popup. + * + * @param target + * the target component for the popup + * @param x + * the X position of the click/request (in case of popup request + * from the keyboard, the center of the target should be selected + * as point of reference) + * @param y + * the Y position of the click/request (in case of popup request + * from the keyboard, the center of the target should be selected + * as point of reference) + */ + public void popup(Component target, int x, int y) { + for (BookActionListener listener : listeners) { + listener.select((GuiReaderBook.this)); + listener.popupRequested(GuiReaderBook.this, target, x, y); + } + } + + /** + * The information about the book represented by this item. * * @return the meta */ - public MetaData getMeta() { - return meta; + public GuiReaderBookInfo getInfo() { + return info; } /** @@ -301,177 +310,30 @@ class GuiReaderBook extends JPanel { } /** - * Paint the item, then call {@link GuiReaderBook#paintOverlay(Graphics)}. + * Update the title, paint the item, then call + * {@link GuiReaderCoverImager#paintOverlay(Graphics, boolean, boolean, boolean, boolean)} + * . */ @Override public void paint(Graphics g) { + updateTitle(); super.paint(g); - paintOverlay(g); - } - - /** - * Draw a partially transparent overlay if needed depending upon the - * selection and mouse-hover states on top of the normal component, as well - * as a possible "cached" icon if the item is cached. - * - * @param g - * the {@link Graphics} to paint onto - */ - public void paintOverlay(Graphics g) { - Rectangle clip = g.getClipBounds(); - if (clip.getWidth() <= 0 || clip.getHeight() <= 0) { - return; - } - - int h = COVER_HEIGHT; - int w = COVER_WIDTH; - int xOffset = (TEXT_WIDTH - COVER_WIDTH) - 1; - int yOffset = HOFFSET; - - if (BORDER != null) { - if (BORDER != null) { - g.setColor(BORDER); - g.drawRect(xOffset, yOffset, COVER_WIDTH, COVER_HEIGHT); - } - - xOffset++; - yOffset++; - } - - int[] xs = new int[] { xOffset, xOffset + SPINE_WIDTH, - xOffset + w + SPINE_WIDTH, xOffset + w }; - int[] ys = new int[] { yOffset + h, yOffset + h + SPINE_HEIGHT, - yOffset + h + SPINE_HEIGHT, yOffset + h }; - g.setColor(SPINE_COLOR_BOTTOM); - g.fillPolygon(new Polygon(xs, ys, xs.length)); - xs = new int[] { xOffset + w, xOffset + w + SPINE_WIDTH, - xOffset + w + SPINE_WIDTH, xOffset + w }; - ys = new int[] { yOffset, yOffset + SPINE_HEIGHT, - yOffset + h + SPINE_HEIGHT, yOffset + h }; - g.setColor(SPINE_COLOR_RIGHT); - g.fillPolygon(new Polygon(xs, ys, xs.length)); - - Color color = new Color(255, 255, 255, 0); - if (!isEnabled()) { - } else if (selected && !hovered) { - color = new Color(80, 80, 100, 40); - } else if (!selected && hovered) { - color = new Color(230, 230, 255, 100); - } else if (selected && hovered) { - color = new Color(200, 200, 255, 100); - } - - g.setColor(color); - g.fillRect(clip.x, clip.y, clip.width, clip.height); - - if (cached) { - UIUtils.drawEllipse3D(g, Color.green.darker(), COVER_WIDTH - + HOFFSET + 30, 10, 20, 20); - } - } - - /** - * Generate a cover icon based upon the given {@link MetaData}. - * - * @return the icon - */ - private ImageIcon generateCoverIcon() { - BufferedImage resizedImage = null; - String id = getIconId(meta); - - InputStream in = Instance.getCache().getFromCache(id); - if (in != null) { - try { - resizedImage = ImageUtilsAwt.fromImage(new Image(in)); - in.close(); - in = null; - } catch (IOException e) { - Instance.getTraceHandler().error(e); - } - } - - if (resizedImage == null) { - try { - Image cover = null; - if (meta.getLuid() != null) { - cover = reader.getLibrary().getCover(meta.getLuid()); - } - if (cover == null) { - cover = reader.getLibrary() - .getSourceCover(meta.getSource()); - } - - resizedImage = new BufferedImage(SPINE_WIDTH + COVER_WIDTH, - SPINE_HEIGHT + COVER_HEIGHT + HOFFSET, - BufferedImage.TYPE_4BYTE_ABGR); - Graphics2D g = resizedImage.createGraphics(); - g.setColor(Color.white); - g.fillRect(0, HOFFSET, COVER_WIDTH, COVER_HEIGHT); - if (cover != null) { - BufferedImage coverb = ImageUtilsAwt.fromImage(cover); - g.drawImage(coverb, 0, HOFFSET, COVER_WIDTH, COVER_HEIGHT, - null); - } else { - g.setColor(Color.black); - g.drawLine(0, HOFFSET, COVER_WIDTH, HOFFSET + COVER_HEIGHT); - g.drawLine(COVER_WIDTH, HOFFSET, 0, HOFFSET + COVER_HEIGHT); - } - g.dispose(); - - if (id != null) { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - ImageIO.write(resizedImage, "png", out); - byte[] imageBytes = out.toByteArray(); - in = new ByteArrayInputStream(imageBytes); - Instance.getCache().addToCache(in, id); - in.close(); - in = null; - } - } catch (MalformedURLException e) { - Instance.getTraceHandler().error(e); - } catch (IOException e) { - Instance.getTraceHandler().error(e); - } - } - - if (resizedImage != null) { - return new ImageIcon(resizedImage); - } - - return null; + GuiReaderCoverImager.paintOverlay(g, isEnabled(), isSelected(), + isHovered(), isCached()); } /** - * Manually clear the icon set for this item. - * - * @param meta - * the meta of the story or source (if luid is null) - */ - public static void clearIcon(MetaData meta) { - String id = getIconId(meta); - Instance.getCache().removeFromCache(id); - } - - /** - * Get a unique ID from this meta (note that if the luid is null, it is - * considered a source and not a {@link Story}). - * - * @param meta - * the meta - * @return the unique ID + * Update the title with the currently registered information. */ - private static String getIconId(MetaData meta) { - String id = null; - - String key = meta.getUuid(); - if (meta.getLuid() == null) { - // a fake meta (== a source) - key = "source_" + meta.getSource(); - } - - id = key + ".thumb_" + SPINE_WIDTH + "x" + COVER_WIDTH + "+" - + SPINE_HEIGHT + "+" + COVER_HEIGHT + "@" + HOFFSET; - - return id; + private void updateTitle() { + String optSecondary = info.getSecondaryInfo(seeWordCount); + title.setText(String + .format("" + + "" + + "%s" + "
" + "" + "%s" + + "" + "" + "", + GuiReaderCoverImager.TEXT_WIDTH, + GuiReaderCoverImager.TEXT_HEIGHT, info.getMainInfo(), + AUTHOR_COLOR, optSecondary)); } }