Guest User

Untitled

a guest
Apr 9th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.68 KB | None | 0 0
  1. public class BookViewController {
  2.  
  3. private String dbUrl = "jdbc:postgresql://localhost:5432/BookingApp";
  4. private String dbUsername = "postgres";
  5. private String dbPassword = "secret";
  6. @FXML
  7. private TextField personnr;
  8. @FXML
  9. private TextField name;
  10. @FXML
  11. private TextField email;
  12. @FXML
  13. private TextField telnr;
  14. @FXML
  15. private Button addTraveler;
  16. @FXML
  17. private Button removeTraveler;
  18. @FXML
  19. private TableView<Traveler> travelers;
  20. @FXML
  21. private TableColumn<?, ?> personnrColumn;
  22. @FXML
  23. private TableColumn<?, ?> nameColumn;
  24. @FXML
  25. private TableColumn<?, ?> emailColumn;
  26. @FXML
  27. private TableColumn<?, ?> telnrColumn;
  28. @FXML
  29. private TableView<Trip> trips;
  30. @FXML
  31. private TableColumn<?, ?> originColumn;
  32. @FXML
  33. private TableColumn<?, ?> destinationColumn;
  34. @FXML
  35. private TableColumn<?, ?> departureColumn;
  36. @FXML
  37. private TableColumn<?, ?> arrivalColumn;
  38. @FXML
  39. private TableColumn<?, ?> driverColumn;
  40. @FXML
  41. private TableColumn<?, ?> priceAmountColumn;
  42. @FXML
  43. private TableColumn<?, ?> seatsColumn;
  44. @FXML
  45. private Button bookTrip;
  46. private Trip rowData;
  47. private ObservableList<Trip> tripData;
  48.  
  49. protected void initialize(Trip rowData) {
  50. this.rowData = rowData;
  51. addTraveler.setDisable(true);
  52. removeTraveler.setDisable(true);
  53. populateTravelPlan();
  54. }
  55.  
  56. @FXML
  57. private void populateTravelPlan() {
  58. originColumn.setCellValueFactory(new PropertyValueFactory<>("origin"));
  59. destinationColumn.setCellValueFactory(new PropertyValueFactory<>("destination"));
  60. departureColumn.setCellValueFactory(new PropertyValueFactory<>("departure"));
  61. arrivalColumn.setCellValueFactory(new PropertyValueFactory<>("arrival"));
  62. driverColumn.setCellValueFactory(new PropertyValueFactory<>("driver"));
  63. priceAmountColumn.setCellValueFactory(new PropertyValueFactory<>("priceAmount"));
  64. seatsColumn.setCellValueFactory(new PropertyValueFactory<>("seats"));
  65.  
  66. try {
  67. Connection conn = DriverManager.getConnection(dbUrl, dbUsername, dbPassword);
  68. tripData = FXCollections.observableArrayList();
  69. ResultSet rs = null;
  70. if(rowData.getTrip1() != 0 && rowData.getTrip2() == 0 && rowData.getTrip3() == 0) {
  71. rs = conn.createStatement().executeQuery(
  72. "select trip.tripid, connexion.origin, connexion.destination, trip.departure, trip.arrival, trip.driverpnr, trip.priceamount, trip.seatsrn" +
  73. "from trip, connexionrn" +
  74. "where trip.tripid = " + rowData.getTrip1() + "rn" +
  75. "and trip.connexionid = connexion.connexionidrn" +
  76. "order by departure;");
  77. } else if(rowData.getTrip1() != 0 && rowData.getTrip2() != 0 && rowData.getTrip3() == 0) {
  78. rs = conn.createStatement().executeQuery(
  79. "select trip.tripid, connexion.origin, connexion.destination, trip.departure, trip.arrival, trip.driverpnr, trip.priceamount, trip.seatsrn" +
  80. "from trip, connexionrn" +
  81. "where trip.tripid = " + rowData.getTrip1() + "rn" +
  82. "and trip.connexionid = connexion.connexionidrn" +
  83. "unionrn" +
  84. "select trip.tripid, connexion.origin, connexion.destination, trip.departure, trip.arrival, trip.driverpnr, trip.priceamount, trip.seatsrn" +
  85. "from trip, connexionrn" +
  86. "where trip.tripid = " + rowData.getTrip2() + "rn" +
  87. "and trip.connexionid = connexion.connexionidrn" +
  88. "order by departure;");
  89. } else if(rowData.getTrip1() != 0 && rowData.getTrip2() != 0 && rowData.getTrip3() != 0) {
  90. rs = conn.createStatement().executeQuery(
  91. "select trip.tripid, connexion.origin, connexion.destination, trip.departure, trip.arrival, trip.driverpnr, trip.priceamount, trip.seatsrn" +
  92. "from trip, connexionrn" +
  93. "where trip.tripid = " + rowData.getTrip1() + "rn" +
  94. "and trip.connexionid = connexion.connexionidrn" +
  95. "unionrn" +
  96. "select trip.tripid, connexion.origin, connexion.destination, trip.departure, trip.arrival, trip.driverpnr, trip.priceamount, trip.seatsrn" +
  97. "from trip, connexionrn" +
  98. "where trip.tripid = " + rowData.getTrip2() + "rn" +
  99. "and trip.connexionid = connexion.connexionidrn" +
  100. "unionrn" +
  101. "select trip.tripid, connexion.origin, connexion.destination, trip.departure, trip.arrival, trip.driverpnr, trip.priceamount, trip.seatsrn" +
  102. "from trip, connexionrn" +
  103. "where trip.tripid = " + rowData.getTrip3() + "rn" +
  104. "and trip.connexionid = connexion.connexionidrn" +
  105. "order by departure;");
  106. }
  107. if(rs.next()) {
  108. tripData.add(new Trip(rs.getInt(1), rs.getString(2), rs.getString(3), ""+rs.getTimestamp(4), ""+rs.getTimestamp(5), rs.getString(6), ""+rs.getInt(7), ""+rs.getInt(8)));
  109. while(rs.next()) {
  110. tripData.add(new Trip(rs.getInt(1), rs.getString(2), rs.getString(3), ""+rs.getTimestamp(4), ""+rs.getTimestamp(5), rs.getString(6), ""+rs.getInt(7), ""+rs.getInt(8)));
  111. }
  112. }
  113. } catch (SQLException e) {
  114. e.printStackTrace();
  115. }
  116. trips.setItems(tripData);
  117. }
  118. }
  119.  
  120. driverColumn.setCellFactory(this::createDriverCell);
  121.  
  122. private TableCell<Trip,String> createDriverCell(TableColumn<Trip, String> table) {
  123. return new TableCell<Trip, String>() {
  124. @Override
  125. protected void updateItem(String item, boolean empty) {
  126. super.updateItem(item, empty);
  127.  
  128. if (empty) {
  129. // configure empty
  130. } else if (item == null) {
  131. // create no driver content
  132. } else {
  133. // create different content
  134. }
  135. }
  136. };
  137. }
  138.  
  139. driverColumn.setCellFactory(param -> new TableCell<Trip,String>{
  140. public void updateItem(Trip t, boolean empty) {
  141. super.updateItem(t, empty);
  142. if(t.getDriver()!=null) {
  143. setText(t.getDriver());
  144. //revert graphic and contentDisplay, as cells may be reused
  145. setGraphic(null);
  146. setContentDisplay(ContentDisplay.TEXT_ONLY);
  147. } else {
  148. Button b = new Button("Choose Driver");
  149. //... hook up button actions, etc
  150. setGraphic(b);
  151. setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
  152. }
  153. }
  154. });
  155.  
  156. @Override
  157. public void start(Stage primaryStage) {
  158. TableView<StringProperty> table = new TableView<>();
  159.  
  160. // fill table
  161. for (int i = 0; i < 20; i++) {
  162. table.getItems().add(new SimpleStringProperty());
  163. }
  164.  
  165. TableColumn<StringProperty, String> column = new TableColumn<>();
  166. column.setCellValueFactory(TableColumn.CellDataFeatures::getValue); // returns the row item itself
  167. column.setCellFactory(col -> new TableCell<StringProperty, String>() {
  168.  
  169. private final Button button;
  170.  
  171. {
  172. button = new Button("Set Value...");
  173. button.setOnAction(evt -> {
  174. TextInputDialog dialog = new TextInputDialog();
  175. Optional<String> opt = dialog.showAndWait();
  176. String result = opt.orElse(null);
  177. if (result != null) {
  178. ObservableValue<String> ov = getTableColumn().getCellObservableValue(getIndex());
  179. if (ov instanceof WritableValue) {
  180. ((WritableValue) ov).setValue(result);
  181. }
  182. }
  183. });
  184. }
  185.  
  186. @Override
  187. protected void updateItem(String item, boolean empty) {
  188. super.updateItem(item, empty);
  189.  
  190. if (empty) {
  191. // empty cell
  192. setText("");
  193. setGraphic(null);
  194. } else {
  195. if (item == null) {
  196. // editable cell
  197. setText("");
  198. setGraphic(button);
  199. } else {
  200. // uneditable cell
  201. setText(item);
  202. setGraphic(null);
  203. }
  204. }
  205. }
  206.  
  207. });
  208.  
  209. table.getColumns().add(column);
  210.  
  211. Scene scene = new Scene(table);
  212.  
  213. primaryStage.setScene(scene);
  214. primaryStage.show();
  215. }
Add Comment
Please, Sign In to add comment