Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package edu.westga.cs1302.contacts.view;
- import edu.westga.cs1302.contacts.model.Contact;
- import edu.westga.cs1302.contacts.viewmodel.ContactViewModel;
- import javafx.collections.FXCollections;
- import javafx.event.ActionEvent;
- import javafx.fxml.FXML;
- import javafx.scene.control.Alert;
- import javafx.scene.control.Button;
- import javafx.scene.control.ListView;
- import javafx.scene.control.TextField;
- import javafx.scene.control.Alert.AlertType;
- import javafx.scene.layout.AnchorPane;
- import javafx.stage.Window;
- /**
- * The Class ContactGuiCodeBehind.
- *
- * @author CS1302
- * @version 1.0
- */
- public class ContactGuiCodeBehind {
- private ContactViewModel viewmodel;
- @FXML
- private AnchorPane guiPane;
- @FXML
- private TextField firstNameTextField;
- @FXML
- private TextField lastNameTextField;
- @FXML
- private TextField phoneNumberTextField;
- @FXML
- private Button addButton;
- @FXML
- private Button updateButton;
- @FXML
- private Button searchButton;
- @FXML
- private ListView<Contact> contactsListView;
- /**
- * Instantiates a new code behind.
- */
- public ContactGuiCodeBehind() {
- this.viewmodel = new ContactViewModel();
- }
- @FXML
- void initialize() {
- this.bindToViewModel();
- this.setupChangeListenerListView();
- this.setupChangeListenersForValidation();
- this.setupDisablingOfButtons();
- }
- private void setupDisablingOfButtons() {
- this.addButton.disableProperty().bind(this.firstNameTextField.textProperty().isEmpty().or(this.lastNameTextField
- .textProperty().isEmpty().or(this.phoneNumberTextField.textProperty().isEmpty())));
- this.searchButton.disableProperty().bind(this.phoneNumberTextField.textProperty().isNotEmpty().or(this.viewmodel.contactSelectedProperty()));
- }
- private void setupChangeListenersForValidation() {
- this.phoneNumberTextField.textProperty().addListener((observable, oldValue, newValue) -> {
- if (newValue != null) {
- if (newValue.matches(
- "\\(|\\(\\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}")) {
- this.phoneNumberTextField.setVisible(true);
- } else {
- this.showAlert("Invalid Phone Number", "Required: (XXX)XXX-XXXX");
- this.phoneNumberTextField.setText(oldValue);
- }
- }
- });
- this.phoneNumberTextField.focusedProperty().addListener((observable, oldValue, newValue) -> {
- if (newValue == false
- && !this.phoneNumberTextField.getText().matches("\\(\\d{0,3}\\)\\d{0,3}\\-\\d{0,4}")) {
- this.showAlert("Invalid Phone Number", "Required: (XXX)XXX-XXXX");
- }
- });
- }
- @FXML
- void handleAdd(ActionEvent event) {
- try {
- if (!this.viewmodel.addContact()) {
- this.showAlert("Add error", "ERROR: contact was not added");
- }
- } catch (IllegalArgumentException | NullPointerException ex) {
- this.showAlert("Add error", "ERROR: couldn't add the contact due to " + ex.getMessage());
- }
- }
- @FXML
- void handleUpdate(ActionEvent event) {
- try {
- if (!this.viewmodel.updateContact()) {
- this.showAlert("Update error", "ERROR: No such a contact");
- }
- } catch (IllegalArgumentException | NullPointerException ex) {
- this.showAlert("Update error", "ERROR: couldn't update the contact due to " + ex.getMessage());
- }
- }
- @FXML
- void handleSearch(ActionEvent event) {
- try {
- if (!this.viewmodel.searchForContact()) {
- this.showAlert("Search error", "ERROR: No such contact");
- }
- } catch (IllegalArgumentException | NullPointerException ex) {
- this.showAlert("Search error", "ERROR: couldn't find the contact due to " + ex.getMessage());
- }
- }
- private void showAlert(String title, String content) {
- Alert alert = new Alert(AlertType.ERROR);
- Window owner = this.guiPane.getScene().getWindow();
- alert.initOwner(owner);
- alert.setTitle(title);
- alert.setContentText(content);
- alert.showAndWait();
- }
- private void setupChangeListenerListView() {
- this.contactsListView.getSelectionModel().selectedItemProperty()
- .addListener((observable, oldcontact, newContact) -> {
- if (newContact != null) {
- this.phoneNumberTextField.setText(newContact.getPhoneNumber());
- this.firstNameTextField.setText(newContact.getFirstName());
- this.lastNameTextField.setText(newContact.getLastName());
- }
- });
- }
- private void bindToViewModel() {
- this.firstNameTextField.textProperty().bindBidirectional(this.viewmodel.firstNameProperty());
- this.lastNameTextField.textProperty().bindBidirectional(this.viewmodel.lastNameProperty());
- this.phoneNumberTextField.textProperty().bindBidirectional(this.viewmodel.phoneNumberProperty());
- this.contactsListView.itemsProperty().bind(this.viewmodel.contactsProperty());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement