01: /**
02: * Copyright 2005, 2006 ToolCafe, Inc. All rights reserved.
03: */package org.columba.mail.gui.composer;
04:
05: import java.util.List;
06: import java.util.regex.Pattern;
07:
08: import javax.swing.text.BadLocationException;
09: import javax.swing.text.Document;
10: import javax.swing.text.JTextComponent;
11:
12: import org.frapuccino.addresscombobox.PatternSeparatedAutoCompleter;
13: import org.frapuccino.addresscombobox.TextParser;
14:
15: /**
16: * @author Rick Horowitz
17: *
18: */
19: public class EmailNameAutoCompleter extends
20: PatternSeparatedAutoCompleter {
21:
22: /**
23: * @param comp
24: * @param completionList
25: * @param separatorPattern
26: * @param ignoreCase
27: */
28: public EmailNameAutoCompleter(JTextComponent comp,
29: List completionList, Pattern separatorPattern,
30: boolean ignoreCase) {
31: super (comp, completionList, separatorPattern, ignoreCase);
32: }
33:
34: /* (non-Javadoc)
35: * @see org.frapuccino.addresscombobox.PatternSeparatedAutoCompleter#acceptedListItem(java.lang.Object)
36: */
37: @Override
38: protected void acceptedListItem(Object selected) {
39: if (selected == null)
40: return;
41:
42: int caret = textComp.getCaretPosition();
43: String value = TextParser.getItemAt(textComp.getText(),
44: separatorPattern, textComp.getCaretPosition());
45:
46: try {
47: Document doc = textComp.getDocument();
48: // Remove leading space after the separator character so that it is not removed from the text component's document, below.
49: value = value.trim();
50: int startingPos = caret - value.length();
51: doc.remove(startingPos, value.length());
52:
53: // Surround the selected element with double-quotes, if necessary
54: String selectedStr = selected.toString();
55: String sep = separatorPattern.toString();
56: if (selectedStr.contains(sep))
57: selectedStr = '"' + selectedStr + '"';
58: textComp.getDocument().insertString(startingPos,
59: selectedStr + separatorPattern.toString() + " ",
60: null);
61: } catch (BadLocationException e) {
62: e.printStackTrace();
63: }
64:
65: popup.setVisible(false);
66: }
67:
68: }
|