Delete a TodoList

Handle empty TodoLists correctly
Use setOnAction for Buttons
Bug fix: Login after a user was created
main
Thomas Battermann 10 years ago committed by Thomas Ba
parent 6b95491141
commit e5f1a76dca

@ -8,6 +8,7 @@ import javafx.fxml.FXMLLoader;
import javafx.scene.Node; import javafx.scene.Node;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.control.*; import javafx.scene.control.*;
import javafx.scene.input.KeyCode;
import javafx.stage.Stage; import javafx.stage.Stage;
import java.io.File; import java.io.File;
@ -86,8 +87,11 @@ public class Controller {
e1.printStackTrace(); e1.printStackTrace();
return false; return false;
} }
this.updateStatusLine("Saved data to'"+this.filename+"'");
return true; return true;
} }
this.updateStatusLine("No filename given. Please choose one!");
this.showSaveAs();
return false; return false;
} }
@ -135,7 +139,7 @@ public class Controller {
// Log in // Log in
Node n = primaryStage.getScene().lookup("#loginButton"); Node n = primaryStage.getScene().lookup("#loginButton");
if ( n != null && n instanceof Button) { if ( n != null && n instanceof Button) {
n.setOnMouseReleased(event -> { ((Button) n).setOnAction(event -> {
Label l = (Label) primaryStage.getScene().lookup("#labelHints"); Label l = (Label) primaryStage.getScene().lookup("#labelHints");
String name = null; String name = null;
String pass = null; String pass = null;
@ -155,11 +159,11 @@ public class Controller {
if (this.currentUser.checkLoginData(pass)) { if (this.currentUser.checkLoginData(pass)) {
this.todoLists = new ObservableListWrapper<>(currentUser.getTodoLists()); this.todoLists = new ObservableListWrapper<>(currentUser.getTodoLists());
this.showMainWindow(); this.showMainWindow();
}else{ } else {
this.currentUser = null; this.currentUser = null;
l.setText("Invalid credentials!"); l.setText("Invalid credentials!");
} }
}else{ } else {
l.setText("Invalid credentials! 1"); l.setText("Invalid credentials! 1");
} }
}); });
@ -168,52 +172,54 @@ public class Controller {
} }
n = primaryStage.getScene().lookup("#registerButton"); n = primaryStage.getScene().lookup("#registerButton");
if ( n != null && n instanceof Button) { if ( n != null && n instanceof Button) {
n.setOnMouseReleased(event -> { ((Button) n).setOnAction(event -> {
Label l = (Label) primaryStage.getScene().lookup("#labelHintsCreateNewUser"); Label l = (Label) primaryStage.getScene().lookup("#labelHintsCreateNewUser");
// username // username
String username; String username;
Node no = primaryStage.getScene().lookup("#registerUsername"); Node no = primaryStage.getScene().lookup("#registerUsername");
if ( no != null && no instanceof TextField && User.checkUsername(((TextField) no).getText())) { if (no != null && no instanceof TextField && User.checkUsername(((TextField) no).getText())) {
username = ((TextField) no).getText(); username = ((TextField) no).getText();
}else{ } else {
l.setText("Invalid username! (Regex: [a-zA-Z0-9_-]{3,})"); l.setText("Invalid username! (Regex: [a-zA-Z0-9_-]{3,})");
return; return;
} }
if ( users.containsKey(username)) { if (users.containsKey(username)) {
l.setText("Username already taken!"); l.setText("Username already taken!");
return; return;
} }
// password // password
String password; String password;
no = primaryStage.getScene().lookup("#registerPassword"); no = primaryStage.getScene().lookup("#registerPassword");
if( no != null && no instanceof PasswordField && User.checkPassword(((PasswordField) no).getText())) { if (no != null && no instanceof PasswordField && User.checkPassword(((PasswordField) no).getText())) {
password = ((PasswordField) no).getText(); password = ((PasswordField) no).getText();
no = primaryStage.getScene().lookup("#registerPasswordRepeat"); no = primaryStage.getScene().lookup("#registerPasswordRepeat");
if( no == null || !(no instanceof PasswordField) || !password.equals(((PasswordField) no).getText())) { if (no == null || !(no instanceof PasswordField) || !password.equals(((PasswordField) no).getText())) {
l.setText("The passwords didnt match!"); l.setText("The passwords didnt match!");
} }
}else{ } else {
l.setText("Password must be longer than 6 chars! And must contain numbers, lower and upper chars!"); l.setText("Password must be longer than 6 chars! And must contain numbers, lower and upper chars!");
return; return;
} }
// eMail // eMail
String email; String email;
no = primaryStage.getScene().lookup("#registerEmail"); no = primaryStage.getScene().lookup("#registerEmail");
if( no != null && no instanceof TextField && User.checkEmail( ((TextField) no).getText() ) ) { if (no != null && no instanceof TextField && User.checkEmail(((TextField) no).getText())) {
email = ((TextField) no).getText(); email = ((TextField) no).getText();
no = primaryStage.getScene().lookup("#registerEmailRepeat"); no = primaryStage.getScene().lookup("#registerEmailRepeat");
if( no == null || !(no instanceof TextField) || !email.equals(((TextField) no).getText())) { if (no == null || !(no instanceof TextField) || !email.equals(((TextField) no).getText())) {
l.setText("The eMail addresses didnt match!"); l.setText("The eMail addresses didnt match!");
} }
}else{ } else {
l.setText("No valid eMail address given!"); l.setText("No valid eMail address given!");
return; return;
} }
User nu = new User(username,password); User nu = new User(username, password);
nu.setEmail(email); nu.setEmail(email);
currentUser = nu; currentUser = nu;
users.put(username, nu); users.put(username, nu);
this.todoLists = new ObservableSequentialListWrapper<>(currentUser.getTodoLists()); this.todoLists = new ObservableSequentialListWrapper<>(currentUser.getTodoLists());
// log in
this.showMainWindow();
}); });
}else{ }else{
this.printError("'#registerButton' not found!"); this.printError("'#registerButton' not found!");
@ -243,33 +249,32 @@ public class Controller {
} }
n = primaryStage.getScene().lookup("#menuSave"); n = primaryStage.getScene().lookup("#menuSave");
if ( n != null && n instanceof Button) { if ( n != null && n instanceof Button) {
n.setOnMouseReleased(event -> { ((Button) n).setOnAction(event -> {
if ( this.filename != null) { if (this.filename != null) {
this.export(this.filename); this.export(this.filename);
}else{ } else {
this.showSaveAs(); this.showSaveAs();
} }
}); });
} }
n = primaryStage.getScene().lookup("#menuSaveAs"); n = primaryStage.getScene().lookup("#menuSaveAs");
if ( n != null && n instanceof Button ) { if ( n != null && n instanceof Button ) {
n.setOnMouseReleased(event -> this.showSaveAs()); ((Button) n).setOnAction(event -> this.showSaveAs());
} }
n = primaryStage.getScene().lookup("#menuClose"); n = primaryStage.getScene().lookup("#menuClose");
if ( n != null && n instanceof Button ) { if ( n != null && n instanceof Button ) {
n.setOnMouseReleased(event -> Platform.exit()); ((Button) n).setOnAction(event -> Platform.exit());
} }
this.primaryStage.setOnCloseRequest(event -> Platform.exit()); this.primaryStage.setOnCloseRequest(event -> Platform.exit());
n = primaryStage.getScene().lookup("#todoDetailSave"); n = primaryStage.getScene().lookup("#todoDetailSave");
if ( n != null && n instanceof Button) { if ( n != null && n instanceof Button) {
n.setOnMouseClicked(event -> this.saveTodoEntry()); ((Button) n).setOnAction(event -> this.saveTodoEntry());
n.setOnKeyPressed(event -> this.saveTodoEntry());
} }
n = primaryStage.getScene().lookup("#todoDetailDueDate"); n = primaryStage.getScene().lookup("#todoDetailDueDate");
if (n != null && n instanceof CheckBox) { if (n != null && n instanceof CheckBox) {
((CheckBox) n).setOnAction(event -> this.detailUpdateDueDatePicker()); ((CheckBox) n).setOnAction(event -> this.detailUpdateDueDatePicker());
} }
// TODO: handle new TodoList // handle new TodoList
n = primaryStage.getScene().lookup("#todoListNew"); n = primaryStage.getScene().lookup("#todoListNew");
if ( n != null && n instanceof Button) { if ( n != null && n instanceof Button) {
((Button) n).setOnAction(event -> { ((Button) n).setOnAction(event -> {
@ -277,7 +282,7 @@ public class Controller {
this.showTodoListEdit(); this.showTodoListEdit();
}); });
} }
// TODO: handle edit TodoList // handle edit TodoList
n = primaryStage.getScene().lookup("#todoListEdit"); n = primaryStage.getScene().lookup("#todoListEdit");
if ( n != null && n instanceof Button) { if ( n != null && n instanceof Button) {
((Button) n).setOnAction(event -> { ((Button) n).setOnAction(event -> {
@ -285,7 +290,11 @@ public class Controller {
this.showTodoListEdit(); this.showTodoListEdit();
}); });
} }
// TODO: handle delete TodoList // handle delete TodoList
n = primaryStage.getScene().lookup("#todoListDelete");
if( n != null && n instanceof Button) {
((Button) n).setOnAction(event -> deleteTodoList());
}
// TODO // TODO
} }
@ -314,6 +323,8 @@ public class Controller {
if (n!=null && n instanceof Button) { if (n!=null && n instanceof Button) {
n.setDisable(!t.isChangeable()); n.setDisable(!t.isChangeable());
} }
// if there is no todo item, empty the currentTodo
this.currentTodo = null;
} }
updateSelectedTodo(); updateSelectedTodo();
} }
@ -330,17 +341,17 @@ public class Controller {
n = primaryStage.getScene().lookup("#todoDetailTitle"); n = primaryStage.getScene().lookup("#todoDetailTitle");
if ( n == null || !(n instanceof TextField)) if ( n == null || !(n instanceof TextField))
return; return;
((TextField) n).setText( this.currentTodo.getTitle() ); ((TextField) n).setText( this.currentTodo == null ? "" : this.currentTodo.getTitle() );
// comment // comment
n = primaryStage.getScene().lookup("#todoDetailDescription"); n = primaryStage.getScene().lookup("#todoDetailDescription");
if ( n == null || !(n instanceof TextArea) ) if ( n == null || !(n instanceof TextArea) )
return; return;
((TextArea) n).setText( this.currentTodo.getComment() ); ((TextArea) n).setText( this.currentTodo == null ? "" : this.currentTodo.getComment() );
// if dueDate set: // if dueDate set:
n = primaryStage.getScene().lookup("#todoDetailDueDate"); n = primaryStage.getScene().lookup("#todoDetailDueDate");
if ( n == null || !(n instanceof CheckBox) ) if ( n == null || !(n instanceof CheckBox) )
return; return;
boolean dueDate = this.currentTodo.getDueDate() != null; boolean dueDate = this.currentTodo != null && this.currentTodo.getDueDate() != null;
((CheckBox) n).setSelected(dueDate); ((CheckBox) n).setSelected(dueDate);
// datePicker // datePicker
n = primaryStage.getScene().lookup("#todoDetailDate"); n = primaryStage.getScene().lookup("#todoDetailDate");
@ -381,17 +392,17 @@ public class Controller {
save.setTitle("Save as ..."); save.setTitle("Save as ...");
save.show(); save.show();
Button s = (Button) save.getScene().lookup("#save"); Button s = (Button) save.getScene().lookup("#save");
if ( s != null ) if ( s != null)
s.setOnMouseReleased(event -> { s.setOnAction(event -> {
TextField f = (TextField)save.getScene().lookup("#filename"); TextField f = (TextField) save.getScene().lookup("#filename");
if ( f != null) { if (f != null) {
File file = new File(f.getText()); File file = new File(f.getText());
if ( !file.isDirectory() ) { if (!file.isDirectory()) {
if ( this.filename == null) if (this.filename == null)
this.filename = f.getText(); this.filename = f.getText();
if(this.export(f.getText())) if (this.export(f.getText()))
save.close(); save.close();
}else{ } else {
this.updateStatusLine("Cant write to file!"); this.updateStatusLine("Cant write to file!");
} }
} }
@ -399,6 +410,10 @@ public class Controller {
} }
private void saveTodoEntry() { private void saveTodoEntry() {
if ( this.currentTodo == null ) {
this.updateStatusLine("No item selected!");
return;
}
Node lv = primaryStage.getScene().lookup("#todos"); Node lv = primaryStage.getScene().lookup("#todos");
if ( lv == null || !(lv instanceof ListView)) { if ( lv == null || !(lv instanceof ListView)) {
this.updateStatusLine("Couldt get todo-ListView!"); this.updateStatusLine("Couldt get todo-ListView!");
@ -505,7 +520,6 @@ public class Controller {
} }
} }
private void saveTodoListEdit() { private void saveTodoListEdit() {
// TODO
Node n = primaryStage.getScene().lookup("#todoListNewName"); Node n = primaryStage.getScene().lookup("#todoListNewName");
if ( n == null || !(n instanceof TextField)) { if ( n == null || !(n instanceof TextField)) {
this.updateStatusLine("Couldnt get 'todoListNewName'"); this.updateStatusLine("Couldnt get 'todoListNewName'");
@ -537,6 +551,23 @@ public class Controller {
n.setDisable(true); n.setDisable(true);
n.setVisible(false); n.setVisible(false);
} }
private void deleteTodoList() {
Node n = primaryStage.getScene().lookup("#todoLists");
if (n == null || !(n instanceof ListView)) {
return;
}
ListView l = (ListView) n;
if (l.getSelectionModel().getSelectedItem() != null && l.getSelectionModel().getSelectedItem() instanceof TodoList) {
TodoList t = (TodoList) l.getSelectionModel().getSelectedItem();
if (!t.isChangeable() ) {
this.updateStatusLine("Cant delete the TodoList!");
return;
}
this.todoLists.remove(t);
//this.notifyList(this.todoLists, this.currentTodo);
this.updateStatusLine("TodoList removed!");
}
}
private void printError(String s) { private void printError(String s) {
SimpleDateFormat format = new SimpleDateFormat(); SimpleDateFormat format = new SimpleDateFormat();
format.applyPattern("yyyyMMdd'T'HH:mm:ssZ"); format.applyPattern("yyyyMMdd'T'HH:mm:ssZ");

Loading…
Cancel
Save