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 9 public class NewStoreWizardController extends WizardController { 10 11 14 public NewStoreWizardController(String sourceTemplate, WizardEditorPane wep) { 15 super(sourceTemplate, wep); 16 } 17 18 22 public void checkStateTransition(String oldState, String newState) throws PropertyValueVetoException { 23 getEditorPane().validateProperty(oldState); 24 if (newState.equals("userInfo") && oldState.equals("storeConfig")) { 25 String protocol = getManager().getCurrentProperty("NewStoreWizard.editors.store.protocol", "imap"); 28 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 getManager().setTemporaryProperty("NewStoreWizard.editors.user.from", user + "@" + server); 34 PropertyEditorUI fromEditor = getManager().getPropertyEditor("NewStoreWizard.editors.user.from"); 35 fromEditor.setOriginalValue(user + "@" + server); 37 fromEditor.resetDefaultValue(); 38 39 } else { 40 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 } 50 String address = username + "@" + hostname; 51 getManager().setTemporaryProperty("NewStoreWizard.editors.user.from", address); 52 PropertyEditorUI fromEditor = getManager().getPropertyEditor("NewStoreWizard.editors.user.from"); 53 fromEditor.setOriginalValue(address); 55 fromEditor.resetDefaultValue(); 56 } 57 } else if (newState.equals("outgoingServer") && oldState.equals("userInfo")) { 58 String protocol = getManager().getCurrentProperty("NewStoreWizard.editors.store.protocol", "imap"); 60 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 String userProfileName= getManager().getCurrentProperty("NewStoreWizard.editors.user.userProfile", "__default"); 92 if (userProfileName.equalsIgnoreCase("__new")) { 93 userProfileName = getManager().getCurrentProperty("NewStoreWizard.editors.user.from", ""); 94 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 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 (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 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 (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 160 protected void saveProperties() throws PropertyValueVetoException { 161 Properties storeProperties = createStoreProperties(); 162 Properties userProperties = createUserProperties(); 163 Properties smtpProperties = createSmtpProperties(); 164 166 addAll(storeProperties); 167 addAll(userProperties); 168 addAll(smtpProperties); 169 170 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 appendProperty("OutgoingServer", smtpServerName); 199 } 200 } 201 } 202 } 203 204 207 public void finishWizard() throws PropertyValueVetoException { 208 saveProperties(); 209 getEditorPane().getWizardContainer().closeWizard(); 210 } 211 212 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 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", System.getProperty("user.home") + java.io.File.separator + ".pooka"))); 248 } else if (protocol.equalsIgnoreCase("maildir")) { 249 returnValue.setProperty("Store." + accountName + ".mailDir", getManager().getCurrentProperty("NewStoreWizard.editors.store.mailDir", System.getProperty("user.home") + java.io.File.separator + "Maildir")); 250 } 251 252 258 259 return returnValue; 260 } 261 262 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 294 public Properties createSmtpProperties() { 295 Properties returnValue = new Properties(); 296 297 String defaultUser = getManager().getCurrentProperty("NewStoreWizard.editors.user.userProfile", "__default"); 298 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 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 } 354 } 355 } 356 357 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 } 366 | Popular Tags |