KickJava   Java API By Example, From Geeks To Geeks.

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


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

9 public class NewUserProfileWizardController extends WizardController {
10
11   /**
12    * Creates a NewUserProfileWizardController.
13    */

14   public NewUserProfileWizardController(String JavaDoc sourceTemplate, WizardEditorPane wep) {
15     super(sourceTemplate, wep);
16   }
17
18   /**
19    * Checks the state transition to make sure that we can move from
20    * state to state.
21    */

22   public void checkStateTransition(String JavaDoc oldState, String JavaDoc newState) throws PropertyValueVetoException {
23     getEditorPane().validateProperty(oldState);
24     if (newState.equals("userName")) {
25
26       String JavaDoc userName = getManager().getCurrentProperty("UserProfile._newValueWizard.user.from", "");
27       PropertyEditorUI userNameEditor = getManager().getPropertyEditor("UserProfile._newValueWizard.userName.userName");
28
29       setUniqueProperty(userNameEditor, userName, "UserProfile._newValueWizard.userName.userName");
30
31       String JavaDoc smtpServerName = "";
32
33       // set the smtp servername
34
PropertyEditorUI smtpServerNameEditor = getManager().getPropertyEditor("UserProfile._newValueWizard.userName.smtpServerName");
35
36       smtpServerName= getManager().getCurrentProperty("NewStoreWizard.editors.smtp.outgoingServer", "__default");
37       if (smtpServerName.equalsIgnoreCase("__new")) {
38         smtpServerName = getManager().getCurrentProperty("NewStoreWizard.editors.smtp.server", "");
39         setUniqueProperty(smtpServerNameEditor, smtpServerName, "UserProfile._newValueWizard.userName.smtpServerName");
40       } else if (smtpServerName.equalsIgnoreCase("__default")) {
41         smtpServerName = getManager().getProperty("NewStoreWizard.editors.smtp.outgoingServer.listMapping.__default.label", "< Global Default SMTP Server >");
42         smtpServerNameEditor.setOriginalValue(smtpServerName);
43         smtpServerNameEditor.resetDefaultValue();
44       } else {
45         smtpServerNameEditor.setOriginalValue("<" + smtpServerName + ">");
46         smtpServerNameEditor.resetDefaultValue();
47       }
48     }
49   }
50
51   /**
52    * Gets the next state.
53    */

54   public String JavaDoc getNextState(String JavaDoc currentState) {
55     int current = mStateList.indexOf(mState);
56     if (current > -1 && current < (mStateList.size() -1)) {
57       String JavaDoc newState = mStateList.get(current + 1);
58       return newState;
59     } else {
60       return null;
61     }
62   }
63
64   /**
65    * Gets the state that should be displayed next from a back request.
66    */

67   public String JavaDoc getBackState(String JavaDoc currentState) {
68     int current = mStateList.indexOf(currentState);
69     if (current >= 1) {
70       String JavaDoc newState = mStateList.get(current - 1);
71       return newState;
72     } else {
73       return null;
74     }
75   }
76
77
78   /**
79    * Saves all of the properties for this wizard.
80    */

81   protected void saveProperties() throws PropertyValueVetoException {
82     Properties userProperties = createUserProperties();
83     Properties smtpProperties = createSmtpProperties();
84     //getManager().clearValues();
85

86     addAll(userProperties);
87     addAll(smtpProperties);
88
89     // now add the values to the store, user, and smtp server editors,
90
// if necessary.
91
String JavaDoc accountName = getManager().getCurrentProperty("UserProfile._newValueWizard.userName.userName", "testUserProfile");
92     MultiEditorPane mep = (MultiEditorPane) getManager().getPropertyEditor("UserProfile");
93     if (mep != null) {
94       mep.addNewValue(accountName);
95     } else {
96       appendProperty("UserProfile", accountName);
97     }
98
99     String JavaDoc defaultSmtpServer= getManager().getCurrentProperty("NewStoreWizard.editors.smtp.outgoingServer", "__default");
100     if (defaultSmtpServer.equals("__new")) {
101       String JavaDoc smtpServerName = getManager().getCurrentProperty("UserProfile._newValueWizard.userName.smtpServerName", "");
102       mep = (MultiEditorPane) getManager().getPropertyEditor("OutgoingServer");
103       if (mep != null) {
104         mep.addNewValue(smtpServerName);
105       } else {
106         // if there's no editor, then set the value itself.
107
appendProperty("OutgoingServer", smtpServerName);
108       }
109     }
110   }
111
112   /**
113    * Finsihes the wizard.
114    */

115   public void finishWizard() throws PropertyValueVetoException {
116     saveProperties();
117     getEditorPane().getWizardContainer().closeWizard();
118   }
119
120   /**
121    * Creates the userProperties from the wizard values.
122    */

123   public Properties createUserProperties() {
124     Properties returnValue = new Properties();
125
126     String JavaDoc userName = getManager().getCurrentProperty("UserProfile._newValueWizard.userName.userName", "testUserProfile");
127
128     String JavaDoc from = getManager().getCurrentProperty("UserProfile._newValueWizard.user.from", "test@example.com");
129     String JavaDoc fromPersonal = getManager().getCurrentProperty("UserProfile._newValueWizard.user.fromPersonal", "");
130     String JavaDoc replyTo = getManager().getCurrentProperty("UserProfile._newValueWizard.user.replyTo", "");
131     String JavaDoc replyToPersonal = getManager().getCurrentProperty("UserProfile._newValueWizard.user.replyToPersonal", "");
132
133     returnValue.setProperty("UserProfile." + userName + ".mailHeaders.From", from );
134     returnValue.setProperty("UserProfile." + userName + ".mailHeaders.FromPersonal", fromPersonal);
135     returnValue.setProperty("UserProfile." + userName + ".mailHeaders.ReplyTo", replyTo);
136     returnValue.setProperty("UserProfile." + userName + ".mailHeaders.ReplyToPersonal", replyToPersonal);
137
138     returnValue.setProperty("UserProfile." + userName + ".mailHeaders.ReplyToPersonal", replyToPersonal);
139
140     return returnValue;
141   }
142
143   /**
144    * Creates the smtpProperties from the wizard values.
145    */

146   public Properties createSmtpProperties() {
147     Properties returnValue = new Properties();
148
149     String JavaDoc userName = getManager().getCurrentProperty("UserProfile._newValueWizard.userName.userName", "testUserProfile");
150
151     String JavaDoc defaultSmtpServer = getManager().getCurrentProperty("NewStoreWizard.editors.smtp.outgoingServer", "__default");
152     if (defaultSmtpServer.equals("__new")) {
153       String JavaDoc serverName = getManager().getCurrentProperty("UserProfile._newValueWizard.userName.smtpServerName", "");
154
155       String JavaDoc server = getManager().getCurrentProperty("NewStoreWizard.editors.smtp.server", "");
156       String JavaDoc port = getManager().getCurrentProperty("NewStoreWizard.editors.smtp.port", "");
157       String JavaDoc authenticated = getManager().getCurrentProperty("NewStoreWizard.editors.smtp.authenticated", "");
158       String JavaDoc user = getManager().getCurrentProperty("NewStoreWizard.editors.smtp.user", "");
159       String JavaDoc password = getManager().getCurrentProperty("NewStoreWizard.editors.smtp.password", "");
160
161       returnValue.setProperty("OutgoingServer." + serverName + ".server", server);
162       returnValue.setProperty("OutgoingServer." + serverName + ".port", port);
163       returnValue.setProperty("OutgoingServer." + serverName + ".authenticated", authenticated);
164       if (authenticated.equalsIgnoreCase("true")) {
165
166         returnValue.setProperty("OutgoingServer." + serverName + ".user", user );
167         returnValue.setProperty("OutgoingServer." + serverName + ".password", password);
168       }
169
170       returnValue.setProperty("UserProfile." + userName + ".mailServer", serverName);
171     } else {
172       returnValue.setProperty("UserProfile." + userName + ".mailServer", defaultSmtpServer);
173     }
174     return returnValue;
175   }
176
177   /**
178    * Adds all of the values from the given Properties to the
179    * PropertyEditorManager.
180    */

181   void addAll(Properties props) {
182     Set<String JavaDoc> names = props.stringPropertyNames();
183     for (String JavaDoc name: names) {
184       getManager().setProperty(name, props.getProperty(name));
185     }
186   }
187
188   public void setUniqueProperty(PropertyEditorUI editor, String JavaDoc originalValue, String JavaDoc propertyName) {
189     String JavaDoc value = originalValue;
190     boolean success = false;
191     for (int i = 0 ; ! success && i < 10; i++) {
192       if (i != 0) {
193         value = originalValue + "_" + i;
194       }
195       try {
196         editor.setOriginalValue(value);
197         editor.resetDefaultValue();
198         getManager().setTemporaryProperty(propertyName, value);
199         success = true;
200       } catch (PropertyValueVetoException pvve) {
201         // on an exception, just start over.
202
}
203     }
204   }
205
206   /**
207    * Appends the given value to the property.
208    */

209   public void appendProperty(String JavaDoc property, String JavaDoc value) {
210     List<String JavaDoc> current = getManager().getPropertyAsList(property, "");
211     current.add(value);
212     getManager().setProperty(property, VariableBundle.convertToString(current));
213   }
214 }
215
Popular Tags