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
.JPanel
;
22 import javax
.swing
.JPopupMenu
;
23 import javax
.swing
.ListCellRenderer
;
24 import javax
.swing
.ListSelectionModel
;
25 import javax
.swing
.SwingUtilities
;
26 import javax
.swing
.SwingWorker
;
28 import be
.nikiroo
.fanfix
.Instance
;
29 import be
.nikiroo
.fanfix
.data
.MetaData
;
30 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
31 import be
.nikiroo
.fanfix_swing
.Actions
;
32 import be
.nikiroo
.fanfix_swing
.gui
.book
.BookBlock
;
33 import be
.nikiroo
.fanfix_swing
.gui
.book
.BookInfo
;
34 import be
.nikiroo
.fanfix_swing
.gui
.book
.BookLine
;
35 import be
.nikiroo
.fanfix_swing
.gui
.book
.BookPopup
;
36 import be
.nikiroo
.fanfix_swing
.gui
.utils
.UiHelper
;
38 public class BooksPanel
extends JPanel
{
39 class ListModel
extends DefaultListModel
<BookInfo
> {
40 public void fireElementChanged(BookInfo element
) {
41 int index
= indexOf(element
);
43 fireContentsChanged(element
, index
, index
);
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();
57 private SearchBar searchBar
;
59 private Queue
<BookBlock
> updateBookQueue
= new LinkedList
<BookBlock
>();
60 private Object updateBookQueueLock
= new Object();
62 public BooksPanel(boolean listMode
) {
63 setLayout(new BorderLayout());
65 searchBar
= new SearchBar();
66 add(searchBar
, BorderLayout
.NORTH
);
68 searchBar
.addActionListener(new ActionListener() {
70 public void actionPerformed(ActionEvent e
) {
71 reload(searchBar
.getText());
75 add(UiHelper
.scroll(initList(listMode
)), BorderLayout
.CENTER
);
77 Thread bookBlocksUpdater
= new Thread(new Runnable() {
81 BasicLibrary lib
= Instance
.getInstance().getLibrary();
84 synchronized (updateBookQueueLock
) {
85 if (!updateBookQueue
.isEmpty()) {
86 book
= updateBookQueue
.remove();
94 final Image coverImage
= BookBlock
.generateCoverImage(lib
, book
.getInfo());
95 SwingUtilities
.invokeLater(new Runnable() {
99 book
.setCoverImage(coverImage
);
100 data
.fireElementChanged(book
.getInfo());
101 } catch (Exception e
) {
105 } catch (Exception e
) {
111 } catch (InterruptedException e
) {
116 bookBlocksUpdater
.setName("BookBlocks visual updater");
117 bookBlocksUpdater
.setDaemon(true);
118 bookBlocksUpdater
.start();
121 // null or empty -> all sources
122 // sources hierarchy supported ("source/" will includes all "source" and
124 public void load(final List
<String
> sources
, final List
<String
> authors
, final List
<String
> tags
) {
125 new SwingWorker
<List
<BookInfo
>, Void
>() {
127 protected List
<BookInfo
> doInBackground() throws Exception
{
128 List
<BookInfo
> bookInfos
= new ArrayList
<BookInfo
>();
129 BasicLibrary lib
= Instance
.getInstance().getLibrary();
130 for (MetaData meta
: lib
.getList(null).filter(sources
, authors
, tags
)) {
131 bookInfos
.add(BookInfo
.fromMeta(lib
, meta
));
138 protected void done() {
141 } catch (InterruptedException e
) {
143 } catch (ExecutionException e
) {
151 public void load(List
<BookInfo
> bookInfos
) {
152 this.bookInfos
.clear();
153 this.bookInfos
.addAll(bookInfos
);
154 synchronized (updateBookQueueLock
) {
155 updateBookQueue
.clear();
158 reload(searchBar
.getText());
162 private void reload(String filter
) {
164 for (BookInfo bookInfo
: bookInfos
) {
165 if (filter
.isEmpty() || bookInfo
.getMainInfo().toLowerCase().contains(filter
.toLowerCase())) {
166 data
.addElement(bookInfo
);
173 * The secondary value content: word count or author.
175 * @return TRUE to see word counts, FALSE to see authors
177 public boolean isSeeWordCount() {
182 * The secondary value content: word count or author.
184 * @param seeWordCount TRUE to see word counts, FALSE to see authors
186 public void setSeeWordCount(boolean seeWordCount
) {
187 if (this.seeWordCount
!= seeWordCount
) {
189 for (BookLine book
: books
.values()) {
190 book
.setSeeWordCount(seeWordCount
);
198 private JList
<BookInfo
> initList(boolean listMode
) {
199 final JList
<BookInfo
> list
= new JList
<BookInfo
>(data
);
201 final JPopupMenu popup
= new BookPopup(Instance
.getInstance().getLibrary(), new BookPopup
.Informer() {
203 public void setCached(BookInfo book
, boolean cached
) {
204 book
.setCached(cached
);
205 fireElementChanged(book
);
208 public void fireElementChanged(BookInfo book
) {
209 data
.fireElementChanged(book
);
213 public List
<BookInfo
> getSelected() {
214 List
<BookInfo
> selected
= new ArrayList
<BookInfo
>();
215 for (int index
: list
.getSelectedIndices()) {
216 selected
.add(data
.get(index
));
223 public BookInfo
getUniqueSelected() {
224 List
<BookInfo
> selected
= getSelected();
225 if (selected
.size() == 1) {
226 return selected
.get(0);
232 list
.addMouseMotionListener(new MouseAdapter() {
234 public void mouseMoved(MouseEvent me
) {
235 if (popup
.isShowing())
238 Point p
= new Point(me
.getX(), me
.getY());
239 int index
= list
.locationToIndex(p
);
240 if (index
!= hoveredIndex
) {
241 hoveredIndex
= index
;
246 list
.addMouseListener(new MouseAdapter() {
248 public void mousePressed(MouseEvent e
) {
253 public void mouseReleased(MouseEvent e
) {
258 public void mouseExited(MouseEvent e
) {
259 if (popup
.isShowing())
262 if (hoveredIndex
> -1) {
269 public void mouseClicked(MouseEvent e
) {
270 super.mouseClicked(e
);
271 if (e
.getClickCount() == 2) {
272 int index
= list
.locationToIndex(e
.getPoint());
273 list
.setSelectedIndex(index
);
275 final BookInfo book
= data
.get(index
);
276 BasicLibrary lib
= Instance
.getInstance().getLibrary();
278 Actions
.openExternal(lib
, book
.getMeta(), BooksPanel
.this, new Runnable() {
281 book
.setCached(true);
282 data
.fireElementChanged(book
);
288 private void check(MouseEvent e
) {
289 if (e
.isPopupTrigger()) {
290 if (list
.getSelectedIndices().length
<= 1) {
291 list
.setSelectedIndex(list
.locationToIndex(e
.getPoint()));
294 popup
.show(list
, e
.getX(), e
.getY());
299 list
.setSelectionMode(ListSelectionModel
.MULTIPLE_INTERVAL_SELECTION
);
300 list
.setSelectedIndex(0);
301 list
.setCellRenderer(generateRenderer());
302 list
.setVisibleRowCount(0);
305 setListMode(listMode
);
309 private ListCellRenderer
<BookInfo
> generateRenderer() {
310 return new ListCellRenderer
<BookInfo
>() {
312 public Component
getListCellRendererComponent(JList
<?
extends BookInfo
> list
, BookInfo value
, int index
,
313 boolean isSelected
, boolean cellHasFocus
) {
314 BookLine book
= books
.get(value
);
317 book
= new BookLine(value
, seeWordCount
);
319 book
= new BookBlock(value
, seeWordCount
);
320 synchronized (updateBookQueueLock
) {
321 updateBookQueue
.add((BookBlock
) book
);
324 books
.put(value
, book
);
327 book
.setSelected(isSelected
);
328 book
.setHovered(index
== hoveredIndex
);
334 public boolean isListMode() {
338 public void setListMode(boolean listMode
) {
339 this.listMode
= listMode
;
341 list
.setLayoutOrientation(listMode ? JList
.VERTICAL
: JList
.HORIZONTAL_WRAP
);
344 synchronized (updateBookQueueLock
) {
345 updateBookQueue
.clear();