OSDN Git Service

候補が表示されるcombo box
[coroid/inqubus.git] / frontend / src / yukihane / inqubus / gui / IdComboBox.java
1 package yukihane.inqubus.gui;
2
3 import java.awt.EventQueue;
4 import java.awt.event.FocusEvent;
5 import java.awt.event.FocusListener;
6 import java.awt.event.KeyAdapter;
7 import java.awt.event.KeyEvent;
8 import java.nio.file.Path;
9 import java.util.Collection;
10 import java.util.HashSet;
11 import java.util.Set;
12 import java.util.SortedSet;
13 import java.util.logging.Level;
14 import java.util.logging.Logger;
15 import javax.swing.DefaultComboBoxModel;
16 import javax.swing.JComboBox;
17 import javax.swing.JTextField;
18 import yukihane.inqubus.filewatch.FileWatch;
19 import yukihane.inqubus.filewatch.FileWatchUtil;
20
21 class IdComboBox extends JComboBox<String> {
22
23     private static final Logger logger = Logger.getLogger(IdComboBox.class.getName());
24     private final Set<Path> alternates = new HashSet<>();
25     private final FileWatch videoFileWatcher;
26
27     IdComboBox(FileWatch videoFileWather) {
28         super();
29         this.videoFileWatcher = videoFileWather;
30         setEditable(true);
31         setSelectedIndex(-1);
32         getEditorComponent().addKeyListener(new AutoCompleteAdapter());
33         getEditorComponent().addFocusListener(new FocusListener() {
34
35             @Override
36             public void focusGained(FocusEvent e) {
37                 logger.log(Level.FINEST, "ID field focus gained");
38                 setAlternates(videoFileWatcher.getFiles());
39             }
40
41             @Override
42             public void focusLost(FocusEvent e) {
43                 logger.log(Level.FINEST, "ID field focus lost");
44             }
45         });
46
47     }
48
49     private JTextField getEditorComponent() {
50         return (JTextField) getEditor().getEditorComponent();
51     }
52
53     void setAlternates(Collection<Path> alternates) {
54         this.alternates.clear();
55         this.alternates.addAll(alternates);
56     }
57
58     String getText() {
59         return getEditorComponent().getText();
60     }
61
62     void setText(String str) {
63         getEditorComponent().setText(str);
64     }
65
66     private class AutoCompleteAdapter extends KeyAdapter {
67
68         @Override
69         public void keyTyped(final KeyEvent e) {
70             logger.log(Level.FINEST, "key typed");
71             EventQueue.invokeLater(new Runnable() {
72
73                 @Override
74                 public void run() {
75                     final String text = getText();
76                     final SortedSet<String> alt = FileWatchUtil.getVideoIds(alternates, text);
77                     final String[] altArray = alt.toArray(new String[0]);
78                     DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(altArray);
79                     IdComboBox.this.setModel(model);
80                     setSelectedIndex(-1);
81                     setText(text);
82                     IdComboBox.this.showPopup();
83                 }
84             });
85         }
86     }
87 }