1 package be
.nikiroo
.fanfix
.reader
.ui
;
3 import java
.awt
.BorderLayout
;
6 import java
.awt
.Toolkit
;
7 import java
.awt
.datatransfer
.DataFlavor
;
8 import java
.awt
.event
.ActionEvent
;
9 import java
.awt
.event
.ActionListener
;
10 import java
.awt
.event
.MouseEvent
;
12 import java
.io
.IOException
;
14 import java
.net
.UnknownHostException
;
15 import java
.util
.ArrayList
;
16 import java
.util
.List
;
18 import java
.util
.TreeMap
;
20 import javax
.swing
.BoxLayout
;
21 import javax
.swing
.JFileChooser
;
22 import javax
.swing
.JLabel
;
23 import javax
.swing
.JMenuBar
;
24 import javax
.swing
.JOptionPane
;
25 import javax
.swing
.JPanel
;
26 import javax
.swing
.JPopupMenu
;
27 import javax
.swing
.JScrollPane
;
28 import javax
.swing
.SwingConstants
;
29 import javax
.swing
.SwingUtilities
;
31 import be
.nikiroo
.fanfix
.Instance
;
32 import be
.nikiroo
.fanfix
.bundles
.UiConfig
;
33 import be
.nikiroo
.fanfix
.data
.MetaData
;
34 import be
.nikiroo
.fanfix
.data
.Story
;
35 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
36 import be
.nikiroo
.fanfix
.library
.BasicLibrary
.Status
;
37 import be
.nikiroo
.fanfix
.library
.LocalLibrary
;
38 import be
.nikiroo
.fanfix
.reader
.BasicReader
;
39 import be
.nikiroo
.fanfix
.reader
.ui
.GuiReaderBook
.BookActionListener
;
40 import be
.nikiroo
.utils
.Progress
;
41 import be
.nikiroo
.utils
.ui
.ProgressBar
;
44 * A {@link Frame} that will show a {@link GuiReaderBook} item for each
45 * {@link Story} in the main cache ({@link Instance#getCache()}), and offer a
46 * way to copy them to the {@link GuiReader} cache (
47 * {@link BasicReader#getLibrary()}), read them, delete them...
51 class GuiReaderMainPanel
extends JPanel
{
52 private static final long serialVersionUID
= 1L;
53 private FrameHelper helper
;
54 private Map
<String
, GuiReaderGroup
> books
;
55 private GuiReaderGroup bookPane
; // for more "All"
58 private ProgressBar pgBar
;
60 private GuiReaderBook selectedBook
;
61 private boolean words
; // words or authors (secondary info on books)
62 private boolean currentType
; // type/source or author mode (All and Listing)
65 * An object that offers some helper methods to access the frame that host
66 * it and the Fanfix-related functions.
70 public interface FrameHelper
{
72 * Return the reader associated to this {@link FrameHelper}.
76 public GuiReader
getReader();
79 * Create the main menu bar.
82 * the library can be queried
86 public void createMenu(boolean b
);
89 * Create a popup menu for a {@link GuiReaderBook} that represents a
92 * @return the popup menu to display
94 public JPopupMenu
createBookPopup();
97 * Create a popup menu for a {@link GuiReaderBook} that represents a
98 * source/type or an author.
100 * @return the popup menu to display
102 public JPopupMenu
createSourceAuthorPopup();
106 * A {@link Runnable} with a {@link Story} parameter.
110 public interface StoryRunnable
{
117 public void run(Story story
);
121 * Create a new {@link GuiReaderMainPanel}.
124 * the associated {@link GuiReader} to forward some commands and
125 * access its {@link LocalLibrary}
127 * the type of {@link Story} to load, or NULL for all types
129 public GuiReaderMainPanel(FrameHelper parent
, String type
) {
130 super(new BorderLayout(), true);
132 this.helper
= parent
;
135 pane
.setLayout(new BoxLayout(pane
, BoxLayout
.PAGE_AXIS
));
137 Integer icolor
= Instance
.getUiConfig().getColor(
138 UiConfig
.BACKGROUND_COLOR
);
139 if (icolor
!= null) {
140 color
= new Color(icolor
);
141 setBackground(color
);
142 pane
.setBackground(color
);
145 JScrollPane scroll
= new JScrollPane(pane
);
146 scroll
.getVerticalScrollBar().setUnitIncrement(16);
147 add(scroll
, BorderLayout
.CENTER
);
149 String message
= parent
.getReader().getLibrary().getLibraryName();
150 if (!message
.isEmpty()) {
151 JLabel name
= new JLabel(message
, SwingConstants
.CENTER
);
152 add(name
, BorderLayout
.NORTH
);
155 pgBar
= new ProgressBar();
156 add(pgBar
, BorderLayout
.SOUTH
);
158 pgBar
.addActionListener(new ActionListener() {
160 public void actionPerformed(ActionEvent e
) {
162 pgBar
.setProgress(null);
168 pgBar
.addUpdateListener(new ActionListener() {
170 public void actionPerformed(ActionEvent e
) {
177 books
= new TreeMap
<String
, GuiReaderGroup
>();
179 pane
.setVisible(false);
180 final Progress pg
= new Progress();
181 final String typeF
= type
;
182 outOfUi(pg
, new Runnable() {
185 BasicLibrary lib
= helper
.getReader().getLibrary();
186 Status status
= lib
.getStatus();
188 if (status
== Status
.READY
) {
191 helper
.createMenu(true);
193 addBookPane(true, false);
195 addBookPane(typeF
, true);
199 pane
.setVisible(true);
202 helper
.createMenu(false);
205 String err
= lib
.getLibraryName() + "\n";
208 err
+= "Library not valid";
212 err
+= "You are not allowed to access this library";
216 err
+= "Library currently unavailable";
220 err
+= "An error occured when contacting the library";
224 error(err
, "Library error", null);
230 public boolean getCurrentType() {
235 * Add a new {@link GuiReaderGroup} on the frame to display all the
236 * sources/types or all the authors, or a listing of all the books sorted
237 * either by source or author.
239 * A display of all the sources/types or all the authors will show one icon
240 * per source/type or author.
242 * A listing of all the books sorted by source/type or author will display
246 * TRUE for type/source, FALSE for author
248 * TRUE to get a listing of all the sources or authors, FALSE to
249 * get one icon per source or author
251 public void addBookPane(boolean type
, boolean listMode
) {
252 this.currentType
= type
;
253 BasicLibrary lib
= helper
.getReader().getLibrary();
256 addListPane("Sources", lib
.getSources(), type
);
258 for (String tt
: lib
.getSources()) {
260 addBookPane(tt
, type
);
266 addListPane("Authors", lib
.getAuthors(), type
);
268 for (String tt
: lib
.getAuthors()) {
270 addBookPane(tt
, type
);
278 * Add a new {@link GuiReaderGroup} on the frame to display the books of the
279 * selected type or author.
283 * the author or the type, or NULL to get all the
286 * TRUE for type/source, FALSE for author
288 public void addBookPane(String value
, boolean type
) {
289 this.currentType
= type
;
291 GuiReaderGroup bookPane
= new GuiReaderGroup(helper
.getReader(), value
,
294 books
.put(value
, bookPane
);
302 bookPane
.setActionListener(new BookActionListener() {
304 public void select(GuiReaderBook book
) {
309 public void popupRequested(GuiReaderBook book
, MouseEvent e
) {
310 JPopupMenu popup
= helper
.createBookPopup();
311 popup
.show(e
.getComponent(), e
.getX(), e
.getY());
315 public void action(final GuiReaderBook book
) {
322 * Clear the pane from any book that may be present, usually prior to adding
325 public void removeBookPanes() {
335 * Refresh the list of {@link GuiReaderBook}s from disk.
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
);
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
, 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("Cannot open the selected book", "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
403 public void outOfUi(Progress progress
, final Runnable run
) {
404 final Progress pg
= new Progress();
405 final Progress reload
= new Progress("Reload books");
406 if (progress
== null) {
407 progress
= new Progress();
410 pg
.addProgress(progress
, 90);
411 pg
.addProgress(reload
, 10);
414 pgBar
.setProgress(pg
);
418 new Thread(new Runnable() {
427 // will trigger pgBar ActionListener:
432 }, "outOfUi thread").start();
436 * Import a {@link Story} into the main {@link LocalLibrary}.
438 * Should be called inside the UI thread.
441 * TRUE for an {@link URL}, false for a {@link File}
443 public void imprt(boolean askUrl
) {
444 JFileChooser fc
= new JFileChooser();
448 String clipboard
= "";
450 clipboard
= ("" + Toolkit
.getDefaultToolkit()
451 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
453 } catch (Exception e
) {
454 // No data will be handled
457 if (clipboard
== null || !clipboard
.startsWith("http")) {
461 url
= JOptionPane
.showInputDialog(GuiReaderMainPanel
.this,
462 "url of the story to import?", "Importing from URL",
463 JOptionPane
.QUESTION_MESSAGE
, null, null, clipboard
);
464 } else if (fc
.showOpenDialog(this) != JFileChooser
.CANCEL_OPTION
) {
465 url
= fc
.getSelectedFile().getAbsolutePath();
470 if (url
!= null && !url
.toString().isEmpty()) {
471 imprt(url
.toString(), null, null);
476 * Actually import the {@link Story} into the main {@link LocalLibrary}.
478 * Should be called inside the UI thread.
481 * the {@link Story} to import by {@link URL}
483 * Action to execute on success
485 public void imprt(final String url
, final StoryRunnable onSuccess
,
486 String onSuccessPgName
) {
487 final Progress pg
= new Progress();
488 final Progress pgImprt
= new Progress();
489 final Progress pgOnSuccess
= new Progress(onSuccessPgName
);
490 pg
.addProgress(pgImprt
, 95);
491 pg
.addProgress(pgOnSuccess
, 5);
493 outOfUi(pg
, new Runnable() {
499 story
= helper
.getReader().getLibrary()
500 .imprt(BasicReader
.getUrl(url
), pgImprt
);
501 } catch (IOException e
) {
505 final Exception e
= ex
;
507 final boolean ok
= (e
== null);
509 pgOnSuccess
.setProgress(0);
511 if (e
instanceof UnknownHostException
) {
512 error("URL not supported: " + url
, "Cannot import URL",
515 error("Failed to import " + url
+ ": \n"
516 + e
.getMessage(), "Cannot import URL", e
);
519 if (onSuccess
!= null) {
520 onSuccess
.run(story
);
529 * Enables or disables this component, depending on the value of the
530 * parameter <code>b</code>. An enabled component can respond to user input
531 * and generate events. Components are enabled initially by default.
533 * Enabling or disabling <b>this</b> component will also affect its
537 * If <code>true</code>, this component is enabled; otherwise
538 * this component is disabled
541 public void setEnabled(boolean b
) {
546 for (GuiReaderGroup group
: books
.values()) {
553 public void setWords(boolean words
) {
557 public GuiReaderBook
getSelectedBook() {
561 public void unsetSelectedBook() {
565 private void addListPane(String name
, List
<String
> values
,
566 final boolean type
) {
567 GuiReader reader
= helper
.getReader();
568 BasicLibrary lib
= reader
.getLibrary();
570 bookPane
= new GuiReaderGroup(reader
, name
, color
);
572 List
<GuiReaderBookInfo
> infos
= new ArrayList
<GuiReaderBookInfo
>();
573 for (String value
: values
) {
575 infos
.add(GuiReaderBookInfo
.fromSource(lib
, value
));
577 infos
.add(GuiReaderBookInfo
.fromAuthor(lib
, value
));
581 bookPane
.refreshBooks(infos
, words
);
589 bookPane
.setActionListener(new BookActionListener() {
591 public void select(GuiReaderBook book
) {
596 public void popupRequested(GuiReaderBook book
, MouseEvent e
) {
597 JPopupMenu popup
= helper
.createSourceAuthorPopup();
598 popup
.show(e
.getComponent(), e
.getX(), e
.getY());
602 public void action(final GuiReaderBook book
) {
604 addBookPane(book
.getInfo().getMainInfo(), type
);
611 * Display an error message and log the linked {@link Exception}.
616 * the title of the error message
618 * the exception to log if any
620 private void error(final String message
, final String title
, Exception e
) {
621 Instance
.getTraceHandler().error(title
+ ": " + message
);
623 Instance
.getTraceHandler().error(e
);
626 SwingUtilities
.invokeLater(new Runnable() {
629 JOptionPane
.showMessageDialog(GuiReaderMainPanel
.this, message
,
630 title
, JOptionPane
.ERROR_MESSAGE
);