PageRenderTime 49ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/spiffyui-app/src/main/java/org/spiffyui/spsample/client/FormPanel.java

http://spiffyui.googlecode.com/
Java | 386 lines | 248 code | 56 blank | 82 comment | 44 complexity | f5c599b8af421ac5899e5071b50af9bc MD5 | raw file
Possible License(s): Apache-2.0
  1. /*******************************************************************************
  2. *
  3. * Copyright 2011 Spiffy UI Team
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. ******************************************************************************/
  18. package org.spiffyui.spsample.client;
  19. import java.util.ArrayList;
  20. import java.util.Date;
  21. import java.util.List;
  22. import org.spiffyui.client.JSUtil;
  23. import org.spiffyui.client.MessageUtil;
  24. import org.spiffyui.client.widgets.DatePickerTextBox;
  25. import org.spiffyui.client.widgets.FormFeedback;
  26. import org.spiffyui.client.widgets.button.FancyButton;
  27. import org.spiffyui.client.widgets.button.FancySaveButton;
  28. import com.google.gwt.core.client.GWT;
  29. import com.google.gwt.event.dom.client.ClickEvent;
  30. import com.google.gwt.event.dom.client.ClickHandler;
  31. import com.google.gwt.event.dom.client.KeyCodes;
  32. import com.google.gwt.event.dom.client.KeyUpEvent;
  33. import com.google.gwt.event.dom.client.KeyUpHandler;
  34. import com.google.gwt.user.client.Timer;
  35. import com.google.gwt.user.client.ui.HTMLPanel;
  36. import com.google.gwt.user.client.ui.PasswordTextBox;
  37. import com.google.gwt.user.client.ui.RadioButton;
  38. import com.google.gwt.user.client.ui.RootPanel;
  39. import com.google.gwt.user.client.ui.TextArea;
  40. import com.google.gwt.user.client.ui.TextBox;
  41. import com.google.gwt.user.client.ui.TextBoxBase;
  42. import com.google.gwt.user.client.ui.Widget;
  43. /**
  44. * This is the form sample panel
  45. *
  46. */
  47. public class FormPanel extends HTMLPanel implements KeyUpHandler
  48. {
  49. private static final SPSampleStrings STRINGS = (SPSampleStrings) GWT.create(SPSampleStrings.class);
  50. private static final String WIDE_TEXT_FIELD = "wideTextField";
  51. private TextBox m_firstName;
  52. private FormFeedback m_firstNameFeedback;
  53. private TextBox m_lastName;
  54. private FormFeedback m_lastNameFeedback;
  55. private TextBox m_email;
  56. private FormFeedback m_emailFeedback;
  57. private TextBox m_password;
  58. private FormFeedback m_passwordFeedback;
  59. private TextBox m_passwordRepeat;
  60. private FormFeedback m_passwordRepeatFeedback;
  61. private DatePickerTextBox m_bDay;
  62. private FormFeedback m_bDayFeedback;
  63. private TextArea m_userDesc;
  64. private FormFeedback m_userDescFeedback;
  65. private TextArea m_securityQuestion;
  66. private FormFeedback m_securityQuestionFeedback;
  67. private TextBox m_securityAnswer;
  68. private FormFeedback m_securityAnswerFeedback;
  69. private FancyButton m_save;
  70. private List<FormFeedback> m_feedbacks = new ArrayList<FormFeedback>();
  71. /**
  72. * Creates a new forms panel
  73. */
  74. public FormPanel()
  75. {
  76. super("div", STRINGS.FormPanel_html() + STRINGS.FormPanelContent_html());
  77. getElement().setId("formPanel");
  78. RootPanel.get("mainContent").add(this);
  79. setVisible(false);
  80. /*
  81. First name
  82. */
  83. m_firstName = new TextBox();
  84. m_firstName.addKeyUpHandler(this);
  85. m_firstName.getElement().setId("firstNameTxt");
  86. m_firstName.getElement().addClassName(WIDE_TEXT_FIELD);
  87. add(m_firstName, "firstName");
  88. m_firstNameFeedback = new FormFeedback();
  89. m_feedbacks.add(m_firstNameFeedback);
  90. add(m_firstNameFeedback, "firstNameRow");
  91. /*
  92. Last name
  93. */
  94. m_lastName = new TextBox();
  95. m_lastName.addKeyUpHandler(this);
  96. m_lastName.getElement().setId("lastNameTxt");
  97. m_lastName.getElement().addClassName(WIDE_TEXT_FIELD);
  98. add(m_lastName, "lastName");
  99. m_lastNameFeedback = new FormFeedback();
  100. m_feedbacks.add(m_lastNameFeedback);
  101. add(m_lastNameFeedback, "lastNameRow");
  102. /*
  103. email
  104. */
  105. m_email = new TextBox();
  106. m_email.addKeyUpHandler(this);
  107. m_email.getElement().setId("emailTxt");
  108. m_email.getElement().addClassName(WIDE_TEXT_FIELD);
  109. add(m_email, "email");
  110. m_emailFeedback = new FormFeedback();
  111. m_feedbacks.add(m_emailFeedback);
  112. add(m_emailFeedback, "emailRow");
  113. /*
  114. User's birthdate
  115. */
  116. m_bDay = new DatePickerTextBox("userBdayTxt");
  117. m_bDay.setMaximumDate(new Date()); //user cannot be born tomorrow
  118. m_bDay.addKeyUpHandler(this);
  119. m_bDay.getElement().addClassName("slimTextField");
  120. add(m_bDay, "userBday");
  121. m_bDayFeedback = new FormFeedback();
  122. add(m_bDayFeedback, "userBdayRow");
  123. /*
  124. User's gender
  125. */
  126. RadioButton female = new RadioButton("userGender", "Female");
  127. add(female, "userGender");
  128. RadioButton male = new RadioButton("userGender", "Male");
  129. male.addStyleName("radioOption");
  130. male.setValue(true);
  131. male.getElement().setId("userMale");
  132. add(male, "userGender");
  133. /*
  134. User description
  135. */
  136. m_userDesc = new TextArea();
  137. m_userDesc.addKeyUpHandler(this);
  138. m_userDesc.getElement().setId("userDescTxt");
  139. m_userDesc.getElement().addClassName(WIDE_TEXT_FIELD);
  140. add(m_userDesc, "userDesc");
  141. m_userDescFeedback = new FormFeedback();
  142. m_feedbacks.add(m_userDescFeedback);
  143. add(m_userDescFeedback, "userDescRow");
  144. /*
  145. Password
  146. */
  147. m_password = new PasswordTextBox();
  148. m_password.addKeyUpHandler(this);
  149. m_password.getElement().setId("passwordTxt");
  150. m_password.getElement().addClassName("slimTextField");
  151. add(m_password, "password");
  152. m_passwordFeedback = new FormFeedback();
  153. m_feedbacks.add(m_passwordFeedback);
  154. add(m_passwordFeedback, "passwordRow");
  155. /*
  156. Password repeat
  157. */
  158. m_passwordRepeat = new PasswordTextBox();
  159. m_passwordRepeat.addKeyUpHandler(this);
  160. m_passwordRepeat.getElement().setId("passwordRepeatTxt");
  161. m_passwordRepeat.getElement().addClassName("slimTextField");
  162. add(m_passwordRepeat, "passwordRepeat");
  163. m_passwordRepeatFeedback = new FormFeedback();
  164. m_feedbacks.add(m_passwordRepeatFeedback);
  165. add(m_passwordRepeatFeedback, "passwordRepeatRow");
  166. /*
  167. Security Question
  168. */
  169. m_securityQuestion = new TextArea();
  170. m_securityQuestion.addKeyUpHandler(this);
  171. m_securityQuestion.getElement().setId("securityQuestionTxt");
  172. m_securityQuestion.getElement().addClassName(WIDE_TEXT_FIELD);
  173. add(m_securityQuestion, "securityQuestion");
  174. m_securityQuestionFeedback = new FormFeedback();
  175. m_feedbacks.add(m_securityQuestionFeedback);
  176. add(m_securityQuestionFeedback, "securityQuestionRow");
  177. /*
  178. Security answer
  179. */
  180. m_securityAnswer = new TextBox();
  181. m_securityAnswer.addKeyUpHandler(this);
  182. m_securityAnswer.getElement().setId("securityAnswerTxt");
  183. m_securityAnswer.getElement().addClassName(WIDE_TEXT_FIELD);
  184. add(m_securityAnswer, "securityAnswer");
  185. m_securityAnswerFeedback = new FormFeedback();
  186. m_feedbacks.add(m_securityAnswerFeedback);
  187. add(m_securityAnswerFeedback, "securityAnswerRow");
  188. /*
  189. The big save button
  190. */
  191. m_save = new FancySaveButton(Index.getStrings().save());
  192. m_save.addClickHandler(new ClickHandler() {
  193. public void onClick(ClickEvent event)
  194. {
  195. save();
  196. }
  197. });
  198. add(m_save, "page2Buttons");
  199. updateFormStatus(null);
  200. }
  201. @Override
  202. public void onKeyUp(KeyUpEvent event)
  203. {
  204. if (event.getNativeKeyCode() != KeyCodes.KEY_TAB) {
  205. updateFormStatus((Widget) event.getSource());
  206. }
  207. }
  208. private void updateFormStatus(Widget w)
  209. {
  210. if (w == m_firstName) {
  211. validateField(m_firstName, 2, m_firstNameFeedback, Index.getStrings().firstName_tt());
  212. } else if (w == m_lastName) {
  213. validateField(m_lastName, 2, m_lastNameFeedback, Index.getStrings().lastName_tt());
  214. } else if (w == m_email) {
  215. validateEmail();
  216. } else if (w == m_password) {
  217. validateField(m_password, 2, m_passwordFeedback, Index.getStrings().pwd_tt());
  218. } else if (w == m_bDay) {
  219. validateBirthday();
  220. } else if (w == m_securityQuestion) {
  221. validateField(m_securityQuestion, 2, m_securityQuestionFeedback, Index.getStrings().question_tt());
  222. } else if (w == m_securityAnswer) {
  223. validateField(m_securityAnswer, 4, m_securityAnswerFeedback, Index.getStrings().answer_tt());
  224. } else if (w == m_passwordRepeat) {
  225. validatePasswordRepeat();
  226. } else if (w == m_userDesc) {
  227. validateField(m_userDesc, 8, m_userDescFeedback, Index.getStrings().desc_tt());
  228. }
  229. enableSaveButton();
  230. }
  231. /**
  232. * Enable or disable the save button based on the state of the fields.
  233. */
  234. private void enableSaveButton()
  235. {
  236. /*
  237. * We only want to enable the save button if every field is valid
  238. */
  239. for (FormFeedback feedback : m_feedbacks) {
  240. if (feedback.getStatus() != FormFeedback.VALID) {
  241. m_save.setEnabled(false);
  242. return;
  243. }
  244. }
  245. m_save.setEnabled(true);
  246. }
  247. /**
  248. * Validate the second password field.
  249. */
  250. private void validatePasswordRepeat()
  251. {
  252. validateField(m_passwordRepeat, 2, m_passwordRepeatFeedback, Index.getStrings().pwdMatch());
  253. if (m_passwordRepeat.getText().equals(m_password.getText())) {
  254. m_passwordRepeatFeedback.setStatus(FormFeedback.VALID);
  255. m_passwordRepeatFeedback.setTitle("");
  256. } else {
  257. m_passwordRepeatFeedback.setStatus(FormFeedback.ERROR);
  258. m_passwordRepeatFeedback.setTitle(Index.getStrings().pwdMatch());
  259. }
  260. }
  261. /**
  262. * Validate that the email field is filled in with a valid email address.
  263. */
  264. private void validateEmail()
  265. {
  266. if (JSUtil.validateEmail(m_email.getText())) {
  267. m_emailFeedback.setStatus(FormFeedback.VALID);
  268. m_emailFeedback.setTitle("");
  269. } else {
  270. m_emailFeedback.setStatus(FormFeedback.ERROR);
  271. m_emailFeedback.setTitle(Index.getStrings().email_tt());
  272. }
  273. }
  274. /**
  275. * Validate that the birthday is filled in, a valid date, and occurs in the past.
  276. */
  277. private void validateBirthday()
  278. {
  279. if (m_bDay.getText().length() > 2) {
  280. if (m_bDay.getDateValue() != null) {
  281. if (m_bDay.getDateValue().after(new Date())) {
  282. // user cannot be born tomorrow
  283. m_bDayFeedback.setStatus(FormFeedback.ERROR);
  284. m_bDayFeedback.setTitle(Index.getStrings().dateFuture_tt());
  285. } else {
  286. m_bDayFeedback.setStatus(FormFeedback.VALID);
  287. m_bDayFeedback.setTitle("");
  288. }
  289. } else {
  290. m_bDayFeedback.setStatus(FormFeedback.ERROR);
  291. m_bDayFeedback.setTitle(Index.getStrings().validDate_tt(m_bDay.getText()));
  292. }
  293. } else {
  294. m_bDayFeedback.setStatus(FormFeedback.WARNING);
  295. m_bDayFeedback.setTitle(Index.getStrings().birthday_tt());
  296. }
  297. }
  298. /**
  299. * Validate that the specified field is filled in and valid.
  300. *
  301. * @param tb the field to validate
  302. * @param minLength the minimum character length of the field
  303. * @param feedback the feedback control for this field
  304. * @param error the error to show in the feedback if the field isn't valid
  305. */
  306. private void validateField(TextBoxBase tb, int minLength, FormFeedback feedback, String error)
  307. {
  308. if (tb.getText().length() > minLength) {
  309. feedback.setStatus(FormFeedback.VALID);
  310. feedback.setTitle("");
  311. } else {
  312. feedback.setStatus(FormFeedback.WARNING);
  313. feedback.setTitle(error);
  314. }
  315. }
  316. private void save()
  317. {
  318. m_save.setInProgress(true);
  319. //a little timer to simulate time it takes to set loading back to false
  320. Timer t = new Timer() {
  321. @Override
  322. public void run()
  323. {
  324. m_save.setInProgress(false);
  325. }
  326. };
  327. t.schedule(2000);
  328. MessageUtil.showMessage(Index.getStrings().formSaveMessage());
  329. }
  330. }