01: /*
02: * MailAddressExpansion.java
03: *
04: * Copyright (C) 2000-2002 Peter Graves
05: * $Id: MailAddressExpansion.java,v 1.1.1.1 2002/09/24 16:09:46 piso Exp $
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * 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;
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: public MailAddressExpansion(Position dot) {
33: savedDot = new Position(dot);
34: final Line dotLine = dot.getLine();
35: final int dotOffset = dot.getOffset();
36: savedText = dotLine.getText();
37: // Get word before caret.
38: int begin = dotOffset - 1;
39: if (begin < 0)
40: return;
41: while (begin > 0) {
42: char c = dotLine.charAt(begin);
43: if (c == ',' || c == ':') {
44: ++begin;
45: break;
46: }
47: --begin;
48: }
49: Debug.assertTrue(begin >= 0);
50: while (begin < dotLine.length()) {
51: char c = dotLine.charAt(begin);
52: if (c == ',' || c == ':' || c == ' ' || c == '\t')
53: ++begin;
54: else
55: break;
56: }
57: int end = dotOffset;
58: if (begin >= end)
59: return;
60: prefix = dotLine.substring(begin, end);
61: prefixOffset = begin;
62: int length = prefix.length();
63: boolean ignoreCase = Utilities.isLowerCase(prefix);
64: candidates = new ArrayList();
65: AddressBook addressBook = AddressBook.getGlobalAddressBook();
66: if (addressBook != null) {
67: final int limit = addressBook.size();
68: for (int i = 0; i < limit; i++) {
69: AddressBookEntry entry = addressBook.getEntry(i);
70: String personal = entry.getPersonal();
71: if (personal != null) {
72: if (personal.regionMatches(ignoreCase, 0, prefix,
73: 0, length)) {
74: candidates.add(entry);
75: continue;
76: }
77: }
78: // Not added yet.
79: if (entry.getAddress().regionMatches(ignoreCase, 0,
80: prefix, 0, length))
81: candidates.add(entry);
82: }
83: }
84: }
85:
86: public String getNextCandidate() {
87: if (candidates == null || candidates.size() == 0)
88: return null;
89: int index = last + 1;
90: if (index == candidates.size())
91: index = 0;
92: last = index;
93: AddressBookEntry entry = (AddressBookEntry) candidates
94: .get(index);
95: return entry.toString();
96: }
97: }
|