Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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 controller;
- import java.io.IOException;
- import java.net.URL;
- import java.util.List;
- import java.util.Optional;
- import java.util.ResourceBundle;
- import javafx.collections.FXCollections;
- import javafx.collections.ObservableList;
- import javafx.event.ActionEvent;
- import javafx.fxml.FXML;
- import javafx.fxml.FXMLLoader;
- import javafx.fxml.Initializable;
- import javafx.scene.Parent;
- import javafx.scene.Scene;
- import javafx.scene.control.Alert;
- import javafx.scene.control.Button;
- import javafx.scene.control.ButtonType;
- import javafx.scene.control.TableColumn;
- import javafx.scene.control.TableView;
- import javafx.scene.control.cell.PropertyValueFactory;
- import javafx.stage.Stage;
- import javax.swing.JOptionPane;
- import model.Cliente;
- import model.Produto;
- import model.dao.ClienteDAO;
- import model.dao.ProdutoDAO;
- import util.JpaUtil;
- /**
- * FXML Controller class
- *
- * @author Lucas
- */
- public class CrudProdutoController implements Initializable {
- @FXML
- private TableColumn<Object, String> tableColumnDesc;
- @FXML
- private TableColumn<Object, Float> tableColumnPreco;
- @FXML
- private TableColumn<Object, Integer> tableColumnQntd;
- /**
- * Initializes the controller class.
- */
- @Override
- public void initialize(URL url, ResourceBundle rb) {
- carregarTableView("");
- }
- @FXML
- private TableView<Object> tableView;
- @FXML
- private Button btnEditar;
- @FXML
- private Button btnExcluir;
- @FXML
- private Button btnCadastrar;
- @FXML
- private Button btnCancelar;
- private ObservableList<Object> observableListProduto;
- private List<Produto> listProduto;
- private void carregarTableView(String valor) {
- try {
- tableColumnDesc.setCellValueFactory(new PropertyValueFactory<>("descricao"));
- tableColumnPreco.setCellValueFactory(new PropertyValueFactory<>("preco"));
- tableColumnQntd.setCellValueFactory(new PropertyValueFactory<>("quant"));
- ProdutoDAO dao = new ProdutoDAO();
- listProduto = dao.listar();
- observableListProduto = FXCollections.observableArrayList(listProduto);
- tableView.setItems(observableListProduto);
- } catch (Exception ex) {
- JOptionPane.showMessageDialog(null, ex.getMessage());
- }
- }
- @FXML
- private void cadastrar(){
- new JpaUtil().chamarTela("/view/cadProduto.fxml");
- carregarTableView("");
- }
- @FXML
- private void editar(ActionEvent event) {
- try {
- if (tableView.selectionModelProperty()
- .getValue().getSelectedItem() == null) {
- JOptionPane.showMessageDialog(null, "Nenhum item selecionado.");
- } else {
- FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/cadProduto.fxml"));
- Parent root = (Parent) loader.load();
- CadProdutoController cadProdutoController = loader.getController();
- Produto p = (Produto) tableView.selectionModelProperty()
- .getValue().getSelectedItem();
- cadProdutoController.setProduto(p);
- Stage stage = new Stage();
- stage.setScene(new Scene(root));
- stage.showAndWait();
- }
- carregarTableView("");
- } catch (IOException ex) {
- JOptionPane.showMessageDialog(null, ex.getMessage());
- }
- }
- @FXML
- private void fechar(){
- new JpaUtil().fecharTela(btnCancelar);
- }
- @FXML
- private void excluir(ActionEvent event) {
- try {
- if (tableView.selectionModelProperty()
- .getValue().getSelectedItem() == null) {
- JOptionPane.showMessageDialog(null, "Nenhum item selecionado.");
- } else {
- Produto p = (Produto) tableView.
- selectionModelProperty().get().getSelectedItem();
- Integer id = p.getId();
- ProdutoDAO dao = new ProdutoDAO();
- Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
- alert.setTitle("Aviso");
- alert.setContentText("Confirma exclusão do produto " + p.getDescricao() + "?");
- Optional<ButtonType> result = alert.showAndWait();
- if (result.get() == ButtonType.OK) {
- String mensagem = "";
- try {
- dao.excluir(id);
- mensagem = "Exclusão efetuada com sucesso.";
- } catch (Exception e) {
- mensagem = "Ocorreu um erro: " + e.getMessage();
- }
- Alert alertErro = new Alert(Alert.AlertType.INFORMATION);
- alertErro.setTitle("Aviso");
- alertErro.setContentText(mensagem);
- alertErro.showAndWait();
- }
- }
- carregarTableView("");
- } catch (Exception ex) {
- JOptionPane.showMessageDialog(null, ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement