/projects/pooka-3.0-080505/net/suberic/pooka/gui/propedit/NewStoreWizardController.java
Java | 365 lines | 253 code | 42 blank | 70 comment | 59 complexity | 4ad3346c706c13a3da96cac79524c725 MD5 | raw file
1package net.suberic.pooka.gui.propedit;
2import java.util.*;
3import net.suberic.util.gui.propedit.*;
4import net.suberic.util.VariableBundle;
5
6/**
7 * The controller class for the NewStoreWizard.
8 */
9public class NewStoreWizardController extends WizardController {
10
11 /**
12 * Creates a NewStoreWizardController.
13 */
14 public NewStoreWizardController(String 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 oldState, String newState) throws PropertyValueVetoException {
23 getEditorPane().validateProperty(oldState);
24 if (newState.equals("userInfo") && oldState.equals("storeConfig")) {
25 // load default values into the user configuration.
26 //System.err.println("moving to userInfo; setting default values.");
27 String protocol = getManager().getCurrentProperty("NewStoreWizard.editors.store.protocol", "imap");
28 //System.err.println("protocol = " + protocol);
29 if (protocol.equalsIgnoreCase("imap") || protocol.equalsIgnoreCase("pop3")) {
30 String user = getManager().getCurrentProperty("NewStoreWizard.editors.store.user", "");
31 String server = getManager().getCurrentProperty("NewStoreWizard.editors.store.server", "");
32 //System.err.println("setting username to " + user + "@" + server);
33 getManager().setTemporaryProperty("NewStoreWizard.editors.user.from", user + "@" + server);
34 PropertyEditorUI fromEditor = getManager().getPropertyEditor("NewStoreWizard.editors.user.from");
35 //System.err.println("got fromEditor " + fromEditor);
36 fromEditor.setOriginalValue(user + "@" + server);
37 fromEditor.resetDefaultValue();
38
39 } else {
40 //System.err.println("local store");
41 String username = System.getProperty("user.name");
42 String hostname = "localhost";
43 try {
44 java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
45 hostname = localMachine.getHostName();
46
47 } catch(java.net.UnknownHostException uhe) {
48 // just use 'localhost'
49 }
50 String address = username + "@" + hostname;
51 getManager().setTemporaryProperty("NewStoreWizard.editors.user.from", address);
52 PropertyEditorUI fromEditor = getManager().getPropertyEditor("NewStoreWizard.editors.user.from");
53 //System.err.println("got fromEditor " + fromEditor);
54 fromEditor.setOriginalValue(address);
55 fromEditor.resetDefaultValue();
56 }
57 } else if (newState.equals("outgoingServer") && oldState.equals("userInfo")) {
58 // load default values into the smtp configuration.
59 String protocol = getManager().getCurrentProperty("NewStoreWizard.editors.store.protocol", "imap");
60 //System.err.println("protocol = " + protocol);
61 if (protocol.equalsIgnoreCase("imap") || protocol.equalsIgnoreCase("pop3")) {
62 String user = getManager().getCurrentProperty("NewStoreWizard.editors.store.user", "");
63 String server = getManager().getCurrentProperty("NewStoreWizard.editors.store.server", "");
64 getManager().setTemporaryProperty("NewStoreWizard.editors.smtp.user", user);
65 PropertyEditorUI userEditor = getManager().getPropertyEditor("NewStoreWizard.editors.smtp.user");
66 userEditor.setOriginalValue(user);
67 userEditor.resetDefaultValue();
68
69 getManager().setTemporaryProperty("NewStoreWizard.editors.smtp.server", server);
70 PropertyEditorUI serverEditor = getManager().getPropertyEditor("NewStoreWizard.editors.smtp.server");
71 serverEditor.setOriginalValue(server);
72 serverEditor.resetDefaultValue();
73 } else {
74 getManager().setTemporaryProperty("NewStoreWizard.editors.smtp.server", "localhost");
75 PropertyEditorUI serverEditor = getManager().getPropertyEditor("NewStoreWizard.editors.smtp.server");
76 serverEditor.setOriginalValue("localhost");
77 serverEditor.resetDefaultValue();
78
79 }
80 } else if (newState.equals("storeName")) {
81 String user = getManager().getCurrentProperty("NewStoreWizard.editors.store.user", "");
82 String server = getManager().getCurrentProperty("NewStoreWizard.editors.store.server", "");
83 String storeName = user + "@" + server;
84 PropertyEditorUI storeNameEditor = getManager().getPropertyEditor("NewStoreWizard.editors.store.storeName");
85
86 setUniqueProperty(storeNameEditor, storeName, "NewStoreWizard.editors.store.storeName");
87
88 String smtpServerName = "";
89
90 // set the username
91 String userProfileName= getManager().getCurrentProperty("NewStoreWizard.editors.user.userProfile", "__default");
92 if (userProfileName.equalsIgnoreCase("__new")) {
93 userProfileName = getManager().getCurrentProperty("NewStoreWizard.editors.user.from", "");
94 // set the smtp server name only for new users
95 smtpServerName= getManager().getCurrentProperty("NewStoreWizard.editors.smtp.outgoingServer", "__default");
96 if (smtpServerName.equalsIgnoreCase("__new")) {
97 smtpServerName = getManager().getCurrentProperty("NewStoreWizard.editors.smtp.server", "");
98 } else if (smtpServerName.equalsIgnoreCase("__default")) {
99 smtpServerName = getManager().getProperty("NewStoreWizard.editors.smtp.outgoingServer.listMapping.__default.label", "< Global Default SMTP Server >");
100 }
101 } else if (userProfileName.equalsIgnoreCase("__default")) {
102 userProfileName = getManager().getProperty("NewStoreWizard.editors.user.userProfile.listMapping.__default.label", "< Global Default Profile >");
103 }
104 PropertyEditorUI userProfileNameEditor = getManager().getPropertyEditor("NewStoreWizard.editors.user.userName");
105 setUniqueProperty(userProfileNameEditor, userProfileName, "NewStoreWizard.editors.user.userName");
106
107 PropertyEditorUI smtpServerNameEditor = getManager().getPropertyEditor("NewStoreWizard.editors.smtp.smtpServerName");
108 setUniqueProperty(smtpServerNameEditor, smtpServerName, "NewStoreWizard.editors.smtp.smtpServerName");
109 }
110 }
111
112 /**
113 * Gets the next state.
114 */
115 public String getNextState(String currentState) {
116 int current = mStateList.indexOf(mState);
117 if (current > -1 && current < (mStateList.size() -1)) {
118 String newState = mStateList.get(current + 1);
119 // if we're not creating a new user, skip the smtp server step.
120 if (newState.equals("outgoingServer")) {
121 String newUser = getManager().getCurrentProperty("NewStoreWizard.editors.user.userProfile", ListEditorPane.SELECTION_DEFAULT);
122 if (! newUser.equalsIgnoreCase(ListEditorPane.SELECTION_NEW)) {
123 if (current < (mStateList.size() -2)) {
124 newState = mStateList.get(current + 2);
125 }
126 }
127 }
128 return newState;
129 } else {
130 return null;
131 }
132 }
133
134 /**
135 * Gets the state that should be displayed next from a back request.
136 */
137 public String getBackState(String currentState) {
138 int current = mStateList.indexOf(currentState);
139 if (current >= 1) {
140 String newState = mStateList.get(current - 1);
141 // if we're not creating a new user, skip the smtp server step.
142 if (newState.equals("outgoingServer")) {
143 String newUser = getManager().getCurrentProperty("NewStoreWizard.editors.user.userProfile", ListEditorPane.SELECTION_DEFAULT);
144 if (! newUser.equalsIgnoreCase(ListEditorPane.SELECTION_NEW)) {
145 if (current >= 2) {
146 newState = mStateList.get(current - 2);
147 }
148 }
149 }
150 return newState;
151 } else {
152 return null;
153 }
154 }
155
156
157 /**
158 * Saves all of the properties for this wizard.
159 */
160 protected void saveProperties() throws PropertyValueVetoException {
161 Properties storeProperties = createStoreProperties();
162 Properties userProperties = createUserProperties();
163 Properties smtpProperties = createSmtpProperties();
164 //getManager().clearValues();
165
166 addAll(storeProperties);
167 addAll(userProperties);
168 addAll(smtpProperties);
169
170 // now add the values to the store, user, and smtp server editors,
171 // if necessary.
172 String accountName = getManager().getCurrentProperty("NewStoreWizard.editors.store.storeName", "testStore");
173 MultiEditorPane mep = (MultiEditorPane) getManager().getPropertyEditor("Store");
174 if (mep != null) {
175 mep.addNewValue(accountName);
176 } else {
177 appendProperty("Store", accountName);
178 }
179
180 String defaultUser = getManager().getCurrentProperty("NewStoreWizard.editors.user.userProfile", "__default");
181 if (defaultUser.equals("__new")) {
182 String userName = getManager().getCurrentProperty("NewStoreWizard.editors.user.userName", "");
183 mep = (MultiEditorPane) getManager().getPropertyEditor("UserProfile");
184 if (mep != null) {
185 mep.addNewValue(userName);
186 } else {
187 appendProperty("UserProfile", userName);
188 }
189
190 String defaultSmtpServer = getManager().getCurrentProperty("NewStoreWizard.editors.smtp.outgoingServer", "__default");
191 if (defaultSmtpServer.equals("__new")) {
192 String smtpServerName = getManager().getCurrentProperty("NewStoreWizard.editors.smtp.smtpServerName", "");
193 mep = (MultiEditorPane) getManager().getPropertyEditor("OutgoingServer");
194 if (mep != null) {
195 mep.addNewValue(smtpServerName);
196 } else {
197 // if there's no editor, then set the value itself.
198 appendProperty("OutgoingServer", smtpServerName);
199 }
200 }
201 }
202 }
203
204 /**
205 * Finsihes the wizard.
206 */
207 public void finishWizard() throws PropertyValueVetoException {
208 saveProperties();
209 getEditorPane().getWizardContainer().closeWizard();
210 }
211
212 /**
213 * Creates the storeProperties from the wizard values.
214 */
215 public Properties createStoreProperties() {
216 Properties returnValue = new Properties();
217
218 String accountName = getManager().getCurrentProperty("NewStoreWizard.editors.store.storeName", "testStore");
219 String protocol = getManager().getCurrentProperty("NewStoreWizard.editors.store.protocol", "imap");
220 returnValue.setProperty("Store." + accountName + ".protocol", protocol);
221
222 if (protocol.equalsIgnoreCase("imap")) {
223 returnValue.setProperty("Store." + accountName + ".server", getManager().getCurrentProperty("NewStoreWizard.editors.store.server", ""));
224 returnValue.setProperty("Store." + accountName + ".user", getManager().getCurrentProperty("NewStoreWizard.editors.store.user", ""));
225 returnValue.setProperty("Store." + accountName + ".password", getManager().getCurrentProperty("NewStoreWizard.editors.store.password", ""));
226 returnValue.setProperty("Store." + accountName + ".port", getManager().getCurrentProperty("NewStoreWizard.editors.store.port", ""));
227
228 returnValue.setProperty("Store." + accountName + ".useSubscribed", "true");
229 returnValue.setProperty("Store." + accountName + ".SSL", getManager().getCurrentProperty("NewStoreWizard.editors.store.SSL", "none"));
230 returnValue.setProperty("Store." + accountName + ".cacheMode", getManager().getCurrentProperty("NewStoreWizard.editors.store.cacheMode", "headersOnly"));
231 } else if (protocol.equalsIgnoreCase("pop3")) {
232 returnValue.setProperty("Store." + accountName + ".server", getManager().getCurrentProperty("NewStoreWizard.editors.store.server", ""));
233 returnValue.setProperty("Store." + accountName + ".user", getManager().getCurrentProperty("NewStoreWizard.editors.store.user", ""));
234 returnValue.setProperty("Store." + accountName + ".password", getManager().getCurrentProperty("NewStoreWizard.editors.store.password", ""));
235 returnValue.setProperty("Store." + accountName + ".port", getManager().getCurrentProperty("NewStoreWizard.editors.store.port", ""));
236
237 returnValue.setProperty("Store." + accountName + ".SSL", getManager().getCurrentProperty("NewStoreWizard.editors.store.SSL", "none"));
238 returnValue.setProperty("Store." + accountName + ".useMaildir", "true");
239 /*
240 returnValue.setProperty("Store." + accountName + ".leaveMessagesOnServer", getManager().getCurrentProperty("NewStoreWizard.editors.store.leaveOnServer", "true"));
241 if (manager.getCurrentProperty("NewStoreWizard.editors.store.leaveOnServer", "true").equalsIgnoreCase("true")) {
242 returnValue.setProperty("Store." + accountName + ".deleteOnServerOnLocalDelete", "true");
243 }
244 */
245 } else if (protocol.equalsIgnoreCase("mbox")) {
246 returnValue.setProperty("Store." + accountName + ".inboxLocation", getManager().getCurrentProperty("NewStoreWizard.editors.store.inboxLocation", "/var/spool/mail/" + System.getProperty("user.name")));
247 returnValue.setProperty("Store." + accountName + ".mailDirectory", getManager().getCurrentProperty("NewStoreWizard.editors.store.mailDirectory", getManager().getCurrentProperty("Pooka.cacheDirectory", "${pooka.root}" + java.io.File.separator + ".pooka")));
248 } else if (protocol.equalsIgnoreCase("maildir")) {
249 returnValue.setProperty("Store." + accountName + ".mailDir", getManager().getCurrentProperty("NewStoreWizard.editors.store.mailDir", "${pooka.root}" + java.io.File.separator + "Maildir"));
250 }
251
252 /*
253 List<String> storeList = getManager().getPropertyAsList("Store", "");
254 storeList.add(accountName);
255
256 returnValue.setProperty("Store", net.suberic.util.VariableBundle.convertToString(storeList));
257 */
258
259 return returnValue;
260 }
261
262 /**
263 * Creates the userProperties from the wizard values.
264 */
265 public Properties createUserProperties() {
266 Properties returnValue = new Properties();
267
268 String storeName = getManager().getCurrentProperty("NewStoreWizard.editors.store.storeName", "testStore");
269
270 String defaultUser = getManager().getCurrentProperty("NewStoreWizard.editors.user.userProfile", "__default");
271 if (defaultUser.equals("__new")) {
272 String from = getManager().getCurrentProperty("NewStoreWizard.editors.user.from", "test@example.com");
273 String fromPersonal = getManager().getCurrentProperty("NewStoreWizard.editors.user.fromPersonal", "");
274 String replyTo = getManager().getCurrentProperty("NewStoreWizard.editors.user.replyTo", "");
275 String replyToPersonal = getManager().getCurrentProperty("NewStoreWizard.editors.user.replyToPersonal", "");
276
277 String userName = getManager().getCurrentProperty("NewStoreWizard.editors.user.userName", from);
278
279 returnValue.setProperty("UserProfile." + userName + ".mailHeaders.From", from );
280 returnValue.setProperty("UserProfile." + userName + ".mailHeaders.FromPersonal", fromPersonal);
281 returnValue.setProperty("UserProfile." + userName + ".mailHeaders.ReplyTo", replyTo);
282 returnValue.setProperty("UserProfile." + userName + ".mailHeaders.ReplyToPersonal", replyToPersonal);
283
284 returnValue.setProperty("Store." + storeName + ".defaultProfile", userName);
285 } else {
286 returnValue.setProperty("Store." + storeName + ".defaultProfile", defaultUser);
287 }
288 return returnValue;
289 }
290
291 /**
292 * Creates the smtpProperties from the wizard values.
293 */
294 public Properties createSmtpProperties() {
295 Properties returnValue = new Properties();
296
297 String defaultUser = getManager().getCurrentProperty("NewStoreWizard.editors.user.userProfile", "__default");
298 // only make smtp server changes if there's a new user.
299 if (defaultUser.equals("__new")) {
300 String userName = getManager().getCurrentProperty("NewStoreWizard.editors.user.userName", "newuser");
301
302 String defaultSmtpServer = getManager().getCurrentProperty("NewStoreWizard.editors.smtp.outgoingServer", "__default");
303 if (defaultSmtpServer.equals("__new")) {
304 String serverName = getManager().getCurrentProperty("NewStoreWizard.editors.smtp.smtpServerName", "newSmtpServer");
305 String server = getManager().getCurrentProperty("NewStoreWizard.editors.smtp.server", "");
306 String port = getManager().getCurrentProperty("NewStoreWizard.editors.smtp.port", "");
307 String authenticated = getManager().getCurrentProperty("NewStoreWizard.editors.smtp.authenticated", "");
308 String user = getManager().getCurrentProperty("NewStoreWizard.editors.smtp.user", "");
309 String password = getManager().getCurrentProperty("NewStoreWizard.editors.smtp.password", "");
310
311 returnValue.setProperty("OutgoingServer." + serverName + ".server", server);
312 returnValue.setProperty("OutgoingServer." + serverName + ".port", port);
313 returnValue.setProperty("OutgoingServer." + serverName + ".authenticated", authenticated);
314 if (authenticated.equalsIgnoreCase("true")) {
315
316 returnValue.setProperty("OutgoingServer." + serverName + ".user", user );
317 returnValue.setProperty("OutgoingServer." + serverName + ".password", password);
318 }
319
320 returnValue.setProperty("UserProfile." + userName + ".mailServer", serverName);
321 } else {
322 returnValue.setProperty("UserProfile." + userName + ".mailServer", defaultSmtpServer);
323 }
324 }
325 return returnValue;
326 }
327
328 /**
329 * Adds all of the values from the given Properties to the
330 * PropertyEditorManager.
331 */
332 void addAll(Properties props) {
333 Set<String> names = props.stringPropertyNames();
334 for (String name: names) {
335 getManager().setProperty(name, props.getProperty(name));
336 }
337 }
338
339 public void setUniqueProperty(PropertyEditorUI editor, String originalValue, String propertyName) {
340 String value = originalValue;
341 boolean success = false;
342 for (int i = 0 ; ! success && i < 10; i++) {
343 if (i != 0) {
344 value = originalValue + "_" + i;
345 }
346 try {
347 editor.setOriginalValue(value);
348 editor.resetDefaultValue();
349 getManager().setTemporaryProperty(propertyName, value);
350 success = true;
351 } catch (PropertyValueVetoException pvve) {
352 // on an exception, just start over.
353 }
354 }
355 }
356
357 /**
358 * Appends the given value to the property.
359 */
360 public void appendProperty(String property, String value) {
361 List<String> current = getManager().getPropertyAsList(property, "");
362 current.add(value);
363 getManager().setProperty(property, VariableBundle.convertToString(current));
364 }
365}