Advertisement
Guest User

View

a guest
May 19th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.97 KB | None | 0 0
  1. import javafx.collections.FXCollections;
  2. import javafx.collections.ObservableList;
  3. import javafx.scene.Group;
  4. import javafx.scene.Parent;
  5. import javafx.scene.Scene;
  6. import javafx.scene.control.*;
  7. import javafx.scene.control.cell.PropertyValueFactory;
  8. import javafx.scene.layout.BorderPane;
  9. import javafx.scene.layout.GridPane;
  10. import javafx.stage.Modality;
  11. import javafx.stage.Stage;
  12.  
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. import java.util.Optional;
  16.  
  17. public class PatientsTableView {
  18.  
  19.     private PatientsModel model = new PatientsModel();
  20.     private PatientsController controller = new PatientsController();
  21.  
  22.     private BorderPane borderPane = new BorderPane();
  23.  
  24.     public PatientsTableView(PatientsController controller,PatientsModel model){
  25.  
  26.         this.controller = controller;
  27.         this.model = model;
  28.  
  29.         controller.ReadBySAX();
  30.         createAndLayoutControls();
  31.  
  32.     }
  33.  
  34.     public Parent asParent() {
  35.         return borderPane ;
  36.     }
  37.  
  38.     private void createAndLayoutControls(){
  39.  
  40.         GridPane tableView = new GridPane();
  41.         configurateTableView(tableView,controller.getModelArray(),1,controller.getModelArray().size());
  42.  
  43.         GridPane navigationView = new GridPane();
  44.         configurateNavigationVIew(navigationView,tableView,controller.getModelArray());
  45.  
  46.         GridPane functionalView = new GridPane();
  47.         configurateFunctionalView(functionalView,tableView,navigationView);
  48.  
  49.  
  50.         borderPane.setTop(tableView);
  51.         borderPane.setCenter(navigationView);
  52.         borderPane.setLeft(functionalView);
  53.  
  54.     }
  55.  
  56.     private TableView<PatientsModel> createTable(int x, int page, List<PatientsModel> newArray){
  57.         GridPane view = new GridPane();
  58.         ObservableList<PatientsModel> patient = FXCollections.observableArrayList();
  59.         for(int i = (page-1)*x;i<page*x;i++)
  60.         {
  61.             if(i == newArray.size()){
  62.                 break;
  63.             }
  64.             patient.add(newArray.get(i));
  65.  
  66.         }
  67.  
  68.         TableView<PatientsModel> table = new TableView<PatientsModel>(patient);
  69.         table.setPrefWidth(1280);
  70.         //table.setPrefHeight(500);
  71.  
  72.         TableColumn<PatientsModel, String> patientFIOcolumn = new TableColumn<PatientsModel, String>("ФИО пациентов");
  73.         patientFIOcolumn.setCellValueFactory(new PropertyValueFactory<PatientsModel, String>("patientSurname"));
  74.         table.getColumns().add(patientFIOcolumn);
  75.  
  76.         TableColumn<PatientsModel, String> addressColumn = new TableColumn<PatientsModel, String>("адрес");
  77.         addressColumn.setCellValueFactory(new PropertyValueFactory<PatientsModel, String>("town"));
  78.         table.getColumns().add(addressColumn);
  79.  
  80.         //FlowPane root = new FlowPane(10, 10, table);
  81.         return table;
  82.     }
  83.  
  84.     private void configurateTableView(GridPane tableView,List<PatientsModel> newArray,int page,int numOfRecordsOnPage) {
  85.         tableView.getChildren().clear();
  86.         tableView.add(createTable(numOfRecordsOnPage,page,newArray),0,0);
  87.     }
  88.  
  89.  
  90.     private void configurateNavigationVIew(GridPane navigativonView, GridPane tableView,List<PatientsModel> newArray){
  91.  
  92.  
  93.         navigativonView.getChildren().clear();
  94.  
  95.         Button setNumberOfRecords = new Button("Уcтановить количество записей");
  96.         Button setPage = new Button ("Выбрать страницу");
  97.  
  98.         Label numOfRecordsOnPageLabel = new Label(Integer.toString(newArray.size()));
  99.         Label totalNumOfRecords = new Label (Integer.toString(newArray.size()));
  100.  
  101.         Label currentPageLabel = new Label("1");
  102.         Label totalPageLabel = new Label("1");
  103.  
  104.         updateTotalPageLabel(totalPageLabel,Integer.parseInt(numOfRecordsOnPageLabel.getText()),
  105.                 Integer.parseInt(totalNumOfRecords.getText()));
  106.  
  107.         setNumberOfRecords.setOnAction(click ->{
  108.             TextInputDialog dialog = new TextInputDialog();
  109.             dialog.setTitle("Input num of records on page");
  110.             dialog.setHeaderText(null);
  111.             dialog.setContentText("Please enter num of records:");
  112.             Optional<String> result = dialog.showAndWait();
  113.             if (result.isPresent()){
  114.                 int tmp = Integer.parseInt(result.get());
  115.                 if( tmp <= newArray.size()) {
  116.                     numOfRecordsOnPageLabel.setText(result.get());
  117.                     configurateTableView(tableView,newArray,1,Integer.parseInt(numOfRecordsOnPageLabel.getText()));
  118.                     updateTotalPageLabel(totalPageLabel,Integer.parseInt(numOfRecordsOnPageLabel.getText()),
  119.                             Integer.parseInt(totalNumOfRecords.getText()));
  120.                 }
  121.                 else{
  122.                     Alert alert = new Alert(Alert.AlertType.INFORMATION);
  123.                     alert.setTitle("Information");
  124.                     alert.setHeaderText(null);
  125.                     alert.setContentText("нету такого количества записей");
  126.                     alert.showAndWait();
  127.                 }
  128.             }
  129.         });
  130.  
  131.         setPage.setOnAction(click ->{
  132.             List<String> choices = new ArrayList<>();
  133.             for(int i = 1; i<=Integer.parseInt(totalPageLabel.getText());i++)
  134.             {
  135.                 choices.add(Integer.toString(i));
  136.             }
  137.             ChoiceDialog<String> dialog = new ChoiceDialog<>("1", choices);
  138.             dialog.setTitle("Choice Dialog");
  139.             dialog.setHeaderText("Look, a Choice Dialog");
  140.             dialog.setContentText("Choose your letter:");
  141.             Optional<String> result = dialog.showAndWait();
  142.             if (result.isPresent()){
  143.                 currentPageLabel.setText(result.get());
  144.                 configurateTableView(tableView,newArray,Integer.parseInt(currentPageLabel.getText()),
  145.                         Integer.parseInt(numOfRecordsOnPageLabel.getText()));
  146.             }
  147.         });
  148.  
  149.         navigativonView.add(setNumberOfRecords,0,0);
  150.         navigativonView.add(new Label(" записей на странице:   "),1,0);
  151.         navigativonView.add(numOfRecordsOnPageLabel,2,0);
  152.         navigativonView.add(new Label("        Всего записей: "),3,0);
  153.         navigativonView.add(totalNumOfRecords,4,0);
  154.  
  155.         navigativonView.add(setPage,0,1);
  156.         navigativonView.add(new Label("текущая страница:"),1,1);
  157.         navigativonView.add(currentPageLabel,2,1);
  158.         navigativonView.add(new Label("        Всего страниц:"),3,1);
  159.         navigativonView.add(totalPageLabel,4,1);
  160.  
  161.     }
  162.  
  163.  
  164.  
  165.     private void updateTotalPageLabel(Label totalPageLabel,int numOfRecordsOnPage,int totalNumOfRecords){
  166.         double allPage =  (double)totalNumOfRecords/(double)(numOfRecordsOnPage);
  167.         int tmp = (int)allPage;
  168.         System.out.println("PAGES double  = " + allPage);
  169.         if(numOfRecordsOnPage == 0){
  170.             totalPageLabel.setText("1");
  171.         }
  172.         else {
  173.             if (tmp < allPage) {
  174.                 tmp++;
  175.                 System.out.println("PAGES = " + tmp);
  176.             }
  177.             totalPageLabel.setText(Integer.toString(tmp));
  178.         }
  179.     }
  180.  
  181.     private void configurateFunctionalView(GridPane functionalView,GridPane tableView,GridPane navigationView) {
  182.         Button addNewRecord = new Button("Добавить запись");
  183.         Button search = new Button ("поиск");
  184.         Button delete = new Button ("Удаление");
  185.  
  186.         functionalView.add(addNewRecord,0,0);
  187.         functionalView.add(search,0,1);
  188.         functionalView.add(delete,0,2);
  189.  
  190.         search.setOnAction(click ->
  191.         {
  192.             newSearchStage();
  193.         });
  194.  
  195.         addNewRecord.setOnAction(click ->
  196.         {
  197.             try {
  198.                 newRecordStage(tableView,navigationView);
  199.  
  200.             } catch (Exception e) {
  201.                 e.printStackTrace();
  202.             }
  203.  
  204.         });
  205.  
  206.         delete.setOnAction(click ->
  207.         {
  208.  
  209.             newDeleteStage(tableView,navigationView);
  210.         });
  211.  
  212.     }
  213.  
  214.     public void newSearchStage(){
  215.  
  216.         final Stage dialogStage = new Stage();
  217.         dialogStage.initModality(Modality.APPLICATION_MODAL);
  218.  
  219.         BorderPane borderPane = new BorderPane();
  220.  
  221.         List<PatientsModel> arrayOfFound = new ArrayList<PatientsModel>();
  222.  
  223.         GridPane tableView = new GridPane();
  224.         configurateTableView(tableView,arrayOfFound,1,arrayOfFound.size());
  225.  
  226.         GridPane navigationView = new GridPane();
  227.         configurateNavigationVIew(navigationView,tableView,arrayOfFound);
  228.  
  229.         GridPane functionalView = new GridPane();
  230.         configurateSearchStageFunctionalView(functionalView,tableView,navigationView,arrayOfFound);
  231.  
  232.         borderPane.setTop(tableView);
  233.         borderPane.setCenter(navigationView);
  234.         borderPane.setLeft(functionalView);
  235.  
  236.         Scene dialogScene = new Scene(borderPane, 1280, 800);
  237.         dialogStage.setScene(dialogScene);
  238.         dialogStage.show();
  239.     }
  240.  
  241.     private void configurateSearchStageFunctionalView(GridPane functionalView,GridPane tableView,GridPane navigationView,
  242.                                                       List<PatientsModel> arrayOfFound){
  243.  
  244.         Button chooseSearchPatientFio = new Button("ФИО пациента");
  245.         Button chooseSearchAddress = new Button("адрес");
  246.  
  247.         //Label patientFioLabel = new Label();
  248.  
  249.         chooseSearchPatientFio.setOnAction(click ->
  250.         {
  251.             TextInputDialog dialog = new TextInputDialog();
  252.             dialog.setTitle("Text Input Dialog");
  253.             dialog.setHeaderText(null);
  254.             dialog.setContentText("Please enter patient FIO:");
  255.             Optional<String> result = dialog.showAndWait();
  256.             if (result.isPresent()) {
  257.                 //patientFioLabel.setText(result.get());
  258.                 arrayOfFound.clear();
  259.                 for (int i = 0; i < controller.getModelArray().size(); i++) {
  260.                     if(result.get().equals(controller.getModelArray().get(i).getPatientSurname())){
  261.                         System.out.println("Yep");
  262.                         arrayOfFound.add(controller.getModelArray().get(i));
  263.                     }
  264.                 }
  265.  
  266.                 configurateTableView(tableView,arrayOfFound,1, arrayOfFound.size());
  267.                 configurateNavigationVIew(navigationView,tableView,arrayOfFound);
  268.             }
  269.         });
  270.  
  271.         chooseSearchAddress.setOnAction(click ->
  272.         {
  273.             TextInputDialog dialog = new TextInputDialog();
  274.             dialog.setTitle("Text Input Dialog");
  275.             dialog.setHeaderText(null);
  276.             dialog.setContentText("Please enter patient address :");
  277.             Optional<String> result = dialog.showAndWait();
  278.             if (result.isPresent()) {
  279.                 arrayOfFound.clear();
  280.                 //addressLabel.setText(result.get());
  281.                 for (int i = 0; i < controller.getModelArray().size(); i++) {
  282.                     String tmpSTR = controller.getModelArray().get(i).getTown();
  283.                     if (tmpSTR.equals(result.get())) {
  284.                         System.out.println("Yep");
  285.                         arrayOfFound.add(controller.getModelArray().get(i));
  286.                     }
  287.                 }
  288.                 configurateTableView(tableView,arrayOfFound,1, arrayOfFound.size());
  289.                 configurateNavigationVIew(navigationView,tableView,arrayOfFound);
  290.                 /////
  291.             }
  292.         });
  293.  
  294.  
  295.         functionalView.add(new Label("Поиск по:"),0,0);
  296.         functionalView.add(chooseSearchPatientFio,1,0);
  297.         functionalView.add(chooseSearchAddress,1,1);
  298.  
  299.     }
  300.  
  301.     private void newRecordStage(GridPane tableView, GridPane navigationView){
  302.  
  303.         final Stage dialogStage = new Stage();
  304.         dialogStage.initModality(Modality.APPLICATION_MODAL);
  305.         BorderPane borderPane = new BorderPane();
  306.         GridPane addElementView = new GridPane();
  307.  
  308.         Button addRecordButton = new Button("добавить запись");
  309.  
  310.         TextField patientSurnameTF = new TextField("");
  311.         TextField townTF = new TextField("");
  312.  
  313.         addRecordButton.setOnAction(click ->
  314.         {
  315.             boolean checkPatientFIO =patientSurnameTF.getText().trim().isEmpty();
  316.             boolean checkAddress = townTF.getText().trim().isEmpty();
  317.             if(!checkPatientFIO && !checkAddress){
  318.                 controller.writeByDOM(patientSurnameTF.getText(),townTF.getText());
  319.                 controller.ReadBySAX();
  320.                 configurateTableView(tableView,controller.getModelArray(),1, controller.getModelArray().size());
  321.                 configurateNavigationVIew(navigationView,tableView,controller.getModelArray());
  322.                 dialogStage.close();
  323.             }
  324.             else{
  325.                 Alert alert = new Alert(Alert.AlertType.INFORMATION);
  326.                 alert.setTitle("Information");
  327.                 alert.setHeaderText(null);
  328.                 alert.setContentText("Заполните информацию корректно");
  329.                 alert.showAndWait();
  330.             }
  331.         });
  332.  
  333.         addElementView.add(new Label("Фамилия Пациента"),0,0);
  334.         addElementView.add(patientSurnameTF,1,0);
  335.  
  336.         addElementView.add(new Label("город"),0,1);
  337.         addElementView.add(townTF,1,1);
  338.  
  339.         addElementView.add(addRecordButton,2,0);
  340.  
  341.  
  342.  
  343.  
  344.  
  345.         borderPane.setLeft(addElementView);
  346.         Scene dialogScene = new Scene(borderPane, 500, 500);
  347.         dialogStage.setScene(dialogScene);
  348.         dialogStage.show();
  349.  
  350.     }
  351.  
  352.     private void newDeleteStage(GridPane tableView,GridPane navigationView){
  353.         final Stage dialogStage = new Stage();
  354.         dialogStage.initModality(Modality.APPLICATION_MODAL);
  355.         GridPane view = new GridPane();
  356.         Button chooseDeletePatientFio = new Button("ФИО пациента");
  357.         Button chooseDeleteAddress = new Button("адрес");
  358.  
  359.         Label patientFioLabel = new Label();
  360.         Label addressLabel = new Label();
  361.  
  362.         Label counterDeleted = new Label("0");
  363.  
  364.         view.add(chooseDeletePatientFio, 0, 0);
  365.         view.add(patientFioLabel, 1, 0);
  366.  
  367.         view.add(chooseDeleteAddress, 0, 1);
  368.         view.add(addressLabel, 1, 1);
  369.  
  370.         view.add(new Label ("Удалено: "),0,2);
  371.         view.add(counterDeleted,1,2);
  372.  
  373.         chooseDeletePatientFio.setOnAction(click ->
  374.         {
  375.             TextInputDialog dialog = new TextInputDialog();
  376.             dialog.setTitle("Text Input Dialog");
  377.             dialog.setHeaderText(null);
  378.             dialog.setContentText("Please enter patient FIO:");
  379.             Optional<String> result = dialog.showAndWait();
  380.             if (result.isPresent()) {
  381.                 patientFioLabel.setText(result.get());;
  382.                 int tmp = 0;
  383.                 counterDeleted.setText("0");
  384.                 for (int i = 0; i < controller.getModelArray().size(); i++) {
  385.                     if (result.get().equals(controller.getModelArray().get(i).getPatientSurname())){
  386.                         System.out.println("DEATH");
  387.                         controller.delTag(i);
  388.                         i--;
  389.                         controller.ReadBySAX();
  390.                         tmp++;
  391.                         counterDeleted.setText(Integer.toString(tmp));
  392.  
  393.                         configurateTableView(tableView,controller.getModelArray(),1, controller.getModelArray().size());
  394.                         configurateNavigationVIew(navigationView,tableView,controller.getModelArray());
  395.                     }
  396.                 }
  397.             }
  398.         });
  399.  
  400.         chooseDeleteAddress.setOnAction(click ->
  401.         {
  402.             TextInputDialog dialog = new TextInputDialog();
  403.             dialog.setTitle("Text Input Dialog");
  404.             dialog.setHeaderText(null);
  405.             dialog.setContentText("Please enter patient address :");
  406.             Optional<String> result = dialog.showAndWait();
  407.             if (result.isPresent()) {
  408.                 counterDeleted.setText("0");
  409.                 addressLabel.setText(result.get());
  410.                 int tmp = 0;
  411.                 for (int i = 0; i < controller.getModelArray().size(); i++) {
  412.                     String tmpSTR = controller.getModelArray().get(i).getTown();
  413.                     if (tmpSTR.equals(result.get())) {
  414.                         System.out.println("DEATH");
  415.                         controller.delTag(i);
  416.                         i--;
  417.                         controller.ReadBySAX();
  418.                         tmp++;
  419.                         counterDeleted.setText(Integer.toString(tmp));
  420.  
  421.                         configurateTableView(tableView,controller.getModelArray(),1, controller.getModelArray().size());
  422.                         configurateNavigationVIew(navigationView,tableView,controller.getModelArray());
  423.                     }
  424.                 }
  425.             }
  426.         });
  427.  
  428.         Scene dialogScene = new Scene(view, 500, 900);
  429.         dialogStage.setScene(dialogScene);
  430.         dialogStage.show();
  431.  
  432.  
  433.     }
  434.  
  435.  
  436. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement