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
.StringIdGui
;
35 import be
.nikiroo
.fanfix
.bundles
.UiConfig
;
36 import be
.nikiroo
.fanfix
.data
.MetaData
;
37 import be
.nikiroo
.fanfix
.data
.Story
;
38 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
39 import be
.nikiroo
.fanfix
.library
.BasicLibrary
.Status
;
40 import be
.nikiroo
.fanfix
.library
.LocalLibrary
;
41 import be
.nikiroo
.fanfix
.reader
.BasicReader
;
42 import be
.nikiroo
.fanfix
.reader
.ui
.GuiReaderBook
.BookActionListener
;
43 import be
.nikiroo
.utils
.Progress
;
44 import be
.nikiroo
.utils
.ui
.ProgressBar
;
47 * A {@link Frame} that will show a {@link GuiReaderBook} item for each
48 * {@link Story} in the main cache ({@link Instance#getCache()}), and offer a
49 * way to copy them to the {@link GuiReader} cache (
50 * {@link BasicReader#getLibrary()}), read them, delete them...
54 class GuiReaderMainPanel
extends JPanel
{
55 private static final long serialVersionUID
= 1L;
56 private FrameHelper helper
;
57 private Map
<String
, GuiReaderGroup
> books
;
58 private GuiReaderGroup bookPane
; // for more "All"
61 private ProgressBar pgBar
;
63 private GuiReaderBook selectedBook
;
64 private boolean words
; // words or authors (secondary info on books)
65 private boolean currentType
; // type/source or author mode (All and Listing)
68 * An object that offers some helper methods to access the frame that host
69 * it and the Fanfix-related functions.
73 public interface FrameHelper
{
75 * Return the reader associated to this {@link FrameHelper}.
79 public GuiReader
getReader();
82 * Create the main menu bar.
84 * Will invalidate the layout.
87 * the library can be queried
89 public void createMenu(boolean libOk
);
92 * Create a popup menu for a {@link GuiReaderBook} that represents a
95 * @return the popup menu to display
97 public JPopupMenu
createBookPopup();
100 * Create a popup menu for a {@link GuiReaderBook} that represents a
101 * source/type or an author.
103 * @return the popup menu to display
105 public JPopupMenu
createSourceAuthorPopup();
109 * A {@link Runnable} with a {@link Story} parameter.
113 public interface StoryRunnable
{
120 public void run(Story story
);
124 * Create a new {@link GuiReaderMainPanel}.
127 * the associated {@link FrameHelper} to forward some commands
128 * and access its {@link LocalLibrary}
130 * the type of {@link Story} to load, or NULL for all types
132 public GuiReaderMainPanel(FrameHelper parent
, String type
) {
133 super(new BorderLayout(), true);
135 this.helper
= parent
;
138 pane
.setLayout(new BoxLayout(pane
, BoxLayout
.PAGE_AXIS
));
140 Integer icolor
= Instance
.getUiConfig().getColor(
141 UiConfig
.BACKGROUND_COLOR
);
142 if (icolor
!= null) {
143 color
= new Color(icolor
);
144 setBackground(color
);
145 pane
.setBackground(color
);
148 JScrollPane scroll
= new JScrollPane(pane
);
149 scroll
.getVerticalScrollBar().setUnitIncrement(16);
150 add(scroll
, BorderLayout
.CENTER
);
152 String message
= parent
.getReader().getLibrary().getLibraryName();
153 if (!message
.isEmpty()) {
154 JLabel name
= new JLabel(message
, SwingConstants
.CENTER
);
155 add(name
, BorderLayout
.NORTH
);
158 pgBar
= new ProgressBar();
159 add(pgBar
, BorderLayout
.SOUTH
);
161 pgBar
.addActionListener(new ActionListener() {
163 public void actionPerformed(ActionEvent e
) {
165 pgBar
.setProgress(null);
171 pgBar
.addUpdateListener(new ActionListener() {
173 public void actionPerformed(ActionEvent e
) {
180 books
= new TreeMap
<String
, GuiReaderGroup
>();
182 pane
.setVisible(false);
183 final Progress pg
= new Progress();
184 final String typeF
= type
;
185 outOfUi(pg
, true, new Runnable() {
188 final BasicLibrary lib
= helper
.getReader().getLibrary();
189 final Status status
= lib
.getStatus();
191 if (status
== Status
.READY
) {
195 inUi(new Runnable() {
198 if (status
== Status
.READY
) {
199 helper
.createMenu(true);
201 addBookPane(true, false);
203 addBookPane(typeF
, true);
205 pane
.setVisible(true);
207 helper
.createMenu(false);
210 String desc
= Instance
.getTransGui().getStringX(
211 StringIdGui
.ERROR_LIB_STATUS
,
215 .trans(StringIdGui
.ERROR_LIB_STATUS
);
218 String err
= lib
.getLibraryName() + "\n" + desc
;
220 .trans(StringIdGui
.TITLE_ERROR_LIBRARY
),
229 public boolean getCurrentType() {
234 * Add a new {@link GuiReaderGroup} on the frame to display all the
235 * sources/types or all the authors, or a listing of all the books sorted
236 * either by source or author.
238 * A display of all the sources/types or all the authors will show one icon
239 * per source/type or author.
241 * A listing of all the books sorted by source/type or author will display
245 * TRUE for type/source, FALSE for author
247 * TRUE to get a listing of all the sources or authors, FALSE to
248 * get one icon per source or author
250 public void addBookPane(boolean type
, boolean listMode
) {
251 this.currentType
= type
;
252 BasicLibrary lib
= helper
.getReader().getLibrary();
255 addListPane(GuiReader
.trans(StringIdGui
.MENU_SOURCES
),
256 lib
.getSources(), type
);
258 for (String tt
: lib
.getSources()) {
260 addBookPane(tt
, type
);
266 addListPane(GuiReader
.trans(StringIdGui
.MENU_AUTHORS
),
267 lib
.getAuthors(), type
);
269 for (String tt
: lib
.getAuthors()) {
271 addBookPane(tt
, type
);
279 * Add a new {@link GuiReaderGroup} on the frame to display the books of the
280 * selected type or author.
282 * Will invalidate the layout.
285 * the author or the type, or NULL to get all the
288 * TRUE for type/source, FALSE for author
290 public void addBookPane(String value
, boolean type
) {
291 this.currentType
= type
;
293 GuiReaderGroup bookPane
= new GuiReaderGroup(helper
.getReader(), value
,
296 books
.put(value
, bookPane
);
301 bookPane
.setActionListener(new BookActionListener() {
303 public void select(GuiReaderBook book
) {
308 public void popupRequested(GuiReaderBook book
, MouseEvent e
) {
309 JPopupMenu popup
= helper
.createBookPopup();
310 popup
.show(e
.getComponent(), e
.getX(), e
.getY());
314 public void action(final GuiReaderBook book
) {
321 * Clear the pane from any book that may be present, usually prior to adding
324 * Will invalidate the layout.
326 public void removeBookPanes() {
333 * Refresh the list of {@link GuiReaderBook}s from disk.
335 * Will validate the layout, as it is a "refresh" operation.
337 public void refreshBooks() {
338 BasicLibrary lib
= helper
.getReader().getLibrary();
339 for (String value
: books
.keySet()) {
340 List
<GuiReaderBookInfo
> infos
= new ArrayList
<GuiReaderBookInfo
>();
342 List
<MetaData
> metas
;
344 metas
= lib
.getListBySource(value
);
346 metas
= lib
.getListByAuthor(value
);
348 for (MetaData meta
: metas
) {
349 infos
.add(GuiReaderBookInfo
.fromMeta(meta
));
352 books
.get(value
).refreshBooks(infos
, words
);
355 if (bookPane
!= null) {
356 bookPane
.refreshBooks(words
);
363 * Open a {@link GuiReaderBook} item.
366 * the {@link GuiReaderBook} to open
368 public void openBook(final GuiReaderBook book
) {
369 final Progress pg
= new Progress();
370 outOfUi(pg
, false, new Runnable() {
374 helper
.getReader().read(book
.getInfo().getMeta().getLuid(),
376 SwingUtilities
.invokeLater(new Runnable() {
379 book
.setCached(true);
382 } catch (IOException e
) {
383 Instance
.getTraceHandler().error(e
);
384 error(GuiReader
.trans(StringIdGui
.ERROR_CANNOT_OPEN
),
385 GuiReader
.trans(StringIdGui
.TITLE_ERROR
), e
);
392 * Process the given action out of the Swing UI thread and link the given
393 * {@link ProgressBar} to the action.
395 * The code will make sure that the {@link ProgressBar} (if not NULL) is set
396 * to done when the action is done.
399 * the {@link ProgressBar} or NULL
400 * @param refreshBooks
401 * TRUE to refresh the books after
405 public void outOfUi(Progress progress
, final boolean refreshBooks
,
406 final Runnable run
) {
407 final Progress pg
= new Progress();
408 final Progress reload
= new Progress(
409 GuiReader
.trans(StringIdGui
.PROGRESS_OUT_OF_UI_RELOAD_BOOKS
));
411 if (progress
== null) {
412 progress
= new Progress();
416 pg
.addProgress(progress
, 100);
418 pg
.addProgress(progress
, 90);
419 pg
.addProgress(reload
, 10);
423 pgBar
.setProgress(pg
);
427 new Thread(new Runnable() {
438 // will trigger pgBar ActionListener:
443 }, "outOfUi thread").start();
447 * Process the given action in the main Swing UI thread.
449 * The code will make sure the current thread is the main UI thread and, if
450 * not, will switch to it before executing the runnable.
452 * Synchronous operation.
457 public void inUi(final Runnable run
) {
458 if (EventQueue
.isDispatchThread()) {
462 EventQueue
.invokeAndWait(run
);
463 } catch (InterruptedException e
) {
464 Instance
.getTraceHandler().error(e
);
465 } catch (InvocationTargetException e
) {
466 Instance
.getTraceHandler().error(e
);
472 * Import a {@link Story} into the main {@link LocalLibrary}.
474 * Should be called inside the UI thread.
477 * TRUE for an {@link URL}, false for a {@link File}
479 public void imprt(boolean askUrl
) {
480 JFileChooser fc
= new JFileChooser();
484 String clipboard
= "";
486 clipboard
= ("" + Toolkit
.getDefaultToolkit()
487 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
489 } catch (Exception e
) {
490 // No data will be handled
493 if (clipboard
== null || !clipboard
.startsWith("http")) {
497 url
= JOptionPane
.showInputDialog(GuiReaderMainPanel
.this,
498 GuiReader
.trans(StringIdGui
.SUBTITLE_IMPORT_URL
),
499 GuiReader
.trans(StringIdGui
.TITLE_IMPORT_URL
),
500 JOptionPane
.QUESTION_MESSAGE
, null, null, clipboard
);
501 } else if (fc
.showOpenDialog(this) != JFileChooser
.CANCEL_OPTION
) {
502 url
= fc
.getSelectedFile().getAbsolutePath();
507 if (url
!= null && !url
.toString().isEmpty()) {
508 imprt(url
.toString(), null, null);
513 * Actually import the {@link Story} into the main {@link LocalLibrary}.
515 * Should be called inside the UI thread.
518 * the {@link Story} to import by {@link URL}
520 * Action to execute on success
521 * @param onSuccessPgName
522 * the name to use for the onSuccess progress bar
524 public void imprt(final String url
, final StoryRunnable onSuccess
,
525 String onSuccessPgName
) {
526 final Progress pg
= new Progress();
527 final Progress pgImprt
= new Progress();
528 final Progress pgOnSuccess
= new Progress(onSuccessPgName
);
529 pg
.addProgress(pgImprt
, 95);
530 pg
.addProgress(pgOnSuccess
, 5);
532 outOfUi(pg
, true, new Runnable() {
538 story
= helper
.getReader().getLibrary()
539 .imprt(BasicReader
.getUrl(url
), pgImprt
);
540 } catch (IOException e
) {
544 final Exception e
= ex
;
546 final boolean ok
= (e
== null);
548 pgOnSuccess
.setProgress(0);
550 if (e
instanceof UnknownHostException
) {
551 error(GuiReader
.trans(
552 StringIdGui
.ERROR_URL_NOT_SUPPORTED
, url
),
553 GuiReader
.trans(StringIdGui
.TITLE_ERROR
), null);
555 error(GuiReader
.trans(
556 StringIdGui
.ERROR_URL_IMPORT_FAILED
, url
,
557 e
.getMessage()), GuiReader
558 .trans(StringIdGui
.TITLE_ERROR
), e
);
561 if (onSuccess
!= null) {
562 onSuccess
.run(story
);
571 * Enables or disables this component, depending on the value of the
572 * parameter <code>b</code>. An enabled component can respond to user input
573 * and generate events. Components are enabled initially by default.
575 * Enabling or disabling <b>this</b> component will also affect its
579 * If <code>true</code>, this component is enabled; otherwise
580 * this component is disabled
583 public void setEnabled(boolean b
) {
588 for (GuiReaderGroup group
: books
.values()) {
595 public void setWords(boolean words
) {
599 public GuiReaderBook
getSelectedBook() {
603 public void unsetSelectedBook() {
607 private void addListPane(String name
, List
<String
> values
,
608 final boolean type
) {
609 GuiReader reader
= helper
.getReader();
610 BasicLibrary lib
= reader
.getLibrary();
612 bookPane
= new GuiReaderGroup(reader
, name
, color
);
614 List
<GuiReaderBookInfo
> infos
= new ArrayList
<GuiReaderBookInfo
>();
615 for (String value
: values
) {
617 infos
.add(GuiReaderBookInfo
.fromSource(lib
, value
));
619 infos
.add(GuiReaderBookInfo
.fromAuthor(lib
, value
));
623 bookPane
.refreshBooks(infos
, words
);
631 bookPane
.setActionListener(new BookActionListener() {
633 public void select(GuiReaderBook book
) {
638 public void popupRequested(GuiReaderBook book
, MouseEvent e
) {
639 JPopupMenu popup
= helper
.createSourceAuthorPopup();
640 popup
.show(e
.getComponent(), e
.getX(), e
.getY());
644 public void action(final GuiReaderBook book
) {
646 addBookPane(book
.getInfo().getMainInfo(), type
);
653 * Display an error message and log the linked {@link Exception}.
658 * the title of the error message
660 * the exception to log if any
662 private void error(final String message
, final String title
, Exception e
) {
663 Instance
.getTraceHandler().error(title
+ ": " + message
);
665 Instance
.getTraceHandler().error(e
);
668 SwingUtilities
.invokeLater(new Runnable() {
671 JOptionPane
.showMessageDialog(GuiReaderMainPanel
.this, message
,
672 title
, JOptionPane
.ERROR_MESSAGE
);