From 94833478359db02471755ad0e8ee3be05c3393fe Mon Sep 17 00:00:00 2001 From: gmungoc Date: Tue, 28 Nov 2017 10:10:49 +0000 Subject: [PATCH] JAL-2087 workaround for IllegalStateException --- test/jalview/ext/ensembl/EnsemblSpeciesDemo.java | 51 ++++++++++++++++++---- 1 file changed, 42 insertions(+), 9 deletions(-) diff --git a/test/jalview/ext/ensembl/EnsemblSpeciesDemo.java b/test/jalview/ext/ensembl/EnsemblSpeciesDemo.java index 456783b..c4d41da 100644 --- a/test/jalview/ext/ensembl/EnsemblSpeciesDemo.java +++ b/test/jalview/ext/ensembl/EnsemblSpeciesDemo.java @@ -4,7 +4,8 @@ import jalview.ext.ensembl.EnsemblSpecies.EnsemblTaxon; import java.awt.Dimension; import java.awt.FlowLayout; -import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; import java.util.List; import javax.swing.DefaultComboBoxModel; @@ -13,6 +14,7 @@ import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; +import javax.swing.SwingUtilities; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import javax.swing.text.AbstractDocument; @@ -20,6 +22,17 @@ import javax.swing.text.JTextComponent; public class EnsemblSpeciesDemo { + private boolean updating; + + private Comparator byDisplayName = new Comparator() + { + @Override + public int compare(EnsemblTaxon o1, EnsemblTaxon o2) + { + return o1.displayName.compareTo(o2.displayName); + } + }; + /** * Main method may be run interactively to explore a dynamic drop-down list that * populates with matches of Ensembl taxon ids, names or aliases @@ -35,7 +48,7 @@ public class EnsemblSpeciesDemo @Override public void run() { - createAndShowGUI(); + new EnsemblSpeciesDemo().createAndShowGUI(); } }); } @@ -44,7 +57,7 @@ public class EnsemblSpeciesDemo * Create the GUI and show it. For thread safety, this method should be invoked * from the event dispatch thread. */ - private static void createAndShowGUI() + private void createAndShowGUI() { JFrame frame = new JFrame("Taxon drop-down demo"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); @@ -94,21 +107,41 @@ public class EnsemblSpeciesDemo * @param combo * @param document */ - protected static void refreshComboList(JComboBox combo, AbstractDocument document) + protected void refreshComboList(JComboBox combo, + AbstractDocument document) { + if (updating) + { + return; + } + String typed = (String) combo.getEditor().getItem(); - if (typed.length() > 1) + if (typed.length() > 0) { + updating = true; List matches = EnsemblSpecies.getSpecies(true) .getNameMatches(typed); - String[] items = new String[matches.size()]; - int i = 0; + Collections.sort(matches, byDisplayName); + String[] items = new String[matches.size() + 1]; + items[0] = typed; + int i = 1; for (EnsemblTaxon m : matches) { items[i++] = String.format("%s (%s)", m.displayName, m.ncbiId); } - Arrays.sort(items, String.CASE_INSENSITIVE_ORDER); - combo.setModel(new DefaultComboBoxModel<>(items)); + + /* + * can't update while handling a notification + */ + SwingUtilities.invokeLater(new Runnable() + { + @Override + public void run() + { + combo.setModel(new DefaultComboBoxModel<>(items)); + updating = false; + } + }); } } } -- 1.7.10.2