+ color = Instance.getUiConfig().getColor(UiConfig.BACKGROUND_COLOR);
+ if (color != null) {
+ setBackground(color);
+ pane.setBackground(color);
+ }
+
+ JScrollPane scroll = new JScrollPane(pane);
+ scroll.getVerticalScrollBar().setUnitIncrement(16);
+ add(scroll, BorderLayout.CENTER);
+
+ pgBar = new ProgressBar();
+ add(pgBar, BorderLayout.SOUTH);
+
+ pgBar.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ invalidate();
+ pgBar.setProgress(null);
+ validate();
+ setEnabled(true);
+ }
+ });
+
+ pgBar.addUpdateListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ invalidate();
+ validate();
+ repaint();
+ }
+ });
+
+ booksByType = new HashMap<LocalReaderGroup, String>();
+ booksByAuthor = new HashMap<LocalReaderGroup, String>();
+
+ pane.setVisible(false);
+ final Progress pg = new Progress();
+ final String typeF = type;
+ outOfUi(pg, new Runnable() {
+ public void run() {
+ Instance.getLibrary().refresh(false, pg);
+ invalidate();
+ setJMenuBar(createMenu());
+ addBookPane(typeF, true);
+ refreshBooks();
+ validate();
+ pane.setVisible(true);
+ }
+ });
+
+ setVisible(true);
+ }
+
+ /**
+ * Add a new {@link LocalReaderGroup} 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) {
+ for (String tt : Instance.getLibrary().getSources()) {
+ if (tt != null) {
+ addBookPane(tt, type);
+ }
+ }
+ } else {
+ for (String tt : Instance.getLibrary().getAuthors()) {
+ if (tt != null) {
+ addBookPane(tt, type);
+ }
+ }
+ }
+
+ return;
+ }
+
+ LocalReaderGroup bookPane = new LocalReaderGroup(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() {
+ public void select(LocalReaderBook book) {
+ selectedBook = book;
+ }
+
+ public void popupRequested(LocalReaderBook book, MouseEvent e) {
+ JPopupMenu popup = new JPopupMenu();
+ popup.add(createMenuItemOpenBook());
+ popup.addSeparator();
+ popup.add(createMenuItemExport());
+ popup.add(createMenuItemMove());
+ popup.add(createMenuItemClearCache());
+ popup.add(createMenuItemRedownload());
+ popup.addSeparator();
+ popup.add(createMenuItemDelete());
+ popup.show(e.getComponent(), e.getX(), e.getY());
+ }
+
+ public void action(final LocalReaderBook book) {
+ openBook(book);
+ }
+ });
+ }
+
+ private void removeBookPanes() {
+ booksByType.clear();
+ booksByAuthor.clear();
+ pane.invalidate();
+ this.invalidate();
+ pane.removeAll();
+ pane.validate();
+ this.validate();
+ }
+
+ /**
+ * Refresh the list of {@link LocalReaderBook}s from disk.
+ *
+ */
+ private void refreshBooks() {
+ for (LocalReaderGroup group : booksByType.keySet()) {
+ List<MetaData> stories = Instance.getLibrary().getListBySource(
+ booksByType.get(group));
+ group.refreshBooks(stories, words);
+ }
+
+ for (LocalReaderGroup group : booksByAuthor.keySet()) {
+ List<MetaData> stories = Instance.getLibrary().getListByAuthor(
+ booksByAuthor.get(group));
+ group.refreshBooks(stories, words);
+ }
+
+ pane.repaint();
+ this.repaint();
+ }
+
+ /**
+ * Create the main menu bar.
+ *
+ * @return the bar
+ */
+ private JMenuBar createMenu() {
+ bar = new JMenuBar();
+
+ JMenu file = new JMenu("File");
+ file.setMnemonic(KeyEvent.VK_F);
+
+ JMenuItem imprt = new JMenuItem("Import URL...", KeyEvent.VK_U);
+ imprt.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ imprt(true);
+ }
+ });
+ JMenuItem imprtF = new JMenuItem("Import File...", KeyEvent.VK_F);
+ imprtF.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ imprt(false);
+ }
+ });
+ JMenuItem exit = new JMenuItem("Exit", KeyEvent.VK_X);
+ exit.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ LocalReaderFrame.this.dispatchEvent(new WindowEvent(
+ LocalReaderFrame.this, WindowEvent.WINDOW_CLOSING));
+ }
+ });
+
+ file.add(createMenuItemOpenBook());
+ file.add(createMenuItemExport());
+ file.add(createMenuItemMove());
+ file.addSeparator();
+ file.add(imprt);
+ file.add(imprtF);
+ file.addSeparator();
+ file.add(exit);
+
+ bar.add(file);
+
+ JMenu edit = new JMenu("Edit");
+ edit.setMnemonic(KeyEvent.VK_E);
+
+ edit.add(createMenuItemClearCache());
+ edit.add(createMenuItemRedownload());
+ edit.addSeparator();
+ edit.add(createMenuItemDelete());
+
+ bar.add(edit);
+
+ JMenu view = new JMenu("View");
+ view.setMnemonic(KeyEvent.VK_V);
+ JMenuItem vauthors = new JMenuItem("Author");
+ vauthors.setMnemonic(KeyEvent.VK_A);
+ vauthors.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ words = false;
+ refreshBooks();
+ }
+ });
+ view.add(vauthors);
+ JMenuItem vwords = new JMenuItem("Word count");
+ vwords.setMnemonic(KeyEvent.VK_W);
+ vwords.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ words = true;
+ refreshBooks();
+ }
+ });
+ view.add(vwords);
+ bar.add(view);
+
+ JMenu sources = new JMenu("Sources");
+ sources.setMnemonic(KeyEvent.VK_S);
+
+ List<String> tt = Instance.getLibrary().getSources();
+ tt.add(0, null);
+ for (final String type : tt) {
+ JMenuItem item = new JMenuItem(type == null ? "All" : type);
+ item.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ removeBookPanes();
+ addBookPane(type, true);
+ refreshBooks();
+ }
+ });
+ sources.add(item);
+
+ if (type == null) {
+ sources.addSeparator();
+ }
+ }
+
+ bar.add(sources);
+
+ JMenu authors = new JMenu("Authors");
+ authors.setMnemonic(KeyEvent.VK_A);
+
+ List<String> aa = Instance.getLibrary().getAuthors();
+ aa.add(0, null);
+ for (final String author : aa) {
+ JMenuItem item = new JMenuItem(author == null ? "All"
+ : author.isEmpty() ? "[unknown]" : author);
+ item.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ removeBookPanes();
+ addBookPane(author, false);
+ refreshBooks();
+ }
+ });
+ authors.add(item);
+
+ if (author == null || author.isEmpty()) {
+ authors.addSeparator();
+ }
+ }
+
+ bar.add(authors);
+
+ JMenu options = new JMenu("Options");
+ options.setMnemonic(KeyEvent.VK_O);
+ options.add(createMenuItemConfig());
+ options.add(createMenuItemUiConfig());
+ bar.add(options);
+
+ return bar;
+ }
+
+ /**
+ * Create the Fanfix Configuration menu item.
+ *
+ * @return the item
+ */
+ private JMenuItem createMenuItemConfig() {
+ final String title = "Fanfix Configuration";
+ JMenuItem item = new JMenuItem(title);
+ item.setMnemonic(KeyEvent.VK_F);
+
+ item.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ ConfigEditor<Config> ed = new ConfigEditor<Config>(
+ Config.class, Instance.getConfig(),
+ "This is where you configure the options of the program.");
+ JFrame frame = new JFrame(title);
+ frame.add(ed);
+ frame.setSize(800, 600);
+ frame.setVisible(true);
+ }
+ });
+
+ return item;
+ }
+
+ /**
+ * Create the UI Configuration menu item.
+ *
+ * @return the item
+ */
+ private JMenuItem createMenuItemUiConfig() {
+ final String title = "UI Configuration";
+ JMenuItem item = new JMenuItem(title);
+ item.setMnemonic(KeyEvent.VK_U);
+
+ item.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ ConfigEditor<UiConfig> ed = new ConfigEditor<UiConfig>(
+ UiConfig.class, Instance.getUiConfig(),
+ "This is where you configure the graphical appearence of the program.");
+ JFrame frame = new JFrame(title);
+ frame.add(ed);
+ frame.setSize(800, 600);
+ frame.setVisible(true);
+ }
+ });
+
+ return item;
+ }
+
+ /**
+ * Create the export menu item.
+ *
+ * @return the item
+ */
+ private JMenuItem createMenuItemExport() {
+ final JFileChooser fc = new JFileChooser();
+ fc.setAcceptAllFileFilterUsed(false);
+
+ final Map<FileFilter, OutputType> filters = new HashMap<FileFilter, OutputType>();
+ for (OutputType type : OutputType.values()) {
+ String ext = type.getDefaultExtension(false);
+ String desc = type.getDesc(false);
+
+ if (ext == null || ext.isEmpty()) {
+ filters.put(createAllFilter(desc), type);
+ } else {
+ filters.put(new FileNameExtensionFilter(desc, ext), type);
+ }
+ }
+
+ // First the "ALL" filters, then, the extension filters
+ for (Entry<FileFilter, OutputType> entry : filters.entrySet()) {
+ if (!(entry.getKey() instanceof FileNameExtensionFilter)) {
+ fc.addChoosableFileFilter(entry.getKey());
+ }
+ }
+ for (Entry<FileFilter, OutputType> entry : filters.entrySet()) {
+ if (entry.getKey() instanceof FileNameExtensionFilter) {
+ fc.addChoosableFileFilter(entry.getKey());
+ }
+ }
+ //
+
+ JMenuItem export = new JMenuItem("Save as...", KeyEvent.VK_S);
+ export.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ if (selectedBook != null) {
+ fc.showDialog(LocalReaderFrame.this, "Save");
+ if (fc.getSelectedFile() != null) {
+ final OutputType type = filters.get(fc.getFileFilter());
+ final String path = fc.getSelectedFile()
+ .getAbsolutePath()
+ + type.getDefaultExtension(false);
+ final Progress pg = new Progress();
+ outOfUi(pg, new Runnable() {
+ public void run() {
+ try {
+ Instance.getLibrary().export(
+ selectedBook.getMeta().getLuid(),
+ type, path, pg);
+ } catch (IOException e) {
+ Instance.syserr(e);
+ }
+ }
+ });
+ }
+ }
+ }
+ });
+
+ return export;
+ }
+
+ /**
+ * Create a {@link FileFilter} that accepts all files and return the given
+ * description.
+ *
+ * @param desc
+ * the description
+ *
+ * @return the filter
+ */
+ private FileFilter createAllFilter(final String desc) {
+ return new FileFilter() {
+ @Override
+ public String getDescription() {
+ return desc;
+ }
+
+ @Override
+ public boolean accept(File f) {
+ return true;
+ }
+ };
+ }
+
+ /**
+ * Create the refresh (delete cache) menu item.
+ *
+ * @return the item
+ */
+ private JMenuItem createMenuItemClearCache() {
+ JMenuItem refresh = new JMenuItem("Clear cache", KeyEvent.VK_C);
+ refresh.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ if (selectedBook != null) {
+ outOfUi(null, new Runnable() {
+ public void run() {
+ reader.clearLocalReaderCache(selectedBook.getMeta()
+ .getLuid());
+ selectedBook.setCached(false);
+ SwingUtilities.invokeLater(new Runnable() {
+ public void run() {
+ selectedBook.repaint();
+ }
+ });
+ }
+ });
+ }
+ }
+ });
+
+ return refresh;
+ }
+
+ /**
+ * Create the delete menu item.
+ *
+ * @return the item
+ */
+ private JMenuItem createMenuItemMove() {
+ JMenu moveTo = new JMenu("Move to...");
+ moveTo.setMnemonic(KeyEvent.VK_M);
+
+ List<String> types = new ArrayList<String>();
+ types.add(null);
+ types.addAll(Instance.getLibrary().getSources());
+
+ for (String type : types) {
+ JMenuItem item = new JMenuItem(type == null ? "New type..." : type);
+
+ moveTo.add(item);
+ if (type == null) {
+ moveTo.addSeparator();
+ }
+
+ final String ftype = type;
+ item.addActionListener(new ActionListener() {