From ee53cdf967ea5399d408ab80ec0ce25ab6daa483 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Ko=C5=BEusznik?= <jan@kozusznik.cz>
Date: Mon, 26 Apr 2021 13:35:44 +0200
Subject: [PATCH] Delete action in UI

---
 .../java2/lab12/client/TableViewSample.java   | 53 +++++++++++++++----
 1 file changed, 43 insertions(+), 10 deletions(-)

diff --git a/src/main/java/java2/lab12/client/TableViewSample.java b/src/main/java/java2/lab12/client/TableViewSample.java
index 76a23c0..cbb1718 100644
--- a/src/main/java/java2/lab12/client/TableViewSample.java
+++ b/src/main/java/java2/lab12/client/TableViewSample.java
@@ -4,6 +4,7 @@ import java.util.Collection;
 import java.util.Collections;
 
 import javafx.application.Application;
+import javafx.beans.property.ReadOnlyObjectWrapper;
 import javafx.collections.FXCollections;
 import javafx.collections.ObservableList;
 import javafx.event.ActionEvent;
@@ -13,6 +14,7 @@ import javafx.scene.Group;
 import javafx.scene.Scene;
 import javafx.scene.control.Button;
 import javafx.scene.control.Label;
+import javafx.scene.control.TableCell;
 import javafx.scene.control.TableColumn;
 import javafx.scene.control.TableColumn.CellEditEvent;
 import javafx.scene.control.TableView;
@@ -50,8 +52,8 @@ public class TableViewSample extends Application {
 	@Override
     public void start(Stage stage) {
         Scene scene = new Scene(new Group());
-        stage.setTitle("Table View Sample");
-        stage.setWidth(450);
+				stage.setTitle("RESTfull Webservice - Course Book");
+				stage.setWidth(500);
         stage.setHeight(550);
  
 				final Label label = new Label("Course Book");
@@ -68,8 +70,30 @@ public class TableViewSample extends Application {
 					"credits", this::handleOnEditCommitCredits);
 				TableColumn<Course, String> semesterCol = constructColumn("Semester",
 					"semester", this::handleOnEditCommitSemester);
+
+				TableColumn<Course, Course> actionCol = new TableColumn<>("");
+				actionCol.setCellValueFactory(param -> new ReadOnlyObjectWrapper<>(
+					param.getValue()));
+				actionCol.setCellFactory(param -> new TableCell<>() {
+
+					private final Button deleteButton = new Button("Delete");
+
+					@Override
+					protected void updateItem(Course course, boolean empty) {
+						super.updateItem(course, empty);
+
+						if (course == null) {
+							setGraphic(null);
+							return;
+						}
+
+						setGraphic(deleteButton);
+						deleteButton.setOnAction(event -> handleOnDelete(event, course));
+					}
+				});
+
 				table.getColumns().addAll(nameCol, desriptionCol, creditsCol,
-					semesterCol);
+					semesterCol, actionCol);
  
         
 				addName = constructTextField(nameCol, "Name");
@@ -115,7 +139,7 @@ public class TableViewSample extends Application {
 		return col;
 	}
 
-	private void handleOnAdd(ActionEvent e) {
+	private void handleOnAdd(@SuppressWarnings("unused") ActionEvent e) {
 		Course p = new Course();
 		p.setName(addName.getText());
 		p.setDescription(addDescription.getText());
@@ -129,6 +153,13 @@ public class TableViewSample extends Application {
 		addSemester.clear();
 	}
     
+	private void handleOnDelete(@SuppressWarnings("unused") ActionEvent event,
+		Course course)
+	{
+		data.remove(course);
+		removeCourse(course);
+	}
+
 	private void handleOnEditCommitName(CellEditEvent<Course, String> t) {
 		Course p = t.getTableView().getItems().get(t.getTablePosition().getRow());
 		p.setName(t.getNewValue());
@@ -162,13 +193,15 @@ public class TableViewSample extends Application {
 		return Collections.emptyList();
 	}
 
-	private void createCourse(Course p) {
-		// TODO Auto-generated method stub
-	
+	private void createCourse(Course course) {
+		// TODO
 	}
 
-	private void updateCourse(Course p) {
-		// TODO Auto-generated method stub
-	
+	private void updateCourse(Course course) {
+		// TODO
+	}
+
+	private void removeCourse(Course course) {
+		// TODO
 	}
 }
\ No newline at end of file
-- 
GitLab