Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package dev.archiecortez.jfacelog.employeemanager; import dev.archiecortez.jfacelog.dialogs.NewPassDialog; import dev.archiecortez.jfacelog.employeemanager.employeeitem.DTREmployeeItem; import dev.archiecortez.jfacelog.entities.DTREmployee; import dev.archiecortez.jfacelog.services.EmployeeDTO; import java.io.IOException; import java.net.URL; import java.util.Optional; import java.util.ResourceBundle; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.Parent; import javafx.scene.control.Alert; import javafx.scene.control.Button; import javafx.scene.control.ListCell; import javafx.scene.control.ListView; import javafx.scene.control.SelectionMode; import javafx.scene.control.TextField; import javafx.util.Pair; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** * FXML Controller class * * @author Administrator */ @Component public class EmployeeManagerPresenter implements Initializable { @Autowired EmployeeDTO employeeDTO; @FXML ListView<DTREmployee> employeesListView; @FXML TextField fullname; @FXML TextField username; @FXML TextField password; @FXML TextField passwordCopy; @FXML TextField otMonday; @FXML TextField otTuesday; @FXML TextField otWednesday; @FXML TextField otThursday; @FXML TextField otFriday; @FXML Button btnSaveEmployee; @FXML Button btnDeleteEmployee; private ObservableList<DTREmployee> employees; /** * Initializes the controller class. */ @Override public void initialize(URL url, ResourceBundle rb) { // TODO employees = FXCollections.observableList(employeeDTO.listEmployees()); employeesListView.setItems(employees); employeesListView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE); employeesListView.setCellFactory(value -> { return new DTREmployeeItemCell(); }); } @FXML public void saveEmployee() { DTREmployee emp = new DTREmployee(); emp.setFullName(fullname.getText()); emp.setUsername(username.getText()); emp.getOfficialTime().setScheduleMonday(otMonday.getText()); emp.getOfficialTime().setScheduleTuesday(otTuesday.getText()); emp.getOfficialTime().setScheduleWednesday(otWednesday.getText()); emp.getOfficialTime().setScheduleThursday(otThursday.getText()); emp.getOfficialTime().setScheduleFriday(otFriday.getText()); employeeDTO.saveEmployee(emp); employees.add(emp); } @FXML public void deleteEmployee() { DTREmployee emp = (DTREmployee) employeesListView.getSelectionModel().getSelectedItem(); employeeDTO.deleteEmployee(emp); employees.remove(emp); } @FXML public void newPass() throws IOException { NewPassDialog newPassDialog = new NewPassDialog(); Optional<Pair<String, String>> rslt = newPassDialog.showAndWait(); try { if (rslt.isPresent()) { DTREmployee emp = (DTREmployee) employeesListView.getSelectionModel().getSelectedItem(); emp.changePassword(rslt.get().getKey(), rslt.get().getValue()); employeeDTO.updateEmployee(emp); } } catch (Exception e) { Alert a = new Alert(Alert.AlertType.ERROR); a.setTitle("Error setting password."); a.setContentText(e.getMessage()); a.showAndWait(); } } private class DTREmployeeItemCell extends ListCell<DTREmployee> { public DTREmployeeItemCell() { super(); } @Override protected void updateItem(DTREmployee item, boolean empty) { super.updateItem(item, empty); //To change body of generated methods, choose Tools | Templates. setText(null); if (empty) { setGraphic(null); } else if (!empty) { DTREmployeeItem emp = new DTREmployeeItem(); emp.usernameProperty().set(item.getUsername()); emp.fullNameProperty().set(item.getFullName()); emp.officialTimeProperty().set(item.getOfficialTime().toString()); setGraphic(emp); } } } }