SHARE
TWEET

Lab 9 - ContactGuiCodeBehind

kmitche9 Apr 22nd, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package edu.westga.cs1302.contacts.view;
  2.  
  3. import edu.westga.cs1302.contacts.model.Contact;
  4. import edu.westga.cs1302.contacts.viewmodel.ContactViewModel;
  5. import javafx.collections.FXCollections;
  6. import javafx.event.ActionEvent;
  7. import javafx.fxml.FXML;
  8. import javafx.scene.control.Alert;
  9. import javafx.scene.control.Button;
  10. import javafx.scene.control.ListView;
  11. import javafx.scene.control.TextField;
  12. import javafx.scene.control.Alert.AlertType;
  13. import javafx.scene.layout.AnchorPane;
  14. import javafx.stage.Window;
  15.  
  16. /**
  17.  * The Class ContactGuiCodeBehind.
  18.  *
  19.  * @author CS1302
  20.  * @version 1.0
  21.  */
  22. public class ContactGuiCodeBehind {
  23.  
  24.     private ContactViewModel viewmodel;
  25.  
  26.     @FXML
  27.     private AnchorPane guiPane;
  28.  
  29.     @FXML
  30.     private TextField firstNameTextField;
  31.  
  32.     @FXML
  33.     private TextField lastNameTextField;
  34.  
  35.     @FXML
  36.     private TextField phoneNumberTextField;
  37.  
  38.     @FXML
  39.     private Button addButton;
  40.  
  41.     @FXML
  42.     private Button updateButton;
  43.  
  44.     @FXML
  45.     private Button searchButton;
  46.  
  47.     @FXML
  48.     private ListView<Contact> contactsListView;
  49.  
  50.     /**
  51.      * Instantiates a new code behind.
  52.      */
  53.     public ContactGuiCodeBehind() {
  54.         this.viewmodel = new ContactViewModel();
  55.     }
  56.  
  57.     @FXML
  58.     void initialize() {
  59.         this.bindToViewModel();
  60.         this.setupChangeListenerListView();
  61.         this.setupChangeListenersForValidation();
  62.         this.setupDisablingOfButtons();
  63.     }
  64.  
  65.     private void setupDisablingOfButtons() {
  66.         this.addButton.disableProperty().bind(this.firstNameTextField.textProperty().isEmpty().or(this.lastNameTextField
  67.                 .textProperty().isEmpty().or(this.phoneNumberTextField.textProperty().isEmpty())));
  68.         this.searchButton.disableProperty().bind(this.phoneNumberTextField.textProperty().isNotEmpty().or(this.viewmodel.contactSelectedProperty()));
  69.     }
  70.  
  71.     private void setupChangeListenersForValidation() {
  72.         this.phoneNumberTextField.textProperty().addListener((observable, oldValue, newValue) -> {
  73.             if (newValue != null) {
  74.                 if (newValue.matches(
  75.                         "\\(|\\(\\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}")) {
  76.                     this.phoneNumberTextField.setVisible(true);
  77.                 } else {
  78.                     this.showAlert("Invalid Phone Number", "Required: (XXX)XXX-XXXX");
  79.                     this.phoneNumberTextField.setText(oldValue);
  80.                 }
  81.             }
  82.         });
  83.         this.phoneNumberTextField.focusedProperty().addListener((observable, oldValue, newValue) -> {
  84.             if (newValue == false
  85.                     && !this.phoneNumberTextField.getText().matches("\\(\\d{0,3}\\)\\d{0,3}\\-\\d{0,4}")) {
  86.                 this.showAlert("Invalid Phone Number", "Required: (XXX)XXX-XXXX");
  87.             }
  88.         });
  89.     }
  90.  
  91.     @FXML
  92.     void handleAdd(ActionEvent event) {
  93.         try {
  94.             if (!this.viewmodel.addContact()) {
  95.                 this.showAlert("Add error", "ERROR: contact was not added");
  96.             }
  97.         } catch (IllegalArgumentException | NullPointerException ex) {
  98.             this.showAlert("Add error", "ERROR: couldn't add the contact due to " + ex.getMessage());
  99.         }
  100.     }
  101.  
  102.     @FXML
  103.     void handleUpdate(ActionEvent event) {
  104.         try {
  105.             if (!this.viewmodel.updateContact()) {
  106.                 this.showAlert("Update error", "ERROR: No such a contact");
  107.             }
  108.         } catch (IllegalArgumentException | NullPointerException ex) {
  109.             this.showAlert("Update error", "ERROR: couldn't update the contact due to " + ex.getMessage());
  110.         }
  111.     }
  112.  
  113.     @FXML
  114.     void handleSearch(ActionEvent event) {
  115.         try {
  116.             if (!this.viewmodel.searchForContact()) {
  117.                 this.showAlert("Search error", "ERROR: No such contact");
  118.             }
  119.         } catch (IllegalArgumentException | NullPointerException ex) {
  120.             this.showAlert("Search error", "ERROR: couldn't find the contact due to " + ex.getMessage());
  121.         }
  122.     }
  123.  
  124.     private void showAlert(String title, String content) {
  125.         Alert alert = new Alert(AlertType.ERROR);
  126.         Window owner = this.guiPane.getScene().getWindow();
  127.         alert.initOwner(owner);
  128.         alert.setTitle(title);
  129.         alert.setContentText(content);
  130.         alert.showAndWait();
  131.     }
  132.  
  133.     private void setupChangeListenerListView() {
  134.         this.contactsListView.getSelectionModel().selectedItemProperty()
  135.                 .addListener((observable, oldcontact, newContact) -> {
  136.                     if (newContact != null) {
  137.                         this.phoneNumberTextField.setText(newContact.getPhoneNumber());
  138.                         this.firstNameTextField.setText(newContact.getFirstName());
  139.                         this.lastNameTextField.setText(newContact.getLastName());
  140.                     }
  141.                 });
  142.     }
  143.  
  144.     private void bindToViewModel() {
  145.         this.firstNameTextField.textProperty().bindBidirectional(this.viewmodel.firstNameProperty());
  146.         this.lastNameTextField.textProperty().bindBidirectional(this.viewmodel.lastNameProperty());
  147.         this.phoneNumberTextField.textProperty().bindBidirectional(this.viewmodel.phoneNumberProperty());
  148.         this.contactsListView.itemsProperty().bind(this.viewmodel.contactsProperty());
  149.     }
  150. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top