/
syunya
/
Course2026
Обзор
Документация
Войти
/
syunya
/
Course2026
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/main/java/ru.machine.service/controller/MachineController.java
362 строки
8 KB
syunya
Update: MachineController.java, machines.fxml
25 июн 2026, 22:26
Верифицирован
25 июн 2026, 22:26
455254f
Код
Авторство
О чём код?
package ru.machine.service.controller; import javafx.collections.FXCollections; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.layout.VBox; import javafx.stage.Stage; import ru.machine.service.dao.MachineDAO; import ru.machine.service.dao.MachineTypeDAO; import ru.machine.service.model.Machine; import ru.machine.service.model.MachineType; import ru.machine.service.util.Session; import ru.machine.service.model.User; public class MachineController { @FXML private TableView<Machine> tableMachines; @FXML private TableColumn<Machine,Integer> colId; @FXML private TableColumn<Machine,String> colInventory; @FXML private Button btnAdd; @FXML private Button btnEdit; @FXML private Button btnDelete; @FXML private TableColumn<Machine,Integer> colType; private MachineDAO machineDAO; private MachineTypeDAO machineTypeDAO; @FXML public void initialize() { try { User currentUser = Session.getCurrentUser(); if (currentUser != null && currentUser.getRoleId() == 2) { btnAdd.setVisible(false); btnEdit.setVisible(false); btnDelete.setVisible(false); btnAdd.setManaged(false); btnEdit.setManaged(false); btnDelete.setManaged(false); } machineDAO = new MachineDAO(); machineTypeDAO = new MachineTypeDAO(); colId.setCellValueFactory( new PropertyValueFactory<>("id") ); colInventory.setCellValueFactory( new PropertyValueFactory<>( "inventoryNumber" ) ); colType.setCellValueFactory( new PropertyValueFactory<>( "machineTypeId" ) ); loadData(); } catch (Exception e) { showError( e.getMessage() ); } } @FXML public void loadData() { try { tableMachines.setItems( FXCollections.observableArrayList( machineDAO.getAll() ) ); } catch (Exception e) { showError( e.getMessage() ); } } @FXML private void addMachine() { Dialog<Machine> dialog = createDialog(null); dialog.showAndWait() .ifPresent(machine -> { try { machineDAO.add(machine); loadData(); } catch (Exception e) { showError( e.getMessage() ); } }); } @FXML private void editMachine() { Machine selected = tableMachines .getSelectionModel() .getSelectedItem(); if(selected == null) { showError( "Выберите станок" ); return; } Dialog<Machine> dialog = createDialog(selected); dialog.showAndWait() .ifPresent(machine -> { try { machineDAO.update(machine); loadData(); } catch (Exception e) { showError( e.getMessage() ); } }); } @FXML private void deleteMachine() { Machine selected = tableMachines .getSelectionModel() .getSelectedItem(); if(selected == null) { showError( "Выберите станок" ); return; } try { machineDAO.delete( selected.getId() ); loadData(); } catch (Exception e) { showError( e.getMessage() ); } } private Dialog<Machine> createDialog( Machine machine ) { Dialog<Machine> dialog = new Dialog<>(); TextField inventoryField = new TextField(); ComboBox<MachineType> typeBox = new ComboBox<>(); try { typeBox.setItems( FXCollections.observableArrayList( machineTypeDAO.getAll() ) ); } catch (Exception e) { e.printStackTrace(); } if(machine != null) { inventoryField.setText( machine.getInventoryNumber() ); for(MachineType type : typeBox.getItems()) { if(type.getId() == machine.getMachineTypeId()) { typeBox.setValue(type); break; } } } dialog.getDialogPane() .setContent( new VBox( 10, new Label( "Инвентарный номер" ), inventoryField, new Label( "Тип станка" ), typeBox ) ); ButtonType saveButton = new ButtonType( "Сохранить", ButtonBar.ButtonData.OK_DONE ); dialog.getDialogPane() .getButtonTypes() .addAll( saveButton, ButtonType.CANCEL ); dialog.setResultConverter(button -> { if(button == saveButton) { Machine result = new Machine(); if(machine != null) { result.setId( machine.getId() ); } result.setInventoryNumber( inventoryField.getText() ); if(typeBox.getValue() != null) { result.setMachineTypeId( typeBox.getValue() .getId() ); } return result; } return null; }); return dialog; } @FXML private void backToMenu() { try { FXMLLoader loader = new FXMLLoader( getClass().getResource( "/main_menu.fxml" ) ); Stage stage = (Stage) tableMachines .getScene() .getWindow(); stage.setScene( new Scene( loader.load(), 1000, 700 ) ); } catch (Exception e) { e.printStackTrace(); } } private void showError( String text ) { Alert alert = new Alert( Alert.AlertType.ERROR ); alert.setContentText(text); alert.showAndWait(); } }