X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Freader%2FGuiReaderFrame.java;h=4e2630ec9e3258fc6b9be5d2373f3027582e9eed;hb=e6249b0f26fd3a7ec4cf5e49ba9f9939018c43d3;hp=3b5fca34bc9de09aeb2dadea9d67a3889dad9838;hpb=14b574483b51d3859acef6a269f8841b5a4eb5f8;p=nikiroo-utils.git diff --git a/src/be/nikiroo/fanfix/reader/GuiReaderFrame.java b/src/be/nikiroo/fanfix/reader/GuiReaderFrame.java index 3b5fca3..4e2630e 100644 --- a/src/be/nikiroo/fanfix/reader/GuiReaderFrame.java +++ b/src/be/nikiroo/fanfix/reader/GuiReaderFrame.java @@ -22,6 +22,7 @@ import java.util.Map.Entry; 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; @@ -29,6 +30,7 @@ import javax.swing.JOptionPane; 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; @@ -38,6 +40,8 @@ import be.nikiroo.fanfix.bundles.Config; 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; @@ -66,6 +70,21 @@ class GuiReaderFrame extends JFrame { private GuiReaderBook selectedBook; private boolean words; // words or authors (secondary info on books) + /** + * A {@link Runnable} with a {@link Story} parameter. + * + * @author niki + */ + private interface StoryRunnable { + /** + * Run the action. + * + * @param story + * the story + */ + public void run(Story story); + } + /** * Create a new {@link GuiReaderFrame}. * @@ -97,6 +116,12 @@ class GuiReaderFrame extends JFrame { 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); @@ -128,13 +153,43 @@ class GuiReaderFrame extends JFrame { 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); + } } }); @@ -243,7 +298,7 @@ class GuiReaderFrame extends JFrame { popup.add(createMenuItemOpenBook()); popup.addSeparator(); popup.add(createMenuItemExport()); - popup.add(createMenuItemMove()); + popup.add(createMenuItemMove(true)); popup.add(createMenuItemSetCover()); popup.add(createMenuItemClearCache()); popup.add(createMenuItemRedownload()); @@ -293,9 +348,12 @@ class GuiReaderFrame extends JFrame { /** * 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"); @@ -326,7 +384,7 @@ class GuiReaderFrame extends JFrame { file.add(createMenuItemOpenBook()); file.add(createMenuItemExport()); - file.add(createMenuItemMove()); + file.add(createMenuItemMove(libOk)); file.addSeparator(); file.add(imprt); file.add(imprtF); @@ -372,8 +430,12 @@ class GuiReaderFrame extends JFrame { JMenu sources = new JMenu("Sources"); sources.setMnemonic(KeyEvent.VK_S); - List tt = reader.getLibrary().getSources(); + List tt = new ArrayList(); + 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() { @@ -396,7 +458,10 @@ class GuiReaderFrame extends JFrame { JMenu authors = new JMenu("Authors"); authors.setMnemonic(KeyEvent.VK_A); - List aa = reader.getLibrary().getAuthors(); + List aa = new ArrayList(); + if (libOk) { + aa.addAll(reader.getLibrary().getAuthors()); + } aa.add(0, null); for (final String author : aa) { JMenuItem item = new JMenuItem(author == null ? "All" @@ -533,7 +598,7 @@ class GuiReaderFrame extends JFrame { selectedBook.getMeta().getLuid(), type, path, pg); } catch (IOException e) { - Instance.syserr(e); + Instance.getTraceHandler().error(e); } } }); @@ -585,6 +650,7 @@ class GuiReaderFrame extends JFrame { reader.clearLocalReaderCache(selectedBook.getMeta() .getLuid()); selectedBook.setCached(false); + GuiReaderBook.clearIcon(selectedBook.getMeta()); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { @@ -603,15 +669,20 @@ class GuiReaderFrame extends JFrame { /** * 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 types = new ArrayList(); 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); @@ -653,7 +724,7 @@ class GuiReaderFrame extends JFrame { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { - setJMenuBar(createMenu()); + setJMenuBar(createMenu(true)); } }); } @@ -678,11 +749,16 @@ class GuiReaderFrame extends JFrame { public void actionPerformed(ActionEvent e) { if (selectedBook != null) { final MetaData meta = selectedBook.getMeta(); - imprt(meta.getUrl(), new Runnable() { + imprt(meta.getUrl(), new StoryRunnable() { @Override - public void run() { + public void run(Story story) { reader.delete(meta.getLuid()); GuiReaderFrame.this.selectedBook = null; + MetaData newMeta = story.getMeta(); + if (!newMeta.getSource().equals(meta.getSource())) { + reader.changeType(newMeta.getLuid(), + meta.getSource()); + } } }, "Removing old copy"); } @@ -757,6 +833,9 @@ class GuiReaderFrame extends JFrame { reader.getLibrary().setSourceCover( selectedBook.getMeta().getSource(), selectedBook.getMeta().getLuid()); + MetaData source = selectedBook.getMeta().clone(); + source.setLuid(null); + GuiReaderBook.clearIcon(source); } } }); @@ -785,7 +864,7 @@ class GuiReaderFrame extends JFrame { }); } catch (IOException e) { // TODO: error message? - Instance.syserr(e); + Instance.getTraceHandler().error(e); } } }); @@ -882,7 +961,7 @@ class GuiReaderFrame extends JFrame { * @param onSuccess * Action to execute on success */ - private void imprt(final String url, final Runnable onSuccess, + private void imprt(final String url, final StoryRunnable onSuccess, String onSuccessPgName) { final Progress pg = new Progress(); final Progress pgImprt = new Progress(); @@ -894,8 +973,10 @@ class GuiReaderFrame extends JFrame { @Override public void run() { Exception ex = null; + Story story = null; try { - reader.getLibrary().imprt(BasicReader.getUrl(url), pgImprt); + story = reader.getLibrary().imprt(BasicReader.getUrl(url), + pgImprt); } catch (IOException e) { ex = e; } @@ -906,18 +987,11 @@ class GuiReaderFrame extends JFrame { 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); - } - }); + error("Cannot import URL", "Failed to import " + url + + ": \n" + e.getMessage(), e); } else { if (onSuccess != null) { - onSuccess.run(); + onSuccess.run(story); } } pgOnSuccess.done(); @@ -951,4 +1025,29 @@ class GuiReaderFrame extends JFrame { 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); + } + }); + } }