CVS: TapestryBook/examples/src/examples/todo1 ToDo.java,NONE,1.1 ToDoItem.java,NONE,1.1

Howard Lewis Ship <[email protected]> Thu, 27 Feb 2003 16:25:49 -0800
Newsgroups gmane.comp.java.tapestry.cvs
Message-ID <[email protected]>
Update of /cvsroot/tapestry/TapestryBook/examples/src/examples/todo1
In directory sc8-pr-cvs1:/tmp/cvs-serv10808/examples/src/examples/todo1

Added Files:
	ToDo.java ToDoItem.java 
Log Message:
Checkpoint; pretty  much done with chapter 3.

--- NEW FILE: ToDo.java ---
package examples.todo1;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;

import net.sf.tapestry.IMarkupWriter;
import net.sf.tapestry.IRequestCycle;
import net.sf.tapestry.RequestCycleException;
import net.sf.tapestry.html.BasePage;

public abstract class ToDo extends BasePage
{
    public abstract List getToDoList();
    public abstract void setToDoList(List toDoList);

    public abstract ToDoItem getItem();

    public abstract ToDoItem getMoveUpItem();
    public abstract void setMoveUpItem(ToDoItem moveUpItem);

    public abstract ToDoItem getMoveDownItem();
    public abstract void setMoveDownItem(ToDoItem moveDownItem);

    // Remember which item to move up until after the form finishes
    // rewinding.

    public void moveUp(IRequestCycle cycle)
    {
        setMoveUpItem(getItem());
    }

    public void moveDown(IRequestCycle cycle)
    {
        setMoveDownItem(getItem());
    }

    public void addTodoItem(IRequestCycle cycle)
    {
        getToDoList().add(new ToDoItem("New Item"));
    }

    // The Submit for this is after the main loop, so its safe to
    // actually edit the list.

    public void deleteCompleted(IRequestCycle cycle)
    {
        ListIterator i = getToDoList().listIterator();

        while (i.hasNext())
        {
            ToDoItem item = (ToDoItem) i.next();

            if (item.isCompleted())
                i.remove();
        }
    }

    public void formSubmit(IRequestCycle cycle)
    {
        List list = getToDoList();

        int count = list.size();
        ToDoItem moveUpItem = getMoveUpItem();
        ToDoItem moveDownItem = getMoveDownItem();

        for (int i = 0; i < count; i++)
        {
            ToDoItem item = (ToDoItem) list.get(i);

            if (item == moveUpItem)
            {
                if (i > 0)
                    Collections.swap(list, i, i - 1);

                break;
            }

            if (item == moveDownItem)
            {
                if (i + 1 < count)
                    Collections.swap(list, i, i + 1);

                break;
            }
        }

        // Always important to set peristent properties; otherwise changes
        // made in this request cycle will be lost.  The framework
        // makes a copy of the list.

        setToDoList(list);
    }

    // Last chance to set persistent properties before a render.  This is where
    // we detect the uninitialized ToDo list and set its initial
    // contents.

    public void beginResponse(IMarkupWriter writer, IRequestCycle cycle)
        throws RequestCycleException
    {
        List list = getToDoList();

        if (list == null)
        {
            list = new ArrayList();
            list.add(new ToDoItem("Finish reading Tapestry Book"));
            list.add(new ToDoItem("Download latest version of Tapestry"));

            setToDoList(list);
        }
    }

}

--- NEW FILE: ToDoItem.java ---
package examples.todo1;

import java.io.Serializable;

public class ToDoItem implements Serializable
{
    private boolean _completed;
    private String _title;

    public ToDoItem()
    {
    }

    public ToDoItem(String title)
    {
        _title = title;
    }

    public boolean isCompleted()
    {
        return _completed;
    }

    public String getTitle()
    {
        return _title;
    }

    public void setCompleted(boolean completed)
    {
        _completed = completed;
    }

    public void setTitle(String title)
    {
        _title = title;
    }

}



-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf