KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > AddressBookManager


1 package net.suberic.pooka;
2
3 import javax.mail.*;
4 import java.util.*;
5 import net.suberic.util.*;
6
7 /**
8  * This class manages the a list of AddressBooks.
9  */

10
11 public class AddressBookManager implements ValueChangeListener {
12
13   private Vector addressBookList;
14   private Vector valueChangeListenerList = new Vector();
15
16   public AddressBookManager() {
17     addressBookList = createAddressBookList();
18     Pooka.getResources().addValueChangeListener(this, "AddressBook");
19   }
20
21   //-----------------------
22
// public interface.
23

24   /**
25    * As defined in net.suberic.util.ValueChangeListener.
26    *
27    * This listens for changes to the "AddressBook" property and calls
28    * refreshAddressBooks() when it gets one.
29    */

30   public void valueChanged(String JavaDoc changedValue) {
31     if (changedValue.equals("AddressBook"))
32       refreshAddressBooks();
33   }
34
35   /**
36    * This returns a Vector with all the currently registered AddressBook
37    * objects.
38    */

39   public java.util.Vector JavaDoc getAddressBookList() {
40     return new Vector(addressBookList);
41   }
42
43   /**
44    * This adds the addressBook with the given addressBookName to the
45    * allAddressBooks list.
46    */

47   public void addAddressBook(String JavaDoc addressBookName) {
48     if (getAddressBook(addressBookName) == null) {
49       appendToAddressBookString(addressBookName);
50     }
51   }
52
53   /**
54    * This adds the addressBooks with the given addressBookNames to the
55    * allAddressBooks list.
56    */

57   public void addAddressBook(String JavaDoc[] addressBookName) {
58     if (addressBookName != null && addressBookName.length > 0) {
59       StringBuffer JavaDoc addressBookString = new StringBuffer JavaDoc();
60       for (int i = 0 ; i < addressBookName.length; i++) {
61         if (getAddressBook(addressBookName[i]) == null)
62           addressBookString.append(addressBookName[i] + ":");
63       }
64       if (addressBookString.length() > 0)
65         appendToAddressBookString(new String JavaDoc(addressBookString.deleteCharAt(addressBookString.length() -1)));
66     }
67   }
68
69   /**
70    * This removes the addressBook with the given addressBookName.
71    */

72   public void removeAddressBook(String JavaDoc addressBookName) {
73     if (getAddressBook(addressBookName) != null)
74       removeFromAddressBookString(new String JavaDoc[] { addressBookName });
75   }
76
77   /**
78    * This removes the addressBooks with the given addressBookNames.
79    */

80   public void removeAddressBook(String JavaDoc[] addressBookNames) {
81     // this is probably not necessary at all, but what the hell?
82

83     if (addressBookNames == null || addressBookNames.length < 1)
84       return;
85
86     Vector matches = new Vector();
87     for ( int i = 0; i < addressBookNames.length; i++) {
88       if (getAddressBook(addressBookNames[i]) != null)
89         matches.add(addressBookNames[i]);
90
91     }
92
93     if (matches.size() < 1)
94       return;
95
96     String JavaDoc[] removedAddressBooks = new String JavaDoc[matches.size()];
97
98     for (int i = 0; i < matches.size(); i++)
99       removedAddressBooks[i] = (String JavaDoc) matches.elementAt(i);
100
101     removeFromAddressBookString(removedAddressBooks);
102   }
103
104   /**
105    * This removes the given AddressBook.
106    */

107   public void removeAddressBook(AddressBook addressBook) {
108     if (addressBook != null)
109       removeAddressBook(addressBook.getAddressBookID());
110   }
111
112   /**
113    * This removes the given AddressBooks.
114    */

115   public void removeAddressBook(AddressBook[] addressBook) {
116     if (addressBook != null && addressBook.length > 0) {
117       String JavaDoc[] addressBookNames = new String JavaDoc[addressBook.length];
118       for (int i = 0; i < addressBook.length; i++) {
119         if (addressBook[i] != null)
120           addressBookNames[i] = addressBook[i].getAddressBookID();
121       }
122
123       removeAddressBook(addressBookNames);
124     }
125   }
126
127   /**
128    * This compares the addressBookList object with the AddressBook property, and
129    * updates the addressBookList appropriately.
130    */

131   public void refreshAddressBooks() {
132     Vector newAddressBookList = new Vector();
133
134     StringTokenizer tokens = new StringTokenizer(Pooka.getProperty("AddressBook", ""), ":");
135
136     String JavaDoc addressBookID;
137     while (tokens.hasMoreTokens()) {
138       addressBookID = tokens.nextToken();
139       AddressBook currentAddressBook = getAddressBook(addressBookID);
140       if (currentAddressBook != null) {
141         newAddressBookList.add(currentAddressBook);
142       } else {
143         currentAddressBook = createAddressBook(addressBookID);
144         if (currentAddressBook != null) {
145           newAddressBookList.add(currentAddressBook);
146
147           if (Pooka.getProperty("AddressBook._default", "").equalsIgnoreCase(""))
148             Pooka.setProperty("AddressBook._default", addressBookID);
149         }
150
151       }
152     }
153
154     if (! newAddressBookList.equals(addressBookList)) {
155       addressBookList = newAddressBookList;
156       fireAddressBookListChangedEvent();
157     }
158   }
159
160   /**
161    * Creates an address book.
162    */

163   public AddressBook createAddressBook(String JavaDoc id) {
164     String JavaDoc type = Pooka.getProperty("AddressBook." + id + ".type", "");
165     if (type.equalsIgnoreCase("file")) {
166       AddressBook returnValue = new net.suberic.pooka.vcard.VcardAddressBook();
167       returnValue.configureAddressBook(id);
168       return returnValue;
169     }
170
171     return null;
172   }
173
174   /**
175    * This returns the AddressBook which corresponds to the given name.
176    */

177   public AddressBook getAddressBook(String JavaDoc name) {
178     if (addressBookList == null)
179       return null;
180
181     for (int i = 0; i < addressBookList.size(); i++) {
182       AddressBook currentBook = (AddressBook) addressBookList.elementAt(i);
183       if (currentBook != null && currentBook.getAddressBookID().equals(name))
184         return currentBook;
185     }
186
187     return null;
188   }
189
190   /**
191    * This adds a ValueChangeListener to the local listener list.
192    */

193   public void addValueChangeListener(ValueChangeListener vcl) {
194     if (! valueChangeListenerList.contains(vcl))
195       valueChangeListenerList.add(vcl);
196   }
197
198   /**
199    * This removes a ValueChangeListener from the local listener list.
200    */

201   public void removeValueChangeListener(ValueChangeListener vcl) {
202     valueChangeListenerList.remove(vcl);
203   }
204
205   /**
206    * This notifies all listeners that the AddressBookList has changed.
207    */

208
209   public void fireAddressBookListChangedEvent() {
210     for (int i = 0; i < valueChangeListenerList.size(); i++)
211       ((ValueChangeListener)valueChangeListenerList.elementAt(i)).valueChanged("AddressBook");
212   }
213
214
215   //---------------------------
216
// the background stuff.
217

218   /**
219    * This loads and creates all the AddressBooks using the "AddressBook"
220    * property of the main Pooka VariableBundle.
221    */

222   private Vector createAddressBookList() {
223     Vector allAddressBooks = new Vector();
224     String JavaDoc addressBookID = null;
225
226     StringTokenizer tokens = new StringTokenizer(Pooka.getProperty("AddressBook", ""), ":");
227
228     while (tokens.hasMoreTokens()) {
229       addressBookID=(String JavaDoc)tokens.nextToken();
230       allAddressBooks.add(createAddressBook(addressBookID));
231     }
232
233     return allAddressBooks;
234   }
235
236   /**
237    * This appends the newAddressBookString to the "AddressBook" property.
238    */

239   private void appendToAddressBookString(String JavaDoc newAddressBookString) {
240     String JavaDoc oldValue = Pooka.getProperty("AddressBook");
241     String JavaDoc newValue;
242     if (oldValue.length() > 0 && oldValue.charAt(oldValue.length() -1) != ':') {
243       newValue = oldValue + ":" + newAddressBookString;
244     } else {
245       newValue = oldValue + newAddressBookString;
246     }
247
248     Pooka.setProperty("AddressBook", newValue);
249   }
250
251   /**
252    * This removes the addressBook names in the addressBookNames array from the
253    * "AddressBook" property.
254    */

255   private void removeFromAddressBookString(String JavaDoc[] addressBookNames) {
256     StringTokenizer tokens = new StringTokenizer(Pooka.getProperty("AddressBook", ""), ":");
257
258     boolean first = true;
259     StringBuffer JavaDoc newValue = new StringBuffer JavaDoc();
260     String JavaDoc addressBookID;
261
262     while (tokens.hasMoreTokens()) {
263       addressBookID=tokens.nextToken();
264       boolean keep=true;
265
266       for (int i = 0; keep == true && i < addressBookNames.length; i++) {
267         if (addressBookID.equals(addressBookNames[i]))
268           keep = false;
269       }
270       if (keep) {
271         if (!first)
272           newValue.append(":");
273
274         newValue.append(addressBookID);
275         first = false;
276       }
277
278     }
279
280     Pooka.setProperty("AddressBook", newValue.toString());
281   }
282
283   /**
284    * Gets the default Address Book, if there is one.
285    */

286   public AddressBook getDefault() {
287     String JavaDoc defaultName = Pooka.getProperty("AddressBook._default", "");
288     if (! defaultName.equals("")) {
289       AddressBook defaultBook = getAddressBook(defaultName);
290       return defaultBook;
291     } else
292       return null;
293
294   }
295
296
297 }
298
299
Popular Tags