+
+ /**
+ * 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 redownload (then delete original) menu item.
+ *
+ * @return the item
+ */
+ private JMenuItem createMenuItemRedownload() {
+ JMenuItem refresh = new JMenuItem("Redownload", KeyEvent.VK_R);
+ refresh.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ if (selectedBook != null) {
+ final MetaData meta = selectedBook.getMeta();
+ imprt(meta.getUrl(), new Runnable() {
+ public void run() {
+ reader.delete(meta.getLuid());
+ LocalReaderFrame.this.selectedBook = null;
+ }
+ }, "Removing old copy");
+ }
+ }
+ });
+
+ return refresh;
+ }
+
+ /**
+ * Create the delete menu item.
+ *
+ * @return the item
+ */
+ private JMenuItem createMenuItemDelete() {
+ JMenuItem delete = new JMenuItem("Delete", KeyEvent.VK_D);
+ delete.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ if (selectedBook != null) {
+ outOfUi(null, new Runnable() {
+ public void run() {
+ reader.delete(selectedBook.getMeta().getLuid());
+ selectedBook = null;
+ }
+ });
+ }
+ }
+ });
+
+ return delete;
+ }
+
+ /**
+ * Create the open menu item.
+ *
+ * @return the item
+ */
+ private JMenuItem createMenuItemOpenBook() {
+ JMenuItem open = new JMenuItem("Open", KeyEvent.VK_O);
+ open.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ if (selectedBook != null) {
+ openBook(selectedBook);
+ }
+ }
+ });
+
+ return open;
+ }
+
+ /**
+ * Open a {@link LocalReaderBook} item.
+ *
+ * @param book
+ * the {@link LocalReaderBook} to open
+ */
+ private void openBook(final LocalReaderBook book) {
+ final Progress pg = new Progress();
+ outOfUi(pg, new Runnable() {
+ public void run() {
+ try {
+ reader.open(book.getMeta().getLuid(), pg);
+ SwingUtilities.invokeLater(new Runnable() {
+ public void run() {
+ book.setCached(true);
+ }
+ });
+ } catch (IOException e) {
+ // TODO: error message?
+ Instance.syserr(e);
+ }
+ }
+ });
+ }
+
+ /**
+ * Process the given action out of the Swing UI thread and link the given
+ * {@link ProgressBar} to the action.
+ * <p>
+ * The code will make sure that the {@link ProgressBar} (if not NULL) is set
+ * to done when the action is done.
+ *
+ * @param pg
+ * 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() {
+ public void run() {
+ run.run();
+ refreshBooks();
+ reload.setProgress(100);
+ if (!pg.isDone()) {
+ // will trigger pgBar ActionListener:
+ pg.setProgress(pg.getMax());
+ }
+ }
+ }, "outOfUi thread").start();
+ }
+
+ /**
+ * Import a {@link Story} into the main {@link Library}.
+ * <p>
+ * 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(LocalReaderFrame.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);
+ }
+ }
+
+ /**
+ * Actually import the {@link Story} into the main {@link Library}.
+ * <p>
+ * Should be called inside the UI thread.
+ *
+ * @param url
+ * the {@link Story} to import by {@link URL}
+ * @param onSuccess
+ * Action to execute on success
+ */
+ private void imprt(final String url, final Runnable 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() {
+ public void run() {
+ Exception ex = null;
+ try {
+ Instance.getLibrary().imprt(BasicReader.getUrl(url),
+ pgImprt);
+ } catch (IOException e) {
+ ex = e;
+ }
+
+ final Exception e = ex;
+
+ final boolean ok = (e == null);
+
+ pgOnSuccess.setProgress(0);
+ if (!ok) {
+ Instance.syserr(e);
+ SwingUtilities.invokeLater(new Runnable() {
+ public void run() {
+ JOptionPane.showMessageDialog(
+ LocalReaderFrame.this, "Cannot import: "
+ + url, e.getMessage(),
+ JOptionPane.ERROR_MESSAGE);
+ }
+ });
+ } else {
+ if (onSuccess != null) {
+ onSuccess.run();
+ }
+ }
+ pgOnSuccess.setProgress(100);
+ }
+ });
+ }
+
+ /**
+ * Enables or disables this component, depending on the value of the
+ * parameter <code>b</code>. An enabled component can respond to user input
+ * and generate events. Components are enabled initially by default.
+ * <p>
+ * Disabling this component will also affect its children.
+ *
+ * @param b
+ * If <code>true</code>, this component is enabled; otherwise
+ * this component is disabled
+ */
+ @Override
+ public void setEnabled(boolean b) {
+ bar.setEnabled(b);
+ for (LocalReaderGroup group : booksByType.keySet()) {
+ group.setEnabled(b);
+ }
+ for (LocalReaderGroup group : booksByAuthor.keySet()) {
+ group.setEnabled(b);
+ }
+ super.setEnabled(b);
+ repaint();
+ }