KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > mail > MailAddressExpansion


1 /*
2  * MailAddressExpansion.java
3  *
4  * Copyright (C) 2000-2002 Peter Graves
5  * $Id: MailAddressExpansion.java,v 1.1.1.1 2002/09/24 16:09:46 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j.mail;
23
24 import java.util.ArrayList JavaDoc;
25 import org.armedbear.j.Debug;
26 import org.armedbear.j.Expansion;
27 import org.armedbear.j.Line;
28 import org.armedbear.j.Position;
29 import org.armedbear.j.Utilities;
30
31 public final class MailAddressExpansion extends Expansion
32 {
33     public MailAddressExpansion(Position dot)
34     {
35         savedDot = new Position(dot);
36         final Line dotLine = dot.getLine();
37         final int dotOffset = dot.getOffset();
38         savedText = dotLine.getText();
39         // Get word before caret.
40
int begin = dotOffset - 1;
41         if (begin < 0)
42             return;
43         while (begin > 0) {
44             char c = dotLine.charAt(begin);
45             if (c == ',' || c ==':') {
46                 ++begin;
47                 break;
48             }
49             --begin;
50         }
51         Debug.assertTrue(begin >= 0);
52         while (begin < dotLine.length()) {
53             char c = dotLine.charAt(begin);
54             if (c ==',' || c == ':' || c == ' ' || c == '\t')
55                 ++begin;
56             else
57                 break;
58         }
59         int end = dotOffset;
60         if (begin >= end)
61             return;
62         prefix = dotLine.substring(begin, end);
63         prefixOffset = begin;
64         int length = prefix.length();
65         boolean ignoreCase = Utilities.isLowerCase(prefix);
66         candidates = new ArrayList JavaDoc();
67         AddressBook addressBook = AddressBook.getGlobalAddressBook();
68         if (addressBook != null) {
69             final int limit = addressBook.size();
70             for (int i = 0; i < limit; i++) {
71                 AddressBookEntry entry = addressBook.getEntry(i);
72                 String JavaDoc personal = entry.getPersonal();
73                 if (personal != null) {
74                     if (personal.regionMatches(ignoreCase, 0, prefix, 0, length)) {
75                         candidates.add(entry);
76                         continue;
77                     }
78                 }
79                 // Not added yet.
80
if (entry.getAddress().regionMatches(ignoreCase, 0, prefix, 0, length))
81                     candidates.add(entry);
82             }
83         }
84     }
85
86     public String JavaDoc getNextCandidate()
87     {
88         if (candidates == null || candidates.size() == 0)
89             return null;
90         int index = last + 1;
91         if (index == candidates.size())
92             index = 0;
93         last = index;
94         AddressBookEntry entry = (AddressBookEntry) candidates.get(index);
95         return entry.toString();
96     }
97 }
98
Popular Tags