Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libIverUtiles / src / com / iver / utiles / swing / JComboBox.java @ 1836

History | View | Annotate | Download (6.03 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.utiles.swing;
42

    
43
import java.awt.event.ActionEvent;
44
import java.awt.event.KeyEvent;
45
import java.awt.event.KeyListener;
46
import java.util.Vector;
47

    
48
import javax.swing.ComboBoxModel;
49
import javax.swing.DefaultComboBoxModel;
50
import javax.swing.JTextField;
51
import javax.swing.plaf.basic.BasicComboBoxEditor;
52

    
53

    
54
/**
55
 * ComboBox autocompletable
56
 *
57
 * @author Fernando Gonz?lez Cort?s
58
 */
59
public class JComboBox extends javax.swing.JComboBox {
60
    /**
61
     * Construye un combobox
62
     */
63
    public JComboBox() {
64
        super();
65
        init();
66
    }
67

    
68
    /**
69
     * Construye un combobox
70
     *
71
     * @param arg0
72
     */
73
    public JComboBox(Object[] arg0) {
74
        super(arg0);
75
        init();
76
    }
77

    
78
    /**
79
     * Construye un combobox
80
     *
81
     * @param arg0
82
     */
83
    public JComboBox(Vector arg0) {
84
        super(arg0);
85
        init();
86
    }
87

    
88
    /**
89
     * Construye un combobox
90
     *
91
     * @param arg0
92
     */
93
    public JComboBox(ComboBoxModel arg0) {
94
        super(arg0);
95
        init();
96
    }
97

    
98
    /**
99
     * Inicializa el combo
100
     */
101
    private void init() {
102
        this.setEditor(new BasicComboBoxEditor());
103

    
104
        JTextField jtext = (JTextField) JComboBox.this.getEditor()
105
                                                      .getEditorComponent();
106
        jtext.addKeyListener(new MyKeyListener());
107
        jtext.setText("");
108
    }
109

    
110
    /**
111
     * Manejador de los eventos Key para hacer el autocompletado
112
     *
113
     * @author Fernando Gonz?lez Cort?s
114
     */
115
    public class MyKeyListener implements KeyListener {
116
        private int lastCaretPosition = 0;
117
        private String lastText = "";
118
        private boolean bAutocompletar=true;
119

    
120
        /**
121
         * Devuelve dada una String, la String del modelo que empieza por dicha
122
         * String si hay alguna, o null si no hay ninguna
123
         *
124
         * @param text texto que se busca en el modelo
125
         *
126
         * @return String del modelo o null
127
         */
128
        private String isInModel(String text) {
129
            DefaultComboBoxModel model = (DefaultComboBoxModel) JComboBox.this.getModel();
130

    
131
            for (int i = 0; i < model.getSize(); i++) {
132
                if ((model.getElementAt(i).toString()).startsWith(text)) {
133
                    return model.getElementAt(i).toString();
134
                }
135
            }
136

    
137
            return null;
138
        }
139

    
140
        /**
141
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
142
         */
143
        public void actionPerformed(ActionEvent arg0) {
144
            JTextField jtext = (JTextField) JComboBox.this.getEditor()
145
                                                          .getEditorComponent();
146
            String texto = jtext.getText();
147

    
148
            String text = isInModel(texto);
149
            
150
            if (text != null) {
151
                int caretPos = texto.length();
152
                jtext.setText(text);
153
                jtext.setCaretPosition(caretPos);
154
                jtext.setSelectionStart(caretPos);
155
                jtext.setSelectionEnd(text.length());
156
            }
157
        }
158

    
159
        /**
160
         * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
161
         */
162
        public void keyPressed(KeyEvent e) {
163
            JTextField jtext = (JTextField) JComboBox.this.getEditor()
164
                                                          .getEditorComponent();
165
            bAutocompletar = true;
166
            if ((e.getKeyCode() == KeyEvent.VK_BACK_SPACE)
167
                    || (e.getKeyCode() == KeyEvent.VK_DELETE))
168
            {
169
                bAutocompletar = false;
170
                /* if ((lastCaretPosition >=0) && 
171
                        (lastCaretPosition <= jtext.getText().length()))
172
                                jtext.setCaretPosition(lastCaretPosition); */
173
            }
174
        }
175

    
176
        /**
177
         * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
178
         */
179
        public void keyReleased(KeyEvent e) {
180
            JTextField jtext = (JTextField) JComboBox.this.getEditor()
181
                                                          .getEditorComponent();
182
            String texto = jtext.getText().substring(0, jtext.getCaretPosition());
183

    
184
            if (!bAutocompletar) return;
185
            String text = isInModel(texto);
186

    
187
            if (!jtext.getText().equals(lastText) && (text != null)) {
188
                int caretPos = texto.length();
189
                jtext.setText(text);
190
                jtext.setCaretPosition(text.length());
191
                jtext.moveCaretPosition(caretPos);
192
                lastText = text;
193
                lastCaretPosition = caretPos;
194
                JComboBox.this.setSelectedItem(text);
195
            } else {
196
                lastCaretPosition = jtext.getCaretPosition();
197
            }
198
        }
199

    
200
        /**
201
         * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
202
         */
203
        public void keyTyped(KeyEvent e) {
204
        }
205
    }
206
}