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 can be queried
91 public void createMenu(boolean libOk
);
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 Story} parameter.
115 public interface StoryRunnable
{
122 public void run(Story story
);
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
));
142 Integer icolor
= Instance
.getUiConfig().getColor(
143 UiConfig
.BACKGROUND_COLOR
);
144 if (icolor
!= null) {
145 color
= new Color(icolor
);
146 setBackground(color
);
147 pane
.setBackground(color
);
150 JScrollPane scroll
= new JScrollPane(pane
);
151 scroll
.getVerticalScrollBar().setUnitIncrement(16);
152 add(scroll
, BorderLayout
.CENTER
);
154 String message
= parent
.getReader().getLibrary().getLibraryName();
155 if (!message
.isEmpty()) {
156 JLabel name
= new JLabel(message
, SwingConstants
.CENTER
);
157 add(name
, BorderLayout
.NORTH
);
160 pgBar
= new ProgressBar();
161 add(pgBar
, BorderLayout
.SOUTH
);
163 pgBar
.addActionListener(new ActionListener() {
165 public void actionPerformed(ActionEvent e
) {
167 pgBar
.setProgress(null);
173 pgBar
.addUpdateListener(new ActionListener() {
175 public void actionPerformed(ActionEvent e
) {
182 books
= new TreeMap
<String
, GuiReaderGroup
>();
184 addFocusListener(new FocusAdapter() {
186 public void focusGained(FocusEvent e
) {
191 pane
.setVisible(false);
192 final Progress pg
= new Progress();
193 final String typeF
= type
;
194 outOfUi(pg
, true, new Runnable() {
197 final BasicLibrary lib
= helper
.getReader().getLibrary();
198 final Status status
= lib
.getStatus();
200 if (status
== Status
.READY
) {
204 inUi(new Runnable() {
207 if (status
== Status
.READY
) {
208 helper
.createMenu(true);
209 pane
.setVisible(true);
211 addBookPane(true, false);
213 addBookPane(typeF
, true);
216 helper
.createMenu(false);
219 String desc
= Instance
.getTransGui().getStringX(
220 StringIdGui
.ERROR_LIB_STATUS
,
224 .trans(StringIdGui
.ERROR_LIB_STATUS
);
227 String err
= lib
.getLibraryName() + "\n" + desc
;
229 .trans(StringIdGui
.TITLE_ERROR_LIBRARY
),
238 public boolean getCurrentType() {
243 * Add a new {@link GuiReaderGroup} on the frame to display all the
244 * sources/types or all the authors, or a listing of all the books sorted
245 * either by source or author.
247 * A display of all the sources/types or all the authors will show one icon
248 * per source/type or author.
250 * A listing of all the books sorted by source/type or author will display
254 * TRUE for type/source, FALSE for author
256 * TRUE to get a listing of all the sources or authors, FALSE to
257 * get one icon per source or author
259 public void addBookPane(boolean type
, boolean listMode
) {
260 this.currentType
= type
;
261 BasicLibrary lib
= helper
.getReader().getLibrary();
264 addListPane(GuiReader
.trans(StringIdGui
.MENU_SOURCES
),
265 lib
.getSources(), type
);
267 for (String tt
: lib
.getSources()) {
269 addBookPane(tt
, type
);
275 addListPane(GuiReader
.trans(StringIdGui
.MENU_AUTHORS
),
276 lib
.getAuthors(), type
);
278 for (String tt
: lib
.getAuthors()) {
280 addBookPane(tt
, type
);
288 * Add a new {@link GuiReaderGroup} on the frame to display the books of the
289 * selected type or author.
291 * Will invalidate the layout.
294 * the author or the type, or NULL to get all the
297 * TRUE for type/source, FALSE for author
299 public void addBookPane(String value
, boolean type
) {
300 this.currentType
= type
;
302 GuiReaderGroup bookPane
= new GuiReaderGroup(helper
.getReader(), value
,
305 books
.put(value
, bookPane
);
310 bookPane
.setActionListener(new BookActionListener() {
312 public void select(GuiReaderBook book
) {
317 public void popupRequested(GuiReaderBook book
, Component target
,
319 JPopupMenu popup
= helper
.createBookPopup();
320 popup
.show(target
, x
, y
);
324 public void action(final GuiReaderBook book
) {
333 * Clear the pane from any book that may be present, usually prior to adding
336 * Will invalidate the layout.
338 public void removeBookPanes() {
345 * Refresh the list of {@link GuiReaderBook}s from disk.
347 * Will validate the layout, as it is a "refresh" operation.
349 public void refreshBooks() {
350 BasicLibrary lib
= helper
.getReader().getLibrary();
351 for (String value
: books
.keySet()) {
352 List
<GuiReaderBookInfo
> infos
= new ArrayList
<GuiReaderBookInfo
>();
354 List
<MetaData
> metas
;
356 metas
= lib
.getListBySource(value
);
358 metas
= lib
.getListByAuthor(value
);
360 for (MetaData meta
: metas
) {
361 infos
.add(GuiReaderBookInfo
.fromMeta(meta
));
364 books
.get(value
).refreshBooks(infos
, words
);
367 if (bookPane
!= null) {
368 bookPane
.refreshBooks(words
);
375 * Open a {@link GuiReaderBook} item.
378 * the {@link GuiReaderBook} to open
380 public void openBook(final GuiReaderBook book
) {
381 final Progress pg
= new Progress();
382 outOfUi(pg
, false, new Runnable() {
386 helper
.getReader().read(book
.getInfo().getMeta().getLuid(),
388 SwingUtilities
.invokeLater(new Runnable() {
391 book
.setCached(true);
394 } catch (IOException e
) {
395 Instance
.getTraceHandler().error(e
);
396 error(GuiReader
.trans(StringIdGui
.ERROR_CANNOT_OPEN
),
397 GuiReader
.trans(StringIdGui
.TITLE_ERROR
), e
);
404 * Process the given action out of the Swing UI thread and link the given
405 * {@link ProgressBar} to the action.
407 * The code will make sure that the {@link ProgressBar} (if not NULL) is set
408 * to done when the action is done.
411 * the {@link ProgressBar} or NULL
412 * @param refreshBooks
413 * TRUE to refresh the books after
417 public void outOfUi(Progress progress
, final boolean refreshBooks
,
418 final Runnable run
) {
419 final Progress pg
= new Progress();
420 final Progress reload
= new Progress(
421 GuiReader
.trans(StringIdGui
.PROGRESS_OUT_OF_UI_RELOAD_BOOKS
));
423 if (progress
== null) {
424 progress
= new Progress();
428 pg
.addProgress(progress
, 100);
430 pg
.addProgress(progress
, 90);
431 pg
.addProgress(reload
, 10);
435 pgBar
.setProgress(pg
);
439 new Thread(new Runnable() {
450 // will trigger pgBar ActionListener:
455 }, "outOfUi thread").start();
459 * Process the given action in the main Swing UI thread.
461 * The code will make sure the current thread is the main UI thread and, if
462 * not, will switch to it before executing the runnable.
464 * Synchronous operation.
469 public void inUi(final Runnable run
) {
470 if (EventQueue
.isDispatchThread()) {
474 EventQueue
.invokeAndWait(run
);
475 } catch (InterruptedException e
) {
476 Instance
.getTraceHandler().error(e
);
477 } catch (InvocationTargetException e
) {
478 Instance
.getTraceHandler().error(e
);
484 * Import a {@link Story} into the main {@link LocalLibrary}.
486 * Should be called inside the UI thread.
489 * TRUE for an {@link URL}, false for a {@link File}
491 public void imprt(boolean askUrl
) {
492 JFileChooser fc
= new JFileChooser();
496 String clipboard
= "";
498 clipboard
= ("" + Toolkit
.getDefaultToolkit()
499 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
501 } catch (Exception e
) {
502 // No data will be handled
505 if (clipboard
== null || !clipboard
.startsWith("http")) {
509 url
= JOptionPane
.showInputDialog(GuiReaderMainPanel
.this,
510 GuiReader
.trans(StringIdGui
.SUBTITLE_IMPORT_URL
),
511 GuiReader
.trans(StringIdGui
.TITLE_IMPORT_URL
),
512 JOptionPane
.QUESTION_MESSAGE
, null, null, clipboard
);
513 } else if (fc
.showOpenDialog(this) != JFileChooser
.CANCEL_OPTION
) {
514 url
= fc
.getSelectedFile().getAbsolutePath();
519 if (url
!= null && !url
.toString().isEmpty()) {
520 imprt(url
.toString(), null, null);
525 * Actually import the {@link Story} into the main {@link LocalLibrary}.
527 * Should be called inside the UI thread.
530 * the {@link Story} to import by {@link URL}
532 * Action to execute on success
533 * @param onSuccessPgName
534 * the name to use for the onSuccess progress bar
536 public void imprt(final String url
, final StoryRunnable onSuccess
,
537 String onSuccessPgName
) {
538 final Progress pg
= new Progress();
539 final Progress pgImprt
= new Progress();
540 final Progress pgOnSuccess
= new Progress(onSuccessPgName
);
541 pg
.addProgress(pgImprt
, 95);
542 pg
.addProgress(pgOnSuccess
, 5);
544 outOfUi(pg
, true, new Runnable() {
550 story
= helper
.getReader().getLibrary()
551 .imprt(BasicReader
.getUrl(url
), pgImprt
);
552 } catch (IOException e
) {
556 final Exception e
= ex
;
558 final boolean ok
= (e
== null);
560 pgOnSuccess
.setProgress(0);
562 if (e
instanceof UnknownHostException
) {
563 error(GuiReader
.trans(
564 StringIdGui
.ERROR_URL_NOT_SUPPORTED
, url
),
565 GuiReader
.trans(StringIdGui
.TITLE_ERROR
), null);
567 error(GuiReader
.trans(
568 StringIdGui
.ERROR_URL_IMPORT_FAILED
, url
,
569 e
.getMessage()), GuiReader
570 .trans(StringIdGui
.TITLE_ERROR
), e
);
573 if (onSuccess
!= null) {
574 onSuccess
.run(story
);
583 * Enables or disables this component, depending on the value of the
584 * parameter <code>b</code>. An enabled component can respond to user input
585 * and generate events. Components are enabled initially by default.
587 * Enabling or disabling <b>this</b> component will also affect its
591 * If <code>true</code>, this component is enabled; otherwise
592 * this component is disabled
595 public void setEnabled(boolean b
) {
600 for (GuiReaderGroup group
: books
.values()) {
607 public void setWords(boolean words
) {
611 public GuiReaderBook
getSelectedBook() {
615 public void unsetSelectedBook() {
619 private void addListPane(String name
, List
<String
> values
,
620 final boolean type
) {
621 GuiReader reader
= helper
.getReader();
622 BasicLibrary lib
= reader
.getLibrary();
624 bookPane
= new GuiReaderGroup(reader
, name
, color
);
626 List
<GuiReaderBookInfo
> infos
= new ArrayList
<GuiReaderBookInfo
>();
627 for (String value
: values
) {
629 infos
.add(GuiReaderBookInfo
.fromSource(lib
, value
));
631 infos
.add(GuiReaderBookInfo
.fromAuthor(lib
, value
));
635 bookPane
.refreshBooks(infos
, words
);
643 bookPane
.setActionListener(new BookActionListener() {
645 public void select(GuiReaderBook book
) {
650 public void popupRequested(GuiReaderBook book
, Component target
,
652 JPopupMenu popup
= helper
.createSourceAuthorPopup();
653 popup
.show(target
, x
, y
);
657 public void action(final GuiReaderBook book
) {
659 addBookPane(book
.getInfo().getMainInfo(), type
);
668 * Focus the first {@link GuiReaderGroup} we find.
670 private void focus() {
671 GuiReaderGroup group
= null;
672 Map
<String
, GuiReaderGroup
> books
= this.books
;
673 if (books
.size() > 0) {
674 group
= books
.values().iterator().next();
682 group
.requestFocusInWindow();
687 * Display an error message and log the linked {@link Exception}.
692 * the title of the error message
694 * the exception to log if any
696 private void error(final String message
, final String title
, Exception e
) {
697 Instance
.getTraceHandler().error(title
+ ": " + message
);
699 Instance
.getTraceHandler().error(e
);
702 SwingUtilities
.invokeLater(new Runnable() {
705 JOptionPane
.showMessageDialog(GuiReaderMainPanel
.this, message
,
706 title
, JOptionPane
.ERROR_MESSAGE
);