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 com.lifecode.smallpos.feature.product.category;
- import com.lifecode.smallpos.library.AlertHelper;
- import com.lifecode.smallpos.orm.entity.Category;
- import de.jensd.fx.glyphs.fontawesome.FontAwesomeIcon;
- import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.List;
- import javafx.collections.FXCollections;
- import javafx.collections.ObservableList;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.scene.control.Alert;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.control.TableCell;
- import javafx.scene.control.TableColumn;
- import javafx.scene.control.cell.PropertyValueFactory;
- import javafx.util.Callback;
- /**
- *
- * @author eby
- */
- public final class ProductCategoryTableModel {
- private final List<Category> list;
- private final ObservableList<Category> row;
- private final Collection<TableColumn<Category, String>> column;
- private ProductCategoryController controller;
- public ProductCategoryController getController() {
- return controller;
- }
- public void setController(ProductCategoryController controller) {
- this.controller = controller;
- }
- public ProductCategoryTableModel() {
- list = new ArrayList<>();
- row = FXCollections.observableArrayList(list);
- column = new ArrayList<>();
- this.initColumn();
- controller = new ProductCategoryController();
- }
- public void initColumn() {
- column.add(this.addTableColumn1("ID", "id"));
- column.add(this.addTableColumn2("NAME", "name"));
- column.add(this.getParent());
- column.add(this.addTableColumn3("DESCRIPTIONS", "descriptions"));
- column.add(this.Edit());
- column.add(this.Delete());
- }
- public void removeAllColumn() {
- while (column.iterator().hasNext()) {
- column.remove(column.iterator().next());
- }
- }
- private TableColumn addTableColumn1(String tableHeader, String entityAttribute) {
- TableColumn t = new TableColumn(tableHeader);
- t.setPrefWidth(40);
- t.setCellValueFactory(new PropertyValueFactory<>(entityAttribute));
- return t;
- }
- private TableColumn addTableColumn2(String tableHeader, String entityAttribute) {
- TableColumn t = new TableColumn(tableHeader);
- t.setPrefWidth(120);
- t.setCellValueFactory(new PropertyValueFactory<>(entityAttribute));
- return t;
- }
- private TableColumn addTableColumn3(String tableHeader, String entityAttribute) {
- TableColumn t = new TableColumn(tableHeader);
- t.setPrefWidth(140);
- t.setCellValueFactory(new PropertyValueFactory<>(entityAttribute));
- return t;
- }
- private TableColumn addTableColumn4(String tableHeader, String entityAttribute) {
- TableColumn t = new TableColumn(tableHeader);
- t.setCellValueFactory(new PropertyValueFactory<>(entityAttribute));
- return t;
- }
- private TableColumn addTableColumn5(String tableHeader) {
- TableColumn t = new TableColumn(tableHeader);
- return t;
- }
- private TableColumn getParent() {
- TableColumn t = this.addTableColumn4("PARENT", "category");
- t.setPrefWidth(120);
- t.setCellFactory(new Callback< TableColumn<Category, Category>, TableCell<Category, Category>>() {
- @Override
- public TableCell<Category, Category> call(TableColumn<Category, Category> param) {
- TableCell<Category, Category> parentCell = new TableCell<Category, Category>() {
- @Override
- protected void updateItem(Category cat, boolean empty) {
- if (cat != null) {
- Label label = new Label(cat.getName());
- label.setStyle("-fx-text-fill: black;");
- setGraphic(label);
- } else {
- setGraphic(null);
- }
- }
- };
- return parentCell;
- }
- ;
- });
- return t;
- }
- private TableColumn Delete() {
- TableColumn t = this.addTableColumn5("#");
- t.setPrefWidth(45);
- t.setCellFactory(new Callback< TableColumn<Category, String>, TableCell<Category, String>>() {
- @Override
- public TableCell<Category, String> call(TableColumn<Category, String> param) {
- return new ButtonCellsDelete();
- }
- ;
- });
- return t;
- }
- private TableColumn Edit() {
- TableColumn t = this.addTableColumn5("#");
- t.setPrefWidth(50);
- t.setCellFactory(new Callback< TableColumn<Category, String>, TableCell<Category, String>>() {
- @Override
- public TableCell<Category, String> call(TableColumn<Category, String> param) {
- return new ButtonCellsEdit();
- }
- ;
- });
- return t;
- }
- /**
- * Include the button into tableCell
- */
- private class ButtonCellsDelete extends TableCell<Category, String> {
- Button b = new Button();
- public ButtonCellsDelete() {
- b.setStyle("-fx-background-color: red;");
- FontAwesomeIconView iconDelete = new FontAwesomeIconView(FontAwesomeIcon.TRASH);
- b.setCenterShape(true);
- iconDelete.setStyle("-fx-fill:white;");
- iconDelete.setSize("16");
- b.setGraphic(iconDelete);
- b.setPrefWidth(21);
- b.setPrefHeight(21);
- setGraphic(b);
- b.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent event) {
- Category c = ButtonCellsDelete.this.getTableView().getItems().get(ButtonCellsDelete.this.getIndex());
- boolean status = AlertHelper.confirmationDialog(Alert.AlertType.WARNING,
- "Do you want to delete this data ?");
- if (status) {
- controller.delete(c);
- controller.loadComboBoxData();
- controller.loadData();
- AlertHelper.dialog(Alert.AlertType.INFORMATION, "Data has been deleted");
- } else {
- }
- }
- });
- }
- @Override
- public void updateItem(String a, boolean e) {
- super.updateItem(a, e);
- if (!e) {
- setGraphic(b);
- } else {
- setGraphic(null);
- }
- }
- }
- private class ButtonCellsEdit extends TableCell<Category, String> {
- Button b = new Button();
- public ButtonCellsEdit() {
- b.getStyleClass().add("edit-button");
- FontAwesomeIconView iconEdit = new FontAwesomeIconView(FontAwesomeIcon.EDIT);
- b.setCenterShape(true);
- iconEdit.setStyle("-fx-fill:white;");
- iconEdit.setSize("16");
- b.setGraphic(iconEdit);
- b.setPrefWidth(21);
- b.setPrefHeight(21);
- setGraphic(b);
- b.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent event) {
- Category e = ButtonCellsEdit.this.getTableView().getItems().get(ButtonCellsEdit.this.getIndex());
- controller.setData(e);
- }
- });
- }
- @Override
- public void updateItem(String a, boolean e) {
- super.updateItem(a, e);
- if (!e) {
- setGraphic(b);
- } else {
- setGraphic(null);
- }
- }
- }
- public Collection<TableColumn<Category, String>> getAllColumn() {
- return column;
- }
- public ObservableList<Category> getItem() {
- return row;
- }
- public void remove(int index) {
- row.remove(index);
- this.removeAllColumn();
- this.initColumn();
- }
- public void removeAllElement() {
- int size = list.size();
- for (int i = 0; i < size; i++) {
- list.remove(i);
- }
- this.removeAllColumn();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement