PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/pooka-3.0-080505/net/suberic/pooka/gui/propedit/NewStoreWizardController.java

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