X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Freader%2FLocalReaderBook.java;h=c716bd3aa47d75f3d3171aaf3d07f261c154bbad;hb=10d558d2429c984327f9e5a16933fefe5cc37314;hp=ef6ba48588df4cfb9cc778343e1b6f26fd665f0a;hpb=d3c84ac3317780016a5a6fff263ac8ff46220db9;p=fanfix.git diff --git a/src/be/nikiroo/fanfix/reader/LocalReaderBook.java b/src/be/nikiroo/fanfix/reader/LocalReaderBook.java index ef6ba48..c716bd3 100644 --- a/src/be/nikiroo/fanfix/reader/LocalReaderBook.java +++ b/src/be/nikiroo/fanfix/reader/LocalReaderBook.java @@ -4,6 +4,7 @@ import java.awt.BorderLayout; import java.awt.Color; 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; @@ -16,7 +17,6 @@ import java.util.List; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JPanel; -import javax.swing.JTextArea; import be.nikiroo.fanfix.data.MetaData; @@ -31,7 +31,7 @@ class LocalReaderBook extends JPanel { * * @author niki */ - interface BookActionListner extends EventListener { + interface BookActionListener extends EventListener { /** * The book was selected (single click). * @@ -49,37 +49,63 @@ class LocalReaderBook extends JPanel { public void action(LocalReaderBook book); } + 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 long serialVersionUID = 1L; + private JLabel icon; - private JTextArea title; - private JTextArea author; + private JLabel title; private boolean selected; private boolean hovered; private Date lastClick; private long doubleClickDelay = 200; // in ms - private List listeners; + private List listeners; + private String luid; + private boolean cached; + + public LocalReaderBook(MetaData meta, boolean cached) { + this.luid = meta.getLuid(); + this.cached = cached; - public LocalReaderBook(MetaData meta) { if (meta.getCover() != null) { - BufferedImage resizedImage = new BufferedImage(100, 150, + BufferedImage resizedImage = new BufferedImage(SPINE_WIDTH + + COVER_WIDTH, SPINE_HEIGHT + COVER_HEIGHT + HOFFSET, BufferedImage.TYPE_4BYTE_ABGR); Graphics2D g = resizedImage.createGraphics(); - g.drawImage(meta.getCover(), 0, 0, 100, 150, null); + g.setColor(Color.white); + g.fillRect(0, HOFFSET, COVER_WIDTH, COVER_HEIGHT); + g.drawImage(meta.getCover(), 0, HOFFSET, COVER_WIDTH, COVER_HEIGHT, + null); g.dispose(); icon = new JLabel(new ImageIcon(resizedImage)); } else { + // TODO: a big black "X" ? icon = new JLabel(" [ no cover ] "); } - title = new JTextArea(meta.getTitle()); - title.setWrapStyleWord(true); - title.setLineWrap(true); - title.setEditable(false); - title.setBackground(new Color(0, true)); - author = new JTextArea("by " + meta.getAuthor()); - - this.setLayout(new BorderLayout()); + String optAuthor = meta.getAuthor(); + if (optAuthor != null && !optAuthor.isEmpty()) { + optAuthor = "(" + optAuthor + ")"; + } + title = new JLabel( + String.format( + "" + + "" + + "%s" + "
" + "" + + "%s" + "" + "" + "", + TEXT_WIDTH, TEXT_HEIGHT, meta.getTitle(), AUTHOR_COLOR, + optAuthor)); + + this.setLayout(new BorderLayout(10, 10)); this.add(icon, BorderLayout.CENTER); this.add(title, BorderLayout.SOUTH); @@ -113,7 +139,7 @@ class LocalReaderBook extends JPanel { } private void setupListeners() { - listeners = new ArrayList(); + listeners = new ArrayList(); addMouseListener(new MouseListener() { public void mouseReleased(MouseEvent e) { } @@ -130,20 +156,22 @@ class LocalReaderBook extends JPanel { } public void mouseClicked(MouseEvent e) { - Date now = new Date(); - if (lastClick != null - && now.getTime() - lastClick.getTime() < doubleClickDelay) { - click(true); - } else { - click(false); + if (isEnabled()) { + Date now = new Date(); + if (lastClick != null + && now.getTime() - lastClick.getTime() < doubleClickDelay) { + click(true); + } else { + click(false); + } + lastClick = now; } - lastClick = now; } }); } private void click(boolean doubleClick) { - for (BookActionListner listener : listeners) { + for (BookActionListener listener : listeners) { if (doubleClick) { listener.action(this); } else { @@ -152,16 +180,57 @@ class LocalReaderBook extends JPanel { } } - public void addActionListener(BookActionListner listener) { + public void addActionListener(BookActionListener listener) { listeners.add(listener); } + public String getLuid() { + return luid; + } + + /** + * This boos is cached into the {@link LocalReader} library. + * + * @return the cached + */ + public boolean isCached() { + return cached; + } + + /** + * This boos is cached into the {@link LocalReader} library. + * + * @param cached + * the cached to set + */ + public void setCached(boolean cached) { + this.cached = cached; + } + @Override public void paint(Graphics g) { super.paint(g); + int h = COVER_HEIGHT; + int w = COVER_WIDTH; + int xOffset = (TEXT_WIDTH - COVER_WIDTH) - 4; + + int[] xs = new int[] { xOffset, xOffset + SPINE_WIDTH, + xOffset + w + SPINE_WIDTH, xOffset + w }; + int[] ys = new int[] { HOFFSET + h, HOFFSET + h + SPINE_HEIGHT, + HOFFSET + h + SPINE_HEIGHT, HOFFSET + 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[] { HOFFSET, HOFFSET + SPINE_HEIGHT, + HOFFSET + h + SPINE_HEIGHT, HOFFSET + h }; + g.setColor(SPINE_COLOR_RIGHT); + g.fillPolygon(new Polygon(xs, ys, xs.length)); + Color color = new Color(255, 255, 255, 0); - if (selected && !hovered) { + if (!isEnabled()) { + } else if (selected && !hovered) { color = new Color(80, 80, 100, 40); } else if (!selected && hovered) { color = new Color(230, 230, 255, 100); @@ -170,6 +239,11 @@ class LocalReaderBook extends JPanel { } Rectangle clip = g.getClipBounds(); + if (cached) { + g.setColor(Color.green); + g.fillOval(clip.x + clip.width - 30, 10, 20, 20); + } + g.setColor(color); g.fillRect(clip.x, clip.y, clip.width, clip.height); }