Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.37 KB | None | 0 0
  1. package controller;
  2.  
  3. import javafx.beans.binding.Bindings;
  4. import javafx.collections.FXCollections;
  5. import javafx.collections.ObservableList;
  6. import javafx.collections.transformation.SortedList;
  7. import javafx.event.ActionEvent;
  8. import javafx.fxml.Initializable;
  9. import javafx.scene.control.*;
  10. import javafx.scene.control.cell.PropertyValueFactory;
  11. import model.CharityCase;
  12. import model.Donation;
  13. import repository.CharityRepository;
  14. import repository.DonationRepository;
  15. import java.net.URL;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. import java.util.ResourceBundle;
  19.  
  20. public class FxController implements Initializable {
  21. public TableView<CharityCase> charityCaseTableView;
  22. public TableColumn<CharityCase, Integer> colID;
  23. public TableColumn<CharityCase, String> colCharityCase;
  24. public TableColumn<CharityCase, Integer> colSum;
  25. public Button buttonDisplay;
  26. public Button buttonAddDonation;
  27. public TextField textAddCharityDonation;
  28. public TextField textAddNameDonation;
  29. public TextField textAddAddressDonation;
  30. public TextField textAddTelDonation;
  31. public TextField textAddAmountDonation;
  32. public TextField textAddIDDonation;
  33. public TableView<Donation> donationTableView;
  34. public TableColumn<Object, Object> colDonationID;
  35. public TableColumn<Object, Object> colCharity;
  36. public TableColumn<Object, Object> colName;
  37. public TableColumn<Object, Object> colAddress;
  38. public TableColumn<Object, Object> colTel;
  39. public TableColumn<Object, Object> colAmount;
  40. public TableView<Donation> searchDonationTableView;
  41. public TableColumn<Object, Object> colSearchID;
  42. public TableColumn<Object, Object> colSearchCharity;
  43. public TableColumn<Object, Object> colSearchName;
  44. public TableColumn<Object, Object> colSearchAddress;
  45. public TableColumn<Object, Object> colSearchTel;
  46. public TableColumn<Object, Object> colSearchAmount;
  47. public Button buttonSearch;
  48. public Button buttonFill;
  49. public TableColumn<Object, Object> colCharityID;
  50. public TableColumn<Object, Object> colSearchCharityID;
  51. private CharityRepository charityRepository = new CharityRepository();
  52. private CharityController charityController = new CharityController(charityRepository);
  53. private DonationRepository donationRepository = new DonationRepository();
  54. private DonationController donationController = new DonationController(donationRepository);
  55.  
  56. @Override
  57. public void initialize(URL location, ResourceBundle resources) {
  58. colID.setCellValueFactory(new PropertyValueFactory<>("caseID"));
  59. colCharityCase.setCellValueFactory(new PropertyValueFactory<>("name"));
  60. colSum.setCellValueFactory(new PropertyValueFactory<>("sum"));
  61. setCellValue(colDonationID, colCharity, colName, colAddress, colTel, colAmount, colCharityID);
  62. setCellValue(colSearchID, colSearchCharity, colSearchName, colSearchAddress, colSearchTel, colSearchAmount, colSearchCharityID);
  63. }
  64.  
  65. private void setCellValue(TableColumn<Object, Object> colDonationID, TableColumn<Object, Object> colCharity, TableColumn<Object, Object> colName, TableColumn<Object, Object> colAddress, TableColumn<Object, Object> colTel, TableColumn<Object, Object> colAmount, TableColumn<Object, Object> colCharityID) {
  66. colDonationID.setCellValueFactory(new PropertyValueFactory<>("donationID"));
  67. colCharity.setCellValueFactory(new PropertyValueFactory<>("charityCaseName"));
  68. colName.setCellValueFactory(new PropertyValueFactory<>("donatorName"));
  69. colAddress.setCellValueFactory(new PropertyValueFactory<>("address"));
  70. colTel.setCellValueFactory(new PropertyValueFactory<>("tel"));
  71. colAmount.setCellValueFactory(new PropertyValueFactory<>("amount"));
  72. colCharityID.setCellValueFactory(new PropertyValueFactory<>("idCharity"));
  73. }
  74.  
  75. public void buttonDisplayClick(ActionEvent actionEvent){
  76. if (Bindings.isEmpty(charityCaseTableView.getItems()).get()) {
  77. List<CharityCase> charityCases = new ArrayList<>();
  78. ObservableList<CharityCase> details = FXCollections.observableArrayList(charityCases);
  79. details.addAll(charityController.getCharityCasesFromDB());
  80. charityCaseTableView.setItems(details);
  81. }
  82. if (Bindings.isEmpty(donationTableView.getItems()).get()) {
  83. List<Donation> donations = new ArrayList<>();
  84. ObservableList<Donation> donationDetails = FXCollections.observableArrayList(donations);
  85. donationDetails.addAll(donationController.getDonationsFromDB());
  86. donationTableView.setItems(donationDetails);
  87. }
  88. }
  89.  
  90. public void buttonAddDonationClick(ActionEvent actionEvent) {
  91. Donation donation = new Donation(Integer.parseInt(textAddIDDonation.getText()), textAddCharityDonation.getText(), textAddNameDonation.getText(),
  92. textAddAddressDonation.getText(), Integer.parseInt(textAddTelDonation.getText()), Integer.parseInt(textAddAmountDonation.getText()), charityController.getCharityIdByName(textAddCharityDonation.getText()));
  93. donationTableView.getItems().add(donation);
  94. donationController.newDonation(donation);
  95. charityCaseTableView.getItems().clear();
  96. List<CharityCase> charityCases = new ArrayList<>();
  97. ObservableList<CharityCase> details = FXCollections.observableArrayList(charityCases);
  98. details.addAll(charityController.getCharityCasesFromDB());
  99. charityCaseTableView.setItems(details);
  100. }
  101.  
  102. public void buttonSearchClick(ActionEvent actionEvent) {
  103. List<Donation> donations = new ArrayList<>();
  104. ObservableList<Donation> searchDonationDetails = FXCollections.observableArrayList(donations);
  105. searchDonationDetails.addAll(donationController.getDonationByName(textAddNameDonation.getText()));
  106. searchDonationTableView.setItems(searchDonationDetails);
  107. }
  108.  
  109. public void buttonFillClick(ActionEvent actionEvent) {
  110. if (searchDonationTableView.getSelectionModel().getSelectedItem() != null) {
  111. Donation donation = searchDonationTableView.getSelectionModel().getSelectedItem();
  112. textAddNameDonation.setText(donation.getDonatorName());
  113. textAddAddressDonation.setText(donation.getAddress());
  114. textAddTelDonation.setText(String.valueOf(donation.getTel()));
  115. }
  116. }
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement