1 package org.sleuthkit.autopsy.guicomponentutils;
3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
5 import java.awt.event.FocusAdapter;
6 import java.awt.event.FocusEvent;
7 import java.awt.event.FocusListener;
8 import java.awt.event.KeyAdapter;
9 import java.awt.event.KeyEvent;
10 import java.awt.event.KeyListener;
11 import java.beans.PropertyChangeEvent;
12 import java.beans.PropertyChangeListener;
13 import javax.swing.ComboBoxEditor;
14 import javax.swing.ComboBoxModel;
15 import javax.swing.JComboBox;
16 import javax.swing.text.AttributeSet;
17 import javax.swing.text.BadLocationException;
18 import javax.swing.text.JTextComponent;
19 import javax.swing.text.PlainDocument;
48 model = comboBox.getModel();
49 comboBox.addActionListener(
new ActionListener() {
51 public void actionPerformed(ActionEvent e) {
57 comboBox.addPropertyChangeListener(
new PropertyChangeListener() {
59 public void propertyChange(PropertyChangeEvent e) {
60 if (e.getPropertyName().equals(
"editor")) {
61 configureEditor((ComboBoxEditor) e.getNewValue());
63 if (e.getPropertyName().equals(
"model")) {
64 model = (ComboBoxModel) e.getNewValue();
68 editorKeyListener =
new KeyAdapter() {
70 public void keyPressed(KeyEvent e) {
71 if (comboBox.isDisplayable()) {
72 comboBox.setPopupVisible(
true);
75 switch (e.getKeyCode()) {
77 case KeyEvent.VK_BACK_SPACE:
79 hitBackspaceOnSelection = editor.getSelectionStart() != editor.getSelectionEnd();
82 case KeyEvent.VK_DELETE:
84 comboBox.getToolkit().beep();
90 hidePopupOnFocusLoss = System.getProperty(
"java.version").startsWith(
"1.5");
92 editorFocusListener =
new FocusAdapter() {
94 public void focusGained(FocusEvent e) {
99 public void focusLost(FocusEvent e) {
101 if (hidePopupOnFocusLoss) {
102 comboBox.setPopupVisible(
false);
106 configureEditor(comboBox.getEditor());
108 Object selected = comboBox.getSelectedItem();
109 if (selected != null) {
115 public static void enable(JComboBox<?> comboBox) {
117 comboBox.setEditable(
true);
122 void configureEditor(ComboBoxEditor newEditor) {
123 if (editor != null) {
124 editor.removeKeyListener(editorKeyListener);
125 editor.removeFocusListener(editorFocusListener);
128 if (newEditor != null) {
129 editor = (JTextComponent) newEditor.getEditorComponent();
130 editor.addKeyListener(editorKeyListener);
131 editor.addFocusListener(editorFocusListener);
132 editor.setDocument(
this);
136 public void remove(
int offs,
int len)
throws BadLocationException {
145 if (hitBackspaceOnSelection) {
150 comboBox.getToolkit().beep();
154 super.remove(offs, len);
159 public void insertString(
int offs, String str, AttributeSet a)
throws BadLocationException {
165 super.insertString(offs, str, a);
167 Object item =
lookupItem(getText(0, getLength()));
172 item = comboBox.getSelectedItem();
174 offs = offs - str.length();
176 comboBox.getToolkit().beep();
186 super.remove(0, getLength());
187 super.insertString(0, text, null);
188 }
catch (BadLocationException e) {
189 throw new RuntimeException(e.toString());
194 editor.setCaretPosition(getLength());
195 editor.moveCaretPosition(start);
200 model.setSelectedItem(item);
205 Object selectedItem = model.getSelectedItem();
211 for (
int i = 0, n = model.getSize(); i < n; i++) {
212 Object currentItem = model.getElementAt(i);
225 return str1.toUpperCase().startsWith(str2.toUpperCase());
AutoCompletion(final JComboBox<?> comboBox)
boolean hitBackspaceOnSelection
void highlightCompletedText(int start)
void insertString(int offs, String str, AttributeSet a)
FocusListener editorFocusListener
void setSelectedItem(Object item)
KeyListener editorKeyListener
Object lookupItem(String pattern)
boolean startsWithIgnoreCase(String str1, String str2)
boolean hidePopupOnFocusLoss
static final long serialVersionUID
static void enable(JComboBox<?> comboBox)
void setText(String text)