package be.nikiroo.fanfix.reader.ui; import java.awt.BorderLayout; import java.awt.Component; import java.awt.Graphics; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.ArrayList; import java.util.Date; import java.util.EventListener; import java.util.List; import javax.swing.JLabel; import javax.swing.JPanel; import be.nikiroo.fanfix.data.Story; import be.nikiroo.fanfix.reader.Reader; /** * A book item presented in a {@link GuiReaderFrame}. *
* Can be a story, or a comic or... a group.
*
* @author niki
*/
class GuiReaderBook extends JPanel {
/**
* Action on a book item.
*
* @author niki
*/
interface BookActionListener extends EventListener {
/**
* The book was selected (single click).
*
* @param book
* the {@link GuiReaderBook} itself
*/
public void select(GuiReaderBook book);
/**
* The book was double-clicked.
*
* @param book
* the {@link GuiReaderBook} itself
*/
public void action(GuiReaderBook book);
/**
* A popup menu was requested for this {@link GuiReaderBook}.
*
* @param book
* the {@link GuiReaderBook} itself
* @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, Component target, int x,
int y);
}
private static final long serialVersionUID = 1L;
private static final String AUTHOR_COLOR = "#888888";
private static final long doubleClickDelay = 200; // in ms
private JLabel icon;
private JLabel title;
private boolean selected;
private boolean hovered;
private Date lastClick;
private List
* 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
*/
public void setSelected(boolean selected) {
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
*/
public void setHovered(boolean hovered) {
if (this.hovered != hovered) {
this.hovered = hovered;
repaint();
}
}
/**
* Setup the mouse listener that will activate {@link BookActionListener}
* events.
*/
private void setupListeners() {
listeners = new ArrayList
* 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 GuiReaderBookInfo getInfo() {
return info;
}
/**
* This item {@link GuiReader} library cache state.
*
* @return TRUE if it is present in the {@link GuiReader} cache
*/
public boolean isCached() {
return cached;
}
/**
* This item {@link GuiReader} library cache state.
*
* @param cached
* TRUE if it is present in the {@link GuiReader} cache
*/
public void setCached(boolean cached) {
if (this.cached != cached) {
this.cached = cached;
repaint();
}
}
/**
* 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);
GuiReaderCoverImager.paintOverlay(g, isEnabled(), isSelected(),
isHovered(), isCached());
}
/**
* Update the title with the currently registered information.
*/
private void updateTitle() {
String optSecondary = info.getSecondaryInfo(seeWordCount);
title.setText(String
.format(""
+ "
" + "" + "%s"
+ "" + "" + "",
GuiReaderCoverImager.TEXT_WIDTH,
GuiReaderCoverImager.TEXT_HEIGHT, info.getMainInfo(),
AUTHOR_COLOR, optSecondary));
}
}