1 package org.columba.addressbook.gui.autocomplete; 17 18 import java.awt.event.ItemEvent ; 19 import java.awt.event.ItemListener ; 20 import java.awt.event.KeyEvent ; 21 import java.awt.event.KeyListener ; 22 import java.util.List ; 23 import java.util.Vector ; 24 25 import javax.swing.DefaultComboBoxModel ; 26 import javax.swing.JComboBox ; 27 import javax.swing.JTextField ; 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 41 public class AddressAutoCompleter implements KeyListener , ItemListener { 42 private JComboBox _comboBox = null; 43 private JTextField _editor = null; 44 int cursor_pos = -1; 45 private Object [] _options; 46 47 public AddressAutoCompleter(JComboBox comboBox, Object [] options) { 48 _comboBox = comboBox; 49 50 _editor = (JTextField ) comboBox.getEditor().getEditorComponent(); 51 _editor.addKeyListener(this); 52 53 _options = options; 54 _comboBox.addItemListener(this); 55 } 56 57 public void keyTyped(KeyEvent e) { 58 } 59 60 public void keyPressed(KeyEvent e) { 61 } 62 63 public void keyReleased(KeyEvent 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 str = _editor.getText(); 75 76 if (str.length() == 0) { 77 return; 78 } 79 80 autoComplete(str, pos); 81 } 82 83 private void autoComplete(String strf, int pos) { 84 Object [] opts = getMatchingOptions(strf.substring(0, pos)); 85 86 if (_comboBox != null) { 87 _comboBox.setModel(new DefaultComboBoxModel (opts)); 88 } 89 90 if (opts.length > 0) { 91 String str = opts[0].toString(); 92 93 IBasicModelPartial item = AddressCollector.getInstance().getHeaderItem((String ) 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 if (_comboBox != null) { 106 try { 107 _comboBox.showPopup(); 108 } catch (Exception ex) { 109 ex.printStackTrace(); 110 } 111 } 112 } 113 } 114 115 private Object [] getMatchingOptions(String str) { 116 _options = AddressCollector.getInstance().getAddresses(); 117 118 List v = new Vector (); 119 120 for (int k = 0; k < _options.length; k++) { 121 String 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 event) { 136 if (event.getStateChange() == ItemEvent.SELECTED) { 137 String selected = (String ) _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 ex) { 154 ex.printStackTrace(); 155 } 156 } 157 } 158 } 159 } 160 | Popular Tags |