import java.io.File;
import java.io.IOException;
import java.net.URL;
+import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
+import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
+import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import be.nikiroo.fanfix.bundles.UiConfig;
import be.nikiroo.fanfix.data.MetaData;
import be.nikiroo.fanfix.data.Story;
+import be.nikiroo.fanfix.library.BasicLibrary;
+import be.nikiroo.fanfix.library.BasicLibrary.Status;
import be.nikiroo.fanfix.library.LocalLibrary;
import be.nikiroo.fanfix.output.BasicOutput.OutputType;
import be.nikiroo.fanfix.reader.GuiReaderBook.BookActionListener;
scroll.getVerticalScrollBar().setUnitIncrement(16);
add(scroll, BorderLayout.CENTER);
+ String message = reader.getLibrary().getLibraryName();
+ if (!message.isEmpty()) {
+ JLabel name = new JLabel(message, SwingConstants.CENTER);
+ add(name, BorderLayout.NORTH);
+ }
+
pgBar = new ProgressBar();
add(pgBar, BorderLayout.SOUTH);
outOfUi(pg, new Runnable() {
@Override
public void run() {
- GuiReaderFrame.this.reader.getLibrary().refresh(false, pg);
- invalidate();
- setJMenuBar(createMenu());
- addBookPane(typeF, true);
- refreshBooks();
- validate();
- pane.setVisible(true);
+ BasicLibrary lib = GuiReaderFrame.this.reader.getLibrary();
+ Status status = lib.getStatus();
+
+ if (status == Status.READY) {
+ lib.refresh(pg);
+ invalidate();
+ setJMenuBar(createMenu(true));
+ addBookPane(typeF, true);
+ refreshBooks();
+ validate();
+ pane.setVisible(true);
+ } else {
+ invalidate();
+ setJMenuBar(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 unavilable";
+ break;
+
+ default:
+ err += "An error occured when contacting the library";
+ break;
+ }
+
+ error(err, "Library error", null);
+ }
}
});
popup.add(createMenuItemOpenBook());
popup.addSeparator();
popup.add(createMenuItemExport());
- popup.add(createMenuItemMove());
+ popup.add(createMenuItemMove(true));
popup.add(createMenuItemSetCover());
popup.add(createMenuItemClearCache());
popup.add(createMenuItemRedownload());
/**
* Create the main menu bar.
*
+ * @param libOk
+ * the library can be queried
+ *
* @return the bar
*/
- private JMenuBar createMenu() {
+ private JMenuBar createMenu(boolean libOk) {
bar = new JMenuBar();
JMenu file = new JMenu("File");
file.add(createMenuItemOpenBook());
file.add(createMenuItemExport());
- file.add(createMenuItemMove());
+ file.add(createMenuItemMove(libOk));
file.addSeparator();
file.add(imprt);
file.add(imprtF);
JMenu sources = new JMenu("Sources");
sources.setMnemonic(KeyEvent.VK_S);
- List<String> tt = reader.getLibrary().getSources();
+ List<String> tt = new ArrayList<String>();
+ if (libOk) {
+ tt.addAll(reader.getLibrary().getSources());
+ }
tt.add(0, null);
+
for (final String type : tt) {
JMenuItem item = new JMenuItem(type == null ? "All" : type);
item.addActionListener(new ActionListener() {
JMenu authors = new JMenu("Authors");
authors.setMnemonic(KeyEvent.VK_A);
- List<String> aa = reader.getLibrary().getAuthors();
+ List<String> aa = new ArrayList<String>();
+ if (libOk) {
+ aa.addAll(reader.getLibrary().getAuthors());
+ }
aa.add(0, null);
for (final String author : aa) {
JMenuItem item = new JMenuItem(author == null ? "All"
selectedBook.getMeta().getLuid(),
type, path, pg);
} catch (IOException e) {
- Instance.syserr(e);
+ Instance.getTraceHandler().error(e);
}
}
});
reader.clearLocalReaderCache(selectedBook.getMeta()
.getLuid());
selectedBook.setCached(false);
+ GuiReaderBook.clearIcon(selectedBook.getMeta());
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
/**
* Create the delete menu item.
*
+ * @param libOk
+ * the library can be queried
+ *
* @return the item
*/
- private JMenuItem createMenuItemMove() {
+ private JMenuItem createMenuItemMove(boolean libOk) {
JMenu moveTo = new JMenu("Move to...");
moveTo.setMnemonic(KeyEvent.VK_M);
List<String> types = new ArrayList<String>();
types.add(null);
- types.addAll(reader.getLibrary().getSources());
+ if (libOk) {
+ types.addAll(reader.getLibrary().getSources());
+ }
for (String type : types) {
JMenuItem item = new JMenuItem(type == null ? "New type..." : type);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
- setJMenuBar(createMenu());
+ setJMenuBar(createMenu(true));
}
});
}
reader.getLibrary().setSourceCover(
selectedBook.getMeta().getSource(),
selectedBook.getMeta().getLuid());
+ MetaData source = selectedBook.getMeta().clone();
+ source.setLuid(null);
+ GuiReaderBook.clearIcon(source);
}
}
});
});
} catch (IOException e) {
// TODO: error message?
- Instance.syserr(e);
+ Instance.getTraceHandler().error(e);
}
}
});
pgOnSuccess.setProgress(0);
if (!ok) {
- Instance.syserr(e);
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- JOptionPane.showMessageDialog(GuiReaderFrame.this,
- "Cannot import: " + url, e.getMessage(),
- JOptionPane.ERROR_MESSAGE);
- }
- });
+ if (e instanceof UnknownHostException) {
+ error("Failed to import " + url, "Cannot import URL",
+ null);
+ } else {
+ error("Failed to import " + url + ": \n"
+ + e.getMessage(), "Cannot import URL", e);
+ }
} else {
if (onSuccess != null) {
onSuccess.run(story);
super.setEnabled(b);
repaint();
}
+
+ /**
+ * Display an error message and log the linked {@link Exception}.
+ *
+ * @param message
+ * the message
+ * @param title
+ * the title of the error message
+ * @param e
+ * the exception to log if any
+ */
+ private void error(final String message, final String title, Exception e) {
+ Instance.getTraceHandler().error(title + ": " + message);
+ if (e != null) {
+ Instance.getTraceHandler().error(e);
+ }
+
+ SwingUtilities.invokeLater(new Runnable() {
+ @Override
+ public void run() {
+ JOptionPane.showMessageDialog(GuiReaderFrame.this, message,
+ title, JOptionPane.ERROR_MESSAGE);
+ }
+ });
+ }
}