Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.utils / src / main / java / org / gvsig / utils / swing / JComboBox.java @ 40561

History | View | Annotate | Download (5.77 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.utils.swing;
25

    
26
import java.awt.event.ActionEvent;
27
import java.awt.event.KeyEvent;
28
import java.awt.event.KeyListener;
29
import java.util.Vector;
30

    
31
import javax.swing.ComboBoxModel;
32
import javax.swing.DefaultComboBoxModel;
33
import javax.swing.JTextField;
34
import javax.swing.plaf.basic.BasicComboBoxEditor;
35

    
36

    
37
/**
38
 * ComboBox autocompletable
39
 *
40
 * @author Fernando Gonz?lez Cort?s
41
 */
42
public class JComboBox extends javax.swing.JComboBox {
43
    /**
44
     * Construye un combobox
45
     */
46
    public JComboBox() {
47
        super();
48
        init();
49
    }
50

    
51
    /**
52
     * Construye un combobox
53
     *
54
     * @param arg0
55
     */
56
    public JComboBox(Object[] arg0) {
57
        super(arg0);
58
        init();
59
    }
60

    
61
    /**
62
     * Construye un combobox
63
     *
64
     * @param arg0
65
     */
66
    public JComboBox(Vector arg0) {
67
        super(arg0);
68
        init();
69
    }
70

    
71
    /**
72
     * Construye un combobox
73
     *
74
     * @param arg0
75
     */
76
    public JComboBox(ComboBoxModel arg0) {
77
        super(arg0);
78
        init();
79
    }
80

    
81
    /**
82
     * Inicializa el combo
83
     */
84
    private void init() {
85
        this.setEditor(new BasicComboBoxEditor());
86

    
87
        JTextField jtext = (JTextField) JComboBox.this.getEditor()
88
                                                      .getEditorComponent();
89
        jtext.addKeyListener(new MyKeyListener());
90
        jtext.setText("");
91
    }
92

    
93
    /**
94
     * Manejador de los eventos Key para hacer el autocompletado
95
     *
96
     * @author Fernando Gonz?lez Cort?s
97
     */
98
    public class MyKeyListener implements KeyListener {
99
        private int lastCaretPosition = 0;
100
        private String lastText = "";
101
        private boolean bAutocompletar=true;
102

    
103
        /**
104
         * Devuelve dada una String, la String del modelo que empieza por dicha
105
         * String si hay alguna, o null si no hay ninguna
106
         *
107
         * @param text texto que se busca en el modelo
108
         *
109
         * @return String del modelo o null
110
         */
111
        private String isInModel(String text) {
112
            DefaultComboBoxModel model = (DefaultComboBoxModel) JComboBox.this.getModel();
113

    
114
            for (int i = 0; i < model.getSize(); i++) {
115
                if ((model.getElementAt(i).toString()).startsWith(text)) {
116
                    return model.getElementAt(i).toString();
117
                }
118
            }
119

    
120
            return null;
121
        }
122

    
123
        /**
124
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
125
         */
126
        public void actionPerformed(ActionEvent arg0) {
127
            JTextField jtext = (JTextField) JComboBox.this.getEditor()
128
                                                          .getEditorComponent();
129
            String texto = jtext.getText();
130

    
131
            String text = isInModel(texto);
132
            
133
            if (text != null) {
134
                int caretPos = texto.length();
135
                jtext.setText(text);
136
                jtext.setCaretPosition(caretPos);
137
                jtext.setSelectionStart(caretPos);
138
                jtext.setSelectionEnd(text.length());
139
            }
140
        }
141

    
142
        /**
143
         * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
144
         */
145
        public void keyPressed(KeyEvent e) {
146
            JTextField jtext = (JTextField) JComboBox.this.getEditor()
147
                                                          .getEditorComponent();
148
            bAutocompletar = true;
149
            if ((e.getKeyCode() == KeyEvent.VK_BACK_SPACE)
150
                    || (e.getKeyCode() == KeyEvent.VK_DELETE))
151
            {
152
                bAutocompletar = false;
153
                /* if ((lastCaretPosition >=0) && 
154
                        (lastCaretPosition <= jtext.getText().length()))
155
                                jtext.setCaretPosition(lastCaretPosition); */
156
            }
157
        }
158

    
159
        /**
160
         * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
161
         */
162
        public void keyReleased(KeyEvent e) {
163
            JTextField jtext = (JTextField) JComboBox.this.getEditor()
164
                                                          .getEditorComponent();
165
            String texto = jtext.getText().substring(0, jtext.getCaretPosition());
166

    
167
            if (!bAutocompletar) return;
168
            String text = isInModel(texto);
169

    
170
            if (!jtext.getText().equals(lastText) && (text != null)) {
171
                int caretPos = texto.length();
172
                jtext.setText(text);
173
                jtext.setCaretPosition(text.length());
174
                jtext.moveCaretPosition(caretPos);
175
                lastText = text;
176
                lastCaretPosition = caretPos;
177
                JComboBox.this.setSelectedItem(text);
178
            } else {
179
                lastCaretPosition = jtext.getCaretPosition();
180
            }
181
        }
182

    
183
        /**
184
         * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
185
         */
186
        public void keyTyped(KeyEvent e) {
187
        }
188
    }
189
}