Advertisement
Guest User

Untitled

a guest
Oct 24th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.07 KB | None | 0 0
  1. package UserInterface;
  2.  
  3. import Bookings.ArrangementBooking;
  4. import Bookings.Booking;
  5. import Bookings.BookingDataAccessor;
  6. import Bookings.LectureBooking;
  7. import Customers.LectureBookingCustomer;
  8. import enums.BookingStatus;
  9. import javafx.collections.FXCollections;
  10. import javafx.collections.ObservableList;
  11. import javafx.event.ActionEvent;
  12. import javafx.fxml.FXML;
  13. import javafx.fxml.FXMLLoader;
  14. import javafx.scene.Parent;
  15. import javafx.scene.Scene;
  16. import javafx.scene.control.*;
  17. import javafx.stage.Modality;
  18. import javafx.stage.Stage;
  19. import javafx.stage.StageStyle;
  20. import org.controlsfx.control.textfield.TextFields;
  21.  
  22. import java.io.IOException;
  23. import java.sql.SQLException;
  24. import java.util.ArrayList;
  25. import java.util.Optional;
  26.  
  27. public class MainScreenController extends GeneralController {
  28. private final BookingDataAccessor bda = new BookingDataAccessor(
  29. "org.postgresql.Driver",
  30. "jdbc:postgresql://packy.db.elephantsql.com/jyjczxth",
  31. "jyjczxth",
  32. "nw51BNKhctporjIFT5Qhhm72jwGVJK95"
  33. );
  34. //private ArrayList<Booking> listOfBookings = new ArrayList<>();
  35.  
  36. @FXML
  37. private Button refreshBookingsButton, overviewButton, pendingBookingsButton, activeBookingsButton,
  38. finishedBookingsButton, archivedBookingsButton;
  39. @FXML
  40. private MenuItem lectureBookingItem, arrangementBookingItem;
  41. @FXML
  42. private TextField searchField;
  43. @FXML
  44. private ListView bookingListView;
  45.  
  46. //Nodes for booking information display area
  47. @FXML
  48. private Label customerCommentLabel, bookingTypeLabel, bookingStatusLabel, creationDateLabel, dateLabel,
  49. timeLabel, pupilNoLabel, teamNoLabel, teacherNoLabel, gradeLabel, topicChoiceLabel, schoolNameLabel,
  50. schoolPhoneNumberLabel, zipcodeLabel, cityLabel, communeLabel, phoneNumberLabel, contactPersonLabel,
  51. emailLabel, eanLabel;
  52. @FXML
  53. private TextArea customerCommentArea;
  54. @FXML
  55. private Button acceptBookingButton, cancelBookingButton, editBookingButton;
  56.  
  57. public MainScreenController() throws SQLException, ClassNotFoundException {
  58. }
  59.  
  60. public void initialize() throws SQLException {
  61. customerCommentLabel.setVisible(false);
  62. customerCommentArea.setVisible(false);
  63. acceptBookingButton.setVisible(false);
  64. cancelBookingButton.setVisible(false);
  65. editBookingButton.setVisible(false);
  66.  
  67. //Takes all Booking objects from listOfBookings and load them into bookingListView
  68. loadBookingsToListView();
  69.  
  70. /* Search field controlsfx */
  71. ArrayList<String> listOfContactPersonNames = new ArrayList<>();
  72. for (Booking temp : fetchBookingsFromDatabase()) {
  73. listOfContactPersonNames.add(temp.getCustomer().getContactPerson());
  74. }
  75. String[] options = listOfContactPersonNames.toArray(new String[0]);
  76. TextFields.bindAutoCompletion(searchField, options);
  77.  
  78. /*
  79. * Event handlers
  80. */
  81.  
  82. //Shows searched for booking in ListView
  83. searchField.setOnKeyTyped(e -> {
  84. try {
  85. if (searchField.getText().isEmpty()) {
  86. loadBookingsToListView();
  87. } else showSearchedForBookingsInListView(fetchBookingsFromDatabase());
  88. } catch (SQLException e1) {
  89. e1.printStackTrace();
  90. }
  91. });
  92.  
  93. bookingListView.setOnMouseClicked(e -> showSelectedBookingInformation());
  94.  
  95. //Opens pop-up window corresponding to chosen menu item (method used from GeneralController)
  96. lectureBookingItem.setOnAction(e -> openNewPopUpWindow("LectureBookingCreation.fxml"));
  97. arrangementBookingItem.setOnAction(e -> openNewPopUpWindow("ArrangementBookingCreation.fxml"));
  98.  
  99. //Reloads the bookings from database into ListView
  100. refreshBookingsButton.setOnMouseClicked(e -> refreshBookingListView());
  101.  
  102. //Opens edit pop-up window corresponding to chosen Booking in ListView
  103. editBookingButton.setOnMouseClicked(e -> {
  104. if (bookingListView.getSelectionModel().getSelectedItem() instanceof LectureBooking) {
  105. editSelectedLectureBooking((LectureBooking) (bookingListView.getSelectionModel().getSelectedItem()));
  106. } else if (bookingListView.getSelectionModel().getSelectedItem() instanceof ArrangementBooking) {
  107. editSelectedArrangementBooking((Bookings.ArrangementBooking) (bookingListView.getSelectionModel().getSelectedItem()));
  108. }
  109. });
  110.  
  111. acceptBookingButton.setOnMouseClicked(e -> {
  112. try {
  113. acceptSelectedBooking(fetchBookingsFromDatabase());
  114. } catch (SQLException e1) {
  115. e1.printStackTrace();
  116. }
  117. });
  118.  
  119. //Cancelling the selected booking when pressing cancelBookingButton
  120. cancelBookingButton.setOnMouseClicked(e -> {
  121. Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
  122. alert.setHeaderText("Vil du slette bookingen?");
  123. alert.setContentText("Handlingen vil slette bookingen");
  124.  
  125. Optional<ButtonType> alertChoice = alert.showAndWait();
  126.  
  127. if (alertChoice.get() == ButtonType.OK) {
  128. deleteSelectedBooking();
  129. removeBookingFromListView();
  130. }
  131. });
  132. }
  133.  
  134. private ArrayList<Booking> fetchBookingsFromDatabase() throws SQLException {
  135. ArrayList<Booking> listOfBookings = new ArrayList<>();
  136. listOfBookings.addAll(bda.fetchLecBooks());
  137. listOfBookings.addAll(bda.fetchArrBooks());
  138.  
  139. return listOfBookings;
  140. }
  141.  
  142. //Takes an ArrayList of bookings to load into ListView of bookings
  143. private void loadBookingsToListView() throws SQLException {
  144. ObservableList<Booking> bookings = FXCollections.observableArrayList();
  145. for (Booking booking : fetchBookingsFromDatabase()) {
  146. bookings.addAll(booking);
  147. }
  148. bookingListView.setItems(bookings);
  149. }
  150.  
  151. private void refreshBookingListView() {
  152. try {
  153. loadBookingsToListView();
  154. bda.getConnection().close();
  155. } catch (SQLException e) {
  156. e.printStackTrace();
  157. }
  158. }
  159.  
  160. private void removeBookingFromListView() {
  161. Booking bookingToRemove = (Booking) bookingListView.getSelectionModel().getSelectedItem();
  162. bookingListView.getItems().remove(bookingToRemove);
  163. }
  164.  
  165. private void deleteSelectedBooking() {
  166. try {
  167. bda.deleteBooking((Booking) bookingListView.getSelectionModel().getSelectedItem());
  168. } catch (SQLException e) {
  169. e.printStackTrace();
  170. }
  171. }
  172.  
  173. //Displays information of the clicked booking in ListView
  174. private void showSelectedBookingInformation() {
  175. if (bookingListView.getSelectionModel().getSelectedItem() instanceof LectureBooking) {
  176. showLectureBookingInformation((LectureBooking) bookingListView.getSelectionModel().getSelectedItem());
  177. } else if (bookingListView.getSelectionModel().getSelectedItem() instanceof ArrangementBooking) {
  178. showArrangementBookingInformation((ArrangementBooking) (bookingListView.getSelectionModel().getSelectedItem()));
  179. }
  180. }
  181.  
  182. private void showSearchedForBookingsInListView(ArrayList<Booking> listOfBookings) {
  183. for (Booking temp : listOfBookings) {
  184. String enteredBooking = searchField.getText();
  185. if (temp.getCustomer().getContactPerson().equals(enteredBooking)) {
  186. bookingListView.getSelectionModel().clearSelection();
  187. ObservableList<Booking> bookings = FXCollections.observableArrayList();
  188. bookings.add(temp);
  189. bookingListView.setItems(bookings);
  190. }
  191. }
  192. }
  193.  
  194. @FXML
  195. private void showChosenCategoryBookings(ActionEvent event) throws SQLException {
  196. //bookingListView.getSelectionModel().clearSelection();
  197.  
  198. Button chosenCategoryBtn = (Button) event.getSource();
  199. String nameOfChosenBtn = chosenCategoryBtn.getText();
  200.  
  201. ObservableList<Booking> categorisedBookings = FXCollections.observableArrayList();
  202.  
  203. for (Booking temp : fetchBookingsFromDatabase()) {
  204. if (!nameOfChosenBtn.equals("Oversigt")) {
  205. BookingStatus chosenBookingStatus = BookingStatus.statusChosen(nameOfChosenBtn);
  206. if (temp.getBookingStatus().equals(chosenBookingStatus)) {
  207. categorisedBookings.add(temp);
  208. }
  209. }
  210. }
  211. bookingListView.setItems(categorisedBookings);
  212. }
  213.  
  214. //TODO
  215. //Accepting the selected booking when pressing acceptBookingButton
  216. private void acceptSelectedBooking(ArrayList<Booking> listOfBookings) {
  217. if (bookingListView.getSelectionModel().getSelectedItem() != null) {
  218. //System.out.println(bookingListView.getSelectionModel().getSelectedItem().toString());
  219. }
  220. }
  221.  
  222. private void editSelectedLectureBooking(LectureBooking selectedLectureBooking) {
  223. try {
  224. FXMLLoader loader = new FXMLLoader(getClass().getResource("EditLectureBooking.fxml"));
  225. Parent root = loader.load();
  226.  
  227. EditLectureBookingController controller = loader.getController();
  228. controller.setSelectedLectureBooking(selectedLectureBooking);
  229. controller.initData();
  230.  
  231. Stage stage = new Stage();
  232. stage.setScene(new Scene(root));
  233. stage.initModality(Modality.APPLICATION_MODAL);
  234. stage.initStyle(StageStyle.UNDECORATED);
  235. stage.showAndWait();
  236. } catch (IOException e) {
  237. e.printStackTrace();
  238. }
  239. }
  240.  
  241. private void editSelectedArrangementBooking(ArrangementBooking selectedArrangementBooking) {
  242. try {
  243. FXMLLoader loader = new FXMLLoader(getClass().getResource("EditArrangementBooking.fxml"));
  244. Parent root = loader.load();
  245.  
  246. EditArrangementBookingController controller = loader.getController();
  247. controller.setSelectedArrangementBooking(selectedArrangementBooking);
  248. controller.initData();
  249.  
  250. Stage stage = new Stage();
  251. stage.setScene(new Scene(root));
  252. stage.initModality(Modality.APPLICATION_MODAL);
  253. stage.initStyle(StageStyle.UNDECORATED);
  254. stage.showAndWait();
  255. } catch (IOException e) {
  256. e.printStackTrace();
  257. }
  258. }
  259.  
  260.  
  261.  
  262. //Changes text on all labels corresponding to the chosen booking in ListView
  263. private void showLectureBookingInformation(LectureBooking selectedLectureBooking) {
  264. customerCommentLabel.setVisible(true);
  265. customerCommentArea.setVisible(true);
  266. acceptBookingButton.setVisible(true);
  267. cancelBookingButton.setVisible(true);
  268. editBookingButton.setVisible(true);
  269.  
  270. communeLabel.setVisible(true);
  271. cityLabel.setVisible(true);
  272. contactPersonLabel.setVisible(true);
  273. phoneNumberLabel.setVisible(true);
  274. emailLabel.setVisible(true);
  275. eanLabel.setVisible(true);
  276.  
  277. LectureBookingCustomer temp = (LectureBookingCustomer) selectedLectureBooking.getCustomer();
  278. bookingTypeLabel.setText(selectedLectureBooking.getBookingType().toString());
  279. bookingStatusLabel.setText(selectedLectureBooking.getBookingStatus().toString());
  280. creationDateLabel.setText("Oprettet: " + selectedLectureBooking.getCreationDate().toString());
  281. dateLabel.setText("Dato: " + selectedLectureBooking.getDateTime().toLocalDate().toString());
  282. timeLabel.setText("Tidspunkt: " + selectedLectureBooking.getDateTime().toLocalTime().toString());
  283. pupilNoLabel.setText("Antal elever: " + selectedLectureBooking.getParticipants());
  284. teamNoLabel.setText("Antal hold: " + selectedLectureBooking.getNoOfTeams());
  285. teacherNoLabel.setText("Antal lærere: " + selectedLectureBooking.getNoOfTeachers());
  286. gradeLabel.setText("Klassetrin: " + selectedLectureBooking.getGrade());
  287. topicChoiceLabel.setText("Valg af emne: " + selectedLectureBooking.getChoiceOfTopic().toString());
  288. communeLabel.setText("Aalborg Kommune (Ja/Nej): " + temp.getCommune());
  289. schoolNameLabel.setText("Skolens navn: " + temp.getSchoolName());
  290. schoolPhoneNumberLabel.setText("Skolens telefonnummer: " + temp.getSchoolPhoneNumber());
  291. zipcodeLabel.setText("Postnummer: " + temp.getZipCode());
  292. cityLabel.setText("By: " + temp.getCity());
  293. contactPersonLabel.setText("Kontaktperson: " + selectedLectureBooking.getCustomer().getContactPerson());
  294. phoneNumberLabel.setText("Telefonnummer: " + selectedLectureBooking.getCustomer().getPhoneNumber());
  295. emailLabel.setText("E-mail: " + selectedLectureBooking.getCustomer().getEmail());
  296. eanLabel.setText("EAN nummer: " + temp.getEanNumber());
  297. customerCommentArea.setText(selectedLectureBooking.getComment());
  298. customerCommentArea.setEditable(false);
  299. }
  300.  
  301. private void showArrangementBookingInformation(ArrangementBooking selectedArrangementBooking) {
  302. customerCommentLabel.setVisible(true);
  303. customerCommentArea.setVisible(true);
  304. acceptBookingButton.setVisible(true);
  305. cancelBookingButton.setVisible(true);
  306. editBookingButton.setVisible(true);
  307.  
  308. communeLabel.setVisible(false);
  309. cityLabel.setVisible(false);
  310. contactPersonLabel.setVisible(false);
  311. phoneNumberLabel.setVisible(false);
  312. emailLabel.setVisible(false);
  313. eanLabel.setVisible(false);
  314. customerCommentArea.setEditable(false);
  315.  
  316. bookingTypeLabel.setText(selectedArrangementBooking.getBookingType().toString());
  317. bookingStatusLabel.setText(selectedArrangementBooking.getBookingStatus().toString());
  318. dateLabel.setText("Dato: " + selectedArrangementBooking.getDateTime().toLocalDate().toString());
  319. creationDateLabel.setText("Oprettet: " + selectedArrangementBooking.getCreationDate().toString());
  320. timeLabel.setText("Tidspunkt: " + selectedArrangementBooking.getDateTime().toLocalTime().toString());
  321. pupilNoLabel.setText("Antal børn: " + selectedArrangementBooking.getParticipants());
  322. teamNoLabel.setText("Fødselsdagsbarnets navn: " + selectedArrangementBooking.getBirthdayChildName());
  323. teacherNoLabel.setText("Barnets alder: " + selectedArrangementBooking.getBirthdayChildAge());
  324. gradeLabel.setText("Tidligere deltager (Ja/Nej): " + selectedArrangementBooking.getFormerParticipant());
  325. topicChoiceLabel.setText("Valg af menu: " + selectedArrangementBooking.getMenuChosen());
  326. schoolNameLabel.setText("Kontaktperson: " + selectedArrangementBooking.getCustomer().getContactPerson());
  327. schoolPhoneNumberLabel.setText("Telefonnummer: " + selectedArrangementBooking.getCustomer().getPhoneNumber());
  328. zipcodeLabel.setText("E-mail: " + selectedArrangementBooking.getCustomer().getEmail());
  329. customerCommentArea.setText(selectedArrangementBooking.getCustomerComment());
  330.  
  331.  
  332. }
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement