Skip to content
Snippets Groups Projects
Commit b7e18200 authored by Hallvard Trætteberg's avatar Hallvard Trætteberg
Browse files

(De)aktivering av knapper og penere liste-elementer (#4)

parent 648100a8
No related branches found
No related tags found
No related merge requests found
package todolist.ui;
import java.util.Collection;
import java.util.List;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import todolist.core.TodoItem;
......@@ -48,11 +50,23 @@ public class TodoController {
@FXML
ListView<TodoItem> todoListView;
@FXML
Button deleteTodoItemButton;
@FXML
Button checkTodoItemButton;
private Collection<Button> selectionButtons;
@FXML
public void initialize() {
selectionButtons = List.of(deleteTodoItemButton, checkTodoItemButton);
// kobler data til list-controll
updateTodoListView();
updateTodoListButtons();
todoList.addTodoListListener(todoList -> updateTodoListView());
todoListView.setCellFactory(listView -> new TodoItemListCell());
todoListView.getSelectionModel().selectedItemProperty().addListener((prop, oldValue, newValue) -> updateTodoListButtons());
}
protected void updateTodoListView() {
......@@ -62,13 +76,22 @@ public class TodoController {
viewList.addAll(todoList.getCheckedTodoItems());
}
private void updateTodoListButtons() {
boolean disable = todoListView.getSelectionModel().getSelectedItem() == null;
for (Button button : selectionButtons) {
button.setDisable(disable);
}
}
@FXML
public void handleNewTodoItemAction() {
TodoItem item = new TodoItem();
TodoItem item = todoList.createTodoItem();
item.setText(newTodoItemText.getText());
todoList.addTodoItem(item);
}
@FXML
public void handleDeleteItemAction() {
TodoItem item = todoListView.getSelectionModel().getSelectedItem();
......@@ -80,6 +103,8 @@ public class TodoController {
@FXML
public void handleCheckItemAction() {
TodoItem item = todoListView.getSelectionModel().getSelectedItem();
if (item != null) {
item.setChecked(true);
}
}
}
package todolist.ui;
import javafx.scene.control.ListCell;
import todolist.core.TodoItem;
public class TodoItemListCell extends ListCell<TodoItem> {
@Override
protected void updateItem(TodoItem item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText(null);
setGraphic(null);
} else {
setText((item.isChecked() ? "v" : "-") + " " + item.getText());
setGraphic(null);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment