From 5e4ead4985078c15b90b863654b56fe8c153b041 Mon Sep 17 00:00:00 2001 From: Thomas Battermann Date: Sat, 20 Jun 2015 23:02:40 +0200 Subject: [PATCH] move items between lists --- .../dhbw/todolist/Controller.java | 85 ++++++++++++++++++- .../dhbw/todolist/moveTodoItem.fxml | 47 ++++++++++ 2 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 src/de/t_battermann/dhbw/todolist/moveTodoItem.fxml diff --git a/src/de/t_battermann/dhbw/todolist/Controller.java b/src/de/t_battermann/dhbw/todolist/Controller.java index e8a90c3..5eda88e 100644 --- a/src/de/t_battermann/dhbw/todolist/Controller.java +++ b/src/de/t_battermann/dhbw/todolist/Controller.java @@ -373,12 +373,20 @@ public class Controller { }else{ ErrorPrinter.printWarning("showMainWindow > Couldn’t find element '#menuChangeEmail'"); } + // log out n = primaryStage.getScene().lookup("#menuLogout"); if ( n != null && n instanceof Button) { ((Button) n).setOnAction(event -> showLoginDialog()); }else{ ErrorPrinter.printWarning("showMainWindow > Couldn’t find element '#menuLogout'"); } + // move todo item + n = primaryStage.getScene().lookup("#todoMove"); + if ( n != null && n instanceof Button) { + ((Button) n).setOnAction(event -> showMoveTodoItem()); + }else{ + ErrorPrinter.printWarning("showMainWindow > Couldn’t find element '#todoMove'"); + } } private void updateSelectedTodoList() { @@ -879,7 +887,6 @@ public class Controller { Node n = change.getScene().lookup("#status"); if ( n == null || !(n instanceof Label)) { ErrorPrinter.printWarning("showChangeEmail > Didn’t find element #status!"); - return; } Label status = (Label) n; @@ -975,6 +982,82 @@ public class Controller { ((Button) n).setOnAction(event -> Platform.exit()); } + private void showMoveTodoItem() { + Stage move = new Stage(); + try { + move.setScene(new Scene(FXMLLoader.load(getClass().getResource("moveTodoItem.fxml")))); + } catch (IOException e) { + ErrorPrinter.printError("showMoveTodoItem > Failed to open window 'moveTodoItem'! Goodbye!"); + e.printStackTrace(); + Platform.exit(); + } + move.setTitle("Move item '" + this.currentTodo.getTitle() + "'"); + move.show(); + // fill in the gaps :) + Node n = move.getScene().lookup("#title"); + if ( n == null || !(n instanceof TextField)) { + ErrorPrinter.printWarning("showMoveTodoItem > Didn’t find element #title"); + return; + } + ((TextField) n).setText(this.currentTodo.getTitle()); + // get current TodoList + n = primaryStage.getScene().lookup("#todoLists"); + if (n == null || !(n instanceof ListView)) { + ErrorPrinter.printWarning("showDeleteList > Didn’t find element #todoLists!"); + return; + } + ListView l = (ListView) n; + TodoList t; + if (l.getSelectionModel().getSelectedItem() != null && l.getSelectionModel().getSelectedItem() instanceof TodoList) { + t = (TodoList) l.getSelectionModel().getSelectedItem(); + }else { + ErrorPrinter.printWarning("showDeleteList > Didn’t find selected item!"); + return; + } + n = move.getScene().lookup("#source"); + if ( n == null || !(n instanceof TextField)) { + ErrorPrinter.printWarning("showDeleteList > Didn’t find element #source"); + return; + } + ((TextField) n).setText(t.getName()); + // populate dropdown + n = move.getScene().lookup("#destination"); + if ( n == null || !(n instanceof ChoiceBox)) { + ErrorPrinter.printWarning("showDeleteList > Didn’t find element #destination"); + return; + } + ChoiceBox c = (ChoiceBox) n; + c.setItems(this.todoLists); + // event handlers + n = move.getScene().lookup("#abort"); + if ( n == null || !(n instanceof Button)) { + ErrorPrinter.printWarning("showDeleteList > Didn’t find element #abort"); + move.close(); + return; + } + ((Button) n).setOnAction(event -> move.close()); + n = move.getScene().lookup("#move"); + if ( n == null || !(n instanceof Button)) { + ErrorPrinter.printWarning("showDeleteList > Didn’t find element #move"); + move.close(); + return; + } + ((Button) n).setOnAction(event -> { + // first add the item to the destination + TodoList list = c.getSelectionModel().getSelectedItem(); + if ( list == null ) { + ErrorPrinter.printWarning("showDeleteList > Invalid selection!"); + this.updateStatusLine("Invalid selection!"); + return; + } + list.addTodo(this.currentTodo); + // then delete the item in the source list + todos.remove(this.currentTodo); + // update current list + move.close(); + }); + } + /** * Notify a list about a changed item * Taken from: http://stackoverflow.com/a/21435063 diff --git a/src/de/t_battermann/dhbw/todolist/moveTodoItem.fxml b/src/de/t_battermann/dhbw/todolist/moveTodoItem.fxml new file mode 100644 index 0000000..73afd9c --- /dev/null +++ b/src/de/t_battermann/dhbw/todolist/moveTodoItem.fxml @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + +