1 package be
.nikiroo
.fanfix
.reader
.ui
;
3 import java
.awt
.BorderLayout
;
6 import java
.awt
.Toolkit
;
7 import java
.awt
.datatransfer
.DataFlavor
;
8 import java
.awt
.event
.ActionEvent
;
9 import java
.awt
.event
.ActionListener
;
10 import java
.awt
.event
.MouseEvent
;
12 import java
.io
.IOException
;
14 import java
.net
.UnknownHostException
;
15 import java
.util
.ArrayList
;
16 import java
.util
.HashMap
;
17 import java
.util
.List
;
20 import javax
.swing
.BoxLayout
;
21 import javax
.swing
.JFileChooser
;
22 import javax
.swing
.JLabel
;
23 import javax
.swing
.JMenuBar
;
24 import javax
.swing
.JOptionPane
;
25 import javax
.swing
.JPanel
;
26 import javax
.swing
.JPopupMenu
;
27 import javax
.swing
.JScrollPane
;
28 import javax
.swing
.SwingConstants
;
29 import javax
.swing
.SwingUtilities
;
31 import be
.nikiroo
.fanfix
.Instance
;
32 import be
.nikiroo
.fanfix
.bundles
.UiConfig
;
33 import be
.nikiroo
.fanfix
.data
.MetaData
;
34 import be
.nikiroo
.fanfix
.data
.Story
;
35 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
36 import be
.nikiroo
.fanfix
.library
.BasicLibrary
.Status
;
37 import be
.nikiroo
.fanfix
.library
.LocalLibrary
;
38 import be
.nikiroo
.fanfix
.reader
.BasicReader
;
39 import be
.nikiroo
.fanfix
.reader
.ui
.GuiReaderBook
.BookActionListener
;
40 import be
.nikiroo
.utils
.Progress
;
41 import be
.nikiroo
.utils
.ui
.ProgressBar
;
44 * A {@link Frame} that will show a {@link GuiReaderBook} item for each
45 * {@link Story} in the main cache ({@link Instance#getCache()}), and offer a
46 * way to copy them to the {@link GuiReader} cache (
47 * {@link BasicReader#getLibrary()}), read them, delete them...
51 class GuiReaderMainPanel
extends JPanel
{
52 private static final long serialVersionUID
= 1L;
53 private FrameHelper helper
;
54 private Map
<GuiReaderGroup
, String
> booksByType
;
55 private Map
<GuiReaderGroup
, String
> booksByAuthor
;
58 private ProgressBar pgBar
;
60 private GuiReaderBook selectedBook
;
61 private boolean words
; // words or authors (secondary info on books)
64 * An object that offers some helper methods to access the frame that host
65 * it and the Fanfix-related functions.
69 public interface FrameHelper
{
71 * Return the reader associated to this {@link FrameHelper}.
75 public GuiReader
getReader();
78 * Create the main menu bar.
81 * the library can be queried
85 public void createMenu(boolean b
);
88 * Create a popup menu for a {@link GuiReaderBook} that represents a
91 * @return the popup menu to display
93 public JPopupMenu
createBookPopup();
96 * Create a popup menu for a {@link GuiReaderBook} that represents a
97 * source/type (no LUID).
99 * @return the popup menu to display
101 public JPopupMenu
createSourcePopup();
105 * A {@link Runnable} with a {@link Story} parameter.
109 public interface StoryRunnable
{
116 public void run(Story story
);
120 * Create a new {@link GuiReaderMainPanel}.
123 * the associated {@link GuiReader} to forward some commands and
124 * access its {@link LocalLibrary}
126 * the type of {@link Story} to load, or NULL for all types
128 public GuiReaderMainPanel(FrameHelper parent
, String type
) {
129 super(new BorderLayout(), true);
131 this.helper
= parent
;
134 pane
.setLayout(new BoxLayout(pane
, BoxLayout
.PAGE_AXIS
));
136 Integer icolor
= Instance
.getUiConfig().getColor(
137 UiConfig
.BACKGROUND_COLOR
);
138 if (icolor
!= null) {
139 color
= new Color(icolor
);
140 setBackground(color
);
141 pane
.setBackground(color
);
144 JScrollPane scroll
= new JScrollPane(pane
);
145 scroll
.getVerticalScrollBar().setUnitIncrement(16);
146 add(scroll
, BorderLayout
.CENTER
);
148 String message
= parent
.getReader().getLibrary().getLibraryName();
149 if (!message
.isEmpty()) {
150 JLabel name
= new JLabel(message
, SwingConstants
.CENTER
);
151 add(name
, BorderLayout
.NORTH
);
154 pgBar
= new ProgressBar();
155 add(pgBar
, BorderLayout
.SOUTH
);
157 pgBar
.addActionListener(new ActionListener() {
159 public void actionPerformed(ActionEvent e
) {
161 pgBar
.setProgress(null);
167 pgBar
.addUpdateListener(new ActionListener() {
169 public void actionPerformed(ActionEvent e
) {
176 booksByType
= new HashMap
<GuiReaderGroup
, String
>();
177 booksByAuthor
= new HashMap
<GuiReaderGroup
, String
>();
179 pane
.setVisible(false);
180 final Progress pg
= new Progress();
181 final String typeF
= type
;
182 outOfUi(pg
, new Runnable() {
185 BasicLibrary lib
= helper
.getReader().getLibrary();
186 Status status
= lib
.getStatus();
188 if (status
== Status
.READY
) {
191 helper
.createMenu(true);
193 addBookPane(true, false);
195 addBookPane(typeF
, true);
199 pane
.setVisible(true);
202 helper
.createMenu(false);
205 String err
= lib
.getLibraryName() + "\n";
208 err
+= "Library not valid";
212 err
+= "You are not allowed to access this library";
216 err
+= "Library currently unavailable";
220 err
+= "An error occured when contacting the library";
224 error(err
, "Library error", null);
231 * Add a new {@link GuiReaderGroup} on the frame to display all the
232 * sources/types or all the authors, or a listing of all the books sorted
233 * either by source or author.
235 * A display of all the sources/types or all the authors will show one icon
236 * per source/type or author.
238 * A listing of all the books sorted by source/type or author will display
242 * TRUE for type/source, FALSE for author
244 * TRUE to get a listing of all the sources or authors, FALSE to
245 * get one icon per source or author
247 public void addBookPane(boolean type
, boolean listMode
) {
248 BasicLibrary lib
= helper
.getReader().getLibrary();
251 addListPane("Sources", lib
.getSources(), type
);
253 for (String tt
: lib
.getSources()) {
255 addBookPane(tt
, type
);
261 addListPane("Authors", lib
.getAuthors(), type
);
263 for (String tt
: lib
.getAuthors()) {
265 addBookPane(tt
, type
);
273 * Add a new {@link GuiReaderGroup} on the frame to display the books of the
274 * selected type or author.
277 * the author or the type, or NULL to get all the
280 * TRUE for type/source, FALSE for author
283 public void addBookPane(String value
, boolean type
) {
284 GuiReaderGroup bookPane
= new GuiReaderGroup(helper
.getReader(), value
,
287 booksByType
.put(bookPane
, value
);
289 booksByAuthor
.put(bookPane
, value
);
298 bookPane
.setActionListener(new BookActionListener() {
300 public void select(GuiReaderBook book
) {
305 public void popupRequested(GuiReaderBook book
, MouseEvent e
) {
306 JPopupMenu popup
= helper
.createBookPopup();
307 popup
.show(e
.getComponent(), e
.getX(), e
.getY());
311 public void action(final GuiReaderBook book
) {
318 * Clear the pane from any book that may be present, usually prior to adding
321 public void removeBookPanes() {
323 booksByAuthor
.clear();
332 * Refresh the list of {@link GuiReaderBook}s from disk.
334 public void refreshBooks() {
335 BasicLibrary lib
= helper
.getReader().getLibrary();
336 for (GuiReaderGroup group
: booksByType
.keySet()) {
337 List
<MetaData
> stories
= lib
338 .getListBySource(booksByType
.get(group
));
339 group
.refreshBooks(stories
, words
);
342 for (GuiReaderGroup group
: booksByAuthor
.keySet()) {
343 List
<MetaData
> stories
= lib
.getListByAuthor(booksByAuthor
345 group
.refreshBooks(stories
, words
);
353 * Open a {@link GuiReaderBook} item.
356 * the {@link GuiReaderBook} to open
358 public void openBook(final GuiReaderBook book
) {
359 final Progress pg
= new Progress();
360 outOfUi(pg
, new Runnable() {
365 .read(book
.getMeta().getLuid(), false, pg
);
366 SwingUtilities
.invokeLater(new Runnable() {
369 book
.setCached(true);
372 } catch (IOException e
) {
373 Instance
.getTraceHandler().error(e
);
374 error("Cannot open the selected book", "Error", e
);
381 * Process the given action out of the Swing UI thread and link the given
382 * {@link ProgressBar} to the action.
384 * The code will make sure that the {@link ProgressBar} (if not NULL) is set
385 * to done when the action is done.
388 * the {@link ProgressBar} or NULL
392 public void outOfUi(Progress progress
, final Runnable run
) {
393 final Progress pg
= new Progress();
394 final Progress reload
= new Progress("Reload books");
395 if (progress
== null) {
396 progress
= new Progress();
399 pg
.addProgress(progress
, 90);
400 pg
.addProgress(reload
, 10);
403 pgBar
.setProgress(pg
);
407 new Thread(new Runnable() {
416 // will trigger pgBar ActionListener:
421 }, "outOfUi thread").start();
425 * Import a {@link Story} into the main {@link LocalLibrary}.
427 * Should be called inside the UI thread.
430 * TRUE for an {@link URL}, false for a {@link File}
432 public void imprt(boolean askUrl
) {
433 JFileChooser fc
= new JFileChooser();
437 String clipboard
= "";
439 clipboard
= ("" + Toolkit
.getDefaultToolkit()
440 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
442 } catch (Exception e
) {
443 // No data will be handled
446 if (clipboard
== null || !clipboard
.startsWith("http")) {
450 url
= JOptionPane
.showInputDialog(GuiReaderMainPanel
.this,
451 "url of the story to import?", "Importing from URL",
452 JOptionPane
.QUESTION_MESSAGE
, null, null, clipboard
);
453 } else if (fc
.showOpenDialog(this) != JFileChooser
.CANCEL_OPTION
) {
454 url
= fc
.getSelectedFile().getAbsolutePath();
459 if (url
!= null && !url
.toString().isEmpty()) {
460 imprt(url
.toString(), null, null);
465 * Actually import the {@link Story} into the main {@link LocalLibrary}.
467 * Should be called inside the UI thread.
470 * the {@link Story} to import by {@link URL}
472 * Action to execute on success
474 public void imprt(final String url
, final StoryRunnable onSuccess
,
475 String onSuccessPgName
) {
476 final Progress pg
= new Progress();
477 final Progress pgImprt
= new Progress();
478 final Progress pgOnSuccess
= new Progress(onSuccessPgName
);
479 pg
.addProgress(pgImprt
, 95);
480 pg
.addProgress(pgOnSuccess
, 5);
482 outOfUi(pg
, new Runnable() {
488 story
= helper
.getReader().getLibrary()
489 .imprt(BasicReader
.getUrl(url
), pgImprt
);
490 } catch (IOException e
) {
494 final Exception e
= ex
;
496 final boolean ok
= (e
== null);
498 pgOnSuccess
.setProgress(0);
500 if (e
instanceof UnknownHostException
) {
501 error("URL not supported: " + url
, "Cannot import URL",
504 error("Failed to import " + url
+ ": \n"
505 + e
.getMessage(), "Cannot import URL", e
);
508 if (onSuccess
!= null) {
509 onSuccess
.run(story
);
518 * Enables or disables this component, depending on the value of the
519 * parameter <code>b</code>. An enabled component can respond to user input
520 * and generate events. Components are enabled initially by default.
522 * Enabling or disabling <b>this</b> component will also affect its
526 * If <code>true</code>, this component is enabled; otherwise
527 * this component is disabled
530 public void setEnabled(boolean b
) {
535 for (GuiReaderGroup group
: booksByType
.keySet()) {
538 for (GuiReaderGroup group
: booksByAuthor
.keySet()) {
545 public void setWords(boolean words
) {
549 public GuiReaderBook
getSelectedBook() {
553 public void unsetSelectedBook() {
557 private void addListPane(String name
, List
<String
> values
,
558 final boolean type
) {
560 GuiReaderGroup bookPane
= new GuiReaderGroup(helper
.getReader(), name
,
563 List
<MetaData
> metas
= new ArrayList
<MetaData
>();
564 for (String source
: values
) {
565 MetaData mSource
= new MetaData();
566 mSource
.setLuid(null);
567 mSource
.setTitle(source
);
568 mSource
.setSource(source
);
572 bookPane
.refreshBooks(metas
, false);
580 bookPane
.setActionListener(new BookActionListener() {
582 public void select(GuiReaderBook book
) {
587 public void popupRequested(GuiReaderBook book
, MouseEvent e
) {
588 JPopupMenu popup
= helper
.createSourcePopup();
589 popup
.show(e
.getComponent(), e
.getX(), e
.getY());
593 public void action(final GuiReaderBook book
) {
595 addBookPane(book
.getMeta().getSource(), type
);
602 * Display an error message and log the linked {@link Exception}.
607 * the title of the error message
609 * the exception to log if any
611 private void error(final String message
, final String title
, Exception e
) {
612 Instance
.getTraceHandler().error(title
+ ": " + message
);
614 Instance
.getTraceHandler().error(e
);
617 SwingUtilities
.invokeLater(new Runnable() {
620 JOptionPane
.showMessageDialog(GuiReaderMainPanel
.this, message
,
621 title
, JOptionPane
.ERROR_MESSAGE
);