X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Freader%2FGuiReaderFrame.java;h=4e2630ec9e3258fc6b9be5d2373f3027582e9eed;hb=e6249b0f26fd3a7ec4cf5e49ba9f9939018c43d3;hp=e88c9777ee3afc01ab27efb2cb8595991a4c4f90;hpb=211f7ddb50f68aa8a999023ef6d63d5756bdace6;p=fanfix.git diff --git a/src/be/nikiroo/fanfix/reader/GuiReaderFrame.java b/src/be/nikiroo/fanfix/reader/GuiReaderFrame.java index e88c977..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,19 +153,92 @@ 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); + } } }); setVisible(true); } + private void addSourcePanes() { + // Sources -> i18n + GuiReaderGroup bookPane = new GuiReaderGroup(reader, "Sources", color); + + List sources = new ArrayList(); + for (String source : reader.getLibrary().getSources()) { + MetaData mSource = new MetaData(); + mSource.setLuid(null); + mSource.setTitle(source); + mSource.setSource(source); + sources.add(mSource); + } + + bookPane.refreshBooks(sources, false); + + this.invalidate(); + pane.invalidate(); + pane.add(bookPane); + pane.validate(); + this.validate(); + + bookPane.setActionListener(new BookActionListener() { + @Override + public void select(GuiReaderBook book) { + selectedBook = book; + } + + @Override + public void popupRequested(GuiReaderBook book, MouseEvent e) { + JPopupMenu popup = new JPopupMenu(); + popup.add(createMenuItemOpenBook()); + popup.show(e.getComponent(), e.getX(), e.getY()); + } + + @Override + public void action(final GuiReaderBook book) { + removeBookPanes(); + addBookPane(book.getMeta().getSource(), true); + refreshBooks(); + } + }); + } + /** * Add a new {@link GuiReaderGroup} on the frame to display the books of the * selected type or author. @@ -154,9 +252,14 @@ class GuiReaderFrame extends JFrame { private void addBookPane(String value, boolean type) { if (value == null) { if (type) { - for (String tt : reader.getLibrary().getSources()) { - if (tt != null) { - addBookPane(tt, type); + if (Instance.getUiConfig().getBoolean(UiConfig.SOURCE_PAGE, + false)) { + addSourcePanes(); + } else { + for (String tt : reader.getLibrary().getSources()) { + if (tt != null) { + addBookPane(tt, type); + } } } } else { @@ -195,7 +298,8 @@ 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()); popup.addSeparator(); @@ -244,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"); @@ -277,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); @@ -323,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() { @@ -347,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" @@ -484,7 +598,7 @@ class GuiReaderFrame extends JFrame { selectedBook.getMeta().getLuid(), type, path, pg); } catch (IOException e) { - Instance.syserr(e); + Instance.getTraceHandler().error(e); } } }); @@ -536,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() { @@ -554,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); @@ -584,7 +704,7 @@ class GuiReaderFrame extends JFrame { "Moving story", JOptionPane.QUESTION_MESSAGE, null, null, selectedBook.getMeta().getSource()); - + if (rep == null) { return; } @@ -604,7 +724,7 @@ class GuiReaderFrame extends JFrame { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { - setJMenuBar(createMenu()); + setJMenuBar(createMenu(true)); } }); } @@ -629,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"); } @@ -669,7 +794,7 @@ class GuiReaderFrame extends JFrame { } /** - * Create the open menu item. + * Create the open menu item for a book or a source (no LUID). * * @return the item */ @@ -679,7 +804,38 @@ class GuiReaderFrame extends JFrame { @Override public void actionPerformed(ActionEvent e) { if (selectedBook != null) { - openBook(selectedBook); + if (selectedBook.getMeta().getLuid() == null) { + removeBookPanes(); + addBookPane(selectedBook.getMeta().getSource(), true); + refreshBooks(); + } else { + openBook(selectedBook); + } + } + } + }); + + return open; + } + + /** + * Create the SetCover menu item for a book to change the linked source + * cover. + * + * @return the item + */ + private JMenuItem createMenuItemSetCover() { + JMenuItem open = new JMenuItem("Set as cover for source", KeyEvent.VK_C); + open.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (selectedBook != null) { + reader.getLibrary().setSourceCover( + selectedBook.getMeta().getSource(), + selectedBook.getMeta().getLuid()); + MetaData source = selectedBook.getMeta().clone(); + source.setLuid(null); + GuiReaderBook.clearIcon(source); } } }); @@ -708,7 +864,7 @@ class GuiReaderFrame extends JFrame { }); } catch (IOException e) { // TODO: error message? - Instance.syserr(e); + Instance.getTraceHandler().error(e); } } }); @@ -805,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(); @@ -817,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; } @@ -829,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(); @@ -874,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); + } + }); + } }