2
* $Id: AutoComplete.java 3699 2010-05-11 19:05:09Z kschaefe $
4
* Copyright 2010 Sun Microsystems, Inc., 4150 Network Circle,
5
* Santa Clara, California 95054, U.S.A. All rights reserved.
7
* This library is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public
9
* License as published by the Free Software Foundation; either
10
* version 2.1 of the License, or (at your option) any later version.
12
* This library is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this library; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
package org.jdesktop.swingx.autocomplete;
23
import static org.jdesktop.swingx.autocomplete.AutoCompleteDecorator.createAutoCompleteDocument;
24
import static org.jdesktop.swingx.autocomplete.AutoCompleteDecorator.decorate;
25
import static org.jdesktop.swingx.autocomplete.AutoCompleteDecorator.undecorate;
27
import java.awt.event.ActionEvent;
28
import java.awt.event.FocusEvent;
29
import java.awt.event.KeyEvent;
30
import java.awt.event.KeyListener;
31
import java.beans.PropertyChangeEvent;
33
import javax.swing.Action;
34
import javax.swing.JComboBox;
35
import javax.swing.text.JTextComponent;
41
final class AutoComplete {
42
static class InputMap extends javax.swing.InputMap {
43
private static final long serialVersionUID = 1L;
46
static class FocusAdapter extends java.awt.event.FocusAdapter {
47
private AbstractAutoCompleteAdaptor adaptor;
49
public FocusAdapter(AbstractAutoCompleteAdaptor adaptor) {
50
this.adaptor = adaptor;
54
public void focusGained(FocusEvent e) {
55
adaptor.markEntireText();
59
static class KeyAdapter extends java.awt.event.KeyAdapter {
60
private JComboBox comboBox;
62
public KeyAdapter(JComboBox comboBox) {
63
this.comboBox = comboBox;
67
public void keyPressed(KeyEvent keyEvent) {
68
// don't popup on action keys (cursor movements, etc...)
69
if (keyEvent.isActionKey()) {
73
// don't popup if the combobox isn't visible anyway
74
if (comboBox.isDisplayable() && !comboBox.isPopupVisible()) {
75
int keyCode = keyEvent.getKeyCode();
76
// don't popup when the user hits shift,ctrl or alt
77
if (keyCode==KeyEvent.VK_SHIFT || keyCode==KeyEvent.VK_CONTROL || keyCode==KeyEvent.VK_ALT) return;
78
// don't popup when the user hits escape (see issue #311)
79
if (keyCode==KeyEvent.VK_ENTER || keyCode==KeyEvent.VK_ESCAPE) return;
80
comboBox.setPopupVisible(true);
85
static class PropertyChangeListener implements java.beans.PropertyChangeListener {
86
private JComboBox comboBox;
88
public PropertyChangeListener(JComboBox comboBox) {
89
this.comboBox = comboBox;
96
public void propertyChange(PropertyChangeEvent evt) {
97
if (evt.getNewValue() instanceof AutoCompleteComboBoxEditor) {
101
AutoCompleteComboBoxEditor acEditor = (AutoCompleteComboBoxEditor) evt.getOldValue();
102
boolean strictMatching = false;
104
if (acEditor.getEditorComponent() != null) {
105
JTextComponent textComponent = (JTextComponent) acEditor.getEditorComponent();
106
strictMatching = ((AutoCompleteDocument) textComponent.getDocument()).strictMatching;
108
undecorate(textComponent);
110
for (KeyListener l : textComponent.getKeyListeners()) {
111
if (l instanceof KeyAdapter) {
112
textComponent.removeKeyListener(l);
118
JTextComponent editorComponent = (JTextComponent) comboBox.getEditor().getEditorComponent();
119
AbstractAutoCompleteAdaptor adaptor = new ComboBoxAdaptor(comboBox);
120
AutoCompleteDocument document = createAutoCompleteDocument(adaptor, strictMatching,
121
acEditor.stringConverter, editorComponent.getDocument());
122
decorate(editorComponent, document, adaptor);
124
editorComponent.addKeyListener(new AutoComplete.KeyAdapter(comboBox));
126
//set before adding the listener for the editor
127
comboBox.setEditor(new AutoCompleteComboBoxEditor(comboBox.getEditor(), document.stringConverter));
131
static class SelectionAction implements Action {
132
private Action delegate;
134
public SelectionAction(Action delegate) {
135
this.delegate = delegate;
142
public void actionPerformed(ActionEvent e) {
143
JComboBox comboBox = (JComboBox) e.getSource();
144
JTextComponent textComponent = (JTextComponent) comboBox.getEditor().getEditorComponent();
145
AutoCompleteDocument doc = (AutoCompleteDocument) textComponent.getDocument();
147
// doing this prevents the updating of the selected item to "" during the remove prior
148
// to the insert in JTextComponent.setText
149
doc.strictMatching = true;
151
delegate.actionPerformed(e);
153
doc.strictMatching = false;
161
public void addPropertyChangeListener(java.beans.PropertyChangeListener listener) {
162
delegate.addPropertyChangeListener(listener);
169
public void removePropertyChangeListener(java.beans.PropertyChangeListener listener) {
170
delegate.removePropertyChangeListener(listener);
177
public Object getValue(String key) {
178
return delegate.getValue(key);
185
public void putValue(String key, Object value) {
186
delegate.putValue(key, value);
193
public boolean isEnabled() {
194
return delegate.isEnabled();
201
public void setEnabled(boolean b) {
202
delegate.setEnabled(b);
206
private AutoComplete() {
207
// prevent instantiation