Commit 4f703d4f authored by Hallvard Trætteberg's avatar Hallvard Trætteberg
Browse files

Dynamisk kontekst-meny for liste-elementer (#11)

parent 44ee0ad2
......@@ -8,13 +8,16 @@ import java.util.function.Predicate;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextField;
import javafx.util.Callback;
import todolist.core.TodoItem;
import todolist.core.TodoList;
import todolist.core.TodoListListener;
import todolist.ui.util.DynamicContextMenu;
/**
* Controller for a TodoList.
......@@ -42,6 +45,8 @@ public class TodoListController {
return todoList;
}
ContextMenu cm;
/**
* Sets the TodoList managed by this controller.
* The corresponding views will be updated.
......@@ -82,6 +87,7 @@ public class TodoListController {
todoItemsView.setCellFactory(listView -> {
TodoItemListCell listCell = new TodoItemListCell();
dragHandler.registerHandlers(listCell);
listCell.setContextMenu(new DynamicContextMenu(() -> createTodoListCellMenuItems(listCell)));
return listCell;
});
todoItemsView.getSelectionModel().selectedItemProperty()
......@@ -89,6 +95,19 @@ public class TodoListController {
todoItemsView.setEditable(true);
}
private Collection<MenuItem> createTodoListCellMenuItems(TodoItemListCell listCell) {
TodoItem todoItem = listCell.getItem();
MenuItem toggleMenuItem = new MenuItem(todoItem.isChecked() ? "Uncheck" : "Check");
toggleMenuItem.setOnAction(event -> {
todoItem.setChecked(! todoItem.isChecked());
});
MenuItem deleteMenuItem = new MenuItem("Delete");
deleteMenuItem.setOnAction(event -> {
deleteTodoItem(todoItem, listCell.getIndex());
});
return List.of(toggleMenuItem, deleteMenuItem);
}
private Function<TodoList, Collection<TodoItem>> todoItemsProvider = TodoList::getTodoItems;
public void setTodoItemsProvider(Function<TodoList, Collection<TodoItem>> todoItemsProvider) {
......@@ -163,11 +182,15 @@ public class TodoListController {
int index = todoItemsView.getSelectionModel().getSelectedIndex();
TodoItem item = todoItemsView.getItems().get(index);
if (item != null) {
getTodoList().removeTodoItem(item);
selectWithinBounds(index);
deleteTodoItem(item, index);
}
}
private void deleteTodoItem(TodoItem item, int index) {
getTodoList().removeTodoItem(item);
selectWithinBounds(index);
}
private int selectWithinBounds(int index) {
int maxIndex = todoItemsView.getItems().size() - 1;
if (index > maxIndex) {
......
package todolist.ui.util;
import java.util.Collection;
import java.util.Collections;
import java.util.function.Supplier;
import javafx.geometry.Side;
import javafx.scene.Node;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
/**
* ContextMenu that populates the list of menu items when it is about to be shown, rather than in
* advance.
*/
public class DynamicContextMenu extends ContextMenu {
private MenuItem dummyItem = new MenuItem("Populating ...");
public DynamicContextMenu() {
resetItems();
}
public DynamicContextMenu(Supplier<Collection<MenuItem>> menuItemsSupplier) {
setMenuItemsSupplier(menuItemsSupplier);
}
private void resetItems() {
getItems().setAll(dummyItem);
}
private Supplier<Collection<MenuItem>> menuItemsSupplier;
public void setMenuItemsSupplier(Supplier<Collection<MenuItem>> menuItemsSupplier) {
this.menuItemsSupplier = menuItemsSupplier;
}
protected Collection<MenuItem> getDynamicItems() {
return menuItemsSupplier != null ? menuItemsSupplier.get() : Collections.emptyList();
}
protected void populateItems() {
getItems().setAll(getDynamicItems());
}
@Override
public void show(Node anchor, Side side, double dx, double dy) {
populateItems();
super.show(anchor, side, dx, dy);
}
@Override
public void show(Node anchor, double screenX, double screenY) {
populateItems();
super.show(anchor, screenX, screenY);
}
@Override
public void hide() {
super.hide();
resetItems();
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment