JAL-2087 /info/species client, alias cache, type-assist demo
[jalview.git] / test / jalview / ext / ensembl / EnsemblSpeciesDemo.java
1 package jalview.ext.ensembl;
2
3 import jalview.ext.ensembl.EnsemblSpecies.EnsemblTaxon;
4
5 import java.awt.Dimension;
6 import java.awt.FlowLayout;
7 import java.util.Arrays;
8 import java.util.List;
9
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;
20
21 public class EnsemblSpeciesDemo
22 {
23   /**
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
26    * 
27    * @param args
28    */
29   public static void main(String[] args)
30   {
31     // Schedule a job for the event dispatch thread:
32     // creating and showing this application's GUI.
33     javax.swing.SwingUtilities.invokeLater(new Runnable()
34     {
35       @Override
36       public void run()
37       {
38         createAndShowGUI();
39       }
40     });
41   }
42
43   /**
44    * Create the GUI and show it. For thread safety, this method should be invoked
45    * from the event dispatch thread.
46    */
47   private static void createAndShowGUI()
48   {
49     JFrame frame = new JFrame("Taxon drop-down demo");
50     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
51
52     JComponent newContentPane = new JPanel(new FlowLayout());
53     newContentPane.setOpaque(true);
54     newContentPane.setPreferredSize(new Dimension(400, 300));
55     frame.setContentPane(newContentPane);
56
57     JLabel label = new JLabel("Taxon:");
58     newContentPane.add(label);
59
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()
67     {
68       @Override
69       public void insertUpdate(DocumentEvent e)
70       {
71         refreshComboList(combo, document);
72       }
73
74       @Override
75       public void removeUpdate(DocumentEvent e)
76       {
77         refreshComboList(combo, document);
78       }
79
80       @Override
81       public void changedUpdate(DocumentEvent e)
82       {
83       }
84     });
85
86     frame.pack();
87     frame.setVisible(true);
88   }
89
90   /**
91    * Refreshes the combo box list to contain what the user has typed, plus any
92    * matches for Ensembl taxon id, name or alias
93    * 
94    * @param combo
95    * @param document 
96    */
97   protected static void refreshComboList(JComboBox<String> combo, AbstractDocument document)
98   {
99     String typed = (String) combo.getEditor().getItem();
100     if (typed.length() > 1)
101     {
102       List<EnsemblTaxon> matches = EnsemblSpecies.getSpecies(true)
103               .getNameMatches(typed);
104       String[] items = new String[matches.size()];
105       int i = 0;
106       for (EnsemblTaxon m : matches)
107       {
108         items[i++] = String.format("%s (%s)", m.displayName, m.ncbiId);
109       }
110       Arrays.sort(items, String.CASE_INSENSITIVE_ORDER);
111       combo.setModel(new DefaultComboBoxModel<>(items));
112     }
113   }
114 }