1 package jalview.ext.ensembl;
3 import jalview.ext.ensembl.EnsemblSpecies.EnsemblTaxon;
5 import java.awt.Dimension;
6 import java.awt.FlowLayout;
7 import java.util.Arrays;
10 import javax.swing.DefaultComboBoxModel;
11 import javax.swing.JComboBox;
12 import javax.swing.JComponent;
13 import javax.swing.JFrame;
14 import javax.swing.JLabel;
15 import javax.swing.JPanel;
16 import javax.swing.event.DocumentEvent;
17 import javax.swing.event.DocumentListener;
18 import javax.swing.text.AbstractDocument;
19 import javax.swing.text.JTextComponent;
21 public class EnsemblSpeciesDemo
24 * Main method may be run interactively to explore a dynamic drop-down list that
25 * populates with matches of Ensembl taxon ids, names or aliases
29 public static void main(String[] args)
31 // Schedule a job for the event dispatch thread:
32 // creating and showing this application's GUI.
33 javax.swing.SwingUtilities.invokeLater(new Runnable()
44 * Create the GUI and show it. For thread safety, this method should be invoked
45 * from the event dispatch thread.
47 private static void createAndShowGUI()
49 JFrame frame = new JFrame("Taxon drop-down demo");
50 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
52 JComponent newContentPane = new JPanel(new FlowLayout());
53 newContentPane.setOpaque(true);
54 newContentPane.setPreferredSize(new Dimension(400, 300));
55 frame.setContentPane(newContentPane);
57 JLabel label = new JLabel("Taxon:");
58 newContentPane.add(label);
60 JComboBox<String> combo = new JComboBox<>();
61 combo.setEditable(true);
62 combo.setPreferredSize(new Dimension(200, 20));
63 newContentPane.add(combo);
64 AbstractDocument document = (AbstractDocument) ((JTextComponent) combo
65 .getEditor().getEditorComponent()).getDocument();
66 document.addDocumentListener(new DocumentListener()
69 public void insertUpdate(DocumentEvent e)
71 refreshComboList(combo, document);
75 public void removeUpdate(DocumentEvent e)
77 refreshComboList(combo, document);
81 public void changedUpdate(DocumentEvent e)
87 frame.setVisible(true);
91 * Refreshes the combo box list to contain what the user has typed, plus any
92 * matches for Ensembl taxon id, name or alias
97 protected static void refreshComboList(JComboBox<String> combo, AbstractDocument document)
99 String typed = (String) combo.getEditor().getItem();
100 if (typed.length() > 1)
102 List<EnsemblTaxon> matches = EnsemblSpecies.getSpecies(true)
103 .getNameMatches(typed);
104 String[] items = new String[matches.size()];
106 for (EnsemblTaxon m : matches)
108 items[i++] = String.format("%s (%s)", m.displayName, m.ncbiId);
110 Arrays.sort(items, String.CASE_INSENSITIVE_ORDER);
111 combo.setModel(new DefaultComboBoxModel<>(items));