001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.addressbook.gui.autocomplete;
017:
018: import java.awt.event.ItemEvent;
019: import java.awt.event.ItemListener;
020: import java.awt.event.KeyEvent;
021: import java.awt.event.KeyListener;
022: import java.util.List;
023: import java.util.Vector;
024:
025: import javax.swing.DefaultComboBoxModel;
026: import javax.swing.JComboBox;
027: import javax.swing.JTextField;
028:
029: import org.columba.addressbook.model.ContactModelPartial;
030: import org.columba.addressbook.model.BasicModelPartial;
031: import org.columba.addressbook.model.IBasicModelPartial;
032:
033: /**
034: *
035: *
036: * Autocompleter component
037: *
038: * @author fdietz
039: */
040: public class AddressAutoCompleter implements KeyListener, ItemListener {
041: private JComboBox _comboBox = null;
042: private JTextField _editor = null;
043: int cursor_pos = -1;
044: private Object[] _options;
045:
046: public AddressAutoCompleter(JComboBox comboBox, Object[] options) {
047: _comboBox = comboBox;
048:
049: _editor = (JTextField) comboBox.getEditor()
050: .getEditorComponent();
051: _editor.addKeyListener(this );
052:
053: _options = options;
054: _comboBox.addItemListener(this );
055: }
056:
057: public void keyTyped(KeyEvent e) {
058: }
059:
060: public void keyPressed(KeyEvent e) {
061: }
062:
063: public void keyReleased(KeyEvent e) {
064: char ch = e.getKeyChar();
065:
066: if ((ch == KeyEvent.CHAR_UNDEFINED)
067: || Character.isISOControl(ch)
068: || (ch == KeyEvent.VK_DELETE)) {
069: return;
070: }
071:
072: int pos = _editor.getCaretPosition();
073: cursor_pos = _editor.getCaretPosition();
074:
075: String str = _editor.getText();
076:
077: if (str.length() == 0) {
078: return;
079: }
080:
081: autoComplete(str, pos);
082: }
083:
084: private void autoComplete(String strf, int pos) {
085: Object[] opts = getMatchingOptions(strf.substring(0, pos));
086:
087: if (_comboBox != null) {
088: _comboBox.setModel(new DefaultComboBoxModel(opts));
089: }
090:
091: if (opts.length > 0) {
092: String str = opts[0].toString();
093:
094: IBasicModelPartial item = AddressCollector.getInstance()
095: .getHeaderItem((String) opts[0]);
096:
097: if (item == null) {
098: item = new ContactModelPartial(str);
099:
100: } else {
101: item = (IBasicModelPartial) item.clone();
102: }
103:
104: _editor.setCaretPosition(cursor_pos);
105:
106: //_editor.moveCaretPosition(cursor_pos);
107: if (_comboBox != null) {
108: try {
109: _comboBox.showPopup();
110: } catch (Exception ex) {
111: ex.printStackTrace();
112: }
113: }
114: }
115: }
116:
117: private Object[] getMatchingOptions(String str) {
118: _options = AddressCollector.getInstance().getAddresses();
119:
120: List v = new Vector();
121:
122: for (int k = 0; k < _options.length; k++) {
123: String item = _options[k].toString().toLowerCase();
124:
125: if (item.startsWith(str.toLowerCase())) {
126: v.add(_options[k]);
127: }
128: }
129:
130: if (v.isEmpty()) {
131: v.add(str);
132: }
133:
134: return v.toArray();
135: }
136:
137: public void itemStateChanged(ItemEvent event) {
138: if (event.getStateChange() == ItemEvent.SELECTED) {
139: String selected = (String) _comboBox.getSelectedItem();
140:
141: IBasicModelPartial item = AddressCollector.getInstance()
142: .getHeaderItem(selected);
143:
144: if (item == null) {
145: item = new ContactModelPartial(selected);
146:
147: } else {
148: item = (IBasicModelPartial) item.clone();
149: }
150:
151: int pos2 = _editor.getCaretPosition();
152:
153: if (cursor_pos != -1) {
154: try {
155: _editor.moveCaretPosition(pos2);
156: } catch (IllegalArgumentException ex) {
157: ex.printStackTrace();
158: }
159: }
160: }
161: }
162: }
|