From e3fef8b566048567da28affadadc66e425561840 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Fri, 10 Apr 2020 17:19:00 +0200 Subject: [PATCH] importer frame: step 1 --- src/be/nikiroo/fanfix_swing/Actions.java | 98 ++++--------------- .../fanfix_swing/gui/ImporterFrame.java | 85 ++++++++++++++++ .../nikiroo/fanfix_swing/gui/MainFrame.java | 7 +- 3 files changed, 109 insertions(+), 81 deletions(-) create mode 100644 src/be/nikiroo/fanfix_swing/gui/ImporterFrame.java diff --git a/src/be/nikiroo/fanfix_swing/Actions.java b/src/be/nikiroo/fanfix_swing/Actions.java index 10c79dd..ec30d5e 100644 --- a/src/be/nikiroo/fanfix_swing/Actions.java +++ b/src/be/nikiroo/fanfix_swing/Actions.java @@ -2,18 +2,14 @@ package be.nikiroo.fanfix_swing; import java.awt.BorderLayout; import java.awt.Container; -import java.awt.Toolkit; import java.awt.Window; -import java.awt.datatransfer.DataFlavor; import java.io.File; import java.io.IOException; import java.net.URL; import java.net.UnknownHostException; import javax.swing.JDialog; -import javax.swing.JFileChooser; import javax.swing.JLabel; -import javax.swing.JOptionPane; import javax.swing.SwingWorker; import be.nikiroo.fanfix.Instance; @@ -183,90 +179,41 @@ public class Actions { } } - /** - * 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} - */ - static public void imprt(final Container parent, boolean askUrl, - final Runnable onSuccess) { - 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.startsWith("https://"))) { - clipboard = ""; - } - - url = JOptionPane.showInputDialog(parent, - Instance.getInstance().getTransGui() - .getString(StringIdGui.SUBTITLE_IMPORT_URL), - Instance.getInstance().getTransGui() - .getString(StringIdGui.TITLE_IMPORT_URL), - JOptionPane.QUESTION_MESSAGE, null, null, clipboard); - } else if (fc.showOpenDialog(parent) != JFileChooser.CANCEL_OPTION) { - url = fc.getSelectedFile().getAbsolutePath(); - } else { - url = null; - } - - if (url != null && !url.toString().isEmpty()) { - imprt(parent, url.toString(), null, null); - } - } - /** * Actually import the {@link Story} into the main {@link LocalLibrary}. *

- * Should be called inside the UI thread. + * Should be called inside the UI thread, will start a worker (i.e., this is + * asynchronous). * + * @param parent + * a container we can use to show error messages if any * @param url * the {@link Story} to import by {@link URL} + * @param pg + * the optional progress reporter * @param onSuccess * Action to execute on success - * @param onSuccessPgName - * the name to use for the onSuccess progress bar */ static public void imprt(final Container parent, 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); - + final Progress pg, final Runnable onSuccess) { + final Progress fpg = pg; new SwingWorker() { @Override protected Void doInBackground() throws Exception { - Exception ex = null; - MetaData meta = null; - try { - meta = Instance.getInstance().getLibrary() - .imprt(BasicReader.getUrl(url), pgImprt); - } catch (IOException e) { - e.printStackTrace(); - ex = e; - } - - final Exception e = ex; + Progress pg = fpg; + if (pg == null) + pg = new Progress(); - final boolean ok = (e == null); + try { + Instance.getInstance().getLibrary() + .imprt(BasicReader.getUrl(url), fpg); - pgOnSuccess.setProgress(0); - if (!ok) { + fpg.done(); + if (onSuccess != null) { + onSuccess.run(); + } + } catch (IOException e) { + fpg.done(); if (e instanceof UnknownHostException) { UiHelper.error(parent, Instance.getInstance().getTransGui().getString( @@ -284,12 +231,7 @@ public class Actions { .getString(StringIdGui.TITLE_ERROR), e); } - } else { - if (onSuccess != null) { - onSuccess.run(); - } } - pgOnSuccess.done(); return null; } diff --git a/src/be/nikiroo/fanfix_swing/gui/ImporterFrame.java b/src/be/nikiroo/fanfix_swing/gui/ImporterFrame.java new file mode 100644 index 0000000..d4ab526 --- /dev/null +++ b/src/be/nikiroo/fanfix_swing/gui/ImporterFrame.java @@ -0,0 +1,85 @@ +package be.nikiroo.fanfix_swing.gui; + +import java.awt.Container; +import java.awt.Toolkit; +import java.awt.datatransfer.DataFlavor; +import java.io.File; +import java.net.URL; + +import javax.swing.JFileChooser; +import javax.swing.JFrame; +import javax.swing.JOptionPane; + +import be.nikiroo.fanfix.Instance; +import be.nikiroo.fanfix.bundles.StringIdGui; +import be.nikiroo.fanfix.library.LocalLibrary; +import be.nikiroo.fanfix_swing.Actions; +import be.nikiroo.utils.Progress; + +public class ImporterFrame extends JFrame { + public ImporterFrame() { + + } + + /** + * Ask for and import an {@link URL} into the main {@link LocalLibrary}. + *

+ * Should be called inside the UI thread. + * + * @param parent + * a container we can use to display the {@link URL} chooser and + * to show error messages if any + * @param onSuccess + * Action to execute on success + */ + public void imprtUrl(final Container parent, final Runnable onSuccess) { + 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.startsWith("https://"))) { + clipboard = ""; + } + + Object url = JOptionPane.showInputDialog(parent, + Instance.getInstance().getTransGui() + .getString(StringIdGui.SUBTITLE_IMPORT_URL), + Instance.getInstance().getTransGui() + .getString(StringIdGui.TITLE_IMPORT_URL), + JOptionPane.QUESTION_MESSAGE, null, null, clipboard); + + Progress pg = null; + if (url != null && !url.toString().isEmpty()) { + Actions.imprt(parent, url.toString(), pg, onSuccess); + } + } + + /** + * Ask for and import a {@link File} into the main {@link LocalLibrary}. + *

+ * Should be called inside the UI thread. + * + * @param parent + * a container we can use to display the {@link File} chooser and + * to show error messages if any + * @param onSuccess + * Action to execute on success + */ + + public void imprtFile(final Container parent, final Runnable onSuccess) { + JFileChooser fc = new JFileChooser(); + + Progress pg = null; + if (fc.showOpenDialog(parent) != JFileChooser.CANCEL_OPTION) { + Object url = fc.getSelectedFile().getAbsolutePath(); + if (url != null && !url.toString().isEmpty()) { + Actions.imprt(parent, url.toString(), pg, onSuccess); + } + } + } +} diff --git a/src/be/nikiroo/fanfix_swing/gui/MainFrame.java b/src/be/nikiroo/fanfix_swing/gui/MainFrame.java index d580c61..313f086 100644 --- a/src/be/nikiroo/fanfix_swing/gui/MainFrame.java +++ b/src/be/nikiroo/fanfix_swing/gui/MainFrame.java @@ -12,7 +12,6 @@ import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JSplitPane; -import be.nikiroo.fanfix_swing.Actions; import be.nikiroo.utils.Version; public class MainFrame extends JFrame { @@ -95,7 +94,8 @@ public class MainFrame extends JFrame { item1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - Actions.imprt(MainFrame.this, true, new Runnable() { + // TODO: correctly use the importer (wip) + new ImporterFrame().imprtUrl(MainFrame.this, new Runnable() { @Override public void run() { browser.reloadData(); @@ -112,7 +112,8 @@ public class MainFrame extends JFrame { item2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - Actions.imprt(MainFrame.this, false, new Runnable() { + // TODO: correctly use the importer (wip) + new ImporterFrame().imprtFile(MainFrame.this, new Runnable() { @Override public void run() { browser.reloadData(); -- 2.27.0