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

ComboBox for å velge TodoList, redigere navn og legge til ny

parent f67eaeec
No related branches found
No related tags found
No related merge requests found
......@@ -19,6 +19,11 @@ public class TodoList implements Iterable<TodoItem> {
addTodoItems(items);
}
@Override
public String toString() {
return String.format("[TodoList name=%s deadline=%s]", getName(), getDeadline());
}
public String getName() {
return name;
}
......
......@@ -18,11 +18,14 @@ import java.util.function.Predicate;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.util.StringConverter;
import todolist.core.TodoItem;
import todolist.core.TodoList;
import todolist.core.TodoListListener;
import todolist.core.TodoModel;
import todolist.json.TodoPersistence;
......@@ -33,11 +36,6 @@ public class TodoController {
private TodoModel todoModel;
// makes class more testable
TodoList getTodoList() {
return todoModel.iterator().next();
}
private TodoPersistence todoPersistence = new TodoPersistence();
@FXML
......@@ -46,16 +44,19 @@ public class TodoController {
@FXML
String sampleTodoListResource;
@FXML
ComboBox<TodoList> todoListsView;
@FXML
TextField newTodoItemText;
@FXML
ListView<TodoItem> todoListView;
ListView<TodoItem> todoItemsView;
@FXML
Button deleteTodoItemButton;
private void initializeTodoList() {
private void initializeTodoModel() {
// setter opp data
Reader reader = null;
// try to read file from home folder first
......@@ -108,45 +109,131 @@ public class TodoController {
@FXML
void initialize() {
initializeTodoList();
initializeTodoModel();
selectionButtons = List.of(deleteTodoItemButton);
// kobler data til list-controll
updateTodoListView();
updateTodoListButtons();
getTodoList().addTodoListListener(todoList -> {
autoSaveTodoList();
updateTodoListView();
initializeTodoListsView();
initializeTodoItemsView();
updateTodoListsView(null);
}
private void initializeTodoListsView() {
todoListsView.setCellFactory(listView -> {
ListCell<TodoList> listCell = new ListCell<TodoList>() {
@Override
protected void updateItem(TodoList item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText(null);
} else if (item.getName() == null) {
setText("<create new list>");
} else {
setText(item.getName());
}
}
};
return listCell;
});
TodoItemListCellDragHandler dragHandler = new TodoItemListCellDragHandler(getTodoList());
todoListView.setCellFactory(listView -> {
todoListsView.setConverter(new StringConverter<TodoList>() {
@Override
public String toString(TodoList todoList) {
return todoList.getName();
}
@Override
public TodoList fromString(String newName) {
TodoList todoList = new TodoList(); // getTodoList()
todoList.setName(newName);
return todoList;
}
});
todoListsView.setEditable(true);
todoListsView.valueProperty().addListener((prop, oldTodoList, newTodoList) -> {
// must identify the case where newTodoList represents an edited name
if (oldTodoList != null && newTodoList != null && (! todoListsView.getItems().contains(newTodoList))) {
// either new name of dummy item or existing item
if (oldTodoList.getName() == null) {
// add as new list
todoModel.addTodoList(newTodoList);
updateTodoListsView(newTodoList);
} else {
// update name
oldTodoList.setName(newTodoList.getName());
updateTodoListsView(oldTodoList);
}
}
});
todoListsView.getSelectionModel().selectedItemProperty().addListener((prop, oldTodoList, newTodoList) -> {
updateTodoItemsView();
});
}
protected void updateTodoListsView(TodoList newSelection) {
List<TodoList> items = new ArrayList<>();
// dummy element used for creating new ones, with null name
items.add(new TodoList());
todoModel.forEach(items::add);
todoListsView.getItems().setAll(items);
if (newSelection != null) {
todoListsView.setValue(newSelection);
} else {
todoListsView.getSelectionModel().select(todoListsView.getItems().size() > 1 ? 1 : 0);
}
System.out.println("TodoLists: " + todoListsView.getItems());
}
private TodoListListener todoListListener = todoList -> {
autoSaveTodoList();
updateTodoItemsView();
};
// makes class more testable
TodoList getSelectedTodoList() {
return todoListsView.getValue();
}
private void initializeTodoItemsView() {
TodoItemListCellDragHandler dragHandler = new TodoItemListCellDragHandler(getSelectedTodoList());
todoItemsView.setCellFactory(listView -> {
TodoItemListCell listCell = new TodoItemListCell();
dragHandler.registerHandlers(listCell);
return listCell;
});
todoListView.getSelectionModel().selectedItemProperty()
todoItemsView.getSelectionModel().selectedItemProperty()
.addListener((prop, oldValue, newValue) -> updateTodoListButtons());
todoListView.setEditable(true);
todoItemsView.setEditable(true);
}
protected void updateTodoListView() {
private TodoList currenTodoList = null;
protected void updateTodoItemsView() {
if (currenTodoList != null) {
currenTodoList.removeTodoListListener(todoListListener);
}
List<TodoItem> items = new ArrayList<>();
items.addAll(getTodoList().getUncheckedTodoItems());
items.addAll(getTodoList().getCheckedTodoItems());
TodoItem selectedItem = todoListView.getSelectionModel().getSelectedItem();
todoListView.getItems().setAll(items);
currenTodoList = getSelectedTodoList();
if (currenTodoList != null) {
items.addAll(getSelectedTodoList().getUncheckedTodoItems());
items.addAll(getSelectedTodoList().getCheckedTodoItems());
}
TodoItem selectedItem = todoItemsView.getSelectionModel().getSelectedItem();
todoItemsView.getItems().setAll(items);
if (currenTodoList != null) {
currenTodoList.addTodoListListener(todoListListener);
}
// keep selection
if (selectedItem != null) {
todoListView.getSelectionModel().select(selectedItem);
todoItemsView.getSelectionModel().select(selectedItem);
}
newTodoItemText.setText(null);
}
private void updateTodoListButtons() {
boolean disable = todoListView.getSelectionModel().getSelectedItem() == null;
boolean disable = todoItemsView.getSelectionModel().getSelectedItem() == null;
for (Button button : selectionButtons) {
button.setDisable(disable);
}
// TODO in progress...
getRowLayoutY(todoListView, listCell -> isSelected(todoListView, listCell), 0);
getRowLayoutY(todoItemsView, listCell -> isSelected(todoItemsView, listCell), 0);
// System.out.println(rowLayoutY);
}
......@@ -155,7 +242,7 @@ public class TodoController {
}
private boolean isSelected(ListView<?> listView, Object item) {
return todoListView.getSelectionModel().getSelectedItems().contains(item);
return todoItemsView.getSelectionModel().getSelectedItems().contains(item);
}
@SuppressWarnings("unchecked")
......@@ -166,7 +253,7 @@ public class TodoController {
if (test.test(listCell) && num-- == 0) {
double dy = 0;
Node node = listCell;
while (node != todoListView) {
while (node != todoItemsView) {
dy += node.getLayoutY();
node = node.getParent();
}
......@@ -179,29 +266,29 @@ public class TodoController {
@FXML
void handleNewTodoItemAction() {
TodoItem item = getTodoList().createTodoItem();
TodoItem item = getSelectedTodoList().createTodoItem();
item.setText(newTodoItemText.getText());
getTodoList().addTodoItem(item);
todoListView.getSelectionModel().select(item);
getSelectedTodoList().addTodoItem(item);
todoItemsView.getSelectionModel().select(item);
}
@FXML
void handleDeleteItemAction() {
int index = todoListView.getSelectionModel().getSelectedIndex();
TodoItem item = todoListView.getItems().get(index);
int index = todoItemsView.getSelectionModel().getSelectedIndex();
TodoItem item = todoItemsView.getItems().get(index);
if (item != null) {
getTodoList().removeTodoItem(item);
getSelectedTodoList().removeTodoItem(item);
selectWithinBounds(index);
}
}
private int selectWithinBounds(int index) {
int maxIndex = todoListView.getItems().size() - 1;
int maxIndex = todoItemsView.getItems().size() - 1;
if (index > maxIndex) {
index = maxIndex;
}
if (index >= 0) {
todoListView.getSelectionModel().select(index);
todoItemsView.getSelectionModel().select(index);
return index;
}
return -1;
......@@ -209,7 +296,7 @@ public class TodoController {
@FXML
void handleCheckItemAction() {
TodoItem item = todoListView.getSelectionModel().getSelectedItem();
TodoItem item = todoItemsView.getSelectionModel().getSelectedItem();
if (item != null) {
// toggle checked flag
item.setChecked(! item.isChecked());
......
......@@ -3,6 +3,7 @@
<?import java.lang.String?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Tooltip?>
<?import javafx.scene.control.TextField?>
......@@ -15,6 +16,9 @@
<String fx:id="userTodoListPath" fx:value="todolist.json"/>
<String fx:id="sampleTodoListResource" fx:value="sample-todolist.json"/>
</fx:define>
<HBox>
<ComboBox fx:id="todoListsView"/>
</HBox>
<HBox>
<Button fx:id="newTodoItemButton" onAction="#handleNewTodoItemAction">
<graphic>
......@@ -26,7 +30,7 @@
</Button>
<TextField fx:id="newTodoItemText" promptText="Skriv inn tekst her" onAction="#handleNewTodoItemAction"/>
</HBox>
<ListView fx:id="todoListView">
<ListView fx:id="todoItemsView">
</ListView>
<HBox>
<Button fx:id="deleteTodoItemButton" onAction="#handleDeleteItemAction">
......
......@@ -28,7 +28,7 @@ public class TodoAppTest extends ApplicationTest {
final FXMLLoader loader = new FXMLLoader(getClass().getResource("TodoTest.fxml"));
final Parent root = loader.load();
this.controller = loader.getController();
this.todoList = this.controller.getTodoList();
this.todoList = this.controller.getSelectedTodoList();
stage.setScene(new Scene(root));
stage.show();
}
......@@ -50,7 +50,7 @@ public class TodoAppTest extends ApplicationTest {
@Test
public void testTodoListView_initialItems() {
final ListView<TodoItem> todoListView = lookup("#todoListView").query();
final ListView<TodoItem> todoListView = lookup("#todoItemsView").query();
// initial todo items, note the unchecked one comes first
checkTodoItems(todoListView.getItems(), item2, item1);
}
......@@ -71,7 +71,7 @@ public class TodoAppTest extends ApplicationTest {
@Test
public void testDeleteTodoItem() {
// final ListView<TodoItem> todoListView = lookup("#todoListView").query();
// final ListView<TodoItem> todoListView = lookup("#todoItemsView").query();
// todoListView.getSelectionModel().select(1);
TodoItemListCell todoItemListCell = findTodoItemListCell(1);
clickOn(todoItemListCell.lookup(".label"));
......@@ -144,7 +144,7 @@ public class TodoAppTest extends ApplicationTest {
}
private void checkTodoListViewItems(TodoItem... items) {
final ListView<TodoItem> todoListView = lookup("#todoListView").query();
final ListView<TodoItem> todoListView = lookup("#todoItemsView").query();
checkTodoItems(todoListView.getItems(), items);
}
......@@ -159,7 +159,7 @@ public class TodoAppTest extends ApplicationTest {
}
private void checkSelectedTodoItem(int index) {
final ListView<TodoItem> todoListView = lookup("#todoListView").query();
final ListView<TodoItem> todoListView = lookup("#todoItemsView").query();
assertEquals(index, todoListView.getSelectionModel().getSelectedIndex());
}
}
......@@ -3,6 +3,7 @@
<?import java.lang.String?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ListView?>
......@@ -14,11 +15,14 @@
-->
<String fx:id="sampleTodoListResource" fx:value="test-todolist.json"/>
</fx:define>
<HBox>
<ComboBox fx:id="todoListsView"/>
</HBox>
<HBox>
<Button fx:id="newTodoItemButton" text="New Item" onAction="#handleNewTodoItemAction"/>
<TextField fx:id="newTodoItemText" promptText="Skriv inn tekst her" onAction="#handleNewTodoItemAction"/>
</HBox>
<ListView fx:id="todoListView">
<ListView fx:id="todoItemsView">
</ListView>
<HBox>
<Button fx:id="deleteTodoItemButton" text="Delete Item" onAction="#handleDeleteItemAction"/>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment