From b597d7a8b790bbfd9c91f840dc2fb0aa0f857c03 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Mon, 4 May 2020 14:50:04 +0200 Subject: [PATCH] waiting dtalog meta --- src/be/nikiroo/fanfix_swing/Actions.java | 77 ++++------------ .../gui/utils/WaitingDialogMeta.java | 90 +++++++++++++++++++ 2 files changed, 107 insertions(+), 60 deletions(-) create mode 100644 src/be/nikiroo/fanfix_swing/gui/utils/WaitingDialogMeta.java diff --git a/src/be/nikiroo/fanfix_swing/Actions.java b/src/be/nikiroo/fanfix_swing/Actions.java index d439f384..b1c73dbe 100644 --- a/src/be/nikiroo/fanfix_swing/Actions.java +++ b/src/be/nikiroo/fanfix_swing/Actions.java @@ -1,6 +1,5 @@ package be.nikiroo.fanfix_swing; -import java.awt.BorderLayout; import java.awt.Container; import java.awt.Window; import java.io.File; @@ -11,9 +10,6 @@ import java.net.UnknownHostException; import java.util.LinkedHashMap; import java.util.Map; -import javax.swing.ImageIcon; -import javax.swing.JDialog; -import javax.swing.JLabel; import javax.swing.SwingWorker; import be.nikiroo.fanfix.Instance; @@ -23,13 +19,13 @@ import be.nikiroo.fanfix.data.MetaData; import be.nikiroo.fanfix.data.Story; import be.nikiroo.fanfix.library.BasicLibrary; import be.nikiroo.fanfix.library.LocalLibrary; -import be.nikiroo.fanfix_swing.gui.book.BookInfo; -import be.nikiroo.fanfix_swing.gui.utils.CoverImager; import be.nikiroo.fanfix_swing.gui.utils.UiHelper; +import be.nikiroo.fanfix_swing.gui.utils.WaitingDialogMeta; import be.nikiroo.fanfix_swing.gui.viewer.ViewerImages; import be.nikiroo.fanfix_swing.gui.viewer.ViewerNonImages; import be.nikiroo.utils.Progress; import be.nikiroo.utils.StringUtils; +import be.nikiroo.utils.ui.WaitingDialog; public class Actions { static public void openBook(final BasicLibrary lib, MetaData meta, @@ -39,44 +35,21 @@ public class Actions { parentWindow = parentWindow.getParent(); } - // TODO: UI - final JDialog wait = new JDialog((Window) parentWindow); - wait.setTitle(meta.getTitle()); - // Image - ImageIcon img = new ImageIcon(CoverImager.generateCoverImage(lib, - BookInfo.fromMeta(lib, meta))); - wait.setLayout(new BorderLayout()); - wait.add(new JLabel("Opening " + meta.getTitle() + "..."), - BorderLayout.NORTH); - wait.add(new JLabel(img), BorderLayout.CENTER); + final WaitingDialog wait = new WaitingDialogMeta((Window) parentWindow, + lib, meta); - wait.setSize(400, 300); + boolean internalImg = Instance.getInstance().getUiConfig() + .getBoolean(UiConfig.IMAGES_DOCUMENT_USE_INTERNAL_READER, true); + boolean internalNonImg = Instance.getInstance().getUiConfig() + .getBoolean(UiConfig.NON_IMAGES_DOCUMENT_USE_INTERNAL_READER, + true); - // TODO: pg? - - final Object waitLock = new Object(); - final Boolean[] waitScreen = new Boolean[] { false }; - new Thread(new Runnable() { - @Override - public void run() { - try { - Thread.sleep(200); - } catch (InterruptedException e) { - } - - synchronized (waitLock) { - if (!waitScreen[0]) { - waitScreen[0] = true; - wait.setVisible(true); - } - } - } - }).start(); - - final String luid = meta.getLuid(); final boolean isImageDocument = meta.isImageDocument(); + final boolean internalReader = (isImageDocument && internalImg + || !isImageDocument && internalNonImg); + final String luid = meta.getLuid(); - final SwingWorker worker = new SwingWorker() { + new SwingWorker() { private File target; private Story story; @@ -91,17 +64,7 @@ public class Actions { protected void done() { try { get(); - boolean internalImg = Instance.getInstance().getUiConfig() - .getBoolean( - UiConfig.IMAGES_DOCUMENT_USE_INTERNAL_READER, - true); - boolean internalNonImg = Instance.getInstance() - .getUiConfig().getBoolean( - UiConfig.NON_IMAGES_DOCUMENT_USE_INTERNAL_READER, - true); - - if (isImageDocument && internalImg - || !isImageDocument && internalNonImg) { + if (internalReader) { openInternal(story); } else { openExternal(target, isImageDocument); @@ -112,20 +75,14 @@ public class Actions { "Cannot open the story", e); } - synchronized (waitLock) { - if (waitScreen[0]) { - wait.setVisible(false); - } - waitScreen[0] = true; - } + // Dismiss the waiting screen if needed + wait.dismiss(); if (onDone != null) { onDone.run(); } } - }; - - worker.execute(); + }.execute(); } /** diff --git a/src/be/nikiroo/fanfix_swing/gui/utils/WaitingDialogMeta.java b/src/be/nikiroo/fanfix_swing/gui/utils/WaitingDialogMeta.java new file mode 100644 index 00000000..afda5684 --- /dev/null +++ b/src/be/nikiroo/fanfix_swing/gui/utils/WaitingDialogMeta.java @@ -0,0 +1,90 @@ +package be.nikiroo.fanfix_swing.gui.utils; + +import java.awt.BorderLayout; +import java.awt.Dimension; +import java.awt.Window; + +import javax.swing.ImageIcon; +import javax.swing.JLabel; +import javax.swing.SwingWorker; + +import be.nikiroo.fanfix.data.MetaData; +import be.nikiroo.fanfix.library.BasicLibrary; +import be.nikiroo.fanfix_swing.gui.book.BookInfo; +import be.nikiroo.utils.Progress; +import be.nikiroo.utils.ui.WaitingDialog; + +/** + * A {@link WaitingDialog} tailored for opening {@link MetaData}. + * + * @author niki + */ +public class WaitingDialogMeta extends WaitingDialog { + private static final long serialVersionUID = 1L; + + private JLabel imgLabel; + + /** + * Create a new {@link WaitingDialogMeta}. + * + * @param parent + * the parent/owner + * @param lib + * the {@link BasicLibrary} with which to retrieve the cover + * image from the {@link MetaData} if needed + * @param meta + * the {@link MetaData} for which we wait + */ + public WaitingDialogMeta(Window parent, final BasicLibrary lib, + final MetaData meta) { + this(parent, lib, meta, null); + } + + /** + * Create a new {@link WaitingDialogMeta}. + * + * @param parent + * the parent/owner + * @param lib + * the {@link BasicLibrary} with which to retrieve the cover + * image from the {@link MetaData} if needed + * @param meta + * the {@link MetaData} for which we wait + * @param pg + * the progress to follow (can be NULL) + */ + public WaitingDialogMeta(Window parent, final BasicLibrary lib, + final MetaData meta, Progress pg) { + super(parent, 400, pg); + + // TODO fix ugly UI + + this.setLayout(new BorderLayout()); + this.setTitle(meta.getTitle()); + this.add(new JLabel("Waiting for " + meta.getTitle() + "..."), + BorderLayout.NORTH); + imgLabel = new JLabel(); + imgLabel.setPreferredSize(new Dimension(CoverImager.getCoverWidth(), + CoverImager.getCoverHeight())); + this.add(imgLabel, BorderLayout.CENTER); + + // Image + new SwingWorker() { + @Override + protected ImageIcon doInBackground() throws Exception { + return new ImageIcon(CoverImager.generateCoverImage(lib, + BookInfo.fromMeta(lib, meta))); + } + + @Override + public void done() { + try { + imgLabel.setIcon(get()); + } catch (Exception e) { + } + } + }.execute(); + + this.setSize(400, 300); + } +} -- 2.27.0