X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Freader%2Fui%2FGuiReaderMainPanel.java;h=9a63f76e8ffb832ebc975af2b24e8526e080d3a9;hb=32224dda790cb3d45884e06107da91a8c5a67529;hp=567c9758cfe0e914aec6c2ee1b8074dabd455989;hpb=fb1ffdd032bb81bfa606a686f371038f2466c6af;p=fanfix.git diff --git a/src/be/nikiroo/fanfix/reader/ui/GuiReaderMainPanel.java b/src/be/nikiroo/fanfix/reader/ui/GuiReaderMainPanel.java index 567c975..9a63f76 100644 --- a/src/be/nikiroo/fanfix/reader/ui/GuiReaderMainPanel.java +++ b/src/be/nikiroo/fanfix/reader/ui/GuiReaderMainPanel.java @@ -2,6 +2,7 @@ package be.nikiroo.fanfix.reader.ui; import java.awt.BorderLayout; import java.awt.Color; +import java.awt.EventQueue; import java.awt.Frame; import java.awt.Toolkit; import java.awt.datatransfer.DataFlavor; @@ -10,12 +11,13 @@ import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.io.File; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; import java.net.URL; import java.net.UnknownHostException; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.TreeMap; import javax.swing.BoxLayout; import javax.swing.JFileChooser; @@ -51,14 +53,15 @@ import be.nikiroo.utils.ui.ProgressBar; class GuiReaderMainPanel extends JPanel { private static final long serialVersionUID = 1L; private FrameHelper helper; - private Map booksByType; - private Map booksByAuthor; + private Map books; + private GuiReaderGroup bookPane; // for more "All" private JPanel pane; private Color color; private ProgressBar pgBar; private JMenuBar bar; private GuiReaderBook selectedBook; private boolean words; // words or authors (secondary info on books) + private boolean currentType; // type/source or author mode (All and Listing) /** * An object that offers some helper methods to access the frame that host @@ -76,6 +79,8 @@ class GuiReaderMainPanel extends JPanel { /** * Create the main menu bar. + *

+ * Wil invalidate the layout. * * @param libOk * the library can be queried @@ -94,11 +99,11 @@ class GuiReaderMainPanel extends JPanel { /** * Create a popup menu for a {@link GuiReaderBook} that represents a - * source/type (no LUID). + * source/type or an author. * * @return the popup menu to display */ - public JPopupMenu createSourcePopup(); + public JPopupMenu createSourceAuthorPopup(); } /** @@ -157,24 +162,23 @@ class GuiReaderMainPanel extends JPanel { pgBar.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - invalidate(); + pgBar.invalidate(); pgBar.setProgress(null); - validate(); setEnabled(true); + validate(); } }); pgBar.addUpdateListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - invalidate(); + pgBar.invalidate(); validate(); repaint(); } }); - booksByType = new HashMap(); - booksByAuthor = new HashMap(); + books = new TreeMap(); pane.setVisible(false); final Progress pg = new Progress(); @@ -182,51 +186,60 @@ class GuiReaderMainPanel extends JPanel { outOfUi(pg, new Runnable() { @Override public void run() { - BasicLibrary lib = helper.getReader().getLibrary(); - Status status = lib.getStatus(); + final BasicLibrary lib = helper.getReader().getLibrary(); + final Status status = lib.getStatus(); if (status == Status.READY) { lib.refresh(pg); - invalidate(); - helper.createMenu(true); - if (typeF == null) { - addBookPane(true, false); - } else { - addBookPane(typeF, true); - } - refreshBooks(); - validate(); - pane.setVisible(true); - } else { - invalidate(); - helper.createMenu(false); - validate(); - - String err = lib.getLibraryName() + "\n"; - switch (status) { - case INVALID: - err += "Library not valid"; - break; - - case UNAUTORIZED: - err += "You are not allowed to access this library"; - break; - - case UNAVAILABLE: - err += "Library currently unavailable"; - break; - - default: - err += "An error occured when contacting the library"; - break; - } - - error(err, "Library error", null); } + + inUi(new Runnable() { + @Override + public void run() { + if (status == Status.READY) { + helper.createMenu(true); + if (typeF == null) { + addBookPane(true, false); + } else { + addBookPane(typeF, true); + } + pane.setVisible(true); + refreshBooks(); + } else { + helper.createMenu(false); + validate(); + + String err = lib.getLibraryName() + "\n"; + switch (status) { + case INVALID: + err += "Library not valid"; + break; + + case UNAUTORIZED: + err += "You are not allowed to access this library"; + break; + + case UNAVAILABLE: + err += "Library currently unavailable"; + break; + + default: + err += "An error occured when contacting the library"; + break; + } + + error(err, "Library error", null); + } + } + }); } }); } + public boolean getCurrentType() { + return currentType; + } + /** * Add a new {@link GuiReaderGroup} on the frame to display all the * sources/types or all the authors, or a listing of all the books sorted @@ -245,6 +258,7 @@ class GuiReaderMainPanel extends JPanel { * get one icon per source or author */ public void addBookPane(boolean type, boolean listMode) { + this.currentType = type; BasicLibrary lib = helper.getReader().getLibrary(); if (type) { if (!listMode) { @@ -272,28 +286,25 @@ class GuiReaderMainPanel extends JPanel { /** * Add a new {@link GuiReaderGroup} on the frame to display the books of the * selected type or author. + *

+ * Will invalidate the layout. * * @param value * the author or the type, or NULL to get all the * authors-or-types * @param type * TRUE for type/source, FALSE for author - * */ public void addBookPane(String value, boolean type) { + this.currentType = type; + GuiReaderGroup bookPane = new GuiReaderGroup(helper.getReader(), value, color); - if (type) { - booksByType.put(bookPane, value); - } else { - booksByAuthor.put(bookPane, value); - } - this.invalidate(); + books.put(value, bookPane); + pane.invalidate(); pane.add(bookPane); - pane.validate(); - this.validate(); bookPane.setActionListener(new BookActionListener() { @Override @@ -317,40 +328,43 @@ class GuiReaderMainPanel extends JPanel { /** * Clear the pane from any book that may be present, usually prior to adding * new ones. + *

+ * Will invalidate the layout. */ public void removeBookPanes() { - booksByType.clear(); - booksByAuthor.clear(); + books.clear(); pane.invalidate(); - this.invalidate(); pane.removeAll(); - pane.validate(); - this.validate(); } /** * Refresh the list of {@link GuiReaderBook}s from disk. + *

+ * Will validate the layout, as it is a "refresh" operation. */ public void refreshBooks() { BasicLibrary lib = helper.getReader().getLibrary(); - for (GuiReaderGroup group : booksByType.keySet()) { + for (String value : books.keySet()) { List infos = new ArrayList(); - for (MetaData meta : lib.getListBySource(booksByType.get(group))) { + + List metas; + if (currentType) { + metas = lib.getListBySource(value); + } else { + metas = lib.getListByAuthor(value); + } + for (MetaData meta : metas) { infos.add(GuiReaderBookInfo.fromMeta(meta)); } - group.refreshBooks(infos, words); + + books.get(value).refreshBooks(infos, words); } - for (GuiReaderGroup group : booksByAuthor.keySet()) { - List infos = new ArrayList(); - for (MetaData meta : lib.getListByAuthor(booksByAuthor.get(group))) { - infos.add(GuiReaderBookInfo.fromMeta(meta)); - } - group.refreshBooks(infos, words); + if (bookPane != null) { + bookPane.refreshBooks(words); } - pane.repaint(); - this.repaint(); + this.validate(); } /** @@ -425,6 +439,31 @@ class GuiReaderMainPanel extends JPanel { }, "outOfUi thread").start(); } + /** + * Process the given action in the main Swing UI thread. + *

+ * The code will make sure the current thread is the main UI thread and, if + * not, will switch to it before executing the runnable. + *

+ * Synchronous operation. + * + * @param run + * the action to run + */ + public void inUi(final Runnable run) { + if (EventQueue.isDispatchThread()) { + run.run(); + } else { + try { + EventQueue.invokeAndWait(run); + } catch (InterruptedException e) { + Instance.getTraceHandler().error(e); + } catch (InvocationTargetException e) { + Instance.getTraceHandler().error(e); + } + } + } + /** * Import a {@link Story} into the main {@link LocalLibrary}. *

@@ -536,10 +575,7 @@ class GuiReaderMainPanel extends JPanel { bar.setEnabled(b); } - for (GuiReaderGroup group : booksByType.keySet()) { - group.setEnabled(b); - } - for (GuiReaderGroup group : booksByAuthor.keySet()) { + for (GuiReaderGroup group : books.values()) { group.setEnabled(b); } super.setEnabled(b); @@ -563,7 +599,7 @@ class GuiReaderMainPanel extends JPanel { GuiReader reader = helper.getReader(); BasicLibrary lib = reader.getLibrary(); - GuiReaderGroup bookPane = new GuiReaderGroup(reader, name, color); + bookPane = new GuiReaderGroup(reader, name, color); List infos = new ArrayList(); for (String value : values) { @@ -590,7 +626,7 @@ class GuiReaderMainPanel extends JPanel { @Override public void popupRequested(GuiReaderBook book, MouseEvent e) { - JPopupMenu popup = helper.createSourcePopup(); + JPopupMenu popup = helper.createSourceAuthorPopup(); popup.show(e.getComponent(), e.getX(), e.getY()); }