Advertisement
WentaoHu

AdminController

Dec 5th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.28 KB | None | 0 0
  1. package OR3.view;
  2.  
  3. import java.io.IOException;
  4. import java.sql.SQLException;
  5. import java.util.concurrent.Executor;
  6. import java.util.concurrent.Executors;
  7.  
  8. import OR3.Main;
  9. import OR3.Account.Account;
  10. import OR3.Account.AccountDAO;
  11. import javafx.collections.FXCollections;
  12. import javafx.collections.ObservableList;
  13. import javafx.event.ActionEvent;
  14. import javafx.fxml.FXML;
  15. import javafx.scene.control.TableColumn;
  16. import javafx.scene.control.TableView;
  17. import javafx.scene.control.TextArea;
  18. import javafx.scene.control.TextField;
  19.  
  20. public class AdminController {
  21. @FXML
  22. private Main main;
  23.  
  24. @FXML
  25. private TextField adminUser;
  26. @FXML
  27. private TextArea resultArea;
  28. @FXML
  29. private TableView<Account> AccountTable;
  30. @FXML
  31. private TableColumn<Account, String> accUserColumn;
  32. @FXML
  33. private TableColumn<Account, String> accPasswordColumn;
  34. @FXML
  35. private TableColumn<Account, String> accEmailColumn;
  36. @FXML
  37. private TableColumn<Account, String> accFirstColumn;
  38. @FXML
  39. private TableColumn<Account, String> accLastColumn;
  40. @FXML
  41. private TableColumn<Account, String> accBirthColumn;
  42. @FXML
  43. private TableColumn<Account, String> accPhoneColumn;
  44. @FXML
  45. private TableColumn<Account, String> accAdminColumn;
  46.  
  47. //For MultiThreading
  48. private Executor exec;
  49.  
  50.  
  51. @FXML
  52. private void initialize () throws SQLException, ClassNotFoundException {
  53.  
  54. //For MultiThreading: Create executor that uses daemon threads:
  55. exec = Executors.newCachedThreadPool((runnable) -> {
  56. Thread t = new Thread (runnable);
  57. t.setDaemon(true);
  58. return t;
  59. });
  60.  
  61. accUserColumn.setCellValueFactory(cellData -> cellData.getValue().userProperty());
  62. accFirstColumn.setCellValueFactory(cellData -> cellData.getValue().firstProperty());
  63. accLastColumn.setCellValueFactory(cellData -> cellData.getValue().lastProperty());
  64. accPasswordColumn.setCellValueFactory(cellData -> cellData.getValue().passwordProperty());
  65. accEmailColumn.setCellValueFactory(cellData -> cellData.getValue().emailProperty());
  66. accBirthColumn.setCellValueFactory(cellData -> cellData.getValue().birthProperty());
  67. accPhoneColumn.setCellValueFactory(cellData -> cellData.getValue().phoneProperty());
  68. accAdminColumn.setCellValueFactory(cellData -> cellData.getValue().adminProperty());
  69. }
  70.  
  71. // Delete an Account with a given Account user from DB
  72. @FXML
  73. private void deleteAccount (ActionEvent actionEvent) throws SQLException, ClassNotFoundException {
  74. try {
  75. AccountDAO.deleteAccWithId(adminUser.getText());
  76. resultArea.setText("Account deleted! Account user: " + adminUser.getText() + "\n");
  77. } catch (SQLException e) {
  78. resultArea.setText("Problem occurred while deleting Account " + e);
  79. throw e;
  80. }
  81. }
  82.  
  83. // Search an Account
  84. @FXML
  85. private void searchAccount (ActionEvent actionEvent) throws ClassNotFoundException, SQLException {
  86. try {
  87. Account acc = AccountDAO.searchAccount(adminUser.getText());
  88. populateAndShowAccount(acc);
  89. } catch (SQLException e) {
  90. e.printStackTrace();
  91. resultArea.setText("Error occurred while getting Account information from DB.\n" + e);
  92. throw e;
  93. }
  94. }
  95.  
  96. // Populate Account for TableView and Display Account on TextArea
  97. @FXML
  98. private void populateAndShowAccount(Account acc) throws ClassNotFoundException {
  99. if (acc != null) {
  100. populateAccount(acc);
  101. setaccInfoToTextArea(acc);
  102. } else {
  103. resultArea.setText("This Account does not exist!\n");
  104. }
  105. }
  106.  
  107. // Populate Account
  108. @FXML
  109. private void populateAccount (Account acc) throws ClassNotFoundException {
  110. //Declare and ObservableList for table view
  111. ObservableList<Account> accData = FXCollections.observableArrayList();
  112. //Add Account to the ObservableList
  113. accData.add(acc);
  114. //Set items to the AccountTable
  115. AccountTable.setItems(accData);
  116. }
  117.  
  118. // Set Account information to Text Area
  119. @FXML
  120. private void setaccInfoToTextArea (Account acc) {
  121. resultArea.setText("User: " + acc.getUser() + "\n" +
  122. "Password: " + acc.getPassword());
  123. }
  124.  
  125. //Search all Accounts
  126. @FXML
  127. private void searchAccounts(ActionEvent actionEvent) throws SQLException, ClassNotFoundException {
  128. try {
  129. ObservableList<Account> accData = AccountDAO.searchAccounts();
  130. populateAccounts(accData);
  131. } catch (SQLException e){
  132. System.out.println("Error occurred while getting Accounts information from DB.\n" + e);
  133. throw e;
  134. }
  135. }
  136.  
  137. // Populate Accounts for TableView
  138. @FXML
  139. private void populateAccounts (ObservableList<Account> accData) throws ClassNotFoundException {
  140. //Set items to the AccountTable
  141. AccountTable.setItems(accData);
  142. }
  143.  
  144. @FXML
  145. private void goAdminRest() throws IOException{
  146. main.showAdminRestView();
  147. }
  148.  
  149. @FXML
  150. private void goMain() throws IOException {
  151. main.showLoginView();
  152. }
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement