1 package be
.nikiroo
.fanfix
.reader
.ui
;
3 import java
.awt
.BorderLayout
;
5 import java
.awt
.Component
;
6 import java
.awt
.EventQueue
;
8 import java
.awt
.Toolkit
;
9 import java
.awt
.datatransfer
.DataFlavor
;
10 import java
.awt
.event
.ActionEvent
;
11 import java
.awt
.event
.ActionListener
;
12 import java
.awt
.event
.FocusAdapter
;
13 import java
.awt
.event
.FocusEvent
;
15 import java
.io
.IOException
;
16 import java
.lang
.reflect
.InvocationTargetException
;
18 import java
.net
.UnknownHostException
;
19 import java
.util
.ArrayList
;
20 import java
.util
.List
;
22 import java
.util
.TreeMap
;
24 import javax
.swing
.BoxLayout
;
25 import javax
.swing
.JFileChooser
;
26 import javax
.swing
.JLabel
;
27 import javax
.swing
.JMenuBar
;
28 import javax
.swing
.JOptionPane
;
29 import javax
.swing
.JPanel
;
30 import javax
.swing
.JPopupMenu
;
31 import javax
.swing
.JScrollPane
;
32 import javax
.swing
.SwingConstants
;
33 import javax
.swing
.SwingUtilities
;
35 import be
.nikiroo
.fanfix
.Instance
;
36 import be
.nikiroo
.fanfix
.bundles
.StringIdGui
;
37 import be
.nikiroo
.fanfix
.bundles
.UiConfig
;
38 import be
.nikiroo
.fanfix
.data
.MetaData
;
39 import be
.nikiroo
.fanfix
.data
.Story
;
40 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
41 import be
.nikiroo
.fanfix
.library
.BasicLibrary
.Status
;
42 import be
.nikiroo
.fanfix
.library
.LocalLibrary
;
43 import be
.nikiroo
.fanfix
.reader
.BasicReader
;
44 import be
.nikiroo
.fanfix
.reader
.ui
.GuiReaderBook
.BookActionListener
;
45 import be
.nikiroo
.utils
.Progress
;
46 import be
.nikiroo
.utils
.ui
.ProgressBar
;
49 * A {@link Frame} that will show a {@link GuiReaderBook} item for each
50 * {@link Story} in the main cache ({@link Instance#getCache()}), and offer a
51 * way to copy them to the {@link GuiReader} cache (
52 * {@link BasicReader#getLibrary()}), read them, delete them...
56 class GuiReaderMainPanel
extends JPanel
{
57 private static final long serialVersionUID
= 1L;
58 private FrameHelper helper
;
59 private Map
<String
, GuiReaderGroup
> books
;
60 private GuiReaderGroup bookPane
; // for more "All"
63 private ProgressBar pgBar
;
65 private GuiReaderBook selectedBook
;
66 private boolean words
; // words or authors (secondary info on books)
67 private boolean currentType
; // type/source or author mode (All and Listing)
70 * An object that offers some helper methods to access the frame that host
71 * it and the Fanfix-related functions.
75 public interface FrameHelper
{
77 * Return the reader associated to this {@link FrameHelper}.
81 public GuiReader
getReader();
84 * Create the main menu bar.
86 * Will invalidate the layout.
89 * the library status, <b>must not</b> be NULL
91 public void createMenu(Status status
);
94 * Create a popup menu for a {@link GuiReaderBook} that represents a
97 * @return the popup menu to display
99 public JPopupMenu
createBookPopup();
102 * Create a popup menu for a {@link GuiReaderBook} that represents a
103 * source/type or an author.
105 * @return the popup menu to display
107 public JPopupMenu
createSourceAuthorPopup();
111 * A {@link Runnable} with a {@link MetaData} parameter.
115 public interface MetaDataRunnable
{
120 * the meta of the story
122 public void run(MetaData meta
);
126 * Create a new {@link GuiReaderMainPanel}.
129 * the associated {@link FrameHelper} to forward some commands
130 * and access its {@link LocalLibrary}
132 * the type of {@link Story} to load, or NULL for all types
134 public GuiReaderMainPanel(FrameHelper parent
, String type
) {
135 super(new BorderLayout(), true);
137 this.helper
= parent
;
140 pane
.setLayout(new BoxLayout(pane
, BoxLayout
.PAGE_AXIS
));
141 JScrollPane scroll
= new JScrollPane(pane
);
143 Integer icolor
= Instance
.getUiConfig().getColor(
144 UiConfig
.BACKGROUND_COLOR
);
145 if (icolor
!= null) {
146 color
= new Color(icolor
);
147 setBackground(color
);
148 pane
.setBackground(color
);
149 scroll
.setBackground(color
);
152 scroll
.getVerticalScrollBar().setUnitIncrement(16);
153 add(scroll
, BorderLayout
.CENTER
);
155 String message
= parent
.getReader().getLibrary().getLibraryName();
156 if (!message
.isEmpty()) {
157 JLabel name
= new JLabel(message
, SwingConstants
.CENTER
);
158 add(name
, BorderLayout
.NORTH
);
161 pgBar
= new ProgressBar();
162 add(pgBar
, BorderLayout
.SOUTH
);
164 pgBar
.addActionListener(new ActionListener() {
166 public void actionPerformed(ActionEvent e
) {
168 pgBar
.setProgress(null);
174 pgBar
.addUpdateListener(new ActionListener() {
176 public void actionPerformed(ActionEvent e
) {
183 books
= new TreeMap
<String
, GuiReaderGroup
>();
185 addFocusListener(new FocusAdapter() {
187 public void focusGained(FocusEvent e
) {
192 pane
.setVisible(false);
193 final Progress pg
= new Progress();
194 final String typeF
= type
;
195 outOfUi(pg
, true, new Runnable() {
198 final BasicLibrary lib
= helper
.getReader().getLibrary();
199 final Status status
= lib
.getStatus();
201 if (status
== Status
.READ_WRITE
) {
205 inUi(new Runnable() {
208 if (status
.isReady()) {
209 helper
.createMenu(status
);
210 pane
.setVisible(true);
213 addBookPane(true, false);
214 } catch (IOException e
) {
215 error(e
.getLocalizedMessage(),
219 addBookPane(typeF
, true);
222 helper
.createMenu(status
);
225 String desc
= Instance
.getTransGui().getStringX(
226 StringIdGui
.ERROR_LIB_STATUS
,
230 .trans(StringIdGui
.ERROR_LIB_STATUS
);
233 String err
= lib
.getLibraryName() + "\n" + desc
;
235 .trans(StringIdGui
.TITLE_ERROR_LIBRARY
),
244 public boolean getCurrentType() {
249 * Add a new {@link GuiReaderGroup} on the frame to display all the
250 * sources/types or all the authors, or a listing of all the books sorted
251 * either by source or author.
253 * A display of all the sources/types or all the authors will show one icon
254 * per source/type or author.
256 * A listing of all the books sorted by source/type or author will display
260 * TRUE for type/source, FALSE for author
262 * TRUE to get a listing of all the sources or authors, FALSE to
263 * get one icon per source or author
265 * @throws IOException
266 * in case of I/O error
268 public void addBookPane(boolean type
, boolean listMode
) throws IOException
{
269 this.currentType
= type
;
270 BasicLibrary lib
= helper
.getReader().getLibrary();
273 addListPane(GuiReader
.trans(StringIdGui
.MENU_SOURCES
),
274 lib
.getSources(), type
);
276 for (String tt
: lib
.getSources()) {
278 addBookPane(tt
, type
);
284 addListPane(GuiReader
.trans(StringIdGui
.MENU_AUTHORS
),
285 lib
.getAuthors(), type
);
287 for (String tt
: lib
.getAuthors()) {
289 addBookPane(tt
, type
);
297 * Add a new {@link GuiReaderGroup} on the frame to display the books of the
298 * selected type or author.
300 * Will invalidate the layout.
303 * the author or the type, or NULL to get all the
306 * TRUE for type/source, FALSE for author
308 public void addBookPane(String value
, boolean type
) {
309 this.currentType
= type
;
311 GuiReaderGroup bookPane
= new GuiReaderGroup(helper
.getReader(), value
,
314 books
.put(value
, bookPane
);
319 bookPane
.setActionListener(new BookActionListener() {
321 public void select(GuiReaderBook book
) {
326 public void popupRequested(GuiReaderBook book
, Component target
,
328 JPopupMenu popup
= helper
.createBookPopup();
329 popup
.show(target
, x
, y
);
333 public void action(final GuiReaderBook book
) {
342 * Clear the pane from any book that may be present, usually prior to adding
345 * Will invalidate the layout.
347 public void removeBookPanes() {
354 * Refresh the list of {@link GuiReaderBook}s from disk.
356 * Will validate the layout, as it is a "refresh" operation.
358 public void refreshBooks() {
359 BasicLibrary lib
= helper
.getReader().getLibrary();
360 for (String value
: books
.keySet()) {
361 List
<GuiReaderBookInfo
> infos
= new ArrayList
<GuiReaderBookInfo
>();
363 List
<MetaData
> metas
;
366 metas
= lib
.getListBySource(value
);
368 metas
= lib
.getListByAuthor(value
);
370 } catch (IOException e
) {
371 error(e
.getLocalizedMessage(), "IOException", e
);
372 metas
= new ArrayList
<MetaData
>();
375 for (MetaData meta
: metas
) {
376 infos
.add(GuiReaderBookInfo
.fromMeta(meta
));
379 books
.get(value
).refreshBooks(infos
, words
);
382 if (bookPane
!= null) {
383 bookPane
.refreshBooks(words
);
390 * Open a {@link GuiReaderBook} item.
393 * the {@link GuiReaderBook} to open
395 public void openBook(final GuiReaderBook book
) {
396 final Progress pg
= new Progress();
397 outOfUi(pg
, false, new Runnable() {
401 helper
.getReader().read(book
.getInfo().getMeta().getLuid(),
403 SwingUtilities
.invokeLater(new Runnable() {
406 book
.setCached(true);
409 } catch (IOException e
) {
410 Instance
.getTraceHandler().error(e
);
411 error(GuiReader
.trans(StringIdGui
.ERROR_CANNOT_OPEN
),
412 GuiReader
.trans(StringIdGui
.TITLE_ERROR
), e
);
419 * Process the given action out of the Swing UI thread and link the given
420 * {@link ProgressBar} to the action.
422 * The code will make sure that the {@link ProgressBar} (if not NULL) is set
423 * to done when the action is done.
426 * the {@link ProgressBar} or NULL
427 * @param refreshBooks
428 * TRUE to refresh the books after
432 public void outOfUi(Progress progress
, final boolean refreshBooks
,
433 final Runnable run
) {
434 final Progress pg
= new Progress();
435 final Progress reload
= new Progress(
436 GuiReader
.trans(StringIdGui
.PROGRESS_OUT_OF_UI_RELOAD_BOOKS
));
438 if (progress
== null) {
439 progress
= new Progress();
443 pg
.addProgress(progress
, 100);
445 pg
.addProgress(progress
, 90);
446 pg
.addProgress(reload
, 10);
450 pgBar
.setProgress(pg
);
454 new Thread(new Runnable() {
465 // will trigger pgBar ActionListener:
470 }, "outOfUi thread").start();
474 * Process the given action in the main Swing UI thread.
476 * The code will make sure the current thread is the main UI thread and, if
477 * not, will switch to it before executing the runnable.
479 * Synchronous operation.
484 public void inUi(final Runnable run
) {
485 if (EventQueue
.isDispatchThread()) {
489 EventQueue
.invokeAndWait(run
);
490 } catch (InterruptedException e
) {
491 Instance
.getTraceHandler().error(e
);
492 } catch (InvocationTargetException e
) {
493 Instance
.getTraceHandler().error(e
);
499 * Import a {@link Story} into the main {@link LocalLibrary}.
501 * Should be called inside the UI thread.
504 * TRUE for an {@link URL}, false for a {@link File}
506 public void imprt(boolean askUrl
) {
507 JFileChooser fc
= new JFileChooser();
511 String clipboard
= "";
513 clipboard
= ("" + Toolkit
.getDefaultToolkit()
514 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
516 } catch (Exception e
) {
517 // No data will be handled
520 if (clipboard
== null || !(clipboard
.startsWith("http://") || //
521 clipboard
.startsWith("https://"))) {
525 url
= JOptionPane
.showInputDialog(GuiReaderMainPanel
.this,
526 GuiReader
.trans(StringIdGui
.SUBTITLE_IMPORT_URL
),
527 GuiReader
.trans(StringIdGui
.TITLE_IMPORT_URL
),
528 JOptionPane
.QUESTION_MESSAGE
, null, null, clipboard
);
529 } else if (fc
.showOpenDialog(this) != JFileChooser
.CANCEL_OPTION
) {
530 url
= fc
.getSelectedFile().getAbsolutePath();
535 if (url
!= null && !url
.toString().isEmpty()) {
536 imprt(url
.toString(), null, null);
541 * Actually import the {@link Story} into the main {@link LocalLibrary}.
543 * Should be called inside the UI thread.
546 * the {@link Story} to import by {@link URL}
548 * Action to execute on success
549 * @param onSuccessPgName
550 * the name to use for the onSuccess progress bar
552 public void imprt(final String url
, final MetaDataRunnable onSuccess
,
553 String onSuccessPgName
) {
554 final Progress pg
= new Progress();
555 final Progress pgImprt
= new Progress();
556 final Progress pgOnSuccess
= new Progress(onSuccessPgName
);
557 pg
.addProgress(pgImprt
, 95);
558 pg
.addProgress(pgOnSuccess
, 5);
560 outOfUi(pg
, true, new Runnable() {
564 MetaData meta
= null;
566 meta
= helper
.getReader().getLibrary()
567 .imprt(BasicReader
.getUrl(url
), pgImprt
);
568 } catch (IOException e
) {
572 final Exception e
= ex
;
574 final boolean ok
= (e
== null);
576 pgOnSuccess
.setProgress(0);
578 if (e
instanceof UnknownHostException
) {
579 error(GuiReader
.trans(
580 StringIdGui
.ERROR_URL_NOT_SUPPORTED
, url
),
581 GuiReader
.trans(StringIdGui
.TITLE_ERROR
), null);
583 error(GuiReader
.trans(
584 StringIdGui
.ERROR_URL_IMPORT_FAILED
, url
,
585 e
.getMessage()), GuiReader
586 .trans(StringIdGui
.TITLE_ERROR
), e
);
589 if (onSuccess
!= null) {
599 * Enables or disables this component, depending on the value of the
600 * parameter <code>b</code>. An enabled component can respond to user input
601 * and generate events. Components are enabled initially by default.
603 * Enabling or disabling <b>this</b> component will also affect its
607 * If <code>true</code>, this component is enabled; otherwise
608 * this component is disabled
611 public void setEnabled(boolean b
) {
616 for (GuiReaderGroup group
: books
.values()) {
623 public void setWords(boolean words
) {
627 public GuiReaderBook
getSelectedBook() {
631 public void unsetSelectedBook() {
635 private void addListPane(String name
, List
<String
> values
,
636 final boolean type
) {
637 GuiReader reader
= helper
.getReader();
638 BasicLibrary lib
= reader
.getLibrary();
640 bookPane
= new GuiReaderGroup(reader
, name
, color
);
642 List
<GuiReaderBookInfo
> infos
= new ArrayList
<GuiReaderBookInfo
>();
643 for (String value
: values
) {
645 infos
.add(GuiReaderBookInfo
.fromSource(lib
, value
));
647 infos
.add(GuiReaderBookInfo
.fromAuthor(lib
, value
));
651 bookPane
.refreshBooks(infos
, words
);
659 bookPane
.setActionListener(new BookActionListener() {
661 public void select(GuiReaderBook book
) {
666 public void popupRequested(GuiReaderBook book
, Component target
,
668 JPopupMenu popup
= helper
.createSourceAuthorPopup();
669 popup
.show(target
, x
, y
);
673 public void action(final GuiReaderBook book
) {
675 addBookPane(book
.getInfo().getMainInfo(), type
);
684 * Focus the first {@link GuiReaderGroup} we find.
686 private void focus() {
687 GuiReaderGroup group
= null;
688 Map
<String
, GuiReaderGroup
> books
= this.books
;
689 if (books
.size() > 0) {
690 group
= books
.values().iterator().next();
698 group
.requestFocusInWindow();
703 * Display an error message and log the linked {@link Exception}.
708 * the title of the error message
710 * the exception to log if any
712 private void error(final String message
, final String title
, Exception e
) {
713 Instance
.getTraceHandler().error(title
+ ": " + message
);
715 Instance
.getTraceHandler().error(e
);
718 SwingUtilities
.invokeLater(new Runnable() {
721 JOptionPane
.showMessageDialog(GuiReaderMainPanel
.this, message
,
722 title
, JOptionPane
.ERROR_MESSAGE
);