ImporterPanel: scroll + do not clear fail items + fail items and color
authorNiki Roo <niki@nikiroo.be>
Sun, 3 May 2020 09:21:09 +0000 (11:21 +0200)
committerNiki Roo <niki@nikiroo.be>
Sun, 3 May 2020 09:21:09 +0000 (11:21 +0200)
src/be/nikiroo/fanfix_swing/Actions.java
src/be/nikiroo/fanfix_swing/gui/importer/ImporterFrame.java
src/be/nikiroo/fanfix_swing/gui/importer/ImporterItem.java

index c7be0f541883968b17a5fe8786435898d089b3ed..a4b57708524bed3d8e86fdf048ed8e3678d5a454 100644 (file)
@@ -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<Void, Void>() {
                        @Override
index fed028020020690d45d6672ba768dfa0ede2e3a3..bce1bcf732ba49be5cd32a95cc1af699eae82a8f 100644 (file)
@@ -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<ImporterItem> 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<ImporterItem> list = new JList6<ImporterItem>();
                data = new ListModel<ImporterItem>(list);
@@ -75,7 +77,7 @@ public class ImporterFrame extends JFrame implements ListenerItem {
                                                .removeItemIf(new Predicate<ImporterItem>() {
                                                        @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() {
index ad44c948085c8b8eb023afad396950fe60f04dfd..7ac8384db8f96711bd1dceafb4b5a1d1c2bfd318 100644 (file)
@@ -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)) {