X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Freader%2Fui%2FGuiReaderMainPanel.java;h=476e130b01bc6acd7eb4804ddd7d19ddc15db4c4;hb=0013f760d69fd7db2b298c3da5d89bc0b102eabf;hp=5192278992ee03e5cfae845dc4891ff5621fd3ad;hpb=484a31aadf54746222f9f35fb607a2ebee4e5e58;p=fanfix.git
diff --git a/src/be/nikiroo/fanfix/reader/ui/GuiReaderMainPanel.java b/src/be/nikiroo/fanfix/reader/ui/GuiReaderMainPanel.java
index 5192278..476e130 100644
--- a/src/be/nikiroo/fanfix/reader/ui/GuiReaderMainPanel.java
+++ b/src/be/nikiroo/fanfix/reader/ui/GuiReaderMainPanel.java
@@ -9,12 +9,15 @@ import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
+import java.awt.event.FocusAdapter;
+import java.awt.event.FocusEvent;
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.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
@@ -40,6 +43,7 @@ import be.nikiroo.fanfix.library.BasicLibrary.Status;
import be.nikiroo.fanfix.library.LocalLibrary;
import be.nikiroo.fanfix.reader.BasicReader;
import be.nikiroo.fanfix.reader.ui.GuiReaderBook.BookActionListener;
+import be.nikiroo.fanfix.reader.ui.GuiReaderBookInfo.Type;
import be.nikiroo.utils.Progress;
import be.nikiroo.utils.ui.ProgressBar;
@@ -83,10 +87,10 @@ class GuiReaderMainPanel extends JPanel {
*
* Will invalidate the layout.
*
- * @param libOk
- * the library can be queried
+ * @param status
+ * the library status, must not be NULL
*/
- public void createMenu(boolean libOk);
+ public void createMenu(Status status);
/**
* Create a popup menu for a {@link GuiReaderBook} that represents a
@@ -106,18 +110,18 @@ class GuiReaderMainPanel extends JPanel {
}
/**
- * A {@link Runnable} with a {@link Story} parameter.
+ * A {@link Runnable} with a {@link MetaData} parameter.
*
* @author niki
*/
- public interface StoryRunnable {
+ public interface MetaDataRunnable {
/**
* Run the action.
*
- * @param story
- * the story
+ * @param meta
+ * the meta of the story
*/
- public void run(Story story);
+ public void run(MetaData meta);
}
/**
@@ -136,6 +140,7 @@ class GuiReaderMainPanel extends JPanel {
pane = new JPanel();
pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
+ JScrollPane scroll = new JScrollPane(pane);
Integer icolor = Instance.getUiConfig().getColor(
UiConfig.BACKGROUND_COLOR);
@@ -143,9 +148,9 @@ class GuiReaderMainPanel extends JPanel {
color = new Color(icolor);
setBackground(color);
pane.setBackground(color);
+ scroll.setBackground(color);
}
- JScrollPane scroll = new JScrollPane(pane);
scroll.getVerticalScrollBar().setUnitIncrement(16);
add(scroll, BorderLayout.CENTER);
@@ -179,6 +184,13 @@ class GuiReaderMainPanel extends JPanel {
books = new TreeMap();
+ addFocusListener(new FocusAdapter() {
+ @Override
+ public void focusGained(FocusEvent e) {
+ focus();
+ }
+ });
+
pane.setVisible(false);
final Progress pg = new Progress();
final String typeF = type;
@@ -188,23 +200,28 @@ class GuiReaderMainPanel extends JPanel {
final BasicLibrary lib = helper.getReader().getLibrary();
final Status status = lib.getStatus();
- if (status == Status.READY) {
+ if (status == Status.READ_WRITE) {
lib.refresh(pg);
}
inUi(new Runnable() {
@Override
public void run() {
- if (status == Status.READY) {
- helper.createMenu(true);
+ if (status.isReady()) {
+ helper.createMenu(status);
+ pane.setVisible(true);
if (typeF == null) {
- addBookPane(true, false);
+ try {
+ addBookPane(true, false);
+ } catch (IOException e) {
+ error(e.getLocalizedMessage(),
+ "IOException", e);
+ }
} else {
addBookPane(typeF, true);
}
- pane.setVisible(true);
} else {
- helper.createMenu(false);
+ helper.createMenu(status);
validate();
String desc = Instance.getTransGui().getStringX(
@@ -246,8 +263,11 @@ class GuiReaderMainPanel extends JPanel {
* @param listMode
* TRUE to get a listing of all the sources or authors, FALSE to
* get one icon per source or author
+ *
+ * @throws IOException
+ * in case of I/O error
*/
- public void addBookPane(boolean type, boolean listMode) {
+ public void addBookPane(boolean type, boolean listMode) throws IOException {
this.currentType = type;
BasicLibrary lib = helper.getReader().getLibrary();
if (type) {
@@ -316,6 +336,8 @@ class GuiReaderMainPanel extends JPanel {
openBook(book);
}
});
+
+ focus();
}
/**
@@ -341,11 +363,17 @@ class GuiReaderMainPanel extends JPanel {
List infos = new ArrayList();
List metas;
- if (currentType) {
- metas = lib.getListBySource(value);
- } else {
- metas = lib.getListByAuthor(value);
+ try {
+ if (currentType) {
+ metas = lib.getListBySource(value);
+ } else {
+ metas = lib.getListByAuthor(value);
+ }
+ } catch (IOException e) {
+ error(e.getLocalizedMessage(), "IOException", e);
+ metas = new ArrayList();
}
+
for (MetaData meta : metas) {
infos.add(GuiReaderBookInfo.fromMeta(meta));
}
@@ -388,6 +416,70 @@ class GuiReaderMainPanel extends JPanel {
}
});
}
+
+ /**
+ * Prefetch a {@link GuiReaderBook} item (which can be a group, in which
+ * case we prefetch all its members).
+ *
+ * @param book
+ * the {@link GuiReaderBook} to open
+ */
+ public void prefetchBook(final GuiReaderBook book) {
+ final List luids = new LinkedList();
+ try {
+ switch (book.getInfo().getType()) {
+ case STORY:
+ luids.add(book.getInfo().getMeta().getLuid());
+ break;
+ case SOURCE:
+ for (MetaData meta : helper.getReader().getLibrary()
+ .getListBySource(book.getInfo().getMainInfo())) {
+ luids.add(meta.getLuid());
+ }
+ break;
+ case AUTHOR:
+ for (MetaData meta : helper.getReader().getLibrary()
+ .getListByAuthor(book.getInfo().getMainInfo())) {
+ luids.add(meta.getLuid());
+ }
+ break;
+ }
+ } catch (IOException e) {
+ Instance.getTraceHandler().error(e);
+ }
+
+ final Progress pg = new Progress();
+ pg.setMax(luids.size());
+
+ outOfUi(pg, false, new Runnable() {
+ @Override
+ public void run() {
+ try {
+ for (String luid : luids) {
+ Progress pgStep = new Progress();
+ pg.addProgress(pgStep, 1);
+
+ helper.getReader().prefetch(luid, pgStep);
+ }
+
+ // TODO: also set the green button on sources/authors?
+ // requires to do the same when all stories inside are green
+ if (book.getInfo().getType() == Type.STORY) {
+ SwingUtilities.invokeLater(new Runnable() {
+ @Override
+ public void run() {
+ book.setCached(true);
+ }
+ });
+ }
+ } catch (IOException e) {
+ Instance.getTraceHandler().error(e);
+ error(GuiReader.trans(StringIdGui.ERROR_CANNOT_OPEN),
+ GuiReader.trans(StringIdGui.TITLE_ERROR), e);
+ }
+ }
+ });
+ }
/**
* Process the given action out of the Swing UI thread and link the given
@@ -491,7 +583,8 @@ class GuiReaderMainPanel extends JPanel {
// No data will be handled
}
- if (clipboard == null || !clipboard.startsWith("http")) {
+ if (clipboard == null || !(clipboard.startsWith("http://") || //
+ clipboard.startsWith("https://"))) {
clipboard = "";
}
@@ -522,7 +615,7 @@ class GuiReaderMainPanel extends JPanel {
* @param onSuccessPgName
* the name to use for the onSuccess progress bar
*/
- public void imprt(final String url, final StoryRunnable onSuccess,
+ public void imprt(final String url, final MetaDataRunnable onSuccess,
String onSuccessPgName) {
final Progress pg = new Progress();
final Progress pgImprt = new Progress();
@@ -534,9 +627,9 @@ class GuiReaderMainPanel extends JPanel {
@Override
public void run() {
Exception ex = null;
- Story story = null;
+ MetaData meta = null;
try {
- story = helper.getReader().getLibrary()
+ meta = helper.getReader().getLibrary()
.imprt(BasicReader.getUrl(url), pgImprt);
} catch (IOException e) {
ex = e;
@@ -560,7 +653,7 @@ class GuiReaderMainPanel extends JPanel {
}
} else {
if (onSuccess != null) {
- onSuccess.run(story);
+ onSuccess.run(meta);
}
}
pgOnSuccess.done();
@@ -649,6 +742,27 @@ class GuiReaderMainPanel extends JPanel {
refreshBooks();
}
});
+
+ focus();
+ }
+
+ /**
+ * Focus the first {@link GuiReaderGroup} we find.
+ */
+ private void focus() {
+ GuiReaderGroup group = null;
+ Map books = this.books;
+ if (books.size() > 0) {
+ group = books.values().iterator().next();
+ }
+
+ if (group == null) {
+ group = bookPane;
+ }
+
+ if (group != null) {
+ group.requestFocusInWindow();
+ }
}
/**