+ }, "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);
+ }
+ });