Reformated code

main
Thomas Battermann 10 years ago committed by thomasba
parent c7ccfb6543
commit 45cd818c2f

@ -14,7 +14,7 @@ import java.util.TreeMap;
/**
* Export the user data in a CSV file
*
* <p>
* File format:
* USER,uuid,username,password,email
* TODOLIST,username,uuid,name,changeable
@ -34,15 +34,15 @@ public class CSVHandler implements ExportHandler {
// date formatter
SimpleDateFormat format = new SimpleDateFormat();
format.applyPattern("yyyyMMdd'T'HH:mm:ssZ");
for( User user: users.values()) {
String userData[] = { "USER", user.getUuid(), user.getUsername(), user.getPassword(), user.getEmail() };
for (User user : users.values()) {
String userData[] = {"USER", user.getUuid(), user.getUsername(), user.getPassword(), user.getEmail()};
w.writeNext(userData);
for( TodoList list: user.getTodoLists() ) {
String listData[] = { "TODOLIST", user.getUsername(), list.getUuid(), list.getName(), list.isChangeable() ? "true" : "false" };
for (TodoList list : user.getTodoLists()) {
String listData[] = {"TODOLIST", user.getUsername(), list.getUuid(), list.getName(), list.isChangeable() ? "true" : "false"};
w.writeNext(listData);
for( Todo todo: list.getTodos() ) {
String todoData[] = { "TODO", user.getUsername(), list.getName(), todo.getUuid(), todo.getTitle(), todo.getComment(),
todo.getDueDate() == null ? "0" : format.format(todo.getDueDate().getTime()), todo.isDone() ? "true" : "false", todo.isPrio() ? "true" : "false" };
for (Todo todo : list.getTodos()) {
String todoData[] = {"TODO", user.getUsername(), list.getName(), todo.getUuid(), todo.getTitle(), todo.getComment(),
todo.getDueDate() == null ? "0" : format.format(todo.getDueDate().getTime()), todo.isDone() ? "true" : "false", todo.isPrio() ? "true" : "false"};
w.writeNext(todoData);
}
}
@ -54,7 +54,7 @@ public class CSVHandler implements ExportHandler {
public void exportToFile(Map<String, User> users, File file) throws IOException {
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
bw.write( doExport(users) );
bw.write(doExport(users));
bw.close();
}
@ -70,7 +70,7 @@ public class CSVHandler implements ExportHandler {
* @return Either the date (if found) or null
*/
private Calendar stringToDate(String date) {
if ( date.equals("0") )
if (date.equals("0"))
return null;
SimpleDateFormat format = new SimpleDateFormat();
format.applyPattern("yyyyMMdd'T'HH:mm:ssZ");
@ -97,40 +97,40 @@ public class CSVHandler implements ExportHandler {
CSVParser c = new CSVParser();
String csv, line[];
Map<String, User> users = new TreeMap<>();
while((csv = r.readLine()) != null) {
while ((csv = r.readLine()) != null) {
line = c.parseLine(csv);
switch( line[0] ) {
switch (line[0]) {
case "USER":
if ( line.length != 5 ) {
if (line.length != 5) {
throw new InvalidDataException("Invalid user: line doesn’t contain 5 elements");
}else if ( users.containsKey( line[2] ) ) {
} else if (users.containsKey(line[2])) {
throw new InvalidDataException("Invalid user: duplicate User!");
}else {
} else {
User u = new User(line[1], line[2], line[3], line[4]);
users.put(line[2], u);
}
break;
case "TODOLIST":
if ( line.length != 5 ) {
if (line.length != 5) {
throw new InvalidDataException("Invalid TodoList: line doesn’t contain 5 elements");
} else if ( !users.containsKey(line[1]) ) {
} else if (!users.containsKey(line[1])) {
throw new InvalidDataException("Invalid TodoList: User not found!");
} else if ( users.get(line[1]).getTodoList(line[3]) != null) {
} else if (users.get(line[1]).getTodoList(line[3]) != null) {
throw new InvalidDataException("Invalid TodoList: duplicate TodoList!");
} else {
TodoList t = new TodoList(line[2],line[3],line[4].equals("true"));
TodoList t = new TodoList(line[2], line[3], line[4].equals("true"));
users.get(line[1]).addTodoList(t);
}
break;
case "TODO":
if ( line.length != 9 ) {
if (line.length != 9) {
throw new InvalidDataException("Invalid Todo: line doesn’t contain 9 elements" + line.length);
} else if ( !users.containsKey(line[1]) ) {
} else if (!users.containsKey(line[1])) {
throw new InvalidDataException("Invalid Todo: User not found!");
} else if ( users.get(line[1]).getTodoList(line[2]) == null) {
} else if (users.get(line[1]).getTodoList(line[2]) == null) {
throw new InvalidDataException("Invalid Todo: TodoList not found!");
} else {
Todo t = new Todo( line[3], line[4], line[5], stringToDate(line[6]), line[7].equals("true"), line[8].equals("true") );
Todo t = new Todo(line[3], line[4], line[5], stringToDate(line[6]), line[7].equals("true"), line[8].equals("true"));
users.get(line[1]).getTodoList(line[2]).addTodo(t);
}
break;

@ -16,10 +16,12 @@ import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class Controller {
private Map<String,User> users = null;
private Map<String, User> users = null;
private User currentUser = null;
private ObservableList<TodoList> todoLists = null;
private ObservableList<Todo> todos;
@ -33,26 +35,6 @@ public class Controller {
showLoadFileDialog();
}
static class TodoListCell extends ListCell<Todo> {
@Override
public void updateItem(Todo item, boolean empty) {
super.updateItem(item, empty);
if ( !empty && item != null ) {
if ( item.isPrio() && !item.isDone() ) {
this.setStyle("-fx-graphic:url(/de/t_battermann/dhbw/todolist/star.png);");
}else{
this.setStyle("-fx-graphic:null;");
}
this.setTextFill(Paint.valueOf(item.isDone() ? "#999999" : (item.pastDue() ? "#aa0000" :"#000000") ));
this.setText(item.getTitle() + (item.getDueDate() != null ? " (due: "+item.getDateTime()+")" : ""));
}else{
this.setStyle("-fx-graphic:null;");
this.setTextFill(Paint.valueOf("#000000"));
this.setText("");
}
}
}
/**
* Initialize new empty
*/
@ -63,13 +45,13 @@ public class Controller {
public void initFromFile(String filename) throws IOException, InvalidDataException {
File f = new File(filename);
if ( !f.isFile() || f.isDirectory() || !f.canRead()) {
if (!f.isFile() || f.isDirectory() || !f.canRead()) {
throw new IOException();
}
ExportHandler e;
if ( filename.endsWith(".csv") ) {
if (filename.endsWith(".csv")) {
e = new CSVHandler();
}else{
} else {
e = new XMLHandler();
}
this.users = e.importFromFile(new File(filename));
@ -77,17 +59,17 @@ public class Controller {
}
public boolean export(String filename) {
if ( filename != null) {
if (filename != null) {
File f = new File(filename);
if ( f.isDirectory() ) {
if (f.isDirectory()) {
this.updateStatusLine("Couldn’t write to file '" + filename + "'");
ErrorPrinter.printError("export > Couldn’t write to file '" + filename + "'");
return false;
}
ExportHandler e;
if ( filename.endsWith(".csv") ) {
if (filename.endsWith(".csv")) {
e = new CSVHandler();
}else {
} else {
e = new XMLHandler();
}
try {
@ -98,8 +80,8 @@ public class Controller {
e1.printStackTrace();
return false;
}
this.updateStatusLine("Saved data to'"+filename+"'");
ErrorPrinter.printInfo("export > Saved data to'"+filename+"'");
this.updateStatusLine("Saved data to'" + filename + "'");
ErrorPrinter.printInfo("export > Saved data to'" + filename + "'");
return true;
}
this.updateStatusLine("No filename given. Please choose one!");
@ -117,7 +99,7 @@ public class Controller {
// show dialog
primaryStage.setTitle("TodoList :: Open database");
try {
primaryStage.setScene(new Scene(FXMLLoader.load(getClass().getResource("openFile.fxml")),500,150));
primaryStage.setScene(new Scene(FXMLLoader.load(getClass().getResource("openFile.fxml")), 500, 150));
} catch (IOException e) {
ErrorPrinter.printError("showLoadFileDialog > Could’t open window 'openFile'! Goodbye!");
e.printStackTrace();
@ -129,7 +111,7 @@ public class Controller {
Button b = (Button) primaryStage.getScene().lookup("#openFileButton");
b.setOnMouseReleased(event -> {
Node n = primaryStage.getScene().lookup("#openFilePath");
if ( n != null && n instanceof TextField ) {
if (n != null && n instanceof TextField) {
try {
this.initFromFile(((TextField) n).getText());
this.showLoginDialog();
@ -137,7 +119,7 @@ public class Controller {
ErrorPrinter.printError("showLoadFileDialog > Can’t read file '" + this.filename + "'");
e1.printStackTrace();
}
}else{
} else {
ErrorPrinter.printWarning("showLoadFileDialog > Didn’t find element #openFilePath!");
}
});
@ -155,7 +137,7 @@ public class Controller {
this.todos = null;
primaryStage.setTitle("TodoList :: Log in");
try {
primaryStage.setScene(new Scene(FXMLLoader.load(getClass().getResource("login.fxml")),500,350));
primaryStage.setScene(new Scene(FXMLLoader.load(getClass().getResource("login.fxml")), 500, 350));
} catch (IOException e) {
ErrorPrinter.printError("showLoginDialog > Failed to open window 'login'! Goodbye!");
e.printStackTrace();
@ -163,12 +145,12 @@ public class Controller {
return;
}
TitledPane a = (TitledPane) primaryStage.getScene().lookup(users.isEmpty() ? "#createNewUserPane" : "#loginPane");
if ( a != null ) {
if (a != null) {
a.setExpanded(true);
}
// Log in
Node n = primaryStage.getScene().lookup("#loginButton");
if ( n != null && n instanceof Button) {
if (n != null && n instanceof Button) {
((Button) n).setOnAction(event -> {
Label l = (Label) primaryStage.getScene().lookup("#labelHints");
String name = null;
@ -197,11 +179,11 @@ public class Controller {
l.setText("Invalid credentials!");
}
});
}else{
} else {
ErrorPrinter.printWarning("showLoginDialog > Didn’t find element #loginButton!");
}
n = primaryStage.getScene().lookup("#registerButton");
if ( n != null && n instanceof Button) {
if (n != null && n instanceof Button) {
((Button) n).setOnAction(event -> {
Label l = (Label) primaryStage.getScene().lookup("#labelHintsCreateNewUser");
// username
@ -251,15 +233,15 @@ public class Controller {
// log in
this.showMainWindow();
});
}else{
} else {
ErrorPrinter.printWarning("showLoginDialog > Didn’t find element #registerButton!");
}
}
private void showMainWindow() {
primaryStage.setTitle("TodoList :: " + currentUser.getUsername() + " > Default");
primaryStage.setTitle("TodoList :: " + currentUser.getUsername() + " > Default");
try {
primaryStage.setScene(new Scene(FXMLLoader.load(getClass().getResource("main.fxml")),950,650));
primaryStage.setScene(new Scene(FXMLLoader.load(getClass().getResource("main.fxml")), 950, 650));
} catch (IOException e) {
ErrorPrinter.printError("showMainWindow > Failed to open window 'main'! Goodbye!");
e.printStackTrace();
@ -267,30 +249,30 @@ public class Controller {
return;
}
Node n = primaryStage.getScene().lookup("#todoLists");
if ( n != null && n instanceof ListView) {
if (n != null && n instanceof ListView) {
ListView<TodoList> lv = (ListView<TodoList>) n;
lv.setItems(this.todoLists);
lv.scrollTo(currentUser.getTodoList("Default"));
lv.getSelectionModel().selectedIndexProperty().addListener(event -> {
this.updateSelectedTodoList();
});
}else{
} else {
ErrorPrinter.printWarning("showMainWindow > Couldn’t find element '#todoLists'");
}
this.todos = new ObservableListWrapper<>(currentUser.getTodoList("Default").getTodos());
n = primaryStage.getScene().lookup("#todos");
if ( n != null && n instanceof ListView) {
if (n != null && n instanceof ListView) {
ListView<Todo> lv = (ListView<Todo>) n;
lv.setItems(this.todos);
lv.getSelectionModel().selectedIndexProperty().addListener(event -> {
this.updateSelectedTodo();
});
lv.setCellFactory(param -> new TodoListCell());
}else{
} else {
ErrorPrinter.printWarning("showMainWindow > Couldn’t find element '#todos'");
}
n = primaryStage.getScene().lookup("#menuSave");
if ( n != null && n instanceof Button) {
if (n != null && n instanceof Button) {
((Button) n).setOnAction(event -> {
if (this.filename != null) {
this.export(this.filename);
@ -298,133 +280,133 @@ public class Controller {
this.showSaveAs();
}
});
}else{
} else {
ErrorPrinter.printWarning("showMainWindow > Couln’t find element '#menuSave'");
}
n = primaryStage.getScene().lookup("#menuSaveAs");
if ( n != null && n instanceof Button ) {
if (n != null && n instanceof Button) {
((Button) n).setOnAction(event -> this.showSaveAs());
}else{
} else {
ErrorPrinter.printWarning("showMainWindow > Couldn’t find element '#menuSaveAs'");
}
n = primaryStage.getScene().lookup("#menuClose");
if ( n != null && n instanceof Button ) {
if (n != null && n instanceof Button) {
((Button) n).setOnAction(event -> showCloseDialog());
}else{
} else {
ErrorPrinter.printWarning("showMainWindow > Couldn’t find element '#menuClose'");
}
this.primaryStage.setOnCloseRequest(event -> showCloseDialog());
n = primaryStage.getScene().lookup("#todoDetailSave");
if ( n != null && n instanceof Button) {
if (n != null && n instanceof Button) {
((Button) n).setOnAction(event -> this.saveTodoEntry());
}else{
} else {
ErrorPrinter.printWarning("showMainWindow > Couldn’t find element '#todoDetailSave'");
}
n = primaryStage.getScene().lookup("#todoDetailDueDate");
if (n != null && n instanceof CheckBox) {
((CheckBox) n).setOnAction(event -> this.detailUpdateDueDatePicker());
}else{
} else {
ErrorPrinter.printWarning("showMainWindow > Couldn’t find element '#todoDetailDueDate'");
}
// handle new TodoList
n = primaryStage.getScene().lookup("#todoListNew");
if ( n != null && n instanceof Button) {
if (n != null && n instanceof Button) {
((Button) n).setOnAction(event -> {
this.buttonAction = "new";
this.showTodoListEdit();
});
}else{
} else {
ErrorPrinter.printWarning("showMainWindow > Couln’t find element '#todoListNew'");
}
// handle edit TodoList
n = primaryStage.getScene().lookup("#todoListEdit");
if ( n != null && n instanceof Button) {
if (n != null && n instanceof Button) {
((Button) n).setOnAction(event -> {
this.buttonAction = "edit";
this.showTodoListEdit();
});
}else{
} else {
ErrorPrinter.printWarning("showMainWindow > Couldn’t find element '#todoListEdit'");
}
// handle delete TodoList
n = primaryStage.getScene().lookup("#todoListDelete");
if( n != null && n instanceof Button) {
if (n != null && n instanceof Button) {
((Button) n).setOnAction(event -> showDeleteList());
}else{
} else {
ErrorPrinter.printWarning("showMainWindow > Couldn’t find element '#todoListDelete'");
}
// toggle todo
n = primaryStage.getScene().lookup("#todoToggleDone");
if ( n!= null && n instanceof ToggleButton) {
if (n != null && n instanceof ToggleButton) {
((ToggleButton) n).setOnAction(event -> toggleDone());
}else{
} else {
ErrorPrinter.printWarning("showMainWindow > Couldn’t find element '#todoToggleDone'");
}
// toggle star
n = primaryStage.getScene().lookup("#todoToggleStar");
if ( n!= null && n instanceof ToggleButton) {
if (n != null && n instanceof ToggleButton) {
((ToggleButton) n).setOnAction(event -> toggleStar());
}else{
} else {
ErrorPrinter.printWarning("showMainWindow > Couldn’t find element '#todoToggleStar'");
}
// add new todo item
n = primaryStage.getScene().lookup("#todoNew");
if ( n!= null && n instanceof Button) {
if (n != null && n instanceof Button) {
((Button) n).setOnAction(event -> newTodoItem());
}else{
} else {
ErrorPrinter.printWarning("showMainWindow > Couldn’t find element '#todoNew'");
}
// delete todo item
n = primaryStage.getScene().lookup("#todoDelete");
if ( n != null && n instanceof Button ) {
if (n != null && n instanceof Button) {
((Button) n).setOnAction(event -> showDeleteItem());
}else{
} else {
ErrorPrinter.printWarning("showMainWindow > Couldn’t find element '#todoDelete'");
}
// change password
n = primaryStage.getScene().lookup("#menuChangePassword");
if ( n != null && n instanceof Button ) {
if (n != null && n instanceof Button) {
((Button) n).setOnAction(event -> showChangePassword());
}else{
} else {
ErrorPrinter.printWarning("showMainWindow > Couldn’t find element '#menuChangePassword'");
}
// change eMail
n = primaryStage.getScene().lookup("#menuChangeEmail");
if ( n != null && n instanceof Button ) {
if (n != null && n instanceof Button) {
((Button) n).setOnAction(event -> showChangeEmail());
}else{
} else {
ErrorPrinter.printWarning("showMainWindow > Couldn’t find element '#menuChangeEmail'");
}
// log out
n = primaryStage.getScene().lookup("#menuLogout");
if ( n != null && n instanceof Button) {
if (n != null && n instanceof Button) {
((Button) n).setOnAction(event -> showLoginDialog());
}else{
} else {
ErrorPrinter.printWarning("showMainWindow > Couldn’t find element '#menuLogout'");
}
// move todo item
n = primaryStage.getScene().lookup("#todoMove");
if ( n != null && n instanceof Button) {
if (n != null && n instanceof Button) {
((Button) n).setOnAction(event -> showMoveTodoItem());
}else{
} else {
ErrorPrinter.printWarning("showMainWindow > Couldn’t find element '#todoMove'");
}
}
private void updateSelectedTodoList() {
Node n = primaryStage.getScene().lookup("#todoLists");
if ( n == null || !(n instanceof ListView) ) {
if (n == null || !(n instanceof ListView)) {
ErrorPrinter.printWarning("updateSelectedTodoList > updateSelectedTodoList > Couldn’t find element '#todoLists'");
return;
}
ListView l = (ListView) n;
n = primaryStage.getScene().lookup("#todos");
if ( n == null || !(n instanceof ListView)) {
if (n == null || !(n instanceof ListView)) {
ErrorPrinter.printWarning("updateSelectedTodoList > updateSelectedTodoList > Couldn’t find element '#todos'");
return;
}
ListView<Todo> lt = (ListView) n;
if ( l.getSelectionModel().getSelectedItem() != null && l.getSelectionModel().getSelectedItem() instanceof TodoList ) {
if (l.getSelectionModel().getSelectedItem() != null && l.getSelectionModel().getSelectedItem() instanceof TodoList) {
TodoList t = (TodoList) l.getSelectionModel().getSelectedItem();
primaryStage.setTitle("TodoList :: " + currentUser.getUsername() + " > " + t.getName());
this.todos = new ObservableListWrapper<>(t.getTodos());
@ -432,11 +414,11 @@ public class Controller {
lt.getSelectionModel().select(0);
// update buttons :)
n = primaryStage.getScene().lookup("#todoListEdit");
if ( n!=null && n instanceof Button) {
if (n != null && n instanceof Button) {
n.setDisable(!t.isChangeable());
}
n = primaryStage.getScene().lookup("#todoListDelete");
if (n!=null && n instanceof Button) {
if (n != null && n instanceof Button) {
n.setDisable(!t.isChangeable());
}
// if there is no todo item, empty the currentTodo
@ -447,29 +429,29 @@ public class Controller {
private void updateSelectedTodo() {
Node n = primaryStage.getScene().lookup("#todos");
if ( n != null && n instanceof ListView) {
if (n != null && n instanceof ListView) {
ListView<Todo> lv = (ListView<Todo>) n;
if(lv.getSelectionModel().getSelectedItem() != null) {
if (lv.getSelectionModel().getSelectedItem() != null) {
this.currentTodo = lv.getSelectionModel().getSelectedItem();
}
}
// title
n = primaryStage.getScene().lookup("#todoDetailTitle");
if ( n == null || !(n instanceof TextField)) {
if (n == null || !(n instanceof TextField)) {
ErrorPrinter.printWarning("updateSelectedTodo > Couldn’t find element '#todoDetailTitle'");
return;
}
((TextField) n).setText( this.currentTodo == null ? "" : this.currentTodo.getTitle() );
((TextField) n).setText(this.currentTodo == null ? "" : this.currentTodo.getTitle());
// comment
n = primaryStage.getScene().lookup("#todoDetailDescription");
if ( n == null || !(n instanceof TextArea) ) {
if (n == null || !(n instanceof TextArea)) {
ErrorPrinter.printWarning("updateSelectedTodo > Couldn’t find element '#todoDetailDescription'");
return;
}
((TextArea) n).setText( this.currentTodo == null ? "" : this.currentTodo.getComment() );
((TextArea) n).setText(this.currentTodo == null ? "" : this.currentTodo.getComment());
// if dueDate set:
n = primaryStage.getScene().lookup("#todoDetailDueDate");
if ( n == null || !(n instanceof CheckBox) ) {
if (n == null || !(n instanceof CheckBox)) {
ErrorPrinter.printWarning("updateSelectedTodo > Couldn’t find element '#todoDetailDueDate'");
return;
}
@ -477,51 +459,51 @@ public class Controller {
((CheckBox) n).setSelected(dueDate);
// datePicker
n = primaryStage.getScene().lookup("#todoDetailDate");
if( n == null || !(n instanceof DatePicker)) {
if (n == null || !(n instanceof DatePicker)) {
ErrorPrinter.printWarning("updateSelectedTodo > Couldn’t find element '#todoDetailDate'");
return;
}
if(dueDate) {
((DatePicker) n).setValue( LocalDateTime.ofInstant(this.currentTodo.getDueDate().getTime().toInstant(), ZoneId.systemDefault()).toLocalDate() );
if (dueDate) {
((DatePicker) n).setValue(LocalDateTime.ofInstant(this.currentTodo.getDueDate().getTime().toInstant(), ZoneId.systemDefault()).toLocalDate());
n.setDisable(false);
}else{
} else {
((DatePicker) n).setValue(null);
n.setDisable(true);
}
// time
n = primaryStage.getScene().lookup("#todoDetailTime");
if ( n == null || !(n instanceof TextField)) {
if (n == null || !(n instanceof TextField)) {
ErrorPrinter.printWarning("updateSelectedTodo > Couldn’t find element '#todoDetailTime'");
return;
}
if ( dueDate ) {
((TextField) n).setText(this.currentTodo.getTime() );
if (dueDate) {
((TextField) n).setText(this.currentTodo.getTime());
n.setDisable(false);
}else{
} else {
n.setDisable(true);
((TextField) n).setText("00:00");
}
// stared
n = primaryStage.getScene().lookup("#todoToggleStar");
if ( n != null && n instanceof ToggleButton ) {
if (n != null && n instanceof ToggleButton) {
((ToggleButton) n).setSelected(currentTodo != null && currentTodo.isPrio());
}else{
} else {
ErrorPrinter.printWarning("updateSelectedTodo > Couldn’t find element '#todoToggleStar'");
}
// done
n = primaryStage.getScene().lookup("#todoToggleDone");
if ( n != null && n instanceof ToggleButton) {
if (n != null && n instanceof ToggleButton) {
((ToggleButton) n).setSelected(currentTodo != null && currentTodo.isDone());
}else{
} else {
ErrorPrinter.printWarning("updateSelectedTodo > Couldn’t find element '#todoToggleDone'");
}
}
private void updateStatusLine(String text) {
Node n = primaryStage.getScene().lookup("#statusLine");
if ( n != null && n instanceof Label) {
if (n != null && n instanceof Label) {
((Label) n).setText(text);
}else{
} else {
ErrorPrinter.printWarning("updateStatusLine > Couldn’t find element '#statusLine'");
}
}
@ -533,7 +515,7 @@ public class Controller {
private void showSaveAs(boolean exitAfterSave) {
Stage save = new Stage();
try {
save.setScene(new Scene(FXMLLoader.load(getClass().getResource("saveAs.fxml")),500,150));
save.setScene(new Scene(FXMLLoader.load(getClass().getResource("saveAs.fxml")), 500, 150));
} catch (IOException e) {
this.updateStatusLine("Failed to open window!");
ErrorPrinter.printError("showSaveAs > Failed to open window 'saveAs'");
@ -543,11 +525,11 @@ public class Controller {
save.setTitle("Save as ...");
save.show();
Node n = primaryStage.getScene().lookup("#filename");
if ( n != null && n instanceof TextField && this.filename != null ) {
((TextField)n).setText(this.filename);
if (n != null && n instanceof TextField && this.filename != null) {
((TextField) n).setText(this.filename);
}
Button s = (Button) save.getScene().lookup("#save");
if ( s != null)
if (s != null)
s.setOnAction(event -> {
TextField f = (TextField) save.getScene().lookup("#filename");
if (f != null) {
@ -570,19 +552,19 @@ public class Controller {
}
private void saveTodoEntry() {
if ( this.currentTodo == null ) {
if (this.currentTodo == null) {
this.updateStatusLine("No item selected!");
return;
}
Node lv = primaryStage.getScene().lookup("#todos");
if ( lv == null || !(lv instanceof ListView)) {
if (lv == null || !(lv instanceof ListView)) {
this.updateStatusLine("Could’t get todo-ListView!");
ErrorPrinter.printWarning("saveTodoEntry > Didn’t find element #todos!");
return;
}
// title
Node n = primaryStage.getScene().lookup("#todoDetailTitle");
if ( n == null || !(n instanceof TextField)) {
if (n == null || !(n instanceof TextField)) {
this.updateStatusLine("Couldn’t load data from todoDetailTitle");
ErrorPrinter.printWarning("saveTodoEntry > Didn’t find element #todoDetailTitle!");
return;
@ -590,7 +572,7 @@ public class Controller {
this.currentTodo.setTitle(((TextField) n).getText());
// description
n = primaryStage.getScene().lookup("#todoDetailDescription");
if ( n == null || !(n instanceof TextArea)) {
if (n == null || !(n instanceof TextArea)) {
this.updateStatusLine("Couldn’t load data from todoDetailDescription");
ErrorPrinter.printWarning("saveTodoEntry > Didn’t find element #statusLine!");
return;
@ -598,14 +580,14 @@ public class Controller {
this.currentTodo.setComment(((TextArea) n).getText());
// date
n = primaryStage.getScene().lookup("#todoDetailDueDate");
if ( n == null || !(n instanceof CheckBox)) {
if (n == null || !(n instanceof CheckBox)) {
this.updateStatusLine("Couldn’t load data from todoDetailDueDate");
ErrorPrinter.printWarning("saveTodoEntry > Didn’t find element #todoDetailDueDate!");
return;
}
if ( !((CheckBox) n).isSelected() ) {
if (!((CheckBox) n).isSelected()) {
this.currentTodo.setDueDate(null);
}else{
} else {
n = primaryStage.getScene().lookup("#todoDetailDate");
if (n == null || !(n instanceof DatePicker)) {
this.updateStatusLine("Couldn’t load data from todoDetailDate");
@ -620,7 +602,7 @@ public class Controller {
ErrorPrinter.printWarning("saveTodoEntry > Didn’t find element #todoDetailDate!");
return;
}
if ( dd == null ) {
if (dd == null) {
this.updateStatusLine("Invalid date!");
return;
}
@ -636,21 +618,21 @@ public class Controller {
private void detailUpdateDueDatePicker() {
Node n = primaryStage.getScene().lookup("#todoDetailDueDate");
if ( n == null || !(n instanceof CheckBox)) {
if (n == null || !(n instanceof CheckBox)) {
this.updateStatusLine("Couldn’t load data from todoDetailDueDate");
ErrorPrinter.printWarning("detailUpdateDueDatePicker > Didn’t find element #todoDetailDueDate!");
return;
}
boolean enable = ((CheckBox) n).isSelected();
n = primaryStage.getScene().lookup("#todoDetailDate");
if ( n == null || !(n instanceof DatePicker)) {
if (n == null || !(n instanceof DatePicker)) {
this.updateStatusLine("Couldn’t load data from todoDetailDate");
ErrorPrinter.printWarning("detailUpdateDueDatePicker > Didn’t find element #todoDetailDue!");
return;
}
n.setDisable(!enable);
n = primaryStage.getScene().lookup("#todoDetailTime");
if ( n == null || !(n instanceof TextField)) {
if (n == null || !(n instanceof TextField)) {
this.updateStatusLine("Couldn’t load data from todoDetailTime");
ErrorPrinter.printWarning("detailUpdateDueDatePicker > Didn’t find element #todoDetailTime!");
return;
@ -660,7 +642,7 @@ public class Controller {
private void showTodoListEdit() {
Node n = this.primaryStage.getScene().lookup("#todoListToolBar");
if ( n == null || !(n instanceof ToolBar)) {
if (n == null || !(n instanceof ToolBar)) {
this.updateStatusLine("Couldn’t get 'todoListToolBar'");
ErrorPrinter.printWarning("showTodoListEdit > Didn’t find element #todoListToolBar!");
return;
@ -668,48 +650,48 @@ public class Controller {
n.setDisable(false);
n.setVisible(true);
n = primaryStage.getScene().lookup("#todoListNewNameSave");
if ( n != null && n instanceof Button) {
if (n != null && n instanceof Button) {
((Button) n).setOnAction(event -> this.saveTodoListEdit());
}else{
} else {
ErrorPrinter.printError("showTodoListEdit > Couldn’t read 'todoListNewNameSave'");
}
n = primaryStage.getScene().lookup("#todoListNewName");
if ( n == null || !(n instanceof TextField)) {
if (n == null || !(n instanceof TextField)) {
this.updateStatusLine("Couldn’t get 'todoListNewName'");
ErrorPrinter.printWarning("showTodoListEdit > Didn’t find element #todoListNewName!");
return;
}
if ( this.buttonAction.equals("edit")) {
if (this.buttonAction.equals("edit")) {
Node l = primaryStage.getScene().lookup("#todoLists");
if ( l == null || !(l instanceof ListView) ) {
if (l == null || !(l instanceof ListView)) {
this.updateStatusLine("Couldn’t get 'todoLists'");
ErrorPrinter.printWarning("showTodoListEdit > Didn’t find element #todoLists!");
return;
}
ListView lv = (ListView) l;
if ( lv.getSelectionModel().getSelectedItem() != null && lv.getSelectionModel().getSelectedItem() instanceof TodoList ) {
((TextField) n).setText(((TodoList)lv.getSelectionModel().getSelectedItem()).getName());
}else{
if (lv.getSelectionModel().getSelectedItem() != null && lv.getSelectionModel().getSelectedItem() instanceof TodoList) {
((TextField) n).setText(((TodoList) lv.getSelectionModel().getSelectedItem()).getName());
} else {
((TextField) n).setText("Unknown Name");
}
}else {
} else {
((TextField) n).setText("New TodoList");
}
}
private void saveTodoListEdit() {
Node n = primaryStage.getScene().lookup("#todoListNewName");
if ( n == null || !(n instanceof TextField)) {
if (n == null || !(n instanceof TextField)) {
this.updateStatusLine("Couldn’t get 'todoListNewName'");
ErrorPrinter.printWarning("saveTodoListEdit > Didn’t find element #todoListNewName!");
return;
}
String name = ((TextField) n).getText();
((TextField) n).setText("");
if ( this.buttonAction.equals("new")) {
if (this.buttonAction.equals("new")) {
this.todoLists.add(new TodoList(name));
this.updateStatusLine("New TodoList generated!");
}else {
} else {
// edit existing one ...
n = primaryStage.getScene().lookup("#todoLists");
if (n == null || !(n instanceof ListView)) {
@ -724,7 +706,7 @@ public class Controller {
}
}
n = this.primaryStage.getScene().lookup("#todoListToolBar");
if ( n == null || !(n instanceof ToolBar)) {
if (n == null || !(n instanceof ToolBar)) {
ErrorPrinter.printWarning("saveTodoListEdit > Didn’t find element #todoListToolBar!");
return;
}
@ -733,29 +715,29 @@ public class Controller {
}
private void toggleDone() {
if(this.currentTodo == null)
if (this.currentTodo == null)
return;
Node n = primaryStage.getScene().lookup("#todoToggleDone");
if ( n == null || !(n instanceof ToggleButton) ) {
if (n == null || !(n instanceof ToggleButton)) {
ErrorPrinter.printWarning("toggleDone > Didn’t find element #todoToggleDone!");
return;
}
this.currentTodo.setDone(!this.currentTodo.isDone());
((ToggleButton) n).setSelected(this.currentTodo.isDone());
this.notifyList(todos,currentTodo);
this.notifyList(todos, currentTodo);
}
private void toggleStar() {
if(this.currentTodo == null)
if (this.currentTodo == null)
return;
Node n = primaryStage.getScene().lookup("#todoToggleStar");
if ( n == null || !(n instanceof ToggleButton) ) {
if (n == null || !(n instanceof ToggleButton)) {
ErrorPrinter.printWarning("toggleStar > Didn’t find element #todoToggleStar!");
return;
}
this.currentTodo.setPrio(!this.currentTodo.isPrio());
((ToggleButton) n).setSelected(this.currentTodo.isPrio());
this.notifyList(todos,currentTodo);
this.notifyList(todos, currentTodo);
}
private void newTodoItem() {
@ -763,7 +745,7 @@ public class Controller {
this.todos.add(t);
this.updateStatusLine("Item added!");
Node n = primaryStage.getScene().lookup("#todos");
if ( n == null || !(n instanceof ListView) ) {
if (n == null || !(n instanceof ListView)) {
ErrorPrinter.printWarning("newTodoItem > Didn’t find element #todos!");
return;
}
@ -781,13 +763,13 @@ public class Controller {
e.printStackTrace();
return;
}
delete.setTitle("Delete '"+this.currentTodo.getTitle()+"'");
delete.setTitle("Delete '" + this.currentTodo.getTitle() + "'");
delete.show();
Node n = delete.getScene().lookup("#no");
if ( n != null && n instanceof Button)
((Button)n).setOnAction(event -> delete.close());
if (n != null && n instanceof Button)
((Button) n).setOnAction(event -> delete.close());
n = delete.getScene().lookup("#yes");
if ( n != null && n instanceof Button )
if (n != null && n instanceof Button)
((Button) n).setOnAction(event -> {
this.todos.remove(this.currentTodo);
this.updateStatusLine("Deleted item!");
@ -814,17 +796,17 @@ public class Controller {
TodoList t;
if (l.getSelectionModel().getSelectedItem() != null && l.getSelectionModel().getSelectedItem() instanceof TodoList) {
t = (TodoList) l.getSelectionModel().getSelectedItem();
}else {
} else {
ErrorPrinter.printWarning("showDeleteList > Didn’t find selected item!");
return;
}
delete.setTitle("Delete '"+t.getName()+"'");
delete.setTitle("Delete '" + t.getName() + "'");
delete.show();
n = delete.getScene().lookup("#no");
if ( n != null && n instanceof Button)
((Button)n).setOnAction(event -> delete.close());
if (n != null && n instanceof Button)
((Button) n).setOnAction(event -> delete.close());
n = delete.getScene().lookup("#yes");
if ( n != null && n instanceof Button )
if (n != null && n instanceof Button)
((Button) n).setOnAction(event -> {
this.todoLists.remove(t);
this.updateStatusLine("Deleted TodoList!");
@ -844,19 +826,19 @@ public class Controller {
change.setTitle("Change password");
change.show();
Node n = change.getScene().lookup("#status");
if ( n == null || !(n instanceof Label)) {
if (n == null || !(n instanceof Label)) {
ErrorPrinter.printWarning("showChangePassword > Didn’t find element #status!");
return;
}
Label status = (Label) n;
n = change.getScene().lookup("#abort");
if ( n == null || !(n instanceof Button)) {
if (n == null || !(n instanceof Button)) {
ErrorPrinter.printWarning("showChangePassword > Didn’t find element #abort!");
return;
}
((Button) n).setOnAction(event -> change.close());
n = change.getScene().lookup("#save");
if ( n == null || !(n instanceof Button)) {
if (n == null || !(n instanceof Button)) {
ErrorPrinter.printWarning("showChangePassword > Didn’t find element #save!");
return;
}
@ -864,31 +846,31 @@ public class Controller {
String pw;
// validate passwords ...
Node l = change.getScene().lookup("#password");
if ( l == null || !(l instanceof PasswordField)) {
if (l == null || !(l instanceof PasswordField)) {
ErrorPrinter.printWarning("showChangePassword > Didn’t find element #password!");
return;
}
if (!currentUser.checkLoginData( ((PasswordField) l).getText() )) {
if (!currentUser.checkLoginData(((PasswordField) l).getText())) {
status.setText("Wrong password!");
return;
}
l = change.getScene().lookup("#newPassword");
if ( l == null || !(l instanceof PasswordField)) {
if (l == null || !(l instanceof PasswordField)) {
ErrorPrinter.printWarning("showChangePassword > Didn’t find element #newPassword!");
return;
}
pw = ((PasswordField)l).getText();
pw = ((PasswordField) l).getText();
if (!User.checkPassword(pw)) {
status.setText("Please use at least 6 chars, upper- and lowercase and numbers!");
return;
}
l = change.getScene().lookup("#newPasswordRepeat");
if ( l == null || !(l instanceof PasswordField)) {
if (l == null || !(l instanceof PasswordField)) {
status.setText("Couldn’t access newPasswordRepeat field!");
ErrorPrinter.printWarning("showChangePassword > Didn’t find element #newPasswordRepeat!");
return;
}
if ( !pw.equals( ((PasswordField) l).getText() )) {
if (!pw.equals(((PasswordField) l).getText())) {
status.setText("Passwords didn’t match!");
return;
}
@ -909,33 +891,33 @@ public class Controller {
change.setTitle("Change eMail");
change.show();
Node n = change.getScene().lookup("#status");
if ( n == null || !(n instanceof Label)) {
if (n == null || !(n instanceof Label)) {
ErrorPrinter.printWarning("showChangeEmail > Didn’t find element #status!");
return;
}
Label status = (Label) n;
// load current email address
n = change.getScene().lookup("#eMail");
if ( n == null || !(n instanceof TextField)) {
if (n == null || !(n instanceof TextField)) {
ErrorPrinter.printWarning("showChangeEmail > Didn’t find element #eMail!");
return;
}
((TextField) n).setText(this.currentUser.getEmail());
n = change.getScene().lookup("#eMailRepeat");
if ( n == null || !(n instanceof TextField)) {
if (n == null || !(n instanceof TextField)) {
ErrorPrinter.printWarning("showChangeEmail > Didn’t find element #eMailRepeat!");
return;
}
((TextField) n).setText(this.currentUser.getEmail());
// actions
n = change.getScene().lookup("#abort");
if ( n == null || !(n instanceof Button)) {
if (n == null || !(n instanceof Button)) {
ErrorPrinter.printWarning("showChangeEmail > Didn’t find element #abort!");
return;
}
((Button) n).setOnAction(event -> change.close());
n = change.getScene().lookup("#save");
if ( n == null || !(n instanceof Button)) {
if (n == null || !(n instanceof Button)) {
ErrorPrinter.printWarning("showChangeEmail > Didn’t find element #save!");
return;
}
@ -943,7 +925,7 @@ public class Controller {
String email;
// validate passwords ...
Node l = change.getScene().lookup("#eMail");
if ( l == null || !(l instanceof TextField)) {
if (l == null || !(l instanceof TextField)) {
status.setText("Couldn’t access eMail field!");
ErrorPrinter.printWarning("showChangeEmail > Didn’t find element #eMail!");
return;
@ -954,12 +936,12 @@ public class Controller {
return;
}
l = change.getScene().lookup("#eMailRepeat");
if ( l == null || !(l instanceof TextField)) {
if (l == null || !(l instanceof TextField)) {
status.setText("Couldn’t access eMailRepeat field!");
ErrorPrinter.printWarning("showChangeEmail > Didn’t find element #eMailRepeat!");
return;
}
if ( !email.equals( ((TextField) l).getText() )) {
if (!email.equals(((TextField) l).getText())) {
status.setText("eMails didn’t match!");
return;
}
@ -980,13 +962,13 @@ public class Controller {
close.setTitle("Close program?");
close.show();
Node n = close.getScene().lookup("#abort");
if ( n == null || !(n instanceof Button)) {
if (n == null || !(n instanceof Button)) {
ErrorPrinter.printWarning("showCloseDialog > Didn’t find element #abort");
return;
}
((Button) n).setOnAction(event -> close.close());
n = close.getScene().lookup("#save");
if ( n == null || !(n instanceof Button)) {
if (n == null || !(n instanceof Button)) {
ErrorPrinter.printWarning("showCloseDialog > Didn’t find element #save");
return;
}
@ -999,7 +981,7 @@ public class Controller {
}
});
n = close.getScene().lookup("#close");
if ( n == null || !(n instanceof Button)) {
if (n == null || !(n instanceof Button)) {
ErrorPrinter.printWarning("showCloseDialog > Didn’t find element #close");
return;
}
@ -1019,7 +1001,7 @@ public class Controller {
move.show();
// fill in the gaps :)
Node n = move.getScene().lookup("#title");
if ( n == null || !(n instanceof TextField)) {
if (n == null || !(n instanceof TextField)) {
ErrorPrinter.printWarning("showMoveTodoItem > Didn’t find element #title");
return;
}
@ -1034,19 +1016,19 @@ public class Controller {
TodoList t;
if (l.getSelectionModel().getSelectedItem() != null && l.getSelectionModel().getSelectedItem() instanceof TodoList) {
t = (TodoList) l.getSelectionModel().getSelectedItem();
}else {
} else {
ErrorPrinter.printWarning("showDeleteList > Didn’t find selected item!");
return;
}
n = move.getScene().lookup("#source");
if ( n == null || !(n instanceof TextField)) {
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)) {
if (n == null || !(n instanceof ChoiceBox)) {
ErrorPrinter.printWarning("showDeleteList > Didn’t find element #destination");
return;
}
@ -1054,14 +1036,14 @@ public class Controller {
c.setItems(this.todoLists);
// event handlers
n = move.getScene().lookup("#abort");
if ( n == null || !(n instanceof Button)) {
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)) {
if (n == null || !(n instanceof Button)) {
ErrorPrinter.printWarning("showDeleteList > Didn’t find element #move");
move.close();
return;
@ -1069,7 +1051,7 @@ public class Controller {
((Button) n).setOnAction(event -> {
// first add the item to the destination
TodoList list = c.getSelectionModel().getSelectedItem();
if ( list == null ) {
if (list == null) {
ErrorPrinter.printWarning("showDeleteList > Invalid selection!");
this.updateStatusLine("Invalid selection!");
return;
@ -1085,7 +1067,8 @@ public class Controller {
/**
* Notify a list about a changed item
* Taken from: http://stackoverflow.com/a/21435063
* @param list the list containing the changed item
*
* @param list the list containing the changed item
* @param changedItem the item itself
*/
protected void notifyList(List list, Object changedItem) {
@ -1099,4 +1082,24 @@ public class Controller {
}
}
static class TodoListCell extends ListCell<Todo> {
@Override
public void updateItem(Todo item, boolean empty) {
super.updateItem(item, empty);
if (!empty && item != null) {
if (item.isPrio() && !item.isDone()) {
this.setStyle("-fx-graphic:url(/de/t_battermann/dhbw/todolist/star.png);");
} else {
this.setStyle("-fx-graphic:null;");
}
this.setTextFill(Paint.valueOf(item.isDone() ? "#999999" : (item.pastDue() ? "#aa0000" : "#000000")));
this.setText(item.getTitle() + (item.getDueDate() != null ? " (due: " + item.getDateTime() + ")" : ""));
} else {
this.setStyle("-fx-graphic:null;");
this.setTextFill(Paint.valueOf("#000000"));
this.setText("");
}
}
}
}

@ -19,7 +19,7 @@ public class ErrorPrinter {
System.out.println("[" + format.format(GregorianCalendar.getInstance().getTime()) + " " + p + "] " + s);
}
public static void printInfo( String s) {
public static void printInfo(String s) {
ErrorPrinter.printInfo("info", s);
}

@ -8,35 +8,35 @@ import java.util.Map;
* Interface for exporting/saving the data
*/
public interface ExportHandler {
/**
* Export to file.
*
* @param users A Map containing the users
* @param filename Path to the file the data should be saved to
*/
void exportToFile(Map<String, User> users, File filename) throws IOException;
/**
* Export to file.
*
* @param users A Map containing the users
* @param filename Path to the file the data should be saved to
*/
void exportToFile(Map<String, User> users, File filename) throws IOException;
/**
* Export to string.
*
* @param users the users
* @return A String containing the data
*/
String exportToString(Map<String, User> users);
/**
* Export to string.
*
* @param users the users
* @return A String containing the data
*/
String exportToString(Map<String, User> users);
/**
* Import from file.
*
* @param filename Path to the saved data
* @return A Map containing the Users, username as index
*/
Map<String, User> importFromFile(File filename) throws IOException, InvalidDataException;
/**
* Import from file.
*
* @param filename Path to the saved data
* @return A Map containing the Users, username as index
*/
Map<String, User> importFromFile(File filename) throws IOException, InvalidDataException;
/**
* Import from string.
*
* @param str A String containing the Data
* @return A Map containing the User, username as index
*/
Map<String, User> importFromString(String str) throws InvalidDataException;
/**
* Import from string.
*
* @param str A String containing the Data
* @return A Map containing the User, username as index
*/
Map<String, User> importFromString(String str) throws InvalidDataException;
}

@ -4,10 +4,10 @@ package de.t_battermann.dhbw.todolist;
* This exception is thrown when the imported data is not as expected.
*/
public class InvalidDataException extends Exception {
public InvalidDataException() {
}
public InvalidDataException() {
}
public InvalidDataException(String message) {
super(message);
}
public InvalidDataException(String message) {
super(message);
}
}

@ -5,13 +5,12 @@ import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception{
public void start(Stage primaryStage) throws Exception {
new Controller(primaryStage);
}
public static void main(String[] args) {
launch(args);
}
}
}

@ -27,12 +27,12 @@ public class Todo {
/**
* Instantiates a new Todo.
*
* @param uuid the uuid
* @param uuid the uuid
* @param title the title
* @param comment the comment
* @param dueDate the due date
* @param done Is the item done?
* @param prio Has the item high priority?
* @param done Is the item done?
* @param prio Has the item high priority?
*/
protected Todo(String uuid, String title, String comment, Calendar dueDate, boolean done, boolean prio) {
this.uuid = uuid;
@ -174,24 +174,24 @@ public class Todo {
Calendar nd = new GregorianCalendar();
int hour = 0;
int minute = 0;
if ( time.matches("\\d{1,2}:\\d{1,2}(:\\d{1,2})?") ) {
if (time.matches("\\d{1,2}:\\d{1,2}(:\\d{1,2})?")) {
String t[] = time.split(":", 3);
hour = Integer.parseInt(t[0]);
minute = Integer.parseInt(t[1]);
}else if( time.matches("\\d{1,4}") ) {
if ( time.length() > 2 ) {
} else if (time.matches("\\d{1,4}")) {
if (time.length() > 2) {
hour = Integer.parseInt(time.substring(0, 2));
minute = Integer.parseInt(time.substring(2, 2));
}else{
hour = Integer.parseInt( time );
} else {
hour = Integer.parseInt(time);
}
}
nd.set(date.getYear(), date.getMonthValue(), date.getDayOfMonth(), hour<24?hour:0, minute<60?minute:0);
nd.set(date.getYear(), date.getMonthValue(), date.getDayOfMonth(), hour < 24 ? hour : 0, minute < 60 ? minute : 0);
this.dueDate = nd;
}
public boolean pastDue() {
if ( this.dueDate == null )
if (this.dueDate == null)
return false;
Calendar today = new GregorianCalendar();
return today.after(this.dueDate);

@ -1,6 +1,8 @@
package de.t_battermann.dhbw.todolist;
import java.util.*;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
/**
* This class contains a todo list with all its items.

@ -3,7 +3,9 @@ package de.t_battermann.dhbw.todolist;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.validator.routines.EmailValidator;
import java.util.*;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
/**
* This class contains all the users data.
@ -35,10 +37,10 @@ public class User {
* Instantiates a new User.
* Used to restore saved data
*
* @param uuid the uuid
* @param username the username
* @param uuid the uuid
* @param username the username
* @param hashedPassword the hashed password
* @param email the email
* @param email the email
*/
protected User(String uuid, String username, String hashedPassword, String email) {
this.username = username;
@ -48,6 +50,24 @@ public class User {
this.todoLists = new LinkedList<>();
}
/**
* Checks if eMail has correct syntax
*
* @param email string containing a eMail address
* @return true if valid syntax
*/
public static boolean checkEmail(String email) {
return email.length() != 0 && EmailValidator.getInstance().isValid(email);
}
public static boolean checkUsername(String username) {
return username.matches("[a-zA-Z0-9_-]{3,}");
}
public static boolean checkPassword(String password) {
return password.length() > 6 && password.matches(".*[A-Z].*") && password.matches(".*[a-z].*") && password.matches(".*[0-9].*");
}
/**
* Gets username.
*
@ -66,6 +86,15 @@ public class User {
return this.password;
}
/**
* Update the users password
*
* @param password the password (cleartext)
*/
public void setPassword(String password) {
this.password = hashPassword(password);
}
/**
* Gets uuid.
*
@ -91,8 +120,8 @@ public class User {
* @return the todo list
*/
public TodoList getTodoList(String name) {
for(TodoList l: todoLists)
if(l.getName().equals(name))
for (TodoList l : todoLists)
if (l.getName().equals(name))
return l;
ErrorPrinter.printDebug("TodoList not found: " + name);
return null;
@ -121,7 +150,7 @@ public class User {
@Override
public String toString() {
return "Username: " + username + "\n"
+ "eMail: " + email + "\n";
+ "eMail: " + email + "\n";
}
/**
@ -131,7 +160,7 @@ public class User {
* @return false if a list with the given name already exists
*/
public boolean addTodoList(TodoList todoList) {
if ( this.getTodoList( todoList.getName()) == null ) {
if (this.getTodoList(todoList.getName()) == null) {
this.todoLists.add(todoList);
return true;
}
@ -139,15 +168,6 @@ public class User {
return false;
}
/**
* Update the users password
*
* @param password the password (cleartext)
*/
public void setPassword(String password) {
this.password = hashPassword(password);
}
/**
* Sets email.
*
@ -172,20 +192,4 @@ public class User {
private String hashPassword(String password) {
return DigestUtils.sha256Hex(uuid + password);
}
/**
* Checks if eMail has correct syntax
* @param email string containing a eMail address
* @return true if valid syntax
*/
public static boolean checkEmail(String email) {
return email.length() != 0 && EmailValidator.getInstance().isValid(email);
}
public static boolean checkUsername(String username) {
return username.matches("[a-zA-Z0-9_-]{3,}");
}
public static boolean checkPassword(String password) {
return password.length() > 6 && password.matches(".*[A-Z].*") && password.matches(".*[a-z].*") && password.matches(".*[0-9].*");
}
}

@ -19,7 +19,10 @@ import java.io.*;
import java.nio.charset.Charset;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Map;
import java.util.TreeMap;
/**
* This class implement the ExportHandler interface. It converts the data to XML and vice versa.
@ -156,8 +159,8 @@ public class XMLHandler implements ExportHandler {
/**
* Get a boolean value from a attribute
*
* @param node The node to be searched in
* @param name The attributes name
* @param node The node to be searched in
* @param name The attributes name
* @param defaultValue if the attribute is not set use this value
* @return either the value of the attribute or the default value if attribute not found
*/

Loading…
Cancel
Save