package be.nikiroo.fanfix.reader.ui; import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.util.ArrayList; import java.util.List; import javax.swing.JLabel; import javax.swing.JPanel; import be.nikiroo.fanfix.reader.ui.GuiReaderBook.BookActionListener; import be.nikiroo.utils.ui.WrapLayout; /** * A group of {@link GuiReaderBook}s for display. * * @author niki */ public class GuiReaderGroup extends JPanel { private static final long serialVersionUID = 1L; private BookActionListener action; private Color backgroundColor; private GuiReader reader; private List infos; private List books; private JPanel pane; private boolean words; // words or authors (secondary info on books) /** * Create a new {@link GuiReaderGroup}. * * @param reader * the {@link GuiReaderBook} used to probe some information about * the stories * @param title * the title of this group * @param backgroundColor * the background colour to use (or NULL for default) */ public GuiReaderGroup(GuiReader reader, String title, Color backgroundColor) { this.reader = reader; this.backgroundColor = backgroundColor; this.pane = new JPanel(); pane.setLayout(new WrapLayout(WrapLayout.LEADING, 5, 5)); if (backgroundColor != null) { pane.setBackground(backgroundColor); setBackground(backgroundColor); } setLayout(new BorderLayout(0, 10)); add(pane, BorderLayout.CENTER); if (title != null) { if (title.isEmpty()) { title = "[unknown]"; } JLabel label = new JLabel(); label.setText(String.format("" + "
" + "%s" + "" + "", title)); label.setHorizontalAlignment(JLabel.CENTER); add(label, BorderLayout.NORTH); } } /** * Set the {@link ActionListener} that will be fired on each * {@link GuiReaderBook} action. * * @param action * the action */ public void setActionListener(BookActionListener action) { this.action = action; refreshBooks(infos, words); } /** * Refresh the list of {@link GuiReaderBook}s displayed in the control. * * @param infos * the new list of infos * @param seeWordcount * TRUE to see word counts, FALSE to see authors */ public void refreshBooks(List infos, boolean seeWordcount) { this.infos = infos; refreshBooks(seeWordcount); } /** * Refresh the list of {@link GuiReaderBook}s displayed in the control. *

* Will not change the current stories. * * @param seeWordcount * TRUE to see word counts, FALSE to see authors */ public void refreshBooks(boolean seeWordcount) { this.words = seeWordcount; books = new ArrayList(); invalidate(); pane.invalidate(); pane.removeAll(); if (infos != null) { for (GuiReaderBookInfo info : infos) { boolean isCached = false; if (info.getMeta() != null) { isCached = reader.isCached(info.getMeta().getLuid()); } GuiReaderBook book = new GuiReaderBook(reader, info, isCached, words); if (backgroundColor != null) { book.setBackground(backgroundColor); } books.add(book); book.addActionListener(new BookActionListener() { @Override public void select(GuiReaderBook book) { for (GuiReaderBook abook : books) { abook.setSelected(abook == book); } } @Override public void popupRequested(GuiReaderBook book, MouseEvent e) { } @Override public void action(GuiReaderBook book) { } }); if (action != null) { book.addActionListener(action); } pane.add(book); } } pane.validate(); pane.repaint(); validate(); repaint(); } /** * Enables or disables this component, depending on the value of the * parameter b. An enabled component can respond to user input * and generate events. Components are enabled initially by default. *

* Disabling this component will also affect its children. * * @param b * If true, this component is enabled; otherwise * this component is disabled */ @Override public void setEnabled(boolean b) { if (books != null) { for (GuiReaderBook book : books) { book.setEnabled(b); book.repaint(); } } pane.setEnabled(b); super.setEnabled(b); repaint(); } }