Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. @FXML
  2. void initialize() {
  3. this.bindToViewModel();
  4. this.setupChangeListenerListView();
  5. this.setupChangeListenersForValidation();
  6. this.setupDisablingOfButtons();
  7. }
  8.  
  9. private void setupDisablingOfButtons() {
  10. this.addButton.disableProperty().bind(this.firstNameTextField.textProperty().isEmpty().or(this.lastNameTextField
  11. .textProperty().isEmpty().or(this.phoneNumberTextField.textProperty().isEmpty())));
  12. this.searchButton.disableProperty().bind(this.phoneNumberTextField.textProperty().isNotEmpty().or(this.viewmodel.contactSelectedProperty()));
  13. }
  14.  
  15. private void setupChangeListenersForValidation() {
  16. this.phoneNumberTextField.textProperty().addListener((observable, oldValue, newValue) -> {
  17. if (newValue != null) {
  18. if (newValue.matches(
  19. "\\(|\\(\\d{0,3}|\\(\\d{0,3}\\)|\\(\\d{0,3}\\)\\d{0,3}|\\(\\d{0,3}\\)\\d{0,3}\\-|\\(\\d{0,3}\\)\\d{0,3}\\-\\d{0,4}")) {
  20. this.phoneNumberTextField.setVisible(true);
  21. } else {
  22. this.showAlert("Invalid Phone Number", "Required: (XXX)XXX-XXXX");
  23. this.phoneNumberTextField.setText(oldValue);
  24. }
  25. }
  26. });
  27. this.phoneNumberTextField.focusedProperty().addListener((observable, oldValue, newValue) -> {
  28. if (newValue == false
  29. && !this.phoneNumberTextField.getText().matches("\\(\\d{0,3}\\)\\d{0,3}\\-\\d{0,4}")) {
  30. this.showAlert("Invalid Phone Number", "Required: (XXX)XXX-XXXX");
  31. }
  32. });
  33. }
  34.  
  35. @FXML
  36. void handleAdd(ActionEvent event) {
  37. try {
  38. if (!this.viewmodel.addContact()) {
  39. this.showAlert("Add error", "ERROR: contact was not added");
  40. }
  41. } catch (IllegalArgumentException | NullPointerException ex) {
  42. this.showAlert("Add error", "ERROR: couldn't add the contact due to " + ex.getMessage());
  43. }
  44. }
  45.  
  46. @FXML
  47. void handleUpdate(ActionEvent event) {
  48. try {
  49. if (!this.viewmodel.updateContact()) {
  50. this.showAlert("Update error", "ERROR: No such a contact");
  51. }
  52. } catch (IllegalArgumentException | NullPointerException ex) {
  53. this.showAlert("Update error", "ERROR: couldn't update the contact due to " + ex.getMessage());
  54. }
  55. }
  56.  
  57. @FXML
  58. void handleSearch(ActionEvent event) {
  59. try {
  60. if (!this.viewmodel.searchForContact()) {
  61. this.showAlert("Search error", "ERROR: No such contact");
  62. }
  63. } catch (IllegalArgumentException | NullPointerException ex) {
  64. this.showAlert("Search error", "ERROR: couldn't find the contact due to " + ex.getMessage());
  65. }
  66. }
  67.  
  68. private void showAlert(String title, String content) {
  69. Alert alert = new Alert(AlertType.ERROR);
  70. Window owner = this.guiPane.getScene().getWindow();
  71. alert.initOwner(owner);
  72. alert.setTitle(title);
  73. alert.setContentText(content);
  74. alert.showAndWait();
  75. }
  76.  
  77. private void setupChangeListenerListView() {
  78. this.contactsListView.getSelectionModel().selectedItemProperty()
  79. .addListener((observable, oldcontact, newContact) -> {
  80. if (newContact != null) {
  81. this.phoneNumberTextField.setText(newContact.getPhoneNumber());
  82. this.firstNameTextField.setText(newContact.getFirstName());
  83. this.lastNameTextField.setText(newContact.getLastName());
  84. }
  85. });
  86. }
  87.  
  88. private void bindToViewModel() {
  89. this.firstNameTextField.textProperty().bindBidirectional(this.viewmodel.firstNameProperty());
  90. this.lastNameTextField.textProperty().bindBidirectional(this.viewmodel.lastNameProperty());
  91. this.phoneNumberTextField.textProperty().bindBidirectional(this.viewmodel.phoneNumberProperty());
  92. this.contactsListView.itemsProperty().bind(this.viewmodel.contactsProperty());
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement