488 lines
18 KiB
Java
488 lines
18 KiB
Java
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<Produit> TableViewListeGestionProduit ;
|
|
|
|
@FXML private TableColumn<Produit ,String> TabColReference;
|
|
@FXML private TableColumn<Produit ,String> TabColDesignation;
|
|
@FXML private TableColumn<Produit ,String> TabColMarque ;
|
|
@FXML private TableColumn<Produit ,String> TabColCategorie;
|
|
@FXML private TableColumn<Produit ,String> TabColPrixTTC;
|
|
@FXML private TableColumn<Produit ,String> TabColQuantite;
|
|
@FXML private TableColumn<Produit, Boolean> TabColAction ;
|
|
|
|
private Service<Void> 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<Produit, String>("reference"));
|
|
TabColDesignation.setCellValueFactory(new PropertyValueFactory<Produit, String>("designation"));
|
|
TabColMarque.setCellValueFactory(new PropertyValueFactory<Produit, String>("marque"));
|
|
TabColCategorie.setCellValueFactory(new PropertyValueFactory<Produit, String>("categorie"));
|
|
TabColPrixTTC.setCellValueFactory(new PropertyValueFactory<Produit, String>("prixventettc"));
|
|
TabColQuantite.setCellValueFactory(new PropertyValueFactory<Produit, String>("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<TableColumn.CellDataFeatures<Produit, Boolean>,ObservableValue<Boolean>>(){
|
|
@Override
|
|
public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<Produit, Boolean> 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<TableColumn<Produit, Boolean>, TableCell<Produit, Boolean>>() {
|
|
@Override
|
|
public TableCell<Produit, Boolean> call(TableColumn<Produit, Boolean> personBooleanTableColumn) {
|
|
return new ButtonCell();
|
|
}
|
|
});
|
|
|
|
|
|
|
|
//double clique sur une ligne
|
|
TableViewListeGestionProduit.setOnMouseClicked(new EventHandler<javafx.scene.input.MouseEvent>(){
|
|
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<MouseEvent>() {
|
|
public void handle(MouseEvent me) {
|
|
SearchProduit();
|
|
}
|
|
});
|
|
|
|
AnchorPaneProduit.setOnKeyPressed(new EventHandler<KeyEvent>() {
|
|
@Override
|
|
public void handle(KeyEvent ke) {
|
|
if (ke.getCode().equals(KeyCode.ENTER)) {
|
|
SearchProduit();
|
|
}
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
|
|
private void getCategorie()
|
|
{
|
|
ProgressBarProduit.setVisible(true);
|
|
|
|
ThreadSearchProduit = new Service<Void>(){
|
|
@Override
|
|
protected Task<Void> createTask(){
|
|
return new Task<Void>(){
|
|
@Override
|
|
protected Void call() throws Exception{
|
|
|
|
TextFields.bindAutoCompletion(GestionProduitCategorie, new CategorieDB().getListCategorie());
|
|
|
|
return null;
|
|
}
|
|
};
|
|
}
|
|
};
|
|
|
|
ThreadSearchProduit.setOnSucceeded(new EventHandler<WorkerStateEvent>(){
|
|
@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<ActionEvent>() {
|
|
@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<Produit, Boolean> {
|
|
|
|
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<ActionEvent>(){
|
|
@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<KeyEvent>() {
|
|
@Override
|
|
public void handle(KeyEvent ke) {
|
|
if(totalcount<10){
|
|
ListerPages.setVisible(false);
|
|
}
|
|
else{
|
|
ListerPages.setVisible(true);
|
|
}
|
|
SearchProduit();
|
|
}
|
|
});
|
|
GestionProduitCategorie.textProperty().addListener(new ChangeListener<String>() {
|
|
@Override
|
|
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
|
|
SearchProduit();
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************************** *
|
|
* *
|
|
* Methode last Next TableView client Entreprise *
|
|
* * *
|
|
******************************************************************************/
|
|
|
|
private void SearchProduit()
|
|
{
|
|
ProgressBarProduit.setVisible(true);
|
|
|
|
ButtonSearchProduit.setDisable(true);
|
|
|
|
ThreadSearchProduit = new Service<Void>(){
|
|
@Override
|
|
protected Task<Void> createTask(){
|
|
return new Task<Void>(){
|
|
@Override
|
|
protected Void call() throws Exception{
|
|
|
|
position = 0;
|
|
|
|
actuellepage = 1;
|
|
|
|
ObservableList<Produit> 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<WorkerStateEvent>(){
|
|
@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<Void>(){
|
|
@Override
|
|
protected Task<Void> createTask(){
|
|
return new Task<Void>(){
|
|
@Override
|
|
protected Void call() throws Exception{//
|
|
|
|
ObservableList<Produit> ListProduits = new ProduitDB().SearchProduit(ParmPosition, ParamNbrligne, true, GestionProduitReference.getText(), GestionProduitCategorie.getText(), GestionProduitCodeBarre.getText(), GestionProduitDesignation.getText());
|
|
|
|
TableViewListeGestionProduit.setItems(ListProduits);
|
|
|
|
return null;
|
|
}
|
|
};
|
|
}
|
|
};
|
|
|
|
ThreadSearchProduit.setOnSucceeded(new EventHandler<WorkerStateEvent>(){
|
|
@Override
|
|
public void handle(WorkerStateEvent event){
|
|
ProgressBarProduit.setVisible(false);
|
|
ButtonSearchProduit.setDisable(false);
|
|
}
|
|
});
|
|
ThreadSearchProduit.start();
|
|
}
|
|
|
|
private void GestionSearchProduit()
|
|
{
|
|
|
|
NbrLigne.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
|
|
|
|
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<MouseEvent>(){
|
|
@Override
|
|
public void handle(MouseEvent event) {
|
|
if(actuellepage<nbrpage)
|
|
{
|
|
position=position+nbrligne;
|
|
NextLastSearchProduit(position, nbrligne);
|
|
actuellepage=actuellepage+1 ;
|
|
ActuellePage.setText(Integer.toString(actuellepage));
|
|
|
|
}
|
|
}
|
|
});
|
|
|
|
//last page
|
|
lastgroupe.setOnMousePressed(new EventHandler<MouseEvent>(){
|
|
@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<MouseEvent>(){
|
|
@Override
|
|
public void handle(MouseEvent event) {
|
|
if(actuellepage+3<nbrpage)
|
|
{
|
|
position=(position+4)+nbrligne;
|
|
NextLastSearchProduit(position, nbrligne);
|
|
actuellepage=actuellepage+3 ;
|
|
ActuellePage.setText(Integer.toString(actuellepage));
|
|
|
|
}
|
|
}
|
|
});
|
|
|
|
//last page avec un pas de -5
|
|
retourgroupe.setOnMousePressed(new EventHandler<MouseEvent>(){
|
|
@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));
|
|
}
|
|
}
|
|
});
|
|
|
|
}
|
|
}
|
|
|