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 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
));
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")) {
524 url
= JOptionPane
.showInputDialog(GuiReaderMainPanel
.this,
525 GuiReader
.trans(StringIdGui
.SUBTITLE_IMPORT_URL
),
526 GuiReader
.trans(StringIdGui
.TITLE_IMPORT_URL
),
527 JOptionPane
.QUESTION_MESSAGE
, null, null, clipboard
);
528 } else if (fc
.showOpenDialog(this) != JFileChooser
.CANCEL_OPTION
) {
529 url
= fc
.getSelectedFile().getAbsolutePath();
534 if (url
!= null && !url
.toString().isEmpty()) {
535 imprt(url
.toString(), null, null);
540 * Actually import the {@link Story} into the main {@link LocalLibrary}.
542 * Should be called inside the UI thread.
545 * the {@link Story} to import by {@link URL}
547 * Action to execute on success
548 * @param onSuccessPgName
549 * the name to use for the onSuccess progress bar
551 public void imprt(final String url
, final StoryRunnable onSuccess
,
552 String onSuccessPgName
) {
553 final Progress pg
= new Progress();
554 final Progress pgImprt
= new Progress();
555 final Progress pgOnSuccess
= new Progress(onSuccessPgName
);
556 pg
.addProgress(pgImprt
, 95);
557 pg
.addProgress(pgOnSuccess
, 5);
559 outOfUi(pg
, true, new Runnable() {
565 story
= helper
.getReader().getLibrary()
566 .imprt(BasicReader
.getUrl(url
), pgImprt
);
567 } catch (IOException e
) {
571 final Exception e
= ex
;
573 final boolean ok
= (e
== null);
575 pgOnSuccess
.setProgress(0);
577 if (e
instanceof UnknownHostException
) {
578 error(GuiReader
.trans(
579 StringIdGui
.ERROR_URL_NOT_SUPPORTED
, url
),
580 GuiReader
.trans(StringIdGui
.TITLE_ERROR
), null);
582 error(GuiReader
.trans(
583 StringIdGui
.ERROR_URL_IMPORT_FAILED
, url
,
584 e
.getMessage()), GuiReader
585 .trans(StringIdGui
.TITLE_ERROR
), e
);
588 if (onSuccess
!= null) {
589 onSuccess
.run(story
);
598 * Enables or disables this component, depending on the value of the
599 * parameter <code>b</code>. An enabled component can respond to user input
600 * and generate events. Components are enabled initially by default.
602 * Enabling or disabling <b>this</b> component will also affect its
606 * If <code>true</code>, this component is enabled; otherwise
607 * this component is disabled
610 public void setEnabled(boolean b
) {
615 for (GuiReaderGroup group
: books
.values()) {
622 public void setWords(boolean words
) {
626 public GuiReaderBook
getSelectedBook() {
630 public void unsetSelectedBook() {
634 private void addListPane(String name
, List
<String
> values
,
635 final boolean type
) {
636 GuiReader reader
= helper
.getReader();
637 BasicLibrary lib
= reader
.getLibrary();
639 bookPane
= new GuiReaderGroup(reader
, name
, color
);
641 List
<GuiReaderBookInfo
> infos
= new ArrayList
<GuiReaderBookInfo
>();
642 for (String value
: values
) {
644 infos
.add(GuiReaderBookInfo
.fromSource(lib
, value
));
646 infos
.add(GuiReaderBookInfo
.fromAuthor(lib
, value
));
650 bookPane
.refreshBooks(infos
, words
);
658 bookPane
.setActionListener(new BookActionListener() {
660 public void select(GuiReaderBook book
) {
665 public void popupRequested(GuiReaderBook book
, Component target
,
667 JPopupMenu popup
= helper
.createSourceAuthorPopup();
668 popup
.show(target
, x
, y
);
672 public void action(final GuiReaderBook book
) {
674 addBookPane(book
.getInfo().getMainInfo(), type
);
683 * Focus the first {@link GuiReaderGroup} we find.
685 private void focus() {
686 GuiReaderGroup group
= null;
687 Map
<String
, GuiReaderGroup
> books
= this.books
;
688 if (books
.size() > 0) {
689 group
= books
.values().iterator().next();
697 group
.requestFocusInWindow();
702 * Display an error message and log the linked {@link Exception}.
707 * the title of the error message
709 * the exception to log if any
711 private void error(final String message
, final String title
, Exception e
) {
712 Instance
.getTraceHandler().error(title
+ ": " + message
);
714 Instance
.getTraceHandler().error(e
);
717 SwingUtilities
.invokeLater(new Runnable() {
720 JOptionPane
.showMessageDialog(GuiReaderMainPanel
.this, message
,
721 title
, JOptionPane
.ERROR_MESSAGE
);