Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package OR3.view;
- import java.io.IOException;
- import java.sql.SQLException;
- import java.util.concurrent.Executor;
- import java.util.concurrent.Executors;
- import OR3.Main;
- import OR3.Account.Account;
- import OR3.Account.AccountDAO;
- import javafx.collections.FXCollections;
- import javafx.collections.ObservableList;
- import javafx.event.ActionEvent;
- import javafx.fxml.FXML;
- import javafx.scene.control.TableColumn;
- import javafx.scene.control.TableView;
- import javafx.scene.control.TextArea;
- import javafx.scene.control.TextField;
- public class AdminController {
- @FXML
- private Main main;
- @FXML
- private TextField adminUser;
- @FXML
- private TextArea resultArea;
- @FXML
- private TableView<Account> AccountTable;
- @FXML
- private TableColumn<Account, String> accUserColumn;
- @FXML
- private TableColumn<Account, String> accPasswordColumn;
- @FXML
- private TableColumn<Account, String> accEmailColumn;
- @FXML
- private TableColumn<Account, String> accFirstColumn;
- @FXML
- private TableColumn<Account, String> accLastColumn;
- @FXML
- private TableColumn<Account, String> accBirthColumn;
- @FXML
- private TableColumn<Account, String> accPhoneColumn;
- @FXML
- private TableColumn<Account, String> accAdminColumn;
- //For MultiThreading
- private Executor exec;
- @FXML
- private void initialize () throws SQLException, ClassNotFoundException {
- //For MultiThreading: Create executor that uses daemon threads:
- exec = Executors.newCachedThreadPool((runnable) -> {
- Thread t = new Thread (runnable);
- t.setDaemon(true);
- return t;
- });
- accUserColumn.setCellValueFactory(cellData -> cellData.getValue().userProperty());
- accFirstColumn.setCellValueFactory(cellData -> cellData.getValue().firstProperty());
- accLastColumn.setCellValueFactory(cellData -> cellData.getValue().lastProperty());
- accPasswordColumn.setCellValueFactory(cellData -> cellData.getValue().passwordProperty());
- accEmailColumn.setCellValueFactory(cellData -> cellData.getValue().emailProperty());
- accBirthColumn.setCellValueFactory(cellData -> cellData.getValue().birthProperty());
- accPhoneColumn.setCellValueFactory(cellData -> cellData.getValue().phoneProperty());
- accAdminColumn.setCellValueFactory(cellData -> cellData.getValue().adminProperty());
- }
- // Delete an Account with a given Account user from DB
- @FXML
- private void deleteAccount (ActionEvent actionEvent) throws SQLException, ClassNotFoundException {
- try {
- AccountDAO.deleteAccWithId(adminUser.getText());
- resultArea.setText("Account deleted! Account user: " + adminUser.getText() + "\n");
- } catch (SQLException e) {
- resultArea.setText("Problem occurred while deleting Account " + e);
- throw e;
- }
- }
- // Search an Account
- @FXML
- private void searchAccount (ActionEvent actionEvent) throws ClassNotFoundException, SQLException {
- try {
- Account acc = AccountDAO.searchAccount(adminUser.getText());
- populateAndShowAccount(acc);
- } catch (SQLException e) {
- e.printStackTrace();
- resultArea.setText("Error occurred while getting Account information from DB.\n" + e);
- throw e;
- }
- }
- // Populate Account for TableView and Display Account on TextArea
- @FXML
- private void populateAndShowAccount(Account acc) throws ClassNotFoundException {
- if (acc != null) {
- populateAccount(acc);
- setaccInfoToTextArea(acc);
- } else {
- resultArea.setText("This Account does not exist!\n");
- }
- }
- // Populate Account
- @FXML
- private void populateAccount (Account acc) throws ClassNotFoundException {
- //Declare and ObservableList for table view
- ObservableList<Account> accData = FXCollections.observableArrayList();
- //Add Account to the ObservableList
- accData.add(acc);
- //Set items to the AccountTable
- AccountTable.setItems(accData);
- }
- // Set Account information to Text Area
- @FXML
- private void setaccInfoToTextArea (Account acc) {
- resultArea.setText("User: " + acc.getUser() + "\n" +
- "Password: " + acc.getPassword());
- }
- //Search all Accounts
- @FXML
- private void searchAccounts(ActionEvent actionEvent) throws SQLException, ClassNotFoundException {
- try {
- ObservableList<Account> accData = AccountDAO.searchAccounts();
- populateAccounts(accData);
- } catch (SQLException e){
- System.out.println("Error occurred while getting Accounts information from DB.\n" + e);
- throw e;
- }
- }
- // Populate Accounts for TableView
- @FXML
- private void populateAccounts (ObservableList<Account> accData) throws ClassNotFoundException {
- //Set items to the AccountTable
- AccountTable.setItems(accData);
- }
- @FXML
- private void goAdminRest() throws IOException{
- main.showAdminRestView();
- }
- @FXML
- private void goMain() throws IOException {
- main.showLoginView();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement