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
.JMenuItem
;
22 import javax
.swing
.JPanel
;
23 import javax
.swing
.JPopupMenu
;
24 import javax
.swing
.ListCellRenderer
;
25 import javax
.swing
.ListSelectionModel
;
26 import javax
.swing
.SwingUtilities
;
27 import javax
.swing
.SwingWorker
;
29 import be
.nikiroo
.fanfix
.Instance
;
30 import be
.nikiroo
.fanfix
.data
.MetaData
;
31 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
32 import be
.nikiroo
.fanfix_swing
.Actions
;
33 import be
.nikiroo
.fanfix_swing
.gui
.book
.BookBlock
;
34 import be
.nikiroo
.fanfix_swing
.gui
.book
.BookInfo
;
35 import be
.nikiroo
.fanfix_swing
.gui
.book
.BookLine
;
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 JPopupMenu();
202 JMenuItem open
= popup
.add("Open");
203 open
.addActionListener(new ActionListener() {
205 public void actionPerformed(ActionEvent e
) {
206 int[] selected
= list
.getSelectedIndices();
207 if (selected
.length
== 1) {
208 final BookInfo book
= data
.get(selected
[0]);
209 BasicLibrary lib
= Instance
.getInstance().getLibrary();
210 Actions
.openExternal(lib
, book
.getMeta(), BooksPanel
.this, new Runnable() {
213 data
.fireElementChanged(book
);
220 list
.addMouseMotionListener(new MouseAdapter() {
222 public void mouseMoved(MouseEvent me
) {
223 if (popup
.isShowing())
226 Point p
= new Point(me
.getX(), me
.getY());
227 int index
= list
.locationToIndex(p
);
228 if (index
!= hoveredIndex
) {
229 hoveredIndex
= index
;
234 list
.addMouseListener(new MouseAdapter() {
236 public void mousePressed(MouseEvent e
) {
241 public void mouseReleased(MouseEvent e
) {
246 public void mouseExited(MouseEvent e
) {
247 if (popup
.isShowing())
250 if (hoveredIndex
> -1) {
257 public void mouseClicked(MouseEvent e
) {
258 super.mouseClicked(e
);
259 if (e
.getClickCount() == 2) {
260 int index
= list
.locationToIndex(e
.getPoint());
261 list
.setSelectedIndex(index
);
263 final BookInfo book
= data
.get(index
);
264 BasicLibrary lib
= Instance
.getInstance().getLibrary();
266 Actions
.openExternal(lib
, book
.getMeta(), BooksPanel
.this, new Runnable() {
269 data
.fireElementChanged(book
);
275 private void check(MouseEvent e
) {
276 if (e
.isPopupTrigger()) {
277 if (list
.getSelectedIndices().length
<= 1) {
278 list
.setSelectedIndex(list
.locationToIndex(e
.getPoint()));
281 popup
.show(list
, e
.getX(), e
.getY());
286 list
.setSelectionMode(ListSelectionModel
.MULTIPLE_INTERVAL_SELECTION
);
287 list
.setSelectedIndex(0);
288 list
.setCellRenderer(generateRenderer());
289 list
.setVisibleRowCount(0);
292 setListMode(listMode
);
296 private ListCellRenderer
<BookInfo
> generateRenderer() {
297 return new ListCellRenderer
<BookInfo
>() {
299 public Component
getListCellRendererComponent(JList
<?
extends BookInfo
> list
, BookInfo value
, int index
,
300 boolean isSelected
, boolean cellHasFocus
) {
301 BookLine book
= books
.get(value
);
304 book
= new BookLine(value
, seeWordCount
);
306 book
= new BookBlock(value
, seeWordCount
);
307 synchronized (updateBookQueueLock
) {
308 updateBookQueue
.add((BookBlock
) book
);
311 books
.put(value
, book
);
314 book
.setSelected(isSelected
);
315 book
.setHovered(index
== hoveredIndex
);
321 public boolean isListMode() {
325 public void setListMode(boolean listMode
) {
326 this.listMode
= listMode
;
328 list
.setLayoutOrientation(listMode ? JList
.VERTICAL
: JList
.HORIZONTAL_WRAP
);
331 synchronized (updateBookQueueLock
) {
332 updateBookQueue
.clear();