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
;
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
, Component target
,
310 JPopupMenu popup
= helper
.createBookPopup();
311 popup
.show(target
, x
, y
);
315 public void action(final GuiReaderBook book
) {
322 * Clear the pane from any book that may be present, usually prior to adding
325 * Will invalidate the layout.
327 public void removeBookPanes() {
334 * Refresh the list of {@link GuiReaderBook}s from disk.
336 * Will validate the layout, as it is a "refresh" operation.
338 public void refreshBooks() {
339 BasicLibrary lib
= helper
.getReader().getLibrary();
340 for (String value
: books
.keySet()) {
341 List
<GuiReaderBookInfo
> infos
= new ArrayList
<GuiReaderBookInfo
>();
343 List
<MetaData
> metas
;
345 metas
= lib
.getListBySource(value
);
347 metas
= lib
.getListByAuthor(value
);
349 for (MetaData meta
: metas
) {
350 infos
.add(GuiReaderBookInfo
.fromMeta(meta
));
353 books
.get(value
).refreshBooks(infos
, words
);
356 if (bookPane
!= null) {
357 bookPane
.refreshBooks(words
);
364 * Open a {@link GuiReaderBook} item.
367 * the {@link GuiReaderBook} to open
369 public void openBook(final GuiReaderBook book
) {
370 final Progress pg
= new Progress();
371 outOfUi(pg
, false, new Runnable() {
375 helper
.getReader().read(book
.getInfo().getMeta().getLuid(),
377 SwingUtilities
.invokeLater(new Runnable() {
380 book
.setCached(true);
383 } catch (IOException e
) {
384 Instance
.getTraceHandler().error(e
);
385 error(GuiReader
.trans(StringIdGui
.ERROR_CANNOT_OPEN
),
386 GuiReader
.trans(StringIdGui
.TITLE_ERROR
), e
);
393 * Process the given action out of the Swing UI thread and link the given
394 * {@link ProgressBar} to the action.
396 * The code will make sure that the {@link ProgressBar} (if not NULL) is set
397 * to done when the action is done.
400 * the {@link ProgressBar} or NULL
401 * @param refreshBooks
402 * TRUE to refresh the books after
406 public void outOfUi(Progress progress
, final boolean refreshBooks
,
407 final Runnable run
) {
408 final Progress pg
= new Progress();
409 final Progress reload
= new Progress(
410 GuiReader
.trans(StringIdGui
.PROGRESS_OUT_OF_UI_RELOAD_BOOKS
));
412 if (progress
== null) {
413 progress
= new Progress();
417 pg
.addProgress(progress
, 100);
419 pg
.addProgress(progress
, 90);
420 pg
.addProgress(reload
, 10);
424 pgBar
.setProgress(pg
);
428 new Thread(new Runnable() {
439 // will trigger pgBar ActionListener:
444 }, "outOfUi thread").start();
448 * Process the given action in the main Swing UI thread.
450 * The code will make sure the current thread is the main UI thread and, if
451 * not, will switch to it before executing the runnable.
453 * Synchronous operation.
458 public void inUi(final Runnable run
) {
459 if (EventQueue
.isDispatchThread()) {
463 EventQueue
.invokeAndWait(run
);
464 } catch (InterruptedException e
) {
465 Instance
.getTraceHandler().error(e
);
466 } catch (InvocationTargetException e
) {
467 Instance
.getTraceHandler().error(e
);
473 * Import a {@link Story} into the main {@link LocalLibrary}.
475 * Should be called inside the UI thread.
478 * TRUE for an {@link URL}, false for a {@link File}
480 public void imprt(boolean askUrl
) {
481 JFileChooser fc
= new JFileChooser();
485 String clipboard
= "";
487 clipboard
= ("" + Toolkit
.getDefaultToolkit()
488 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
490 } catch (Exception e
) {
491 // No data will be handled
494 if (clipboard
== null || !clipboard
.startsWith("http")) {
498 url
= JOptionPane
.showInputDialog(GuiReaderMainPanel
.this,
499 GuiReader
.trans(StringIdGui
.SUBTITLE_IMPORT_URL
),
500 GuiReader
.trans(StringIdGui
.TITLE_IMPORT_URL
),
501 JOptionPane
.QUESTION_MESSAGE
, null, null, clipboard
);
502 } else if (fc
.showOpenDialog(this) != JFileChooser
.CANCEL_OPTION
) {
503 url
= fc
.getSelectedFile().getAbsolutePath();
508 if (url
!= null && !url
.toString().isEmpty()) {
509 imprt(url
.toString(), null, null);
514 * Actually import the {@link Story} into the main {@link LocalLibrary}.
516 * Should be called inside the UI thread.
519 * the {@link Story} to import by {@link URL}
521 * Action to execute on success
522 * @param onSuccessPgName
523 * the name to use for the onSuccess progress bar
525 public void imprt(final String url
, final StoryRunnable onSuccess
,
526 String onSuccessPgName
) {
527 final Progress pg
= new Progress();
528 final Progress pgImprt
= new Progress();
529 final Progress pgOnSuccess
= new Progress(onSuccessPgName
);
530 pg
.addProgress(pgImprt
, 95);
531 pg
.addProgress(pgOnSuccess
, 5);
533 outOfUi(pg
, true, new Runnable() {
539 story
= helper
.getReader().getLibrary()
540 .imprt(BasicReader
.getUrl(url
), pgImprt
);
541 } catch (IOException e
) {
545 final Exception e
= ex
;
547 final boolean ok
= (e
== null);
549 pgOnSuccess
.setProgress(0);
551 if (e
instanceof UnknownHostException
) {
552 error(GuiReader
.trans(
553 StringIdGui
.ERROR_URL_NOT_SUPPORTED
, url
),
554 GuiReader
.trans(StringIdGui
.TITLE_ERROR
), null);
556 error(GuiReader
.trans(
557 StringIdGui
.ERROR_URL_IMPORT_FAILED
, url
,
558 e
.getMessage()), GuiReader
559 .trans(StringIdGui
.TITLE_ERROR
), e
);
562 if (onSuccess
!= null) {
563 onSuccess
.run(story
);
572 * Enables or disables this component, depending on the value of the
573 * parameter <code>b</code>. An enabled component can respond to user input
574 * and generate events. Components are enabled initially by default.
576 * Enabling or disabling <b>this</b> component will also affect its
580 * If <code>true</code>, this component is enabled; otherwise
581 * this component is disabled
584 public void setEnabled(boolean b
) {
589 for (GuiReaderGroup group
: books
.values()) {
596 public void setWords(boolean words
) {
600 public GuiReaderBook
getSelectedBook() {
604 public void unsetSelectedBook() {
608 private void addListPane(String name
, List
<String
> values
,
609 final boolean type
) {
610 GuiReader reader
= helper
.getReader();
611 BasicLibrary lib
= reader
.getLibrary();
613 bookPane
= new GuiReaderGroup(reader
, name
, color
);
615 List
<GuiReaderBookInfo
> infos
= new ArrayList
<GuiReaderBookInfo
>();
616 for (String value
: values
) {
618 infos
.add(GuiReaderBookInfo
.fromSource(lib
, value
));
620 infos
.add(GuiReaderBookInfo
.fromAuthor(lib
, value
));
624 bookPane
.refreshBooks(infos
, words
);
632 bookPane
.setActionListener(new BookActionListener() {
634 public void select(GuiReaderBook book
) {
639 public void popupRequested(GuiReaderBook book
, Component target
,
641 JPopupMenu popup
= helper
.createSourceAuthorPopup();
642 popup
.show(target
, x
, y
);
646 public void action(final GuiReaderBook book
) {
648 addBookPane(book
.getInfo().getMainInfo(), type
);
655 * Display an error message and log the linked {@link Exception}.
660 * the title of the error message
662 * the exception to log if any
664 private void error(final String message
, final String title
, Exception e
) {
665 Instance
.getTraceHandler().error(title
+ ": " + message
);
667 Instance
.getTraceHandler().error(e
);
670 SwingUtilities
.invokeLater(new Runnable() {
673 JOptionPane
.showMessageDialog(GuiReaderMainPanel
.this, message
,
674 title
, JOptionPane
.ERROR_MESSAGE
);