X-Git-Url: http://git.nikiroo.be/?p=fanfix.git;a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Freader%2Fui%2FGuiReaderFrame.java;h=97541026188404fd27420d5184f38c41a02e2f54;hp=b1ba90c9acde6cca6d23ad060eaf9526bac0a479;hb=2d56db730853fae42f5099cbca8cbed66129551c;hpb=c1b93db3213970c86b766c92a0b0d546c9d1c2ae diff --git a/src/be/nikiroo/fanfix/reader/ui/GuiReaderFrame.java b/src/be/nikiroo/fanfix/reader/ui/GuiReaderFrame.java index b1ba90c..9754102 100644 --- a/src/be/nikiroo/fanfix/reader/ui/GuiReaderFrame.java +++ b/src/be/nikiroo/fanfix/reader/ui/GuiReaderFrame.java @@ -1,42 +1,25 @@ package be.nikiroo.fanfix.reader.ui; import java.awt.BorderLayout; -import java.awt.Color; -import java.awt.Font; import java.awt.Frame; -import java.awt.Toolkit; -import java.awt.datatransfer.DataFlavor; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; -import java.awt.event.MouseEvent; import java.awt.event.WindowEvent; import java.io.File; import java.io.IOException; -import java.net.URL; -import java.net.UnknownHostException; -import java.util.ArrayList; -import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import javax.swing.BorderFactory; -import javax.swing.BoxLayout; -import javax.swing.ImageIcon; 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.JOptionPane; -import javax.swing.JPanel; import javax.swing.JPopupMenu; -import javax.swing.JScrollPane; -import javax.swing.JTextArea; -import javax.swing.SwingConstants; import javax.swing.SwingUtilities; import javax.swing.filechooser.FileFilter; import javax.swing.filechooser.FileNameExtensionFilter; @@ -47,15 +30,14 @@ 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.BasicReader; -import be.nikiroo.fanfix.reader.ui.GuiReaderBook.BookActionListener; +import be.nikiroo.fanfix.reader.ui.GuiReaderMainPanel.FrameHelper; +import be.nikiroo.fanfix.reader.ui.GuiReaderMainPanel.StoryRunnable; import be.nikiroo.utils.Progress; import be.nikiroo.utils.Version; import be.nikiroo.utils.ui.ConfigEditor; -import be.nikiroo.utils.ui.ProgressBar; /** * A {@link Frame} that will show a {@link GuiReaderBook} item for each @@ -65,31 +47,23 @@ import be.nikiroo.utils.ui.ProgressBar; * * @author niki */ -class GuiReaderFrame extends JFrame { +class GuiReaderFrame extends JFrame implements FrameHelper { private static final long serialVersionUID = 1L; private GuiReader reader; - private Map booksByType; - private Map booksByAuthor; - 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 GuiReaderMainPanel mainPanel; /** - * A {@link Runnable} with a {@link Story} parameter. + * The different modification actions you can use on {@link Story} items. * * @author niki */ - private interface StoryRunnable { - /** - * Run the action. - * - * @param story - * the story - */ - public void run(Story story); + private enum ChangeAction { + /** Change the source/type, that is, move it to another source. */ + SOURCE, + /** Change its name. */ + TITLE, + /** Change its author. */ + AUTHOR } /** @@ -106,268 +80,46 @@ class GuiReaderFrame extends JFrame { this.reader = reader; - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + mainPanel = new GuiReaderMainPanel(this, type); + setSize(800, 600); setLayout(new BorderLayout()); - - pane = new JPanel(); - pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS)); - - Integer icolor = Instance.getUiConfig().getColor( - UiConfig.BACKGROUND_COLOR); - if (icolor != null) { - color = new Color(icolor); - setBackground(color); - pane.setBackground(color); - } - - JScrollPane scroll = new JScrollPane(pane); - 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); - - pgBar.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - invalidate(); - pgBar.setProgress(null); - validate(); - setEnabled(true); - } - }); - - pgBar.addUpdateListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - invalidate(); - validate(); - repaint(); - } - }); - - booksByType = new HashMap(); - booksByAuthor = new HashMap(); - - pane.setVisible(false); - final Progress pg = new Progress(); - final String typeF = type; - outOfUi(pg, new Runnable() { - @Override - public void run() { - 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 unavailable"; - break; - - default: - err += "An error occured when contacting the library"; - break; - } - - error(err, "Library error", null); - } - } - }); - - setVisible(true); + add(mainPanel, BorderLayout.CENTER); } - 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. - * - * @param value - * the author or the type, or NULL to get all the - * authors-or-types - * @param type - * TRUE for type, FALSE for author - */ - private void addBookPane(String value, boolean type) { - if (value == null) { - if (type) { - if (Instance.getUiConfig().getBoolean(UiConfig.SOURCE_PAGE, - false)) { - addSourcePanes(); - } else { - for (String tt : reader.getLibrary().getSources()) { - if (tt != null) { - addBookPane(tt, type); - } - } - } - } else { - for (String tt : reader.getLibrary().getAuthors()) { - if (tt != null) { - addBookPane(tt, type); - } - } - } - - return; - } - - GuiReaderGroup bookPane = new GuiReaderGroup(reader, value, color); - if (type) { - booksByType.put(bookPane, value); - } else { - booksByAuthor.put(bookPane, value); - } - - 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.addSeparator(); - popup.add(createMenuItemExport()); - popup.add(createMenuItemMoveTo(true)); - popup.add(createMenuItemSetCover()); - popup.add(createMenuItemClearCache()); - popup.add(createMenuItemRedownload()); - popup.addSeparator(); - popup.add(createMenuItemRename(true)); - popup.add(createMenuItemSetAuthor(true)); - popup.addSeparator(); - popup.add(createMenuItemDelete()); - popup.addSeparator(); - popup.add(createMenuItemProperties()); - popup.show(e.getComponent(), e.getX(), e.getY()); - } - - @Override - public void action(final GuiReaderBook book) { - openBook(book); - } - }); + @Override + public JPopupMenu createBookPopup() { + JPopupMenu popup = new JPopupMenu(); + popup.add(createMenuItemOpenBook()); + popup.addSeparator(); + popup.add(createMenuItemExport()); + popup.add(createMenuItemMoveTo(true)); + popup.add(createMenuItemSetCoverForSource()); + popup.add(createMenuItemSetCoverForAuthor()); + popup.add(createMenuItemClearCache()); + popup.add(createMenuItemRedownload()); + popup.addSeparator(); + popup.add(createMenuItemRename(true)); + popup.add(createMenuItemSetAuthor(true)); + popup.addSeparator(); + popup.add(createMenuItemDelete()); + popup.addSeparator(); + popup.add(createMenuItemProperties()); + return popup; } - private void removeBookPanes() { - booksByType.clear(); - booksByAuthor.clear(); - pane.invalidate(); - this.invalidate(); - pane.removeAll(); - pane.validate(); - this.validate(); + @Override + public JPopupMenu createSourceAuthorPopup() { + JPopupMenu popup = new JPopupMenu(); + popup.add(createMenuItemOpenBook()); + return popup; } - /** - * Refresh the list of {@link GuiReaderBook}s from disk. - */ - private void refreshBooks() { - for (GuiReaderGroup group : booksByType.keySet()) { - List stories = reader.getLibrary().getListBySource( - booksByType.get(group)); - group.refreshBooks(stories, words); - } - - for (GuiReaderGroup group : booksByAuthor.keySet()) { - List stories = reader.getLibrary().getListByAuthor( - booksByAuthor.get(group)); - group.refreshBooks(stories, words); - } - - pane.repaint(); - this.repaint(); - } + @Override + public void createMenu(boolean libOk) { + invalidate(); - /** - * Create the main menu bar. - * - * @param libOk - * the library can be queried - * - * @return the bar - */ - private JMenuBar createMenu(boolean libOk) { - bar = new JMenuBar(); + JMenuBar bar = new JMenuBar(); JMenu file = new JMenu("File"); file.setMnemonic(KeyEvent.VK_F); @@ -376,14 +128,14 @@ class GuiReaderFrame extends JFrame { imprt.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - imprt(true); + mainPanel.imprt(true); } }); JMenuItem imprtF = new JMenuItem("Import File...", KeyEvent.VK_F); imprtF.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - imprt(false); + mainPanel.imprt(false); } }); JMenuItem exit = new JMenuItem("Exit", KeyEvent.VK_X); @@ -426,8 +178,8 @@ class GuiReaderFrame extends JFrame { vauthors.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - words = false; - refreshBooks(); + mainPanel.setWords(false); + mainPanel.refreshBooks(); } }); view.add(vauthors); @@ -436,89 +188,95 @@ class GuiReaderFrame extends JFrame { vwords.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - words = true; - refreshBooks(); + mainPanel.setWords(true); + mainPanel.refreshBooks(); } }); view.add(vwords); bar.add(view); + Map> groupedSources = new HashMap>(); + if (libOk) { + groupedSources = reader.getLibrary().getSourcesGrouped(); + } JMenu sources = new JMenu("Sources"); sources.setMnemonic(KeyEvent.VK_S); + populateMenuSA(sources, groupedSources, true); + bar.add(sources); - Map> groupedSources = new HashMap>(); + Map> goupedAuthors = new HashMap>(); if (libOk) { - groupedSources = reader.getLibrary().getSourcesGrouped(); + goupedAuthors = reader.getLibrary().getAuthorsGrouped(); } + JMenu authors = new JMenu("Authors"); + authors.setMnemonic(KeyEvent.VK_A); + populateMenuSA(authors, goupedAuthors, false); + bar.add(authors); - JMenuItem item = new JMenuItem("All"); - item.addActionListener(getActionOpenSource(null)); - sources.add(item); - sources.addSeparator(); + JMenu options = new JMenu("Options"); + options.setMnemonic(KeyEvent.VK_O); + options.add(createMenuItemConfig()); + options.add(createMenuItemUiConfig()); + bar.add(options); - for (final String type : groupedSources.keySet()) { - List list = groupedSources.get(type); - if (list.size() == 1 && list.get(0).isEmpty()) { - item = new JMenuItem(type); - item.addActionListener(getActionOpenSource(type)); - sources.add(item); + setJMenuBar(bar); + } + + // "" = [unknown] + private void populateMenuSA(JMenu menu, + Map> groupedValues, boolean type) { + + // "All" and "Listing" special items + JMenuItem item = new JMenuItem("All"); + item.addActionListener(getActionOpenList(type, false)); + menu.add(item); + item = new JMenuItem("Listing"); + item.addActionListener(getActionOpenList(type, true)); + menu.add(item); + menu.addSeparator(); + + for (final String value : groupedValues.keySet()) { + List list = groupedValues.get(value); + if (type && list.size() == 1 && list.get(0).isEmpty()) { + // leaf item source/type + item = new JMenuItem(value.isEmpty() ? "[unknown]" : value); + item.addActionListener(getActionOpen(value, type)); + menu.add(item); } else { - JMenu dir = new JMenu(type); + JMenu dir; + if (!type && groupedValues.size() == 1) { + // only one group of authors + dir = menu; + } else { + dir = new JMenu(value.isEmpty() ? "[unknown]" : value); + } + for (String sub : list) { // " " instead of "" for the visual height - String itemName = sub.isEmpty() ? " " : sub; - String actualType = type; - if (!sub.isEmpty()) { - actualType += "/" + sub; + String itemName = sub; + if (itemName.isEmpty()) { + itemName = type ? " " : "[unknown]"; + } + + String actualValue = value; + if (type) { + if (!sub.isEmpty()) { + actualValue += "/" + sub; + } + } else { + actualValue = sub; } item = new JMenuItem(itemName); - item.addActionListener(getActionOpenSource(actualType)); + item.addActionListener(getActionOpen(actualValue, type)); dir.add(item); } - sources.add(dir); - } - } - - bar.add(sources); - - JMenu authors = new JMenu("Authors"); - authors.setMnemonic(KeyEvent.VK_A); - List>> authorGroups = reader.getLibrary() - .getAuthorsGrouped(); - if (authorGroups.size() > 1) { - // Multiple groups - - // null -> "All" authors special item - populateMenuAuthorList(authors, Arrays.asList((String) null)); - - for (Entry> group : authorGroups) { - JMenu thisGroup = new JMenu(group.getKey()); - populateMenuAuthorList(thisGroup, group.getValue()); - authors.add(thisGroup); - } - } else { - // Only one group - - // null -> "All" authors special item - List authorNames = new ArrayList(); - authorNames.add(null); - if (authorGroups.size() > 0) { - authorNames.addAll(authorGroups.get(0).getValue()); + if (menu != dir) { + menu.add(dir); + } } - populateMenuAuthorList(authors, authorNames); } - - bar.add(authors); - - JMenu options = new JMenu("Options"); - options.setMnemonic(KeyEvent.VK_O); - options.add(createMenuItemConfig()); - options.add(createMenuItemUiConfig()); - bar.add(options); - - return bar; } /** @@ -526,50 +284,31 @@ class GuiReaderFrame extends JFrame { * the selected/displayed one. * * @param type - * the type (source) to select + * the type (source) to select, cannot be NULL * * @return the {@link ActionListener} */ - private ActionListener getActionOpenSource(final String type) { + private ActionListener getActionOpen(final String source, final boolean type) { return new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - removeBookPanes(); - addBookPane(type, true); - refreshBooks(); + mainPanel.removeBookPanes(); + mainPanel.addBookPane(source, type); + mainPanel.refreshBooks(); } }; } - /** - * Populate a list of authors as {@link JMenuItem}s into the given - * {@link JMenu}. - *

- * Each item will select the author when clicked. - * - * @param authors - * the parent {@link JMenuItem} - * @param names - * the authors' names - */ - private void populateMenuAuthorList(JMenu authors, List names) { - for (final String name : names) { - JMenuItem item = new JMenuItem(name == null ? "All" - : name.isEmpty() ? "[unknown]" : name); - item.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - removeBookPanes(); - addBookPane(name, false); - refreshBooks(); - } - }); - authors.add(item); - - if (name == null || name.isEmpty()) { - authors.addSeparator(); + private ActionListener getActionOpenList(final boolean type, + final boolean listMode) { + return new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + mainPanel.removeBookPanes(); + mainPanel.addBookPane(type, listMode); + mainPanel.refreshBooks(); } - } + }; } /** @@ -662,6 +401,7 @@ class GuiReaderFrame extends JFrame { export.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { + final GuiReaderBook selectedBook = mainPanel.getSelectedBook(); if (selectedBook != null) { fc.showDialog(GuiReaderFrame.this, "Save"); if (fc.getSelectedFile() != null) { @@ -670,13 +410,13 @@ class GuiReaderFrame extends JFrame { .getAbsolutePath() + type.getDefaultExtension(false); final Progress pg = new Progress(); - outOfUi(pg, new Runnable() { + mainPanel.outOfUi(pg, false, new Runnable() { @Override public void run() { try { reader.getLibrary().export( - selectedBook.getMeta().getLuid(), - type, path, pg); + selectedBook.getInfo().getMeta() + .getLuid(), type, path, pg); } catch (IOException e) { Instance.getTraceHandler().error(e); } @@ -723,15 +463,16 @@ class GuiReaderFrame extends JFrame { refresh.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { + final GuiReaderBook selectedBook = mainPanel.getSelectedBook(); if (selectedBook != null) { - outOfUi(null, new Runnable() { + mainPanel.outOfUi(null, false, new Runnable() { @Override public void run() { - reader.clearLocalReaderCache(selectedBook.getMeta() - .getLuid()); + reader.clearLocalReaderCache(selectedBook.getInfo() + .getMeta().getLuid()); selectedBook.setCached(false); GuiReaderCoverImager.clearIcon(selectedBook - .getMeta()); + .getInfo()); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { @@ -765,7 +506,7 @@ class GuiReaderFrame extends JFrame { } JMenuItem item = new JMenuItem("New type..."); - item.addActionListener(createMoveAction("SOURCE", null)); + item.addActionListener(createMoveAction(ChangeAction.SOURCE, null)); changeTo.add(item); changeTo.addSeparator(); @@ -773,7 +514,8 @@ class GuiReaderFrame extends JFrame { List list = groupedSources.get(type); if (list.size() == 1 && list.get(0).isEmpty()) { item = new JMenuItem(type); - item.addActionListener(createMoveAction("SOURCE", type)); + item.addActionListener(createMoveAction(ChangeAction.SOURCE, + type)); changeTo.add(item); } else { JMenu dir = new JMenu(type); @@ -786,8 +528,8 @@ class GuiReaderFrame extends JFrame { } item = new JMenuItem(itemName); - item.addActionListener(createMoveAction("SOURCE", - actualType)); + item.addActionListener(createMoveAction( + ChangeAction.SOURCE, actualType)); dir.add(item); } changeTo.add(dir); @@ -813,27 +555,31 @@ class GuiReaderFrame extends JFrame { JMenuItem newItem = new JMenuItem("New author..."); changeTo.add(newItem); changeTo.addSeparator(); - newItem.addActionListener(createMoveAction("AUTHOR", null)); + newItem.addActionListener(createMoveAction(ChangeAction.AUTHOR, null)); // Existing authors if (libOk) { - List>> authorGroups = reader - .getLibrary().getAuthorsGrouped(); - - if (authorGroups.size() > 1) { - for (Entry> entry : authorGroups) { - JMenu group = new JMenu(entry.getKey()); - for (String value : entry.getValue()) { - JMenuItem item = new JMenuItem(value); - item.addActionListener(createMoveAction("AUTHOR", value)); + Map> groupedAuthors = reader.getLibrary() + .getAuthorsGrouped(); + + if (groupedAuthors.size() > 1) { + for (String key : groupedAuthors.keySet()) { + JMenu group = new JMenu(key); + for (String value : groupedAuthors.get(key)) { + JMenuItem item = new JMenuItem( + value.isEmpty() ? "[unknown]" : value); + item.addActionListener(createMoveAction( + ChangeAction.AUTHOR, value)); group.add(item); } changeTo.add(group); } - } else if (authorGroups.size() == 1) { - for (String value : authorGroups.get(0).getValue()) { - JMenuItem item = new JMenuItem(value); - item.addActionListener(createMoveAction("AUTHOR", value)); + } else if (groupedAuthors.size() == 1) { + for (String value : groupedAuthors.values().iterator().next()) { + JMenuItem item = new JMenuItem( + value.isEmpty() ? "[unknown]" : value); + item.addActionListener(createMoveAction( + ChangeAction.AUTHOR, value)); changeTo.add(item); } } @@ -854,24 +600,27 @@ class GuiReaderFrame extends JFrame { @SuppressWarnings("unused") boolean libOk) { JMenuItem changeTo = new JMenuItem("Rename..."); changeTo.setMnemonic(KeyEvent.VK_R); - changeTo.addActionListener(createMoveAction("TITLE", null)); + changeTo.addActionListener(createMoveAction(ChangeAction.TITLE, null)); return changeTo; } - private ActionListener createMoveAction(final String what, final String type) { + private ActionListener createMoveAction(final ChangeAction what, + final String type) { return new ActionListener() { @Override public void actionPerformed(ActionEvent e) { + final GuiReaderBook selectedBook = mainPanel.getSelectedBook(); if (selectedBook != null) { String changeTo = type; if (type == null) { + MetaData meta = selectedBook.getInfo().getMeta(); String init = ""; - if (what.equals("SOURCE")) { - init = selectedBook.getMeta().getSource(); - } else if (what.equals("TITLE")) { - init = selectedBook.getMeta().getTitle(); - } else if (what.equals("AUTHOR")) { - init = selectedBook.getMeta().getAuthor(); + if (what == ChangeAction.SOURCE) { + init = meta.getSource(); + } else if (what == ChangeAction.TITLE) { + init = meta.getTitle(); + } else if (what == ChangeAction.AUTHOR) { + init = meta.getAuthor(); } Object rep = JOptionPane.showInputDialog( @@ -887,26 +636,25 @@ class GuiReaderFrame extends JFrame { } final String fChangeTo = changeTo; - outOfUi(null, new Runnable() { + mainPanel.outOfUi(null, true, new Runnable() { @Override public void run() { - if (what.equals("SOURCE")) { - reader.changeSource(selectedBook.getMeta() - .getLuid(), fChangeTo); - } else if (what.equals("TITLE")) { - reader.changeTitle(selectedBook.getMeta() - .getLuid(), fChangeTo); - } else if (what.equals("AUTHOR")) { - reader.changeAuthor(selectedBook.getMeta() - .getLuid(), fChangeTo); + String luid = selectedBook.getInfo().getMeta() + .getLuid(); + if (what == ChangeAction.SOURCE) { + reader.changeSource(luid, fChangeTo); + } else if (what == ChangeAction.TITLE) { + reader.changeTitle(luid, fChangeTo); + } else if (what == ChangeAction.AUTHOR) { + reader.changeAuthor(luid, fChangeTo); } - selectedBook = null; + mainPanel.unsetSelectedBook(); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { - setJMenuBar(createMenu(true)); + createMenu(true); } }); } @@ -917,7 +665,7 @@ class GuiReaderFrame extends JFrame { } /** - * Create the redownload (then delete original) menu item. + * Create the re-download (then delete original) menu item. * * @return the item */ @@ -926,13 +674,14 @@ class GuiReaderFrame extends JFrame { refresh.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { + final GuiReaderBook selectedBook = mainPanel.getSelectedBook(); if (selectedBook != null) { - final MetaData meta = selectedBook.getMeta(); - imprt(meta.getUrl(), new StoryRunnable() { + final MetaData meta = selectedBook.getInfo().getMeta(); + mainPanel.imprt(meta.getUrl(), new StoryRunnable() { @Override public void run(Story story) { reader.delete(meta.getLuid()); - GuiReaderFrame.this.selectedBook = null; + mainPanel.unsetSelectedBook(); MetaData newMeta = story.getMeta(); if (!newMeta.getSource().equals(meta.getSource())) { reader.changeSource(newMeta.getLuid(), @@ -957,12 +706,14 @@ class GuiReaderFrame extends JFrame { delete.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { + final GuiReaderBook selectedBook = mainPanel.getSelectedBook(); if (selectedBook != null) { - outOfUi(null, new Runnable() { + mainPanel.outOfUi(null, true, new Runnable() { @Override public void run() { - reader.delete(selectedBook.getMeta().getLuid()); - selectedBook = null; + reader.delete(selectedBook.getInfo().getMeta() + .getLuid()); + mainPanel.unsetSelectedBook(); } }); } @@ -982,91 +733,14 @@ class GuiReaderFrame extends JFrame { delete.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { + final GuiReaderBook selectedBook = mainPanel.getSelectedBook(); if (selectedBook != null) { - outOfUi(null, new Runnable() { + mainPanel.outOfUi(null, false, new Runnable() { @Override public void run() { - final MetaData meta = selectedBook.getMeta(); - new JFrame() { - private static final long serialVersionUID = 1L; - @SuppressWarnings("unused") - private Object init = init(); - - private Object init() { - // Borders - int top = 20; - int space = 10; - - // Image - ImageIcon img = GuiReaderCoverImager - .generateCoverIcon( - reader.getLibrary(), meta); - - // frame - setTitle(meta.getLuid() + ": " - + meta.getTitle()); - - setSize(800, img.getIconHeight() + 2 * top); - setLayout(new BorderLayout()); - - // Main panel - JPanel mainPanel = new JPanel( - new BorderLayout()); - JPanel mainPanelKeys = new JPanel(); - mainPanelKeys.setLayout(new BoxLayout( - mainPanelKeys, BoxLayout.Y_AXIS)); - JPanel mainPanelValues = new JPanel(); - mainPanelValues.setLayout(new BoxLayout( - mainPanelValues, BoxLayout.Y_AXIS)); - - mainPanel.add(mainPanelKeys, - BorderLayout.WEST); - mainPanel.add(mainPanelValues, - BorderLayout.CENTER); - - List> infos = BasicReader - .getMetaDesc(meta); - - Color trans = new Color(0, 0, 0, 1); - for (Entry info : infos) { - JTextArea key = new JTextArea(info - .getKey()); - key.setFont(new Font(key.getFont() - .getFontName(), Font.BOLD, key - .getFont().getSize())); - key.setEditable(false); - key.setLineWrap(false); - key.setBackground(trans); - mainPanelKeys.add(key); - - JTextArea value = new JTextArea(info - .getValue()); - value.setEditable(false); - value.setLineWrap(false); - value.setBackground(trans); - mainPanelValues.add(value); - } - - // Image - JLabel imgLabel = new JLabel(img); - imgLabel.setVerticalAlignment(JLabel.TOP); - - // Borders - mainPanelKeys.setBorder(BorderFactory - .createEmptyBorder(top, space, 0, 0)); - mainPanelValues.setBorder(BorderFactory - .createEmptyBorder(top, space, 0, 0)); - imgLabel.setBorder(BorderFactory - .createEmptyBorder(0, space, 0, 0)); - - // Add all - add(imgLabel, BorderLayout.WEST); - add(mainPanel, BorderLayout.CENTER); - - return null; - } - - }.setVisible(true); + new GuiReaderPropertiesFrame(reader.getLibrary(), + selectedBook.getInfo().getMeta()) + .setVisible(true); } }); } @@ -1077,22 +751,24 @@ class GuiReaderFrame extends JFrame { } /** - * Create the open menu item for a book or a source (no LUID). + * Create the open menu item for a book, a source/type or an author. * * @return the item */ - private JMenuItem createMenuItemOpenBook() { + public JMenuItem createMenuItemOpenBook() { JMenuItem open = new JMenuItem("Open", KeyEvent.VK_O); open.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { + final GuiReaderBook selectedBook = mainPanel.getSelectedBook(); if (selectedBook != null) { - if (selectedBook.getMeta().getLuid() == null) { - removeBookPanes(); - addBookPane(selectedBook.getMeta().getSource(), true); - refreshBooks(); + if (selectedBook.getInfo().getMeta() == null) { + mainPanel.removeBookPanes(); + mainPanel.addBookPane(selectedBook.getInfo() + .getMainInfo(), mainPanel.getCurrentType()); + mainPanel.refreshBooks(); } else { - openBook(selectedBook); + mainPanel.openBook(selectedBook); } } } @@ -1107,214 +783,58 @@ class GuiReaderFrame extends JFrame { * * @return the item */ - private JMenuItem createMenuItemSetCover() { + private JMenuItem createMenuItemSetCoverForSource() { JMenuItem open = new JMenuItem("Set as cover for source", KeyEvent.VK_C); open.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { + final GuiReaderBook selectedBook = mainPanel.getSelectedBook(); if (selectedBook != null) { - reader.getLibrary().setSourceCover( - selectedBook.getMeta().getSource(), - selectedBook.getMeta().getLuid()); - MetaData source = selectedBook.getMeta().clone(); - source.setLuid(null); - GuiReaderCoverImager.clearIcon(source); - } - } - }); + BasicLibrary lib = reader.getLibrary(); + String luid = selectedBook.getInfo().getMeta().getLuid(); + String source = selectedBook.getInfo().getMeta() + .getSource(); - return open; - } + lib.setSourceCover(source, luid); - /** - * Open a {@link GuiReaderBook} item. - * - * @param book - * the {@link GuiReaderBook} to open - */ - private void openBook(final GuiReaderBook book) { - final Progress pg = new Progress(); - outOfUi(pg, new Runnable() { - @Override - public void run() { - try { - reader.read(book.getMeta().getLuid(), false, pg); - SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - book.setCached(true); - } - }); - } catch (IOException e) { - // TODO: error message? - Instance.getTraceHandler().error(e); + GuiReaderBookInfo sourceInfo = GuiReaderBookInfo + .fromSource(lib, source); + GuiReaderCoverImager.clearIcon(sourceInfo); } } }); - } - - /** - * Process the given action out of the Swing UI thread and link the given - * {@link ProgressBar} to the action. - *

- * The code will make sure that the {@link ProgressBar} (if not NULL) is set - * to done when the action is done. - * - * @param progress - * the {@link ProgressBar} or NULL - * @param run - * the action to run - */ - private void outOfUi(Progress progress, final Runnable run) { - final Progress pg = new Progress(); - final Progress reload = new Progress("Reload books"); - if (progress == null) { - progress = new Progress(); - } - - pg.addProgress(progress, 90); - pg.addProgress(reload, 10); - - invalidate(); - pgBar.setProgress(pg); - validate(); - setEnabled(false); - - new Thread(new Runnable() { - @Override - public void run() { - try { - run.run(); - refreshBooks(); - } finally { - reload.done(); - if (!pg.isDone()) { - // will trigger pgBar ActionListener: - pg.done(); - } - } - } - }, "outOfUi thread").start(); - } - - /** - * Import a {@link Story} into the main {@link LocalLibrary}. - *

- * Should be called inside the UI thread. - * - * @param askUrl - * TRUE for an {@link URL}, false for a {@link File} - */ - private void imprt(boolean askUrl) { - JFileChooser fc = new JFileChooser(); - - Object url; - if (askUrl) { - String clipboard = ""; - try { - clipboard = ("" + Toolkit.getDefaultToolkit() - .getSystemClipboard().getData(DataFlavor.stringFlavor)) - .trim(); - } catch (Exception e) { - // No data will be handled - } - - if (clipboard == null || !clipboard.startsWith("http")) { - clipboard = ""; - } - - url = JOptionPane.showInputDialog(GuiReaderFrame.this, - "url of the story to import?", "Importing from URL", - JOptionPane.QUESTION_MESSAGE, null, null, clipboard); - } else if (fc.showOpenDialog(this) != JFileChooser.CANCEL_OPTION) { - url = fc.getSelectedFile().getAbsolutePath(); - } else { - url = null; - } - if (url != null && !url.toString().isEmpty()) { - imprt(url.toString(), null, null); - } + return open; } /** - * Actually import the {@link Story} into the main {@link LocalLibrary}. - *

- * Should be called inside the UI thread. + * Create the SetCover menu item for a book to change the linked source + * cover. * - * @param url - * the {@link Story} to import by {@link URL} - * @param onSuccess - * Action to execute on success + * @return the item */ - private void imprt(final String url, final StoryRunnable onSuccess, - String onSuccessPgName) { - final Progress pg = new Progress(); - final Progress pgImprt = new Progress(); - final Progress pgOnSuccess = new Progress(onSuccessPgName); - pg.addProgress(pgImprt, 95); - pg.addProgress(pgOnSuccess, 5); - - outOfUi(pg, new Runnable() { + private JMenuItem createMenuItemSetCoverForAuthor() { + JMenuItem open = new JMenuItem("Set as cover for author", KeyEvent.VK_A); + open.addActionListener(new ActionListener() { @Override - public void run() { - Exception ex = null; - Story story = null; - try { - story = reader.getLibrary().imprt(BasicReader.getUrl(url), - pgImprt); - } catch (IOException e) { - ex = e; - } - - final Exception e = ex; + public void actionPerformed(ActionEvent e) { + final GuiReaderBook selectedBook = mainPanel.getSelectedBook(); + if (selectedBook != null) { + BasicLibrary lib = reader.getLibrary(); + String luid = selectedBook.getInfo().getMeta().getLuid(); + String author = selectedBook.getInfo().getMeta() + .getAuthor(); - final boolean ok = (e == null); + lib.setAuthorCover(author, luid); - pgOnSuccess.setProgress(0); - if (!ok) { - if (e instanceof UnknownHostException) { - error("URL not supported: " + 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); - } + GuiReaderBookInfo authorInfo = GuiReaderBookInfo + .fromAuthor(lib, author); + GuiReaderCoverImager.clearIcon(authorInfo); } - pgOnSuccess.done(); } }); - } - - /** - * Enables or disables this component, depending on the value of the - * parameter b. An enabled component can respond to user input - * and generate events. Components are enabled initially by default. - *

- * Disabling this component will also affect its children. - * - * @param b - * If true, this component is enabled; otherwise - * this component is disabled - */ - @Override - public void setEnabled(boolean b) { - if (bar != null) { - bar.setEnabled(b); - } - for (GuiReaderGroup group : booksByType.keySet()) { - group.setEnabled(b); - } - for (GuiReaderGroup group : booksByAuthor.keySet()) { - group.setEnabled(b); - } - super.setEnabled(b); - repaint(); + return open; } /** @@ -1327,7 +847,7 @@ class GuiReaderFrame extends JFrame { * @param e * the exception to log if any */ - private void error(final String message, final String title, Exception e) { + public void error(final String message, final String title, Exception e) { Instance.getTraceHandler().error(title + ": " + message); if (e != null) { Instance.getTraceHandler().error(e); @@ -1341,4 +861,9 @@ class GuiReaderFrame extends JFrame { } }); } + + @Override + public GuiReader getReader() { + return reader; + } }