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

Bedre håndtering av seleksjon

parent e7249d07
Branches
No related tags found
No related merge requests found
......@@ -129,7 +129,12 @@ public class TodoController {
List<TodoItem> items = new ArrayList<>();
items.addAll(todoList.getUncheckedTodoItems());
items.addAll(todoList.getCheckedTodoItems());
TodoItem selectedItem = todoListView.getSelectionModel().getSelectedItem();
todoListView.getItems().setAll(items);
// keep selection
if (selectedItem != null) {
todoListView.getSelectionModel().select(selectedItem);
}
}
private void updateTodoListButtons() {
......@@ -138,13 +143,14 @@ public class TodoController {
button.setDisable(disable);
}
// TODO in progress...
double rowLayoutY = getRowLayoutY(todoListView, listCell -> isSelected(todoListView, listCell), 0);
getRowLayoutY(todoListView, listCell -> isSelected(todoListView, listCell), 0);
// System.out.println(rowLayoutY);
}
private boolean isSelected(ListView<?> listView, ListCell<?> listCell) {
return isSelected(listView, listCell.getItem());
}
private boolean isSelected(ListView<?> listView, Object item) {
return todoListView.getSelectionModel().getSelectedItems().contains(item);
}
......@@ -173,21 +179,37 @@ public class TodoController {
TodoItem item = todoList.createTodoItem();
item.setText(newTodoItemText.getText());
todoList.addTodoItem(item);
todoListView.getSelectionModel().select(item);
}
@FXML
void handleDeleteItemAction() {
TodoItem item = todoListView.getSelectionModel().getSelectedItem();
int index = todoListView.getSelectionModel().getSelectedIndex();
TodoItem item = todoListView.getItems().get(index);
if (item != null) {
todoList.removeTodoItem(item);
selectWithinBounds(index);
}
}
private int selectWithinBounds(int index) {
int maxIndex = todoListView.getItems().size() - 1;
if (index > maxIndex) {
index = maxIndex;
}
if (index >= 0) {
todoListView.getSelectionModel().select(index);
return index;
}
return -1;
}
@FXML
void handleCheckItemAction() {
TodoItem item = todoListView.getSelectionModel().getSelectedItem();
if (item != null) {
item.setChecked(true);
// toggle checked flag
item.setChecked(! item.isChecked());
}
}
......
......@@ -44,22 +44,28 @@ public class TodoItemListCellDragHandler {
private void handleDragOver(DragEvent event) {
if (event.getGestureSource() instanceof TodoItemListCell
&& event.getSource() instanceof TodoItemListCell) {
TodoItemListCell listCell = (TodoItemListCell) event.getSource();
if (!listCell.isEmpty()) {
TodoItem sourceItem = ((TodoItemListCell) event.getGestureSource()).getItem();
TodoItemListCell targetCell = (TodoItemListCell) event.getSource();
if ((!targetCell.isEmpty()) && acceptDrop(sourceItem, targetCell.getItem())) {
event.acceptTransferModes(TransferMode.MOVE);
// TODO: give feedback that drop will be accepted
}
}
event.consume();
}
private boolean acceptDrop(TodoItem sourceItem, TodoItem targetItem) {
return todoList.indexOf(sourceItem) >= 0 && sourceItem.isChecked() == targetItem.isChecked();
}
private void handleDragEnd(DragEvent event) {
boolean success = false;
if (event.getGestureSource() instanceof TodoItemListCell
&& event.getGestureTarget() instanceof TodoItemListCell) {
TodoItem sourceItem = ((TodoItemListCell) event.getGestureSource()).getItem();
TodoItemListCell target = (TodoItemListCell) event.getGestureTarget();
if (todoList.indexOf(sourceItem) >= 0 && (!target.isEmpty())) {
int newIndex = todoList.indexOf(target.getItem());
TodoItemListCell targetCell = (TodoItemListCell) event.getGestureTarget();
if ((!targetCell.isEmpty()) && acceptDrop(sourceItem, targetCell.getItem())) {
int newIndex = todoList.indexOf(targetCell.getItem());
if (newIndex >= 0) {
todoList.moveTodoItem(sourceItem, newIndex);
success = true;
......
......@@ -65,6 +65,8 @@ public class TodoAppTest extends ApplicationTest {
checkTodoListItems(item1, item2, newItem);
// item is last of the unchecked items in list view
checkTodoListViewItems(item2, newItem, item1);
// check element is selected
checkSelectedTodoItem(1);
}
@Test
......@@ -79,10 +81,12 @@ public class TodoAppTest extends ApplicationTest {
checkTodoListItems(item1);
// item2 is removed, only item1 is left
checkTodoListViewItems(item1);
// check remaining item is selected
checkSelectedTodoItem(0);
}
@Test
public void testCheckTodoItem() {
public void testCheckTodoItemListCell() {
TodoItemListCell todoItemListCell = findTodoItemListCell(cell -> ! cell.getItem().isChecked());
clickOn(todoItemListCell.lookup(".check-box"));
TodoItem newItem2 = item2.withChecked(true);
......@@ -153,4 +157,9 @@ public class TodoAppTest extends ApplicationTest {
}
assertTrue(i == items.length);
}
private void checkSelectedTodoItem(int index) {
final ListView<TodoItem> todoListView = lookup("#todoListView").query();
assertEquals(index, todoListView.getSelectionModel().getSelectedIndex());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment