package be.nikiroo.fanfix_swing;
-import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Window;
import java.io.File;
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;
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,
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<File, Void> worker = new SwingWorker<File, Void>() {
+ new SwingWorker<File, Void>() {
private File target;
private Story story;
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);
"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();
}
/**
--- /dev/null
+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<ImageIcon, Void>() {
+ @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);
+ }
+}