Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.39 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package com.lifecode.smallpos.feature.product.category;
  7.  
  8. import com.lifecode.smallpos.library.AlertHelper;
  9. import com.lifecode.smallpos.orm.entity.Category;
  10. import de.jensd.fx.glyphs.fontawesome.FontAwesomeIcon;
  11. import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView;
  12. import java.util.ArrayList;
  13. import java.util.Collection;
  14. import java.util.List;
  15. import javafx.collections.FXCollections;
  16. import javafx.collections.ObservableList;
  17. import javafx.event.ActionEvent;
  18. import javafx.event.EventHandler;
  19. import javafx.scene.control.Alert;
  20. import javafx.scene.control.Button;
  21. import javafx.scene.control.Label;
  22. import javafx.scene.control.TableCell;
  23. import javafx.scene.control.TableColumn;
  24. import javafx.scene.control.cell.PropertyValueFactory;
  25. import javafx.util.Callback;
  26.  
  27. /**
  28. *
  29. * @author eby
  30. */
  31. public final class ProductCategoryTableModel {
  32.  
  33. private final List<Category> list;
  34. private final ObservableList<Category> row;
  35. private final Collection<TableColumn<Category, String>> column;
  36. private ProductCategoryController controller;
  37.  
  38. public ProductCategoryController getController() {
  39. return controller;
  40. }
  41.  
  42. public void setController(ProductCategoryController controller) {
  43. this.controller = controller;
  44. }
  45.  
  46. public ProductCategoryTableModel() {
  47. list = new ArrayList<>();
  48. row = FXCollections.observableArrayList(list);
  49. column = new ArrayList<>();
  50. this.initColumn();
  51. controller = new ProductCategoryController();
  52. }
  53.  
  54. public void initColumn() {
  55. column.add(this.addTableColumn1("ID", "id"));
  56. column.add(this.addTableColumn2("NAME", "name"));
  57. column.add(this.getParent());
  58. column.add(this.addTableColumn3("DESCRIPTIONS", "descriptions"));
  59. column.add(this.Edit());
  60. column.add(this.Delete());
  61. }
  62.  
  63. public void removeAllColumn() {
  64. while (column.iterator().hasNext()) {
  65. column.remove(column.iterator().next());
  66. }
  67. }
  68.  
  69. private TableColumn addTableColumn1(String tableHeader, String entityAttribute) {
  70. TableColumn t = new TableColumn(tableHeader);
  71. t.setPrefWidth(40);
  72. t.setCellValueFactory(new PropertyValueFactory<>(entityAttribute));
  73. return t;
  74. }
  75.  
  76. private TableColumn addTableColumn2(String tableHeader, String entityAttribute) {
  77. TableColumn t = new TableColumn(tableHeader);
  78. t.setPrefWidth(120);
  79. t.setCellValueFactory(new PropertyValueFactory<>(entityAttribute));
  80. return t;
  81. }
  82.  
  83. private TableColumn addTableColumn3(String tableHeader, String entityAttribute) {
  84. TableColumn t = new TableColumn(tableHeader);
  85. t.setPrefWidth(140);
  86. t.setCellValueFactory(new PropertyValueFactory<>(entityAttribute));
  87. return t;
  88. }
  89.  
  90. private TableColumn addTableColumn4(String tableHeader, String entityAttribute) {
  91. TableColumn t = new TableColumn(tableHeader);
  92. t.setCellValueFactory(new PropertyValueFactory<>(entityAttribute));
  93. return t;
  94. }
  95.  
  96. private TableColumn addTableColumn5(String tableHeader) {
  97. TableColumn t = new TableColumn(tableHeader);
  98. return t;
  99. }
  100.  
  101. private TableColumn getParent() {
  102. TableColumn t = this.addTableColumn4("PARENT", "category");
  103. t.setPrefWidth(120);
  104. t.setCellFactory(new Callback< TableColumn<Category, Category>, TableCell<Category, Category>>() {
  105. @Override
  106. public TableCell<Category, Category> call(TableColumn<Category, Category> param) {
  107. TableCell<Category, Category> parentCell = new TableCell<Category, Category>() {
  108.  
  109. @Override
  110. protected void updateItem(Category cat, boolean empty) {
  111. if (cat != null) {
  112. Label label = new Label(cat.getName());
  113. label.setStyle("-fx-text-fill: black;");
  114. setGraphic(label);
  115.  
  116. } else {
  117. setGraphic(null);
  118. }
  119. }
  120. };
  121. return parentCell;
  122. }
  123. ;
  124. });
  125.  
  126. return t;
  127. }
  128.  
  129. private TableColumn Delete() {
  130. TableColumn t = this.addTableColumn5("#");
  131. t.setPrefWidth(45);
  132. t.setCellFactory(new Callback< TableColumn<Category, String>, TableCell<Category, String>>() {
  133. @Override
  134. public TableCell<Category, String> call(TableColumn<Category, String> param) {
  135. return new ButtonCellsDelete();
  136. }
  137. ;
  138. });
  139.  
  140. return t;
  141. }
  142.  
  143. private TableColumn Edit() {
  144. TableColumn t = this.addTableColumn5("#");
  145. t.setPrefWidth(50);
  146. t.setCellFactory(new Callback< TableColumn<Category, String>, TableCell<Category, String>>() {
  147. @Override
  148. public TableCell<Category, String> call(TableColumn<Category, String> param) {
  149. return new ButtonCellsEdit();
  150. }
  151. ;
  152. });
  153.  
  154. return t;
  155. }
  156.  
  157. /**
  158. * Include the button into tableCell
  159. */
  160. private class ButtonCellsDelete extends TableCell<Category, String> {
  161.  
  162. Button b = new Button();
  163.  
  164. public ButtonCellsDelete() {
  165.  
  166. b.setStyle("-fx-background-color: red;");
  167. FontAwesomeIconView iconDelete = new FontAwesomeIconView(FontAwesomeIcon.TRASH);
  168. b.setCenterShape(true);
  169. iconDelete.setStyle("-fx-fill:white;");
  170. iconDelete.setSize("16");
  171. b.setGraphic(iconDelete);
  172. b.setPrefWidth(21);
  173. b.setPrefHeight(21);
  174. setGraphic(b);
  175.  
  176. b.setOnAction(new EventHandler<ActionEvent>() {
  177. @Override
  178. public void handle(ActionEvent event) {
  179. Category c = ButtonCellsDelete.this.getTableView().getItems().get(ButtonCellsDelete.this.getIndex());
  180. boolean status = AlertHelper.confirmationDialog(Alert.AlertType.WARNING,
  181. "Do you want to delete this data ?");
  182. if (status) {
  183. controller.delete(c);
  184. controller.loadComboBoxData();
  185. controller.loadData();
  186. AlertHelper.dialog(Alert.AlertType.INFORMATION, "Data has been deleted");
  187. } else {
  188.  
  189. }
  190.  
  191. }
  192.  
  193. });
  194. }
  195.  
  196. @Override
  197. public void updateItem(String a, boolean e) {
  198. super.updateItem(a, e);
  199. if (!e) {
  200. setGraphic(b);
  201. } else {
  202. setGraphic(null);
  203. }
  204. }
  205. }
  206.  
  207. private class ButtonCellsEdit extends TableCell<Category, String> {
  208.  
  209. Button b = new Button();
  210.  
  211. public ButtonCellsEdit() {
  212.  
  213. b.getStyleClass().add("edit-button");
  214. FontAwesomeIconView iconEdit = new FontAwesomeIconView(FontAwesomeIcon.EDIT);
  215. b.setCenterShape(true);
  216. iconEdit.setStyle("-fx-fill:white;");
  217. iconEdit.setSize("16");
  218. b.setGraphic(iconEdit);
  219. b.setPrefWidth(21);
  220. b.setPrefHeight(21);
  221. setGraphic(b);
  222.  
  223. b.setOnAction(new EventHandler<ActionEvent>() {
  224. @Override
  225. public void handle(ActionEvent event) {
  226. Category e = ButtonCellsEdit.this.getTableView().getItems().get(ButtonCellsEdit.this.getIndex());
  227. controller.setData(e);
  228. }
  229.  
  230. });
  231. }
  232.  
  233. @Override
  234. public void updateItem(String a, boolean e) {
  235. super.updateItem(a, e);
  236. if (!e) {
  237. setGraphic(b);
  238. } else {
  239. setGraphic(null);
  240. }
  241. }
  242. }
  243.  
  244. public Collection<TableColumn<Category, String>> getAllColumn() {
  245. return column;
  246. }
  247.  
  248. public ObservableList<Category> getItem() {
  249. return row;
  250. }
  251.  
  252. public void remove(int index) {
  253. row.remove(index);
  254. this.removeAllColumn();
  255. this.initColumn();
  256. }
  257.  
  258. public void removeAllElement() {
  259. int size = list.size();
  260. for (int i = 0; i < size; i++) {
  261. list.remove(i);
  262. }
  263.  
  264. this.removeAllColumn();
  265. }
  266.  
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement