~ubuntu-branches/ubuntu/natty/libswingx-java/natty

« back to all changes in this revision

Viewing changes to src/java/org/jdesktop/swingx/autocomplete/AutoComplete.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2010-07-26 12:11:27 UTC
  • mfrom: (4.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20100726121127-k0d3b21nhja0dn93
Tags: 1:1.6.1-1
* New upstream release.
* Switch to 3.0 (quilt) format.
* Bump Standards-Version to 3.9.1: no changes needed.
* Drop Depends on JRE: not requested anymore by new Java Policy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Id: AutoComplete.java 3699 2010-05-11 19:05:09Z kschaefe $
 
3
 *
 
4
 * Copyright 2010 Sun Microsystems, Inc., 4150 Network Circle,
 
5
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 
6
 *
 
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.
 
11
 * 
 
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.
 
16
 * 
 
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
 
20
 */
 
21
package org.jdesktop.swingx.autocomplete;
 
22
 
 
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;
 
26
 
 
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;
 
32
 
 
33
import javax.swing.Action;
 
34
import javax.swing.JComboBox;
 
35
import javax.swing.text.JTextComponent;
 
36
 
 
37
/**
 
38
 * 
 
39
 * @author kschaefer
 
40
 */
 
41
final class AutoComplete {
 
42
    static class InputMap extends javax.swing.InputMap {
 
43
        private static final long serialVersionUID = 1L;
 
44
    }
 
45
 
 
46
    static class FocusAdapter extends java.awt.event.FocusAdapter {
 
47
        private AbstractAutoCompleteAdaptor adaptor;
 
48
 
 
49
        public FocusAdapter(AbstractAutoCompleteAdaptor adaptor) {
 
50
            this.adaptor = adaptor;
 
51
        }
 
52
 
 
53
        @Override
 
54
        public void focusGained(FocusEvent e) {
 
55
            adaptor.markEntireText();
 
56
        }
 
57
    }
 
58
    
 
59
    static class KeyAdapter extends java.awt.event.KeyAdapter {
 
60
        private JComboBox comboBox;
 
61
        
 
62
        public KeyAdapter(JComboBox comboBox) {
 
63
            this.comboBox = comboBox;
 
64
        }
 
65
        
 
66
        @Override
 
67
        public void keyPressed(KeyEvent keyEvent) {
 
68
            // don't popup on action keys (cursor movements, etc...)
 
69
            if (keyEvent.isActionKey()) {
 
70
                return;
 
71
            }
 
72
            
 
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);
 
81
            }
 
82
        }
 
83
    }
 
84
 
 
85
    static class PropertyChangeListener implements java.beans.PropertyChangeListener {
 
86
        private JComboBox comboBox;
 
87
        
 
88
        public PropertyChangeListener(JComboBox comboBox) {
 
89
            this.comboBox = comboBox;
 
90
        }
 
91
        
 
92
        /**
 
93
         * {@inheritDoc}
 
94
         */
 
95
        @Override
 
96
        public void propertyChange(PropertyChangeEvent evt) {
 
97
            if (evt.getNewValue() instanceof AutoCompleteComboBoxEditor) {
 
98
                return;
 
99
            }
 
100
            
 
101
            AutoCompleteComboBoxEditor acEditor = (AutoCompleteComboBoxEditor) evt.getOldValue();
 
102
            boolean strictMatching = false;
 
103
            
 
104
            if (acEditor.getEditorComponent() != null) {
 
105
                JTextComponent textComponent = (JTextComponent) acEditor.getEditorComponent();
 
106
                strictMatching = ((AutoCompleteDocument) textComponent.getDocument()).strictMatching;
 
107
                
 
108
                undecorate(textComponent);
 
109
                
 
110
                for (KeyListener l : textComponent.getKeyListeners()) {
 
111
                    if (l instanceof KeyAdapter) {
 
112
                        textComponent.removeKeyListener(l);
 
113
                        break;
 
114
                    }
 
115
                }
 
116
            }
 
117
 
 
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);
 
123
            
 
124
            editorComponent.addKeyListener(new AutoComplete.KeyAdapter(comboBox));
 
125
            
 
126
            //set before adding the listener for the editor
 
127
            comboBox.setEditor(new AutoCompleteComboBoxEditor(comboBox.getEditor(), document.stringConverter));
 
128
        }
 
129
    }
 
130
 
 
131
    static class SelectionAction implements Action {
 
132
        private Action delegate;
 
133
        
 
134
        public SelectionAction(Action delegate) {
 
135
            this.delegate = delegate;
 
136
        }
 
137
        
 
138
        /**
 
139
         * {@inheritDoc}
 
140
         */
 
141
        @Override
 
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();
 
146
            
 
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;
 
150
            try {
 
151
                delegate.actionPerformed(e);
 
152
            } finally {
 
153
                doc.strictMatching = false;
 
154
            }
 
155
        }
 
156
 
 
157
        /**
 
158
         * {@inheritDoc}
 
159
         */
 
160
        @Override
 
161
        public void addPropertyChangeListener(java.beans.PropertyChangeListener listener) {
 
162
            delegate.addPropertyChangeListener(listener);
 
163
        }
 
164
        
 
165
        /**
 
166
         * {@inheritDoc}
 
167
         */
 
168
        @Override
 
169
        public void removePropertyChangeListener(java.beans.PropertyChangeListener listener) {
 
170
            delegate.removePropertyChangeListener(listener);
 
171
        }
 
172
        
 
173
        /**
 
174
         * {@inheritDoc}
 
175
         */
 
176
        @Override
 
177
        public Object getValue(String key) {
 
178
            return delegate.getValue(key);
 
179
        }
 
180
 
 
181
        /**
 
182
         * {@inheritDoc}
 
183
         */
 
184
        @Override
 
185
        public void putValue(String key, Object value) {
 
186
            delegate.putValue(key, value);
 
187
        }
 
188
        
 
189
        /**
 
190
         * {@inheritDoc}
 
191
         */
 
192
        @Override
 
193
        public boolean isEnabled() {
 
194
            return delegate.isEnabled();
 
195
        }
 
196
 
 
197
        /**
 
198
         * {@inheritDoc}
 
199
         */
 
200
        @Override
 
201
        public void setEnabled(boolean b) {
 
202
            delegate.setEnabled(b);
 
203
        }
 
204
    }
 
205
    
 
206
    private AutoComplete() {
 
207
        // prevent instantiation
 
208
    }
 
209
}