KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > addressbook > gui > autocomplete > AddressAutoCompleter


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.addressbook.gui.autocomplete;
17
18 import java.awt.event.ItemEvent JavaDoc;
19 import java.awt.event.ItemListener JavaDoc;
20 import java.awt.event.KeyEvent JavaDoc;
21 import java.awt.event.KeyListener JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 import javax.swing.DefaultComboBoxModel JavaDoc;
26 import javax.swing.JComboBox JavaDoc;
27 import javax.swing.JTextField JavaDoc;
28
29 import org.columba.addressbook.model.ContactModelPartial;
30 import org.columba.addressbook.model.BasicModelPartial;
31 import org.columba.addressbook.model.IBasicModelPartial;
32
33
34 /**
35  *
36  *
37  * Autocompleter component
38  *
39  * @author fdietz
40  */

41 public class AddressAutoCompleter implements KeyListener JavaDoc, ItemListener JavaDoc {
42     private JComboBox JavaDoc _comboBox = null;
43     private JTextField JavaDoc _editor = null;
44     int cursor_pos = -1;
45     private Object JavaDoc[] _options;
46
47     public AddressAutoCompleter(JComboBox JavaDoc comboBox, Object JavaDoc[] options) {
48         _comboBox = comboBox;
49
50         _editor = (JTextField JavaDoc) comboBox.getEditor().getEditorComponent();
51         _editor.addKeyListener(this);
52
53         _options = options;
54         _comboBox.addItemListener(this);
55     }
56
57     public void keyTyped(KeyEvent JavaDoc e) {
58     }
59
60     public void keyPressed(KeyEvent JavaDoc e) {
61     }
62
63     public void keyReleased(KeyEvent JavaDoc e) {
64         char ch = e.getKeyChar();
65
66         if ((ch == KeyEvent.CHAR_UNDEFINED) || Character.isISOControl(ch) ||
67                 (ch == KeyEvent.VK_DELETE)) {
68             return;
69         }
70
71         int pos = _editor.getCaretPosition();
72         cursor_pos = _editor.getCaretPosition();
73
74         String JavaDoc str = _editor.getText();
75
76         if (str.length() == 0) {
77             return;
78         }
79
80         autoComplete(str, pos);
81     }
82
83     private void autoComplete(String JavaDoc strf, int pos) {
84         Object JavaDoc[] opts = getMatchingOptions(strf.substring(0, pos));
85
86         if (_comboBox != null) {
87             _comboBox.setModel(new DefaultComboBoxModel JavaDoc(opts));
88         }
89
90         if (opts.length > 0) {
91             String JavaDoc str = opts[0].toString();
92
93             IBasicModelPartial item = AddressCollector.getInstance().getHeaderItem((String JavaDoc) opts[0]);
94
95             if (item == null) {
96                 item = new ContactModelPartial(str);
97                 
98             } else {
99                 item = (IBasicModelPartial) item.clone();
100             }
101
102             _editor.setCaretPosition(cursor_pos);
103
104             //_editor.moveCaretPosition(cursor_pos);
105
if (_comboBox != null) {
106                 try {
107                     _comboBox.showPopup();
108                 } catch (Exception JavaDoc ex) {
109                     ex.printStackTrace();
110                 }
111             }
112         }
113     }
114
115     private Object JavaDoc[] getMatchingOptions(String JavaDoc str) {
116         _options = AddressCollector.getInstance().getAddresses();
117
118         List JavaDoc v = new Vector JavaDoc();
119
120         for (int k = 0; k < _options.length; k++) {
121             String JavaDoc item = _options[k].toString().toLowerCase();
122
123             if (item.startsWith(str.toLowerCase())) {
124                 v.add(_options[k]);
125             }
126         }
127
128         if (v.isEmpty()) {
129             v.add(str);
130         }
131
132         return v.toArray();
133     }
134
135     public void itemStateChanged(ItemEvent JavaDoc event) {
136         if (event.getStateChange() == ItemEvent.SELECTED) {
137             String JavaDoc selected = (String JavaDoc) _comboBox.getSelectedItem();
138
139             IBasicModelPartial item = AddressCollector.getInstance().getHeaderItem(selected);
140
141             if (item == null) {
142                 item = new ContactModelPartial(selected);
143                 
144             } else {
145                 item = (IBasicModelPartial) item.clone();
146             }
147
148             int pos2 = _editor.getCaretPosition();
149
150             if (cursor_pos != -1) {
151                 try {
152                     _editor.moveCaretPosition(pos2);
153                 } catch (IllegalArgumentException JavaDoc ex) {
154                     ex.printStackTrace();
155                 }
156             }
157         }
158     }
159 }
160
Popular Tags