package Controllers.Produit; import Models.Categorie.CategorieDB; import Models.Produit.Produit; import Models.Produit.ProduitDB; import com.gluonhq.charm.glisten.control.ProgressBar; import java.io.IOException; import java.net.URL; import java.util.ResourceBundle; import java.util.logging.Level; import java.util.logging.Logger; import javafx.beans.property.SimpleBooleanProperty; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.concurrent.Service; import javafx.concurrent.Task; import javafx.concurrent.WorkerStateEvent; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.fxml.Initializable; import javafx.scene.Group; import javafx.scene.Node; import javafx.scene.Parent; import javafx.scene.control.Button; import javafx.scene.control.ChoiceBox; import javafx.scene.control.Label; import javafx.scene.control.TableCell; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.TextField; import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; import javafx.scene.input.MouseEvent; import javafx.scene.layout.AnchorPane; import javafx.scene.text.Text; import javafx.util.Callback; import org.controlsfx.control.textfield.TextFields; /** * FXML Controller class * @author Maher Ben Tili */ public class GestionProduitController implements Initializable { @FXML private Button addProduit ; @FXML private Button ButtonSearchProduit ; @FXML private Label LabelCount ; @FXML private ChoiceBox NbrLigne ; @FXML private AnchorPane AnchorPaneGestionClt; @FXML private AnchorPane ListerPages; @FXML private ProgressBar ProgressBarProduit; @FXML private Text ActuellePage ; @FXML private Text NbrPage ; @FXML private Group nextgroupe ; @FXML private Group avancergroupe ; @FXML private Group lastgroupe ; @FXML private Group retourgroupe ; @FXML private AnchorPane AnchorPaneProduit; @FXML private TextField GestionProduitReference ; @FXML private TextField GestionProduitCodeBarre; @FXML private TextField GestionProduitCategorie; @FXML private TextField GestionProduitDesignation; @FXML private TableView TableViewListeGestionProduit ; @FXML private TableColumn TabColReference; @FXML private TableColumn TabColDesignation; @FXML private TableColumn TabColMarque ; @FXML private TableColumn TabColCategorie; @FXML private TableColumn TabColPrixTTC; @FXML private TableColumn TabColQuantite; @FXML private TableColumn TabColAction ; private Service ThreadSearchProduit; ObservableList cursors = FXCollections.observableArrayList("10","15","20"); private Integer nbrligne = 10 ; private Integer totalcount = 0 ; private Integer nbrpage = 0; private Integer actuellepage = 1; Integer position = 0; public static String refprod ; ProduitDB ProdDB =new ProduitDB(); @Override public void initialize(URL url, ResourceBundle rb) { getCategorie(); //comboboxnombre des lignes tableview NbrLigne.setItems(cursors); NbrLigne.getSelectionModel().selectFirst(); this.AjouterProduit(); TabColReference.setCellValueFactory(new PropertyValueFactory("reference")); TabColDesignation.setCellValueFactory(new PropertyValueFactory("designation")); TabColMarque.setCellValueFactory(new PropertyValueFactory("marque")); TabColCategorie.setCellValueFactory(new PropertyValueFactory("categorie")); TabColPrixTTC.setCellValueFactory(new PropertyValueFactory("prixventettc")); TabColQuantite.setCellValueFactory(new PropertyValueFactory("quantite")); TabColQuantite.setStyle( "-fx-alignment: CENTER; -fx-font-weight:bold; -fx-font-size: 10pt;"); TabColQuantite.getStyleClass().add("Center"); TabColAction.setSortable(true); TabColAction.setStyle( "-fx-alignment: CENTER; -fx-font-weight:bold; -fx-font-size: 10pt;"); TabColAction.getStyleClass().add("Center"); TabColAction.setCellValueFactory(new Callback,ObservableValue>(){ @Override public ObservableValue call(TableColumn.CellDataFeatures p) { return new SimpleBooleanProperty(p.getValue() != null); } }); // create a cell value factory with an add button for each row in the table. TabColAction.setCellFactory(new Callback, TableCell>() { @Override public TableCell call(TableColumn personBooleanTableColumn) { return new ButtonCell(); } }); //double clique sur une ligne TableViewListeGestionProduit.setOnMouseClicked(new EventHandler(){ public void handle(MouseEvent event){ if(event.getClickCount()>1){ if(TableViewListeGestionProduit.getSelectionModel().getSelectedIndex()>=0){ //System.out.println(getCode()); GestionProduitController.refprod=TableViewListeGestionProduit.getSelectionModel().getSelectedItem().getReference(); AnchorPaneProduit.getChildren().clear(); try { FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/Views/Produit/ModifierProduit.fxml")); Parent root = (Parent)fxmlLoader.load(); AnchorPaneProduit.getChildren().add(root); } catch (IOException ex) { Logger.getLogger(GestionProduitController.class.getName()).log(Level.SEVERE, null, ex); } } } } }); GestionSearchProduit(); ButtonSearchProduit.setOnMouseClicked(new EventHandler() { public void handle(MouseEvent me) { SearchProduit(); } }); AnchorPaneProduit.setOnKeyPressed(new EventHandler() { @Override public void handle(KeyEvent ke) { if (ke.getCode().equals(KeyCode.ENTER)) { SearchProduit(); } } }); } private void getCategorie() { ProgressBarProduit.setVisible(true); ThreadSearchProduit = new Service(){ @Override protected Task createTask(){ return new Task(){ @Override protected Void call() throws Exception{ TextFields.bindAutoCompletion(GestionProduitCategorie, new CategorieDB().getListCategorie()); return null; } }; } }; ThreadSearchProduit.setOnSucceeded(new EventHandler(){ @Override public void handle(WorkerStateEvent event){ ProgressBarProduit.setVisible(false); } }); ThreadSearchProduit.start(); } private void AjouterProduit() { addProduit.getStyleClass().add("btn-success"); Image coffeeImage = new Image(getClass().getResourceAsStream("/Public/icon/iconadd.png")); ImageView buttonGraphic = new ImageView(); buttonGraphic.setImage(coffeeImage); addProduit.setGraphic(buttonGraphic); addProduit.setOnAction(new EventHandler() { @Override public void handle(ActionEvent e) { try { AnchorPaneProduit.getChildren().clear(); AnchorPaneProduit.getChildren().add((Node) FXMLLoader.load(getClass().getResource("/Views/Produit/AjouterProduit.fxml"))); } catch (IOException ex) { Logger.getLogger(GestionProduitController.class.getName()).log(Level.SEVERE, null, ex); } } }); } //Define the button cell private class ButtonCell extends TableCell { final Button cellButton = new Button(); ButtonCell(){ cellButton.getStyleClass().add("btn-icon-primary"); Image coffeeImage = new Image(getClass().getResourceAsStream("/Public/icon/iconedit.png")); ImageView buttonGraphic = new ImageView(); buttonGraphic.setImage(coffeeImage); cellButton.setGraphic(buttonGraphic); //Action when the button is pressed cellButton.setOnAction(new EventHandler(){ @Override public void handle(ActionEvent t) { // get Selected Item Produit currentPerson = (Produit) ButtonCell.this.getTableView().getItems().get(ButtonCell.this.getIndex()); GestionProduitController.refprod=currentPerson.getReference(); AnchorPaneProduit.getChildren().clear(); try { FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/Views/Produit/ModifierProduit.fxml")); Parent root = (Parent)fxmlLoader.load(); AnchorPaneProduit.getChildren().add(root); } catch (IOException ex) { Logger.getLogger(GestionProduitController.class.getName()).log(Level.SEVERE, null, ex); } } }); } //Display button if the row is not empty @Override protected void updateItem(Boolean t, boolean empty) { if(!empty){ setGraphic(cellButton); } else { setGraphic(null); } } } private void getCategorieProduitOnclick() { GestionProduitCategorie.setOnKeyReleased(new EventHandler() { @Override public void handle(KeyEvent ke) { if(totalcount<10){ ListerPages.setVisible(false); } else{ ListerPages.setVisible(true); } SearchProduit(); } }); GestionProduitCategorie.textProperty().addListener(new ChangeListener() { @Override public void changed(ObservableValue observable, String oldValue, String newValue) { SearchProduit(); } }); } /****************************************************************************** * * * * Methode last Next TableView client Entreprise * * * * ******************************************************************************/ private void SearchProduit() { ProgressBarProduit.setVisible(true); ButtonSearchProduit.setDisable(true); ThreadSearchProduit = new Service(){ @Override protected Task createTask(){ return new Task(){ @Override protected Void call() throws Exception{ position = 0; actuellepage = 1; ObservableList ListProduits = new ProduitDB().SearchProduit(position, nbrligne, true, GestionProduitReference.getText(), GestionProduitCategorie.getText(), GestionProduitCodeBarre.getText(), GestionProduitDesignation.getText()); TableViewListeGestionProduit.setItems(ListProduits); totalcount = new ProduitDB().nbrSearchProduit(true, GestionProduitReference.getText(), GestionProduitCategorie.getText(), GestionProduitCodeBarre.getText(), GestionProduitDesignation.getText()); if(totalcount > 0){ ActuellePage.setText("1"); }else{ ActuellePage.setText("0"); } if(totalcount % nbrligne==0){ nbrpage = totalcount / nbrligne ; } else{ nbrpage = (totalcount / nbrligne) + 1 ; } NbrPage.setText(Integer.toString(nbrpage)); return null; } }; } }; ThreadSearchProduit.setOnSucceeded(new EventHandler(){ @Override public void handle(WorkerStateEvent event){ ProgressBarProduit.setVisible(false); ButtonSearchProduit.setDisable(false); LabelCount.setText(totalcount.toString()); } }); ThreadSearchProduit.start(); } private void NextLastSearchProduit(Integer ParmPosition, Integer ParamNbrligne) { ProgressBarProduit.setVisible(true); ButtonSearchProduit.setDisable(true); ThreadSearchProduit = new Service(){ @Override protected Task createTask(){ return new Task(){ @Override protected Void call() throws Exception{// ObservableList ListProduits = new ProduitDB().SearchProduit(ParmPosition, ParamNbrligne, true, GestionProduitReference.getText(), GestionProduitCategorie.getText(), GestionProduitCodeBarre.getText(), GestionProduitDesignation.getText()); TableViewListeGestionProduit.setItems(ListProduits); return null; } }; } }; ThreadSearchProduit.setOnSucceeded(new EventHandler(){ @Override public void handle(WorkerStateEvent event){ ProgressBarProduit.setVisible(false); ButtonSearchProduit.setDisable(false); } }); ThreadSearchProduit.start(); } private void GestionSearchProduit() { NbrLigne.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener() { public void changed(ObservableValue ov, Number value, Number new_value) { nbrligne= Integer.parseInt((String) cursors.get(new_value.intValue())); if(totalcount % nbrligne==0){ nbrpage = totalcount / nbrligne ;} else{ nbrpage = (totalcount / nbrligne) + 1 ; } NbrPage.setText(Integer.toString(nbrpage)); position=0; ActuellePage.setText("1"); actuellepage=1; NextLastSearchProduit(position, nbrligne); } }); if(totalcount % nbrligne==0){ nbrpage = totalcount / nbrligne ;} else{ nbrpage = (totalcount / nbrligne) + 1 ; } NbrPage.setText(Integer.toString(nbrpage)); //next page nextgroupe.setOnMousePressed(new EventHandler(){ @Override public void handle(MouseEvent event) { if(actuellepage(){ @Override public void handle(MouseEvent event) { if(actuellepage>1) { position=position-nbrligne; NextLastSearchProduit(position, nbrligne); actuellepage=actuellepage-1 ; ActuellePage.setText(Integer.toString(actuellepage)); } } }); //next page avec pas de 5 avancergroupe.setOnMousePressed(new EventHandler(){ @Override public void handle(MouseEvent event) { if(actuellepage+3(){ @Override public void handle(MouseEvent event) { if(actuellepage-3>1){ position=(position-4)-nbrligne; NextLastSearchProduit(position, nbrligne); actuellepage=actuellepage-3 ; ActuellePage.setText(Integer.toString(actuellepage)); } } }); } }