Java tutorial
/* * TasksPage.java * Copyright (c) 2009 Felix Cachaldora Sanchez * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE */ package com.wickettasks.web.pages; import java.util.List; import org.apache.wicket.PageParameters; import org.apache.wicket.behavior.SimpleAttributeModifier; import org.apache.wicket.markup.html.WebMarkupContainer; import org.apache.wicket.markup.html.basic.Label; import org.apache.wicket.markup.html.form.Form; import org.apache.wicket.markup.html.form.TextField; import org.apache.wicket.markup.html.link.Link; import org.apache.wicket.markup.html.link.PageLink; import org.apache.wicket.markup.html.list.ListItem; import org.apache.wicket.markup.html.list.ListView; import org.apache.wicket.model.CompoundPropertyModel; import org.apache.wicket.model.IModel; import org.apache.wicket.model.LoadableDetachableModel; import org.apache.wicket.spring.injection.annot.SpringBean; import org.apache.wicket.validation.validator.StringValidator; import com.wickettasks.business.entities.task.Task; import com.wickettasks.business.entities.tasklist.TaskList; import com.wickettasks.business.exceptions.user.AccessRestrictionException; import com.wickettasks.business.services.task.TaskService; import com.wickettasks.business.services.tasklist.TaskListService; import com.wickettasks.web.session.WicketTasksSession; public class TasksPage extends BasePage { @SpringBean TaskService taskService; @SpringBean TaskListService taskListService; final Integer taskListId; final Integer userId; public TasksPage(PageParameters parameters) { this.taskListId = Integer.valueOf(parameters.getInt("taskListId")); this.userId = WicketTasksSession.get().getUserId(); final IModel tasks = new LoadableDetachableModel() { private static final long serialVersionUID = 6025219231860695439L; @Override protected Object load() { return TasksPage.this.taskService.findByTaskListIdAndUserId(TasksPage.this.taskListId, TasksPage.this.userId); } }; add(new PageLink("taskListsPageLink", UserTaskListsPage.class)); add(new Label("taskListName", new LoadableDetachableModel() { private static final long serialVersionUID = 1598798159290381978L; @Override protected Object load() { TaskList taskList = TasksPage.this.taskListService .findByTaskListIdAndUserId(TasksPage.this.taskListId, TasksPage.this.userId); return taskList.getName(); } })); WebMarkupContainer tasksDiv = new WebMarkupContainer("tasksDiv") { private static final long serialVersionUID = 3449396171479717013L; @SuppressWarnings("unchecked") @Override public boolean isVisible() { return !((List<TaskList>) tasks.getObject()).isEmpty(); } }; add(tasksDiv); tasksDiv.add(new ListView("taskList", tasks) { private static final long serialVersionUID = 86745945345971L; @Override protected void populateItem(final ListItem item) { Task task = (Task) item.getModelObject(); final String content = task.getContent(); final Integer taskId = task.getId(); final Boolean completed = task.getCompleted(); Label contentLabel = new Label("content", content); item.add(contentLabel); if (completed.booleanValue()) { contentLabel.add(new SimpleAttributeModifier("class", "completed")); } item.add(new Link("complete") { private static final long serialVersionUID = -2560837622401317239L; @Override public void onClick() { try { TasksPage.this.taskService.complete(taskId, TasksPage.this.userId); PageParameters tasksPageParameters = new PageParameters(); tasksPageParameters.put("taskListId", TasksPage.this.taskListId); setResponsePage(TasksPage.class, tasksPageParameters); } catch (AccessRestrictionException e) { error("Error marking your task as completed"); } } @Override public boolean isVisible() { return !completed.booleanValue(); } }); } }); add(new NewTaskForm("newTaskForm", this.taskListId, this.userId)); } private class NewTaskForm extends Form { private static final long serialVersionUID = 6594623741967230283L; private Integer formTaskListId; private Integer formUserId; private String content; public NewTaskForm(String id, Integer taskListId, Integer userId) { super(id); this.formTaskListId = taskListId; this.formUserId = userId; setModel(new CompoundPropertyModel(this)); TextField contentTextField = new TextField("content"); contentTextField.setRequired(true); contentTextField.add(StringValidator.maximumLength(200)); add(contentTextField); } @Override public void onSubmit() { try { TasksPage.this.taskService.add(this.content, this.formTaskListId, this.formUserId); } catch (AccessRestrictionException e) { error("An error happened while creating you task"); } PageParameters tasksPageParameters = new PageParameters(); tasksPageParameters.put("taskListId", this.formTaskListId); setResponsePage(TasksPage.class, tasksPageParameters); } } }