- /**
- * 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(), pg);
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- book.setCached(true);
- }
- });
- } catch (IOException e) {
- // TODO: error message?
- Instance.getTraceHandler().error(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 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}.
- * <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(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);
- }
- }
-
- /**
- * Actually import the {@link Story} into the main {@link LocalLibrary}.
- * <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 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() {
- @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;
-
- final boolean ok = (e == null);
-
- 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);
- }
- }
- pgOnSuccess.done();
- }
- });
- }
-
- /**
- * 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) {
- 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();
- }
-