1 package be
.nikiroo
.fanfix
.reader
.ui
;
3 import java
.awt
.BorderLayout
;
5 import java
.awt
.EventQueue
;
7 import java
.awt
.Toolkit
;
8 import java
.awt
.datatransfer
.DataFlavor
;
9 import java
.awt
.event
.ActionEvent
;
10 import java
.awt
.event
.ActionListener
;
11 import java
.awt
.event
.MouseEvent
;
13 import java
.io
.IOException
;
14 import java
.lang
.reflect
.InvocationTargetException
;
16 import java
.net
.UnknownHostException
;
17 import java
.util
.ArrayList
;
18 import java
.util
.List
;
20 import java
.util
.TreeMap
;
22 import javax
.swing
.BoxLayout
;
23 import javax
.swing
.JFileChooser
;
24 import javax
.swing
.JLabel
;
25 import javax
.swing
.JMenuBar
;
26 import javax
.swing
.JOptionPane
;
27 import javax
.swing
.JPanel
;
28 import javax
.swing
.JPopupMenu
;
29 import javax
.swing
.JScrollPane
;
30 import javax
.swing
.SwingConstants
;
31 import javax
.swing
.SwingUtilities
;
33 import be
.nikiroo
.fanfix
.Instance
;
34 import be
.nikiroo
.fanfix
.bundles
.UiConfig
;
35 import be
.nikiroo
.fanfix
.data
.MetaData
;
36 import be
.nikiroo
.fanfix
.data
.Story
;
37 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
38 import be
.nikiroo
.fanfix
.library
.BasicLibrary
.Status
;
39 import be
.nikiroo
.fanfix
.library
.LocalLibrary
;
40 import be
.nikiroo
.fanfix
.reader
.BasicReader
;
41 import be
.nikiroo
.fanfix
.reader
.ui
.GuiReaderBook
.BookActionListener
;
42 import be
.nikiroo
.utils
.Progress
;
43 import be
.nikiroo
.utils
.ui
.ProgressBar
;
46 * A {@link Frame} that will show a {@link GuiReaderBook} item for each
47 * {@link Story} in the main cache ({@link Instance#getCache()}), and offer a
48 * way to copy them to the {@link GuiReader} cache (
49 * {@link BasicReader#getLibrary()}), read them, delete them...
53 class GuiReaderMainPanel
extends JPanel
{
54 private static final long serialVersionUID
= 1L;
55 private FrameHelper helper
;
56 private Map
<String
, GuiReaderGroup
> books
;
57 private GuiReaderGroup bookPane
; // for more "All"
60 private ProgressBar pgBar
;
62 private GuiReaderBook selectedBook
;
63 private boolean words
; // words or authors (secondary info on books)
64 private boolean currentType
; // type/source or author mode (All and Listing)
67 * An object that offers some helper methods to access the frame that host
68 * it and the Fanfix-related functions.
72 public interface FrameHelper
{
74 * Return the reader associated to this {@link FrameHelper}.
78 public GuiReader
getReader();
81 * Create the main menu bar.
83 * Will invalidate the layout.
86 * the library can be queried
88 public void createMenu(boolean libOk
);
91 * Create a popup menu for a {@link GuiReaderBook} that represents a
94 * @return the popup menu to display
96 public JPopupMenu
createBookPopup();
99 * Create a popup menu for a {@link GuiReaderBook} that represents a
100 * source/type or an author.
102 * @return the popup menu to display
104 public JPopupMenu
createSourceAuthorPopup();
108 * A {@link Runnable} with a {@link Story} parameter.
112 public interface StoryRunnable
{
119 public void run(Story story
);
123 * Create a new {@link GuiReaderMainPanel}.
126 * the associated {@link FrameHelper} to forward some commands
127 * and access its {@link LocalLibrary}
129 * the type of {@link Story} to load, or NULL for all types
131 public GuiReaderMainPanel(FrameHelper parent
, String type
) {
132 super(new BorderLayout(), true);
134 this.helper
= parent
;
137 pane
.setLayout(new BoxLayout(pane
, BoxLayout
.PAGE_AXIS
));
139 Integer icolor
= Instance
.getUiConfig().getColor(
140 UiConfig
.BACKGROUND_COLOR
);
141 if (icolor
!= null) {
142 color
= new Color(icolor
);
143 setBackground(color
);
144 pane
.setBackground(color
);
147 JScrollPane scroll
= new JScrollPane(pane
);
148 scroll
.getVerticalScrollBar().setUnitIncrement(16);
149 add(scroll
, BorderLayout
.CENTER
);
151 String message
= parent
.getReader().getLibrary().getLibraryName();
152 if (!message
.isEmpty()) {
153 JLabel name
= new JLabel(message
, SwingConstants
.CENTER
);
154 add(name
, BorderLayout
.NORTH
);
157 pgBar
= new ProgressBar();
158 add(pgBar
, BorderLayout
.SOUTH
);
160 pgBar
.addActionListener(new ActionListener() {
162 public void actionPerformed(ActionEvent e
) {
164 pgBar
.setProgress(null);
170 pgBar
.addUpdateListener(new ActionListener() {
172 public void actionPerformed(ActionEvent e
) {
179 books
= new TreeMap
<String
, GuiReaderGroup
>();
181 pane
.setVisible(false);
182 final Progress pg
= new Progress();
183 final String typeF
= type
;
184 outOfUi(pg
, true, new Runnable() {
187 final BasicLibrary lib
= helper
.getReader().getLibrary();
188 final Status status
= lib
.getStatus();
190 if (status
== Status
.READY
) {
194 inUi(new Runnable() {
197 if (status
== Status
.READY
) {
198 helper
.createMenu(true);
200 addBookPane(true, false);
202 addBookPane(typeF
, true);
204 pane
.setVisible(true);
206 helper
.createMenu(false);
209 String err
= lib
.getLibraryName() + "\n";
212 err
+= "Library not valid";
216 err
+= "You are not allowed to access this library";
220 err
+= "Library currently unavailable";
224 err
+= "An error occured when contacting the library";
228 error(err
, "Library error", null);
236 public boolean getCurrentType() {
241 * Add a new {@link GuiReaderGroup} on the frame to display all the
242 * sources/types or all the authors, or a listing of all the books sorted
243 * either by source or author.
245 * A display of all the sources/types or all the authors will show one icon
246 * per source/type or author.
248 * A listing of all the books sorted by source/type or author will display
252 * TRUE for type/source, FALSE for author
254 * TRUE to get a listing of all the sources or authors, FALSE to
255 * get one icon per source or author
257 public void addBookPane(boolean type
, boolean listMode
) {
258 this.currentType
= type
;
259 BasicLibrary lib
= helper
.getReader().getLibrary();
262 addListPane("Sources", lib
.getSources(), type
);
264 for (String tt
: lib
.getSources()) {
266 addBookPane(tt
, type
);
272 addListPane("Authors", lib
.getAuthors(), type
);
274 for (String tt
: lib
.getAuthors()) {
276 addBookPane(tt
, type
);
284 * Add a new {@link GuiReaderGroup} on the frame to display the books of the
285 * selected type or author.
287 * Will invalidate the layout.
290 * the author or the type, or NULL to get all the
293 * TRUE for type/source, FALSE for author
295 public void addBookPane(String value
, boolean type
) {
296 this.currentType
= type
;
298 GuiReaderGroup bookPane
= new GuiReaderGroup(helper
.getReader(), value
,
301 books
.put(value
, bookPane
);
306 bookPane
.setActionListener(new BookActionListener() {
308 public void select(GuiReaderBook book
) {
313 public void popupRequested(GuiReaderBook book
, MouseEvent e
) {
314 JPopupMenu popup
= helper
.createBookPopup();
315 popup
.show(e
.getComponent(), e
.getX(), e
.getY());
319 public void action(final GuiReaderBook book
) {
326 * Clear the pane from any book that may be present, usually prior to adding
329 * Will invalidate the layout.
331 public void removeBookPanes() {
338 * Refresh the list of {@link GuiReaderBook}s from disk.
340 * Will validate the layout, as it is a "refresh" operation.
342 public void refreshBooks() {
343 BasicLibrary lib
= helper
.getReader().getLibrary();
344 for (String value
: books
.keySet()) {
345 List
<GuiReaderBookInfo
> infos
= new ArrayList
<GuiReaderBookInfo
>();
347 List
<MetaData
> metas
;
349 metas
= lib
.getListBySource(value
);
351 metas
= lib
.getListByAuthor(value
);
353 for (MetaData meta
: metas
) {
354 infos
.add(GuiReaderBookInfo
.fromMeta(meta
));
357 books
.get(value
).refreshBooks(infos
, words
);
360 if (bookPane
!= null) {
361 bookPane
.refreshBooks(words
);
368 * Open a {@link GuiReaderBook} item.
371 * the {@link GuiReaderBook} to open
373 public void openBook(final GuiReaderBook book
) {
374 final Progress pg
= new Progress();
375 outOfUi(pg
, false, new Runnable() {
379 helper
.getReader().read(book
.getInfo().getMeta().getLuid(),
381 SwingUtilities
.invokeLater(new Runnable() {
384 book
.setCached(true);
387 } catch (IOException e
) {
388 Instance
.getTraceHandler().error(e
);
389 error("Cannot open the selected book", "Error", e
);
396 * Process the given action out of the Swing UI thread and link the given
397 * {@link ProgressBar} to the action.
399 * The code will make sure that the {@link ProgressBar} (if not NULL) is set
400 * to done when the action is done.
403 * the {@link ProgressBar} or NULL
404 * @param refreshBooks
405 * TRUE to refresh the books after
409 public void outOfUi(Progress progress
, final boolean refreshBooks
,
410 final Runnable run
) {
411 final Progress pg
= new Progress();
412 final Progress reload
= new Progress("Reload books");
414 if (progress
== null) {
415 progress
= new Progress();
419 pg
.addProgress(progress
, 100);
421 pg
.addProgress(progress
, 90);
422 pg
.addProgress(reload
, 10);
426 pgBar
.setProgress(pg
);
430 new Thread(new Runnable() {
441 // will trigger pgBar ActionListener:
446 }, "outOfUi thread").start();
450 * Process the given action in the main Swing UI thread.
452 * The code will make sure the current thread is the main UI thread and, if
453 * not, will switch to it before executing the runnable.
455 * Synchronous operation.
460 public void inUi(final Runnable run
) {
461 if (EventQueue
.isDispatchThread()) {
465 EventQueue
.invokeAndWait(run
);
466 } catch (InterruptedException e
) {
467 Instance
.getTraceHandler().error(e
);
468 } catch (InvocationTargetException e
) {
469 Instance
.getTraceHandler().error(e
);
475 * Import a {@link Story} into the main {@link LocalLibrary}.
477 * Should be called inside the UI thread.
480 * TRUE for an {@link URL}, false for a {@link File}
482 public void imprt(boolean askUrl
) {
483 JFileChooser fc
= new JFileChooser();
487 String clipboard
= "";
489 clipboard
= ("" + Toolkit
.getDefaultToolkit()
490 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
492 } catch (Exception e
) {
493 // No data will be handled
496 if (clipboard
== null || !clipboard
.startsWith("http")) {
500 url
= JOptionPane
.showInputDialog(GuiReaderMainPanel
.this,
501 "url of the story to import?", "Importing from URL",
502 JOptionPane
.QUESTION_MESSAGE
, null, null, clipboard
);
503 } else if (fc
.showOpenDialog(this) != JFileChooser
.CANCEL_OPTION
) {
504 url
= fc
.getSelectedFile().getAbsolutePath();
509 if (url
!= null && !url
.toString().isEmpty()) {
510 imprt(url
.toString(), null, null);
515 * Actually import the {@link Story} into the main {@link LocalLibrary}.
517 * Should be called inside the UI thread.
520 * the {@link Story} to import by {@link URL}
522 * Action to execute on success
523 * @param onSuccessPgName
524 * the name to use for the onSuccess progress bar
526 public void imprt(final String url
, final StoryRunnable onSuccess
,
527 String onSuccessPgName
) {
528 final Progress pg
= new Progress();
529 final Progress pgImprt
= new Progress();
530 final Progress pgOnSuccess
= new Progress(onSuccessPgName
);
531 pg
.addProgress(pgImprt
, 95);
532 pg
.addProgress(pgOnSuccess
, 5);
534 outOfUi(pg
, true, new Runnable() {
540 story
= helper
.getReader().getLibrary()
541 .imprt(BasicReader
.getUrl(url
), pgImprt
);
542 } catch (IOException e
) {
546 final Exception e
= ex
;
548 final boolean ok
= (e
== null);
550 pgOnSuccess
.setProgress(0);
552 if (e
instanceof UnknownHostException
) {
553 error("URL not supported: " + url
, "Cannot import URL",
556 error("Failed to import " + url
+ ": \n"
557 + e
.getMessage(), "Cannot import URL", e
);
560 if (onSuccess
!= null) {
561 onSuccess
.run(story
);
570 * Enables or disables this component, depending on the value of the
571 * parameter <code>b</code>. An enabled component can respond to user input
572 * and generate events. Components are enabled initially by default.
574 * Enabling or disabling <b>this</b> component will also affect its
578 * If <code>true</code>, this component is enabled; otherwise
579 * this component is disabled
582 public void setEnabled(boolean b
) {
587 for (GuiReaderGroup group
: books
.values()) {
594 public void setWords(boolean words
) {
598 public GuiReaderBook
getSelectedBook() {
602 public void unsetSelectedBook() {
606 private void addListPane(String name
, List
<String
> values
,
607 final boolean type
) {
608 GuiReader reader
= helper
.getReader();
609 BasicLibrary lib
= reader
.getLibrary();
611 bookPane
= new GuiReaderGroup(reader
, name
, color
);
613 List
<GuiReaderBookInfo
> infos
= new ArrayList
<GuiReaderBookInfo
>();
614 for (String value
: values
) {
616 infos
.add(GuiReaderBookInfo
.fromSource(lib
, value
));
618 infos
.add(GuiReaderBookInfo
.fromAuthor(lib
, value
));
622 bookPane
.refreshBooks(infos
, words
);
630 bookPane
.setActionListener(new BookActionListener() {
632 public void select(GuiReaderBook book
) {
637 public void popupRequested(GuiReaderBook book
, MouseEvent e
) {
638 JPopupMenu popup
= helper
.createSourceAuthorPopup();
639 popup
.show(e
.getComponent(), e
.getX(), e
.getY());
643 public void action(final GuiReaderBook book
) {
645 addBookPane(book
.getInfo().getMainInfo(), type
);
652 * Display an error message and log the linked {@link Exception}.
657 * the title of the error message
659 * the exception to log if any
661 private void error(final String message
, final String title
, Exception e
) {
662 Instance
.getTraceHandler().error(title
+ ": " + message
);
664 Instance
.getTraceHandler().error(e
);
667 SwingUtilities
.invokeLater(new Runnable() {
670 JOptionPane
.showMessageDialog(GuiReaderMainPanel
.this, message
,
671 title
, JOptionPane
.ERROR_MESSAGE
);