From c8ac6b9f2d0d0a270e61c65a2647e4b5169e864e Mon Sep 17 00:00:00 2001 From: drdev Date: Sat, 4 Jan 2014 16:27:24 +0000 Subject: [PATCH] Prevent ListChooser getting caught in infinite loop of reprompting --- .../src/main/java/forge/gui/ListChooser.java | 38 +++++++++---------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/forge-gui/src/main/java/forge/gui/ListChooser.java b/forge-gui/src/main/java/forge/gui/ListChooser.java index 4fa59f7104b..5bae5f18bc6 100644 --- a/forge-gui/src/main/java/forge/gui/ListChooser.java +++ b/forge-gui/src/main/java/forge/gui/ListChooser.java @@ -103,9 +103,24 @@ public class ListChooser { this.lstChoices.setCellRenderer(new TransformedCellRenderer(display)); } - this.optionPane = new FOptionPane(null, title, null, new JScrollPane(this.lstChoices), options, 0); + this.optionPane = new FOptionPane(null, title, null, new JScrollPane(this.lstChoices), options, -1); this.optionPane.setButtonEnabled(0, minChoices == 0); + this.optionPane.addWindowFocusListener(new WindowFocusListener() { + @Override + public void windowGainedFocus(final WindowEvent e) { + ListChooser.this.lstChoices.grabFocus(); + } + + @Override + public void windowLostFocus(final WindowEvent e) { + } + }); + + if (this.minChoices != 0) { + this.optionPane.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); + } + this.lstChoices.getSelectionModel().addListSelectionListener(new SelListener()); this.lstChoices.addKeyListener(new KeyAdapter() { @Override @@ -152,40 +167,23 @@ public class ListChooser { } int result; do { - if (this.minChoices != 0) { - this.optionPane.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); - } - if (list.contains(item)) { lstChoices.setSelectedValue(item, true); } else { lstChoices.setSelectedIndex(0); } - - this.optionPane.addWindowFocusListener(new WindowFocusListener() { - @Override - public void windowGainedFocus(final WindowEvent e) { - ListChooser.this.lstChoices.grabFocus(); - } - - @Override - public void windowLostFocus(final WindowEvent e) { - } - }); this.optionPane.setVisible(true); result = this.optionPane.getResult(); if (result != 0) { this.lstChoices.clearSelection(); - break; } // can't stop closing by ESC, so repeat if cancelled - } while (this.minChoices != 0); + } while (this.minChoices != 0 && result != 0); this.optionPane.dispose(); - // this assert checks if we really don't return on a cancel if input is - // mandatory + // this assert checks if we really don't return on a cancel if input is mandatory assert (this.minChoices == 0) || (result == 0); this.called = true; return (result == 0);