From 075ffbc9404ad5073a5a20e54e92505a7f03360f Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Sun, 3 May 2020 11:21:09 +0200 Subject: [PATCH] ImporterPanel: scroll + do not clear fail items + fail items and color --- src/be/nikiroo/fanfix_swing/Actions.java | 4 +- .../gui/importer/ImporterFrame.java | 38 ++++++++++---- .../gui/importer/ImporterItem.java | 49 +++++++++++++------ 3 files changed, 67 insertions(+), 24 deletions(-) diff --git a/src/be/nikiroo/fanfix_swing/Actions.java b/src/be/nikiroo/fanfix_swing/Actions.java index c7be0f54..a4b57708 100644 --- a/src/be/nikiroo/fanfix_swing/Actions.java +++ b/src/be/nikiroo/fanfix_swing/Actions.java @@ -235,9 +235,11 @@ public class Actions { * the optional progress reporter * @param onSuccess * Action to execute on success + * @param onFailure + * Action to execute on failure */ static public void imprt(final Container parent, final String url, - Progress pg, final Runnable onSuccess) { + Progress pg, final Runnable onSuccess, final Runnable onFailure) { final Progress fpg = pg; new SwingWorker() { @Override diff --git a/src/be/nikiroo/fanfix_swing/gui/importer/ImporterFrame.java b/src/be/nikiroo/fanfix_swing/gui/importer/ImporterFrame.java index fed02802..bce1bcf7 100644 --- a/src/be/nikiroo/fanfix_swing/gui/importer/ImporterFrame.java +++ b/src/be/nikiroo/fanfix_swing/gui/importer/ImporterFrame.java @@ -29,18 +29,20 @@ import be.nikiroo.utils.ui.ListModel; import be.nikiroo.utils.ui.ListModel.Predicate; import be.nikiroo.utils.ui.ListenerItem; import be.nikiroo.utils.ui.ListenerPanel; +import be.nikiroo.utils.ui.UIUtils; public class ImporterFrame extends JFrame implements ListenerItem { static public final String IMPORTED = "imported"; - private ListenerPanel root = new ListenerPanel(); + private ListenerPanel root; private ListModel data; private String filter = ""; public ImporterFrame() { + root = new ListenerPanel(); setLayout(new BorderLayout()); root.setLayout(new BorderLayout()); - this.add(root); + this.add(UIUtils.scroll(root, false)); JList6 list = new JList6(); data = new ListModel(list); @@ -75,7 +77,7 @@ public class ImporterFrame extends JFrame implements ListenerItem { .removeItemIf(new Predicate() { @Override public boolean test(ImporterItem item) { - return item.isDone(); + return item.isDone(false); } }); @@ -140,16 +142,24 @@ public class ImporterFrame extends JFrame implements ListenerItem { */ public void imprtFile(final Container parent) { + Progress pg = new Progress(); JFileChooser fc = new JFileChooser(); if (fc.showOpenDialog(parent) != JFileChooser.CANCEL_OPTION) { Object url = fc.getSelectedFile().getAbsolutePath(); if (url != null && !url.toString().isEmpty()) { - Progress pg = new Progress(); - add(pg, "File", fc.getSelectedFile().getName()); - + final ImporterItem item = add(pg, "File", + fc.getSelectedFile().getName()); Actions.imprt(parent, url.toString(), pg, new Runnable() { @Override public void run() { + item.setDone(true); + fireActionPerformed(IMPORTED); + } + }, new Runnable() { + @Override + public void run() { + item.setFailed(true); + item.setDone(true); fireActionPerformed(IMPORTED); } }); @@ -183,11 +193,18 @@ public class ImporterFrame extends JFrame implements ListenerItem { } if (url != null && !url.isEmpty()) { - add(pg, basename, null); - + final ImporterItem item = add(pg, basename, null); Actions.imprt(parent, url, pg, new Runnable() { @Override public void run() { + item.setDone(true); + fireActionPerformed(IMPORTED); + } + }, new Runnable() { + @Override + public void run() { + item.setFailed(true); + item.setDone(true); fireActionPerformed(IMPORTED); } }); @@ -196,7 +213,8 @@ public class ImporterFrame extends JFrame implements ListenerItem { } } - private void add(Progress pg, final String basename, String storyName) { + private ImporterItem add(Progress pg, final String basename, + String storyName) { final ImporterItem item = new ImporterItem(pg, basename, storyName); item.addActionListener(new ActionListener() { @Override @@ -207,6 +225,8 @@ public class ImporterFrame extends JFrame implements ListenerItem { data.addItem(item); filter(); + + return item; } private void filter() { diff --git a/src/be/nikiroo/fanfix_swing/gui/importer/ImporterItem.java b/src/be/nikiroo/fanfix_swing/gui/importer/ImporterItem.java index ad44c948..7ac8384d 100644 --- a/src/be/nikiroo/fanfix_swing/gui/importer/ImporterItem.java +++ b/src/be/nikiroo/fanfix_swing/gui/importer/ImporterItem.java @@ -27,6 +27,7 @@ public class ImporterItem extends ListenerPanel implements Hoverable { private boolean hovered; private boolean selected; private boolean done; + private boolean failed; private JLabel labelName; private JLabel labelAction; @@ -96,22 +97,42 @@ public class ImporterItem extends ListenerPanel implements Hoverable { } } - public boolean isDone() { - return done; + public boolean isDone(boolean acceptFailure) { + return done && (acceptFailure || !failed); } public void setDone(boolean done) { if (this.done != done) { this.done = done; - if (done) { - labelAction.setForeground(Color.green.darker()); - labelAction - .setFont(labelAction.getFont().deriveFont(Font.BOLD)); - } else { - labelAction.setForeground(Color.gray); - labelAction - .setFont(labelAction.getFont().deriveFont(Font.PLAIN)); - } + setHighlight(); + } + } + + public boolean isFailed() { + return failed; + } + + public void setFailed(boolean failed) { + if (this.failed != failed) { + this.failed = failed; + setHighlight(); + } + } + + private void setHighlight() { + Color highlight = null; + if (failed) { + highlight = Color.red.darker(); + } else if (done) { + highlight = Color.green.darker(); + } + + if (highlight != null) { + labelAction.setForeground(highlight); + labelAction.setFont(labelAction.getFont().deriveFont(Font.BOLD)); + } else { + labelAction.setForeground(Color.gray); + labelAction.setFont(labelAction.getFont().deriveFont(Font.PLAIN)); } } @@ -121,12 +142,12 @@ public class ImporterItem extends ListenerPanel implements Hoverable { public void progress(Progress notUsed, String currentAction) { currentAction = currentAction == null ? "" : currentAction; String currentStoryName = null; - - MetaData meta = (MetaData)pg.get("meta"); + + MetaData meta = (MetaData) pg.get("meta"); if (meta != null) { currentStoryName = meta.getTitle(); } - + if (pg.getRelativeProgress() != progress || !action.equals(currentAction) || !storyName.equals(currentStoryName)) { -- 2.27.0