OSDN Git Service

候補が表示されるcombo box
authoryukihane <yukihane.feather@gmail.com>
Fri, 2 Sep 2011 05:32:33 +0000 (14:32 +0900)
committeryukihane <yukihane.feather@gmail.com>
Fri, 9 Sep 2011 11:39:18 +0000 (20:39 +0900)
frontend/src/yukihane/inqubus/gui/AutoCompleteAdapter.java [deleted file]
frontend/src/yukihane/inqubus/gui/IdComboBox.java [new file with mode: 0644]

diff --git a/frontend/src/yukihane/inqubus/gui/AutoCompleteAdapter.java b/frontend/src/yukihane/inqubus/gui/AutoCompleteAdapter.java
deleted file mode 100644 (file)
index 2065b44..0000000
+++ /dev/null
@@ -1,45 +0,0 @@
-package yukihane.inqubus.gui;
-
-import java.awt.EventQueue;
-import java.awt.event.KeyAdapter;
-import java.awt.event.KeyEvent;
-import java.nio.file.Path;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Set;
-import java.util.SortedSet;
-import javax.swing.DefaultComboBoxModel;
-import javax.swing.JComboBox;
-import javax.swing.JTextField;
-import yukihane.inqubus.filewatch.FileWatchUtil;
-
-class AutoCompleteAdapter extends KeyAdapter {
-
-    private final JComboBox<String> component;
-    private final Set<Path> alternates = new HashSet<>();
-
-    AutoCompleteAdapter(JComboBox<String> component) {
-        this.component = component;
-    }
-
-    void setAlternates(Collection<Path> alternates) {
-        this.alternates.clear();
-        this.alternates.addAll(alternates);
-    }
-
-    @Override
-    public void keyTyped(final KeyEvent e) {
-        EventQueue.invokeLater(new Runnable() {
-
-            @Override
-            public void run() {
-                final String text = ((JTextField) e.getSource()).getText();
-                final SortedSet<String> alt = FileWatchUtil.contain(alternates, text);
-                final String[] altArray = alt.toArray(new String[0]);
-                DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(altArray);
-                component.setModel(model);
-                component.showPopup();
-            }
-        });
-    }
-}
diff --git a/frontend/src/yukihane/inqubus/gui/IdComboBox.java b/frontend/src/yukihane/inqubus/gui/IdComboBox.java
new file mode 100644 (file)
index 0000000..0336a96
--- /dev/null
@@ -0,0 +1,87 @@
+package yukihane.inqubus.gui;
+
+import java.awt.EventQueue;
+import java.awt.event.FocusEvent;
+import java.awt.event.FocusListener;
+import java.awt.event.KeyAdapter;
+import java.awt.event.KeyEvent;
+import java.nio.file.Path;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.SortedSet;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import javax.swing.DefaultComboBoxModel;
+import javax.swing.JComboBox;
+import javax.swing.JTextField;
+import yukihane.inqubus.filewatch.FileWatch;
+import yukihane.inqubus.filewatch.FileWatchUtil;
+
+class IdComboBox extends JComboBox<String> {
+
+    private static final Logger logger = Logger.getLogger(IdComboBox.class.getName());
+    private final Set<Path> alternates = new HashSet<>();
+    private final FileWatch videoFileWatcher;
+
+    IdComboBox(FileWatch videoFileWather) {
+        super();
+        this.videoFileWatcher = videoFileWather;
+        setEditable(true);
+        setSelectedIndex(-1);
+        getEditorComponent().addKeyListener(new AutoCompleteAdapter());
+        getEditorComponent().addFocusListener(new FocusListener() {
+
+            @Override
+            public void focusGained(FocusEvent e) {
+                logger.log(Level.FINEST, "ID field focus gained");
+                setAlternates(videoFileWatcher.getFiles());
+            }
+
+            @Override
+            public void focusLost(FocusEvent e) {
+                logger.log(Level.FINEST, "ID field focus lost");
+            }
+        });
+
+    }
+
+    private JTextField getEditorComponent() {
+        return (JTextField) getEditor().getEditorComponent();
+    }
+
+    void setAlternates(Collection<Path> alternates) {
+        this.alternates.clear();
+        this.alternates.addAll(alternates);
+    }
+
+    String getText() {
+        return getEditorComponent().getText();
+    }
+
+    void setText(String str) {
+        getEditorComponent().setText(str);
+    }
+
+    private class AutoCompleteAdapter extends KeyAdapter {
+
+        @Override
+        public void keyTyped(final KeyEvent e) {
+            logger.log(Level.FINEST, "key typed");
+            EventQueue.invokeLater(new Runnable() {
+
+                @Override
+                public void run() {
+                    final String text = getText();
+                    final SortedSet<String> alt = FileWatchUtil.getVideoIds(alternates, text);
+                    final String[] altArray = alt.toArray(new String[0]);
+                    DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(altArray);
+                    IdComboBox.this.setModel(model);
+                    setSelectedIndex(-1);
+                    setText(text);
+                    IdComboBox.this.showPopup();
+                }
+            });
+        }
+    }
+}
\ No newline at end of file