Advertisement
Guest User

Enrollment

a guest
Nov 19th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.67 KB | None | 0 0
  1. package guifx;
  2.  
  3. import java.time.LocalDate;
  4. import java.time.temporal.ChronoUnit;
  5. import java.util.ArrayList;
  6.  
  7. import application.controller.Controller;
  8. import application.model.Addition;
  9. import application.model.Conferee;
  10. import application.model.Conference;
  11. import application.model.Enrollment;
  12. import application.model.Excursion;
  13. import application.model.Hotel;
  14. import javafx.beans.value.ChangeListener;
  15. import javafx.collections.ObservableList;
  16. import javafx.geometry.HPos;
  17. import javafx.geometry.Insets;
  18. import javafx.geometry.Pos;
  19. import javafx.scene.Scene;
  20. import javafx.scene.control.Button;
  21. import javafx.scene.control.CheckBox;
  22. import javafx.scene.control.ComboBox;
  23. import javafx.scene.control.Label;
  24. import javafx.scene.control.ListView;
  25. import javafx.scene.control.SelectionMode;
  26. import javafx.scene.control.TextField;
  27. import javafx.scene.layout.GridPane;
  28. import javafx.scene.layout.VBox;
  29. import javafx.scene.text.Font;
  30. import javafx.scene.text.Text;
  31. import javafx.stage.Modality;
  32. import javafx.stage.Stage;
  33. import javafx.stage.StageStyle;
  34.  
  35. public class AddWindow extends Stage {
  36.     private Conference conference;
  37.     private Conferee conferee;
  38.     private Text confTitle;
  39.     private ComboBox<LocalDate> cbbArrival = new ComboBox<>(), cbbDeparture = new ComboBox<>();
  40.     private CheckBox cbxSpeaker = new CheckBox(), cbxCompanion = new CheckBox(), cbxHotel = new CheckBox();
  41.     private TextField txfCompanion = new TextField();
  42.     private ListView<Excursion> lvwExcursions = new ListView<>();
  43.     private ListView<Hotel> lvwHotels = new ListView<>();
  44.     private ListView<Addition> lvwAdditions = new ListView<>();
  45.     private TextField txfName = new TextField(), txfPhone = new TextField(), txfCountry = new TextField(),
  46.             txfCompany = new TextField();
  47.  
  48.     public AddWindow(Conference conference, String title, Conferee conferee) {
  49.         this.conference = conference;
  50.         this.conferee = conferee;
  51.         this.confTitle = new Text(conference.getName());
  52.  
  53.         this.initStyle(StageStyle.UTILITY);
  54.         this.initModality(Modality.APPLICATION_MODAL);
  55.         this.setResizable(false);
  56.  
  57.         this.setTitle(title);
  58.         GridPane pane = new GridPane();
  59.         this.initContent(pane);
  60.  
  61.         Scene scene = new Scene(pane);
  62.         this.setScene(scene);
  63.         updateControls();
  64.     }
  65.  
  66.     public AddWindow(Conference conference, String title) {
  67.         this(conference, title, null);
  68.     }
  69.  
  70.     private void initContent(GridPane pane) {
  71.         pane.setPadding(new Insets(20));
  72.         pane.setHgap(10);
  73.         pane.setVgap(10);
  74.         //pane.setGridLinesVisible(true);
  75.  
  76.         //Personal info panel
  77.         Label lblPersonInfo = new Label("Personal info");
  78.         lblPersonInfo.setFont(Font.font(15));
  79.         pane.add(lblPersonInfo, 0, 0, 2, 1);
  80.         VBox pInfo = new VBox(15);
  81.         pInfo.getChildren().addAll(new Label("Name:"), new Label("Phone:"), new Label("Country:"),
  82.                 new Label("Company:"));
  83.         pane.add(pInfo, 0, 1);
  84.  
  85.         VBox txfBox = new VBox(5);
  86.  
  87.         txfBox.getChildren().addAll(txfName, txfPhone, txfCountry, txfCompany);
  88.         pane.add(txfBox, 1, 1);
  89.  
  90.         //Conference info Panel
  91.         Label lblConfInfo = new Label("Conference info");
  92.         lblConfInfo.setFont(Font.font(15));
  93.         pane.add(lblConfInfo, 2, 0, 2, 1);
  94.  
  95.         VBox confLblBox = new VBox(10);
  96.         confLblBox.getChildren().addAll(new Label("Conference:"), new Label("Arrival:"), new Label("Departure:"),
  97.                 new Label("Speaker:"));
  98.         pane.add(confLblBox, 2, 1);
  99.  
  100.         VBox edidableStuffBox = new VBox(5);
  101.         edidableStuffBox.getChildren().addAll(confTitle, cbbArrival, cbbDeparture, cbxSpeaker);
  102.         edidableStuffBox.setAlignment(Pos.TOP_RIGHT);
  103.         pane.add(edidableStuffBox, 3, 1);
  104.  
  105.         ///Companion info panel
  106.         Label lblCompanion = new Label("Companion info");
  107.         lblCompanion.setStyle("-fx-font-size: 15");
  108.         pane.add(lblCompanion, 0, 2, 2, 1);
  109.  
  110.         VBox companionLblBox = new VBox(10);
  111.         companionLblBox.getChildren().addAll(new Label("Companion:"), new Label("Name:"),
  112.                 new Label("Choose excursions:\n(ctrl+click to\nselect more)"));
  113.         pane.add(companionLblBox, 0, 3);
  114.  
  115.         VBox edidableCompanionStuff = new VBox(5);
  116.         lvwExcursions.setPrefHeight(120);
  117.         lvwExcursions.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
  118.         edidableCompanionStuff.getChildren().addAll(cbxCompanion, txfCompanion, lvwExcursions);
  119.         pane.add(edidableCompanionStuff, 1, 3);
  120.  
  121.         ChangeListener<Boolean> listener = (pv, ov, nv) -> this.companionChanged(nv);
  122.         cbxCompanion.selectedProperty().addListener(listener);
  123.         cbxCompanion.setSelected(true);
  124.  
  125.         ////Hotel info panel
  126.  
  127.         Label lblHotel = new Label("Hotel info");
  128.         lblHotel.setStyle("-fx-font-size:15");
  129.         pane.add(lblHotel, 2, 2, 2, 1);
  130.  
  131.         VBox hotelLblBox = new VBox(2);
  132.         hotelLblBox.getChildren().addAll(new Label("Hotels"), lvwHotels);
  133.         lvwHotels.setMaxSize(120, 150);
  134.         pane.add(hotelLblBox, 2, 3);
  135.         ChangeListener<Hotel> hotelListener = (pv, ov, nv) -> this.selectedHotelChanged();
  136.         lvwHotels.getSelectionModel().selectedItemProperty().addListener(hotelListener);
  137.  
  138.         VBox addBox = new VBox(2);
  139.         addBox.getChildren().addAll(new Label("Additions"), lvwAdditions);
  140.  
  141.         lvwAdditions.setMaxSize(120, 150);
  142.         lvwAdditions.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
  143.         pane.add(addBox, 3, 3);
  144.  
  145.         cbxHotel.setText("Book hotel");
  146.         cbxHotel.setSelected(true);
  147.         ChangeListener<Boolean> hotelListenerCbx = (pv, ov, nv) -> this.hotelChanged(nv);
  148.         cbxHotel.selectedProperty().addListener(hotelListenerCbx);
  149.         cbxHotel.setSelected(true);
  150.         pane.add(cbxHotel, 3, 2);
  151.  
  152.         Button btnOk = new Button("Add registration");
  153.         pane.add(btnOk, 2, 4);
  154.         Button btnCancel = new Button("Cancel");
  155.         GridPane.setHalignment(btnCancel, HPos.RIGHT);
  156.         pane.add(btnCancel, 1, 4);
  157.  
  158.         btnOk.setOnAction(event -> this.okAction());
  159.         btnCancel.setOnAction(event -> this.cancelAction());
  160.  
  161.     }
  162.  
  163.     private void hotelChanged(Boolean nv) {
  164.         lvwHotels.setDisable(!nv);
  165.         lvwAdditions.setDisable(!nv);
  166.     }
  167.  
  168.     private void okAction() {
  169.         if (conferee == null) {
  170.             this.conferee = Controller.createConferee(txfName.getText(),
  171.                     txfPhone.getText(), txfCountry.getText(),
  172.                     Controller.createCompany(txfCompany.getText(), "123456"));
  173.         }
  174.         Hotel hotel = null;
  175.         if (cbxHotel.isSelected()) {
  176.             hotel = lvwHotels.getSelectionModel().getSelectedItem();
  177.         }
  178.         Enrollment enrollment = Controller.createEnrollment(conference,
  179.                 cbbArrival.getSelectionModel().getSelectedItem(), cbbDeparture.getSelectionModel().getSelectedItem(),
  180.                 hotel, cbxSpeaker.isSelected(), cbxCompanion.isSelected(), txfCompanion.getText(),
  181.                 getSelectedAdditions(),
  182.                 getSelectedExcursions());
  183.         ConfirmEnrollmentWindow window = new ConfirmEnrollmentWindow(conferee, enrollment);
  184.         window.showAndWait();
  185.         if (window.getResult()) {
  186.             Controller.enrollConferee(conferee, enrollment);
  187.             this.close();
  188.         } else {
  189.             conferee = null;
  190.         }
  191.     }
  192.  
  193.     private void cancelAction() {
  194.         this.close();
  195.     }
  196.  
  197.     private void selectedHotelChanged() {
  198.         Hotel selected = lvwHotels.getSelectionModel().getSelectedItem();
  199.         lvwAdditions.getItems().setAll(selected.getAdditions());
  200.     }
  201.  
  202.     private void companionChanged(Boolean nv) {
  203.         txfCompanion.setDisable(!nv);
  204.         lvwExcursions.setDisable(!nv);
  205.     }
  206.  
  207.     private ArrayList<Addition> getSelectedAdditions() {
  208.         ArrayList<Addition> list = new ArrayList<>();
  209.         ObservableList<Addition> oList = lvwAdditions.getSelectionModel().getSelectedItems();
  210.         for (Addition a : oList) {
  211.             list.add(a);
  212.         }
  213.         if (list.isEmpty()) {
  214.             return null;
  215.         } else {
  216.             return list;
  217.         }
  218.     }
  219.  
  220.     private ArrayList<Excursion> getSelectedExcursions() {
  221.         ObservableList<Excursion> list = lvwExcursions.getSelectionModel().getSelectedItems();
  222.         ArrayList<Excursion> reList = new ArrayList<>();
  223.         for (Excursion e : list) {
  224.             reList.add(e);
  225.         }
  226.         if (reList.isEmpty()) {
  227.             return null;
  228.         } else
  229.             return reList;
  230.     }
  231.  
  232.     private void updateControls() {
  233.         ArrayList<LocalDate> arrivalDates = new ArrayList<>();
  234.         int days = (int) conference.getStartDate().until(conference.getEndDate(), ChronoUnit.DAYS);
  235.         for (int i = 0; i <= days; i++) {
  236.             arrivalDates.add(conference.getStartDate().plusDays(i));
  237.         }
  238.         lvwHotels.getItems().setAll(conference.getHotels());
  239.         lvwExcursions.getItems().setAll(conference.getExcursions());
  240.         cbbArrival.getItems().setAll(arrivalDates);
  241.         cbbArrival.getSelectionModel().select(0);
  242.         cbbDeparture.getItems().setAll(arrivalDates);
  243.         cbbDeparture.getSelectionModel().select(conference.getEndDate());
  244.         if (conferee != null) {
  245.  
  246.             txfName.setText(conferee.getName());
  247.             txfName.setDisable(true);
  248.             txfPhone.setText(conferee.getPhone());
  249.             txfPhone.setDisable(true);
  250.             txfCountry.setText(conferee.getCountry());
  251.             txfCountry.setDisable(true);
  252.             if (conferee.getCompany() != null) {
  253.                 txfCompany.setText(conferee.getCompany().getName());
  254.             }
  255.             txfCompany.setDisable(true);
  256.         }
  257.     }
  258.  
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement