1 package be
.nikiroo
.fanfix_swing
.gui
;
3 import java
.awt
.BorderLayout
;
4 import java
.awt
.Component
;
7 import java
.awt
.event
.ActionEvent
;
8 import java
.awt
.event
.ActionListener
;
9 import java
.awt
.event
.MouseAdapter
;
10 import java
.awt
.event
.MouseEvent
;
11 import java
.util
.ArrayList
;
12 import java
.util
.HashMap
;
13 import java
.util
.List
;
15 import java
.util
.concurrent
.ExecutionException
;
17 import javax
.swing
.DefaultListModel
;
18 import javax
.swing
.JList
;
19 import javax
.swing
.JPopupMenu
;
20 import javax
.swing
.ListCellRenderer
;
21 import javax
.swing
.ListSelectionModel
;
22 import javax
.swing
.SwingWorker
;
24 import be
.nikiroo
.fanfix
.Instance
;
25 import be
.nikiroo
.fanfix
.data
.MetaData
;
26 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
27 import be
.nikiroo
.fanfix_swing
.Actions
;
28 import be
.nikiroo
.fanfix_swing
.gui
.book
.BookBlock
;
29 import be
.nikiroo
.fanfix_swing
.gui
.book
.BookInfo
;
30 import be
.nikiroo
.fanfix_swing
.gui
.book
.BookLine
;
31 import be
.nikiroo
.fanfix_swing
.gui
.book
.BookPopup
;
32 import be
.nikiroo
.fanfix_swing
.gui
.utils
.DelayWorker
;
33 import be
.nikiroo
.fanfix_swing
.gui
.utils
.ListenerPanel
;
34 import be
.nikiroo
.fanfix_swing
.gui
.utils
.UiHelper
;
36 public class BooksPanel
extends ListenerPanel
{
37 private class ListModel
extends DefaultListModel
<BookInfo
> {
38 public void fireElementChanged(BookInfo element
) {
39 int index
= indexOf(element
);
41 fireContentsChanged(element
, index
, index
);
46 static public final String INVALIDATE_CACHE
= "invalidate_cache";
48 private List
<BookInfo
> bookInfos
= new ArrayList
<BookInfo
>();
49 private Map
<BookInfo
, BookLine
> books
= new HashMap
<BookInfo
, BookLine
>();
50 private boolean seeWordCount
;
51 private boolean listMode
;
53 private JList
<BookInfo
> list
;
54 private int hoveredIndex
= -1;
55 private ListModel data
= new ListModel();
56 private DelayWorker bookCoverUpdater
;
58 private SearchBar searchBar
;
60 public BooksPanel(boolean listMode
) {
61 setLayout(new BorderLayout());
63 searchBar
= new SearchBar();
64 add(searchBar
, BorderLayout
.NORTH
);
66 searchBar
.addActionListener(new ActionListener() {
68 public void actionPerformed(ActionEvent e
) {
69 filter(searchBar
.getText());
73 bookCoverUpdater
= new DelayWorker(20);
74 bookCoverUpdater
.start();
75 add(UiHelper
.scroll(initList(listMode
)), BorderLayout
.CENTER
);
78 // null or empty -> all sources
79 // sources hierarchy supported ("source/" will includes all "source" and
81 public void load(final List
<String
> sources
, final List
<String
> authors
,
82 final List
<String
> tags
) {
83 new SwingWorker
<List
<BookInfo
>, Void
>() {
85 protected List
<BookInfo
> doInBackground() throws Exception
{
86 List
<BookInfo
> bookInfos
= new ArrayList
<BookInfo
>();
87 BasicLibrary lib
= Instance
.getInstance().getLibrary();
88 for (MetaData meta
: lib
.getList().filter(sources
, authors
,
90 bookInfos
.add(BookInfo
.fromMeta(lib
, meta
));
97 protected void done() {
100 } catch (InterruptedException e
) {
102 } catch (ExecutionException e
) {
110 public void load(List
<BookInfo
> bookInfos
) {
111 this.bookInfos
.clear();
112 this.bookInfos
.addAll(bookInfos
);
113 bookCoverUpdater
.clear();
115 filter(searchBar
.getText());
119 private void filter(String filter
) {
121 for (BookInfo bookInfo
: bookInfos
) {
122 if (bookInfo
.getMainInfo() == null || filter
.isEmpty()
123 || bookInfo
.getMainInfo().toLowerCase()
124 .contains(filter
.toLowerCase())) {
125 data
.addElement(bookInfo
);
132 * The secondary value content: word count or author.
134 * @return TRUE to see word counts, FALSE to see authors
136 public boolean isSeeWordCount() {
141 * The secondary value content: word count or author.
143 * @param seeWordCount
144 * TRUE to see word counts, FALSE to see authors
146 public void setSeeWordCount(boolean seeWordCount
) {
147 if (this.seeWordCount
!= seeWordCount
) {
149 for (BookLine book
: books
.values()) {
150 book
.setSeeWordCount(seeWordCount
);
158 private JList
<BookInfo
> initList(boolean listMode
) {
159 final JList
<BookInfo
> list
= new JList
<BookInfo
>(data
);
161 final JPopupMenu popup
= new BookPopup(
162 Instance
.getInstance().getLibrary(), new BookPopup
.Informer() {
164 public void setCached(BookInfo book
, boolean cached
) {
165 book
.setCached(cached
);
166 fireElementChanged(book
);
169 public void fireElementChanged(BookInfo book
) {
170 data
.fireElementChanged(book
);
174 public List
<BookInfo
> getSelected() {
175 List
<BookInfo
> selected
= new ArrayList
<BookInfo
>();
176 for (int index
: list
.getSelectedIndices()) {
177 selected
.add(data
.get(index
));
184 public BookInfo
getUniqueSelected() {
185 List
<BookInfo
> selected
= getSelected();
186 if (selected
.size() == 1) {
187 return selected
.get(0);
193 public void invalidateCache() {
194 // TODO: also reset the popup menu for sources/author
195 fireActionPerformed(INVALIDATE_CACHE
);
199 list
.addMouseMotionListener(new MouseAdapter() {
201 public void mouseMoved(MouseEvent me
) {
202 if (popup
.isShowing())
205 Point p
= new Point(me
.getX(), me
.getY());
206 int index
= list
.locationToIndex(p
);
207 if (index
!= hoveredIndex
) {
208 hoveredIndex
= index
;
213 list
.addMouseListener(new MouseAdapter() {
215 public void mousePressed(MouseEvent e
) {
220 public void mouseReleased(MouseEvent e
) {
225 public void mouseExited(MouseEvent e
) {
226 if (popup
.isShowing())
229 if (hoveredIndex
> -1) {
236 public void mouseClicked(MouseEvent e
) {
237 super.mouseClicked(e
);
238 if (e
.getClickCount() == 2) {
239 int index
= list
.locationToIndex(e
.getPoint());
240 list
.setSelectedIndex(index
);
242 final BookInfo book
= data
.get(index
);
243 BasicLibrary lib
= Instance
.getInstance().getLibrary();
245 Actions
.openExternal(lib
, book
.getMeta(), BooksPanel
.this,
249 book
.setCached(true);
250 data
.fireElementChanged(book
);
256 private void check(MouseEvent e
) {
257 if (e
.isPopupTrigger()) {
258 if (list
.getSelectedIndices().length
<= 1) {
259 list
.setSelectedIndex(
260 list
.locationToIndex(e
.getPoint()));
263 popup
.show(list
, e
.getX(), e
.getY());
268 list
.setSelectionMode(ListSelectionModel
.MULTIPLE_INTERVAL_SELECTION
);
269 list
.setSelectedIndex(0);
270 list
.setCellRenderer(generateRenderer());
271 list
.setVisibleRowCount(0);
274 setListMode(listMode
);
278 private ListCellRenderer
<BookInfo
> generateRenderer() {
279 return new ListCellRenderer
<BookInfo
>() {
281 public Component
getListCellRendererComponent(
282 JList
<?
extends BookInfo
> list
, BookInfo value
, int index
,
283 boolean isSelected
, boolean cellHasFocus
) {
284 BookLine book
= books
.get(value
);
287 book
= new BookLine(value
, seeWordCount
);
289 book
= new BookBlock(value
, seeWordCount
);
290 startUpdateBookCover((BookBlock
) book
);
292 books
.put(value
, book
);
295 book
.setSelected(isSelected
);
296 book
.setHovered(index
== hoveredIndex
);
302 private void startUpdateBookCover(final BookBlock book
) {
303 bookCoverUpdater
.delay(book
.getInfo().getId(),
304 new SwingWorker
<Image
, Void
>() {
306 protected Image
doInBackground() throws Exception
{
307 BasicLibrary lib
= Instance
.getInstance().getLibrary();
308 return BookBlock
.generateCoverImage(lib
,
312 protected void done() {
314 book
.setCoverImage(get());
315 data
.fireElementChanged(book
.getInfo());
316 } catch (Exception e
) {
317 // TODO ? probably just log
323 public boolean isListMode() {
327 public void setListMode(boolean listMode
) {
328 this.listMode
= listMode
;
330 list
.setLayoutOrientation(
331 listMode ? JList
.VERTICAL
: JList
.HORIZONTAL_WRAP
);
334 bookCoverUpdater
.clear();