OSDN Git Service

ログ出力
[coroid/inqubus.git] / frontend / src / yukihane / inqubus / gui / AutoCompleteAdapter.java
1 package yukihane.inqubus.gui;
2
3 import java.awt.EventQueue;
4 import java.awt.event.KeyAdapter;
5 import java.awt.event.KeyEvent;
6 import java.nio.file.Path;
7 import java.util.Collection;
8 import java.util.HashSet;
9 import java.util.Set;
10 import java.util.SortedSet;
11 import javax.swing.DefaultComboBoxModel;
12 import javax.swing.JComboBox;
13 import javax.swing.JTextField;
14 import yukihane.inqubus.filewatch.FileWatchUtil;
15
16 class AutoCompleteAdapter extends KeyAdapter {
17
18     private final JComboBox<String> component;
19     private final Set<Path> alternates = new HashSet<>();
20
21     AutoCompleteAdapter(JComboBox<String> component) {
22         this.component = component;
23     }
24
25     void setAlternates(Collection<Path> alternates) {
26         this.alternates.clear();
27         this.alternates.addAll(alternates);
28     }
29
30     @Override
31     public void keyTyped(final KeyEvent e) {
32         EventQueue.invokeLater(new Runnable() {
33
34             @Override
35             public void run() {
36                 final String text = ((JTextField) e.getSource()).getText();
37                 final SortedSet<String> alt = FileWatchUtil.contain(alternates, text);
38                 final String[] altArray = alt.toArray(new String[0]);
39                 DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(altArray);
40                 component.setModel(model);
41                 component.showPopup();
42             }
43         });
44     }
45 }