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
.LinkedList
;
14 import java
.util
.List
;
16 import java
.util
.Queue
;
17 import java
.util
.concurrent
.ExecutionException
;
19 import javax
.swing
.DefaultListModel
;
20 import javax
.swing
.JList
;
21 import javax
.swing
.JPopupMenu
;
22 import javax
.swing
.ListCellRenderer
;
23 import javax
.swing
.ListSelectionModel
;
24 import javax
.swing
.SwingUtilities
;
25 import javax
.swing
.SwingWorker
;
27 import be
.nikiroo
.fanfix
.Instance
;
28 import be
.nikiroo
.fanfix
.data
.MetaData
;
29 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
30 import be
.nikiroo
.fanfix_swing
.Actions
;
31 import be
.nikiroo
.fanfix_swing
.gui
.book
.BookBlock
;
32 import be
.nikiroo
.fanfix_swing
.gui
.book
.BookInfo
;
33 import be
.nikiroo
.fanfix_swing
.gui
.book
.BookLine
;
34 import be
.nikiroo
.fanfix_swing
.gui
.book
.BookPopup
;
35 import be
.nikiroo
.fanfix_swing
.gui
.utils
.ListenerPanel
;
36 import be
.nikiroo
.fanfix_swing
.gui
.utils
.UiHelper
;
38 public class BooksPanel
extends ListenerPanel
{
39 private class ListModel
extends DefaultListModel
<BookInfo
> {
40 public void fireElementChanged(BookInfo element
) {
41 int index
= indexOf(element
);
43 fireContentsChanged(element
, index
, index
);
48 static public final String INVALIDATE_CACHE
= "invalidate_cache";
50 private List
<BookInfo
> bookInfos
= new ArrayList
<BookInfo
>();
51 private Map
<BookInfo
, BookLine
> books
= new HashMap
<BookInfo
, BookLine
>();
52 private boolean seeWordCount
;
53 private boolean listMode
;
55 private JList
<BookInfo
> list
;
56 private int hoveredIndex
= -1;
57 private ListModel data
= new ListModel();
59 private SearchBar searchBar
;
61 private Queue
<BookBlock
> updateBookQueue
= new LinkedList
<BookBlock
>();
62 private Object updateBookQueueLock
= new Object();
64 public BooksPanel(boolean listMode
) {
65 setLayout(new BorderLayout());
67 searchBar
= new SearchBar();
68 add(searchBar
, BorderLayout
.NORTH
);
70 searchBar
.addActionListener(new ActionListener() {
72 public void actionPerformed(ActionEvent e
) {
73 filter(searchBar
.getText());
77 add(UiHelper
.scroll(initList(listMode
)), BorderLayout
.CENTER
);
79 Thread bookBlocksUpdater
= new Thread(new Runnable() {
83 BasicLibrary lib
= Instance
.getInstance().getLibrary();
86 synchronized (updateBookQueueLock
) {
87 if (!updateBookQueue
.isEmpty()) {
88 book
= updateBookQueue
.remove();
96 final Image coverImage
= BookBlock
97 .generateCoverImage(lib
, book
.getInfo());
98 SwingUtilities
.invokeLater(new Runnable() {
102 book
.setCoverImage(coverImage
);
103 data
.fireElementChanged(book
.getInfo());
104 } catch (Exception e
) {
108 } catch (Exception e
) {
114 } catch (InterruptedException e
) {
119 bookBlocksUpdater
.setName("BookBlocks visual updater");
120 bookBlocksUpdater
.setDaemon(true);
121 bookBlocksUpdater
.start();
124 // null or empty -> all sources
125 // sources hierarchy supported ("source/" will includes all "source" and
127 public void load(final List
<String
> sources
, final List
<String
> authors
,
128 final List
<String
> tags
) {
129 new SwingWorker
<List
<BookInfo
>, Void
>() {
131 protected List
<BookInfo
> doInBackground() throws Exception
{
132 List
<BookInfo
> bookInfos
= new ArrayList
<BookInfo
>();
133 BasicLibrary lib
= Instance
.getInstance().getLibrary();
134 for (MetaData meta
: lib
.getList().filter(sources
, authors
,
136 bookInfos
.add(BookInfo
.fromMeta(lib
, meta
));
143 protected void done() {
146 } catch (InterruptedException e
) {
148 } catch (ExecutionException e
) {
156 public void load(List
<BookInfo
> bookInfos
) {
157 this.bookInfos
.clear();
158 this.bookInfos
.addAll(bookInfos
);
159 synchronized (updateBookQueueLock
) {
160 updateBookQueue
.clear();
163 filter(searchBar
.getText());
167 private void filter(String filter
) {
169 for (BookInfo bookInfo
: bookInfos
) {
170 if (bookInfo
.getMainInfo() == null || filter
.isEmpty()
171 || bookInfo
.getMainInfo().toLowerCase()
172 .contains(filter
.toLowerCase())) {
173 data
.addElement(bookInfo
);
180 * The secondary value content: word count or author.
182 * @return TRUE to see word counts, FALSE to see authors
184 public boolean isSeeWordCount() {
189 * The secondary value content: word count or author.
191 * @param seeWordCount
192 * TRUE to see word counts, FALSE to see authors
194 public void setSeeWordCount(boolean seeWordCount
) {
195 if (this.seeWordCount
!= seeWordCount
) {
197 for (BookLine book
: books
.values()) {
198 book
.setSeeWordCount(seeWordCount
);
206 private JList
<BookInfo
> initList(boolean listMode
) {
207 final JList
<BookInfo
> list
= new JList
<BookInfo
>(data
);
209 final JPopupMenu popup
= new BookPopup(
210 Instance
.getInstance().getLibrary(), new BookPopup
.Informer() {
212 public void setCached(BookInfo book
, boolean cached
) {
213 book
.setCached(cached
);
214 fireElementChanged(book
);
217 public void fireElementChanged(BookInfo book
) {
218 data
.fireElementChanged(book
);
222 public List
<BookInfo
> getSelected() {
223 List
<BookInfo
> selected
= new ArrayList
<BookInfo
>();
224 for (int index
: list
.getSelectedIndices()) {
225 selected
.add(data
.get(index
));
232 public BookInfo
getUniqueSelected() {
233 List
<BookInfo
> selected
= getSelected();
234 if (selected
.size() == 1) {
235 return selected
.get(0);
241 public void invalidateCache() {
242 // TODO: also reset the popup menu for sources/author
243 fireActionPerformed(INVALIDATE_CACHE
);
247 list
.addMouseMotionListener(new MouseAdapter() {
249 public void mouseMoved(MouseEvent me
) {
250 if (popup
.isShowing())
253 Point p
= new Point(me
.getX(), me
.getY());
254 int index
= list
.locationToIndex(p
);
255 if (index
!= hoveredIndex
) {
256 hoveredIndex
= index
;
261 list
.addMouseListener(new MouseAdapter() {
263 public void mousePressed(MouseEvent e
) {
268 public void mouseReleased(MouseEvent e
) {
273 public void mouseExited(MouseEvent e
) {
274 if (popup
.isShowing())
277 if (hoveredIndex
> -1) {
284 public void mouseClicked(MouseEvent e
) {
285 super.mouseClicked(e
);
286 if (e
.getClickCount() == 2) {
287 int index
= list
.locationToIndex(e
.getPoint());
288 list
.setSelectedIndex(index
);
290 final BookInfo book
= data
.get(index
);
291 BasicLibrary lib
= Instance
.getInstance().getLibrary();
293 Actions
.openExternal(lib
, book
.getMeta(), BooksPanel
.this,
297 book
.setCached(true);
298 data
.fireElementChanged(book
);
304 private void check(MouseEvent e
) {
305 if (e
.isPopupTrigger()) {
306 if (list
.getSelectedIndices().length
<= 1) {
307 list
.setSelectedIndex(
308 list
.locationToIndex(e
.getPoint()));
311 popup
.show(list
, e
.getX(), e
.getY());
316 list
.setSelectionMode(ListSelectionModel
.MULTIPLE_INTERVAL_SELECTION
);
317 list
.setSelectedIndex(0);
318 list
.setCellRenderer(generateRenderer());
319 list
.setVisibleRowCount(0);
322 setListMode(listMode
);
326 private ListCellRenderer
<BookInfo
> generateRenderer() {
327 return new ListCellRenderer
<BookInfo
>() {
329 public Component
getListCellRendererComponent(
330 JList
<?
extends BookInfo
> list
, BookInfo value
, int index
,
331 boolean isSelected
, boolean cellHasFocus
) {
332 BookLine book
= books
.get(value
);
335 book
= new BookLine(value
, seeWordCount
);
337 book
= new BookBlock(value
, seeWordCount
);
338 synchronized (updateBookQueueLock
) {
339 updateBookQueue
.add((BookBlock
) book
);
342 books
.put(value
, book
);
345 book
.setSelected(isSelected
);
346 book
.setHovered(index
== hoveredIndex
);
352 public boolean isListMode() {
356 public void setListMode(boolean listMode
) {
357 this.listMode
= listMode
;
359 list
.setLayoutOrientation(
360 listMode ? JList
.VERTICAL
: JList
.HORIZONTAL_WRAP
);
363 synchronized (updateBookQueueLock
) {
364 updateBookQueue
.clear();