KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > composer > EmailNameAutoCompleter


1 /**
2  * Copyright 2005, 2006 ToolCafe, Inc. All rights reserved.
3  */

4 package org.columba.mail.gui.composer;
5
6 import java.util.List JavaDoc;
7 import java.util.regex.Pattern JavaDoc;
8
9 import javax.swing.text.BadLocationException JavaDoc;
10 import javax.swing.text.Document JavaDoc;
11 import javax.swing.text.JTextComponent JavaDoc;
12
13 import org.frapuccino.addresscombobox.PatternSeparatedAutoCompleter;
14 import org.frapuccino.addresscombobox.TextParser;
15
16 /**
17  * @author Rick Horowitz
18  *
19  */

20 public class EmailNameAutoCompleter extends PatternSeparatedAutoCompleter {
21
22     /**
23      * @param comp
24      * @param completionList
25      * @param separatorPattern
26      * @param ignoreCase
27      */

28     public EmailNameAutoCompleter(JTextComponent JavaDoc comp, List JavaDoc completionList, Pattern JavaDoc separatorPattern, boolean ignoreCase) {
29         super(comp, completionList, separatorPattern, ignoreCase);
30     }
31
32     /* (non-Javadoc)
33      * @see org.frapuccino.addresscombobox.PatternSeparatedAutoCompleter#acceptedListItem(java.lang.Object)
34      */

35     @Override JavaDoc
36     protected void acceptedListItem(Object JavaDoc selected) {
37         if (selected == null)
38             return;
39
40         int caret = textComp.getCaretPosition();
41         String JavaDoc value = TextParser.getItemAt(textComp.getText(), separatorPattern, textComp
42                 .getCaretPosition());
43
44         try {
45             Document JavaDoc doc = textComp.getDocument();
46             // Remove leading space after the separator character so that it is not removed from the text component's document, below.
47
value = value.trim();
48             int startingPos = caret - value.length();
49             doc.remove(startingPos, value.length());
50             
51             // Surround the selected element with double-quotes, if necessary
52
String JavaDoc selectedStr = selected.toString();
53             String JavaDoc sep = separatorPattern.toString();
54             if (selectedStr.contains(sep))
55                 selectedStr = '"' + selectedStr + '"';
56             textComp.getDocument().insertString(startingPos, selectedStr + separatorPattern.toString() +" ", null);
57         } catch (BadLocationException JavaDoc e) {
58             e.printStackTrace();
59         }
60
61         popup.setVisible(false);
62     }
63
64 }
65
Popular Tags