KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > gui > propedit > AddressEntryController


1 package net.suberic.pooka.gui.propedit;
2 import java.util.*;
3 import net.suberic.pooka.*;
4 import net.suberic.util.gui.propedit.*;
5 import net.suberic.util.VariableBundle;
6
7 /**
8  * The controller class for the AddressEntry.
9  */

10 public class AddressEntryController extends WizardController {
11
12   AddressBook mBook;
13   AddressBookEntry mEntry = null;
14   String JavaDoc mOriginalEntryName = null;
15
16   /**
17    * Creates an AddressEntryController.
18    */

19   public AddressEntryController(String JavaDoc sourceTemplate, WizardEditorPane wep) {
20     super(sourceTemplate, wep);
21   }
22
23   /**
24    * Saves all of the properties for this wizard.
25    */

26   protected void saveProperties() throws PropertyValueVetoException {
27     // check to make sure that the properties are valid.
28
String JavaDoc name = getManager().getCurrentProperty("AddressBook.editor.addressList._newAddress.personalName", "");
29     if (mOriginalEntryName == null || ! name.equalsIgnoreCase(mOriginalEntryName)) {
30       if (mBook.getAddressMatcher().matchExactly(name).length > 0) {
31         throw new PropertyValueVetoException("AddressBook.editor.addressList._newAddress.personalName", name, "Address already exists", null);
32       }
33       mEntry = mBook.newAddressBookEntry();
34     } else {
35       AddressBookEntry[] matches = mBook.getAddressMatcher().matchExactly(name);
36       if (matches != null && matches.length > 0) {
37         mEntry = matches[0];
38       } else {
39         mEntry = mBook.newAddressBookEntry();
40       }
41     }
42
43
44     mEntry.setPersonalName(name);
45     mEntry.setFirstName(getManager().getCurrentProperty("AddressBook.editor.addressList._newAddress.firstName", ""));
46     mEntry.setLastName(getManager().getCurrentProperty("AddressBook.editor.addressList._newAddress.lastName", ""));
47
48
49     try {
50       mEntry.setAddress(new javax.mail.internet.InternetAddress JavaDoc (getManager().getCurrentProperty("AddressBook.editor.addressList._newAddress.address", "")));
51     } catch (Exception JavaDoc e) {
52       throw new PropertyValueVetoException(e.getMessage());
53     }
54
55     mBook.addAddress(mEntry);
56
57     // and clear the property.
58

59     getManager().setProperty("AddressBook.editor.addressList._newAddress.personalName", "");
60     getManager().setProperty("AddressBook.editor.addressList._newAddress.firstName", "");
61     getManager().setProperty("AddressBook.editor.addressList._newAddress.lastName", "");
62
63     getManager().setProperty("AddressBook.editor.addressList._newAddress.address", "");
64
65   }
66   /**
67    * Finsihes the wizard.
68    */

69   public void finishWizard() throws PropertyValueVetoException {
70     getEditorPane().validateProperty(mState);
71
72     saveProperties();
73     getEditorPane().getWizardContainer().closeWizard();
74   }
75
76   public void setUniqueProperty(PropertyEditorUI editor, String JavaDoc originalValue, String JavaDoc propertyName) {
77     String JavaDoc value = originalValue;
78     boolean success = false;
79     for (int i = 0 ; ! success && i < 10; i++) {
80       if (i != 0) {
81         value = originalValue + "_" + i;
82       }
83       try {
84         editor.setOriginalValue(value);
85         editor.resetDefaultValue();
86         getManager().setTemporaryProperty(propertyName, value);
87         success = true;
88       } catch (PropertyValueVetoException pvve) {
89         // on an exception, just start over.
90
}
91     }
92   }
93
94   /**
95    * Loads the given entry.
96    */

97   public void loadEntry(AddressBookEntry pEntry) {
98     try {
99       getManager().getPropertyEditor("AddressBook.editor.addressList._newAddress.personalName").setOriginalValue(pEntry.getPersonalName());
100       getManager().getPropertyEditor("AddressBook.editor.addressList._newAddress.personalName").resetDefaultValue();
101       getManager().getPropertyEditor("AddressBook.editor.addressList._newAddress.firstName").setOriginalValue(pEntry.getFirstName());
102       getManager().getPropertyEditor("AddressBook.editor.addressList._newAddress.firstName").resetDefaultValue();
103       getManager().getPropertyEditor("AddressBook.editor.addressList._newAddress.lastName").setOriginalValue(pEntry.getLastName());
104       getManager().getPropertyEditor("AddressBook.editor.addressList._newAddress.lastName").resetDefaultValue();
105       getManager().getPropertyEditor("AddressBook.editor.addressList._newAddress.address").setOriginalValue(pEntry.getAddressString() != null ? pEntry.getAddressString() : "");
106       getManager().getPropertyEditor("AddressBook.editor.addressList._newAddress.address").resetDefaultValue();
107     } catch (Exception JavaDoc e) {
108       e.printStackTrace();
109     }
110     mOriginalEntryName = pEntry.getPersonalName();
111   }
112
113   /**
114    * Sets the AddressBook.
115    */

116   public void setAddressBook(AddressBook pBook) {
117     mBook = pBook;
118   }
119
120   /**
121    * Gets the AddressBookEntry created by this controller.
122    */

123   public AddressBookEntry getEntry() {
124     return mEntry;
125   }
126 }
127
Popular Tags