package se.dat255.grupp12; import java.util.ArrayList; import java.util.Collections; import java.util.List; /** * Represents a todo-list * Created by Oscar on 2013-10-03. */ public class TodoList { public static final AssignedList THE_ASSIGNED_LIST = new AssignedList(); private static TodoList currentList; private static List<TodoList> todoLists = new ArrayList<TodoList>(); private ArrayList<Task> tasks; private ArrayList<User> collaborators = new ArrayList<User>(); private String name; private long id; private static long nextLocalId = -1; /** * Constructor for creating a local, synced TodoList with a name, * tasks, collaborators and an id; * @param tasks * @param collaborators * @param name * @param id */ public TodoList(ArrayList<Task> tasks, ArrayList<User> collaborators, String name, long id) { this.tasks = tasks; this.collaborators = collaborators; this.name = name; this.id = id; } /** * Constructor for creating a local, unsynced TodoList with a name, tasks, and collaborators * Negative id represents an unsynced TodoList. * @param name * @param tasks * @param collaborators */ public TodoList(String name, ArrayList<Task> tasks, ArrayList<User> collaborators){ this.tasks = tasks; this.name = name; this.collaborators = collaborators; this.id = nextLocalId--; } /** * Constructor for creating a local, unsynced TodoList with a name and tasks * Negative id represents an unsynced TodoList. * @param name * @param tasks */ public TodoList(String name, ArrayList<Task> tasks){ this.tasks = tasks; this.name = name; this.collaborators = new ArrayList<User>(); this.id = nextLocalId--; } /** * Constructor for creating a local, unsynced TodoList with a name. * Negative id represents an unsynced TodoList. * @param name */ public TodoList(String name){ this.tasks = new ArrayList<Task>(); this.name = name; this.collaborators = new ArrayList<User>(); this.id = nextLocalId--; } /** * * @param newCurrentList * @throws java.lang.IllegalArgumentException if a selected list does * not exist in list of lists. */ public static void setCurrentList(TodoList newCurrentList){ if (!TodoList.getTodoLists().contains(newCurrentList) && newCurrentList!=THE_ASSIGNED_LIST) { throw new IllegalArgumentException("Tried to select list that doesn't" + " exist in list of lists"); } currentList = newCurrentList; } public static TodoList getListWithID(long id) { for (TodoList list : todoLists) { if (list.getId()==id) { return list; } } throw new IllegalArgumentException("List not found"); } /** * * @param newTodo */ public static void addTodoList(TodoList newTodo){ todoLists.add(0, newTodo); } public static void removeTodoList(TodoList listToRemove){ if (TodoList.getTodoListsWithoutAssigned().size()==1) { TodoList emptyTodo = new TodoList("Empty list"); TodoList.addTodoList(emptyTodo); TodoList.setCurrentList(emptyTodo); TodoList.getTodoListsWithoutAssigned().remove(listToRemove); return; } if (listToRemove == currentList) { TodoList nextList = TodoList.getTodoLists() .get(TodoList.getTodoLists().indexOf(listToRemove)==0 ? 1 : TodoList.getTodoLists().indexOf(listToRemove) - 1); TodoList.setCurrentList(nextList); TodoList.getTodoListsWithoutAssigned().remove(listToRemove); } else { TodoList.getTodoListsWithoutAssigned().remove(listToRemove); } } /** * Get the current chosen todolist * @return the current list */ public static TodoList getCurrentList() { return currentList; } /** * Returns an arraylist with all local todolists * @return arraylist with all local todolists */ public static List<TodoList> getTodoLists() { ArrayList<TodoList> todoListsWithAssignedList = new ArrayList<TodoList>(); todoListsWithAssignedList.add(THE_ASSIGNED_LIST); todoListsWithAssignedList.addAll(todoLists); return todoListsWithAssignedList; } /** * Getter for the name of the TodoList * @return the name */ public String getName(){ return name; } /** * Getter for the tasks in the TodoList * @return the tasks in the TodoList */ public ArrayList<Task> getTasks(){ return tasks; } /** * Getter for the id of the TodoList * @return the id of the TodoList */ public long getId() { return id; } /** * Get the ID of the list for the specified task. * By default, this always returns the current listid. * * @param task * @return the current listid. */ public long getIdForPosition(Task task) { return this.getId(); } public boolean isSoleCollaborator() { if (collaborators==null || collaborators.size()<=1) { return true; } for (User user : this.collaborators) { if (!User.getUserEmails().contains(user.getEmail())) { return false; } } return true; } /** * Resets the count of new local lists */ public static void resetNextLocalId(){ nextLocalId = -1; } /** * Metod for checking if a TodoList is synced with server * @return A boolean, true if TodoList is synced with server, false otherwise */ public boolean isSynced(){ return id >= 0; } /** * Exchange all todolists to the provided list of lists * @param list */ public static void setTodoLists(ArrayList list){ // Never add the assignedlist. list.remove(THE_ASSIGNED_LIST); todoLists = list; } /** * Set the name of a TodoList * @param name */ public void setName(String name){ this.name = name; } /** * Sorts the array list after task priority */ public static void sortListAfterPrio(){ Collections.sort(currentList.getTasks(), new TaskComparator()); } /** * Adds the provided user to the todo * @param user */ public void addCollaborator(User user){ this.collaborators.add(0, user); } /** * Adds the provided user to the current todo * @param user */ public static void addCollaboratorToCurrentTodo(User user){ TodoList.getCurrentList().collaborators.add(user); } public static ArrayList<User> getCollaboratorsFromCurrentList(){ return TodoList.getCurrentList().collaborators; } public TodoList(){} public static List<TodoList> getTodoListsWithoutAssigned() { return todoLists; } }