001: package wicket.examples.ajax.builtin;
002:
003: import java.util.ArrayList;
004: import java.util.Date;
005: import java.util.List;
006:
007: import wicket.Component;
008: import wicket.ajax.AjaxRequestTarget;
009: import wicket.ajax.IAjaxCallDecorator;
010: import wicket.ajax.calldecorator.AjaxCallDecorator;
011: import wicket.ajax.form.AjaxFormSubmitBehavior;
012: import wicket.examples.guestbook.Comment;
013: import wicket.markup.html.WebMarkupContainer;
014: import wicket.markup.html.basic.Label;
015: import wicket.markup.html.basic.MultiLineLabel;
016: import wicket.markup.html.form.Form;
017: import wicket.markup.html.form.TextArea;
018: import wicket.markup.html.list.ListItem;
019: import wicket.markup.html.list.ListView;
020: import wicket.model.CompoundPropertyModel;
021: import wicket.model.Model;
022: import wicket.model.PropertyModel;
023:
024: /**
025: * Ajax enabled example for the guestbook.
026: *
027: * @author Martijn Dashorst
028: */
029: public class GuestBook extends BasePage {
030: /** A global list of all comments from all users across all sessions */
031: public static final List commentList = new ArrayList();
032:
033: /** The list view that shows comments */
034: private final ListView commentListView;
035: /** Container for the comments, used to update the listview. */
036: private WebMarkupContainer comments;
037:
038: /** The textarea for entering the comments, is updated in the ajax call. */
039: private Component text;
040:
041: /**
042: * Constructor.
043: */
044: public GuestBook() {
045: // Add comment form
046: CommentForm commentForm = new CommentForm("commentForm");
047: add(commentForm);
048:
049: // the WebMarkupContainer is used to update the listview in an ajax call
050: comments = new WebMarkupContainer("comments");
051: add(comments.setOutputMarkupId(true));
052:
053: // Add commentListView of existing comments
054: comments.add(commentListView = new ListView("comments",
055: new PropertyModel(this , "commentList")) {
056: public void populateItem(final ListItem listItem) {
057: final Comment comment = (Comment) listItem
058: .getModelObject();
059: listItem.add(new Label("date", new Model(comment
060: .getDate())));
061: listItem.add(new MultiLineLabel("text", comment
062: .getText()));
063: }
064: });
065:
066: // we need to cancel the standard submit of the form in the onsubmit handler,
067: // otherwise we'll get double submits. To do so, we return false after the
068: // ajax submit has occurred.
069:
070: // The AjaxFormSubmitBehavior already calls the onSubmit of the form, all
071: // we need to do in the onSubmit(AjaxRequestTarget) handler is do our Ajax
072: // specific stuff, like rendering our components.
073: commentForm.add(new AjaxFormSubmitBehavior(commentForm,
074: "onsubmit") {
075: protected IAjaxCallDecorator getAjaxCallDecorator() {
076: return new AjaxCallDecorator() {
077: public CharSequence decorateScript(
078: CharSequence script) {
079: return script + "return false;";
080: }
081: };
082: }
083:
084: protected void onSubmit(AjaxRequestTarget target) {
085: // add the list of components that need to be updated
086: target.addComponent(comments);
087: target.addComponent(text);
088:
089: // focus the textarea again
090: target.appendJavascript("document.getElementById('"
091: + text.getMarkupId() + "').focus();");
092: }
093: });
094: }
095:
096: /**
097: * A form that allows a user to add a comment.
098: *
099: * @author Jonathan Locke
100: */
101: public final class CommentForm extends Form {
102: /**
103: * Constructor
104: *
105: * @param id
106: * The name of this component
107: */
108: public CommentForm(final String id) {
109: // Construct form with no validation listener
110: super (id, new CompoundPropertyModel(new Comment()));
111:
112: // Add text entry widget
113: text = new TextArea("text").setOutputMarkupId(true);
114: add(text);
115: }
116:
117: /**
118: * Show the resulting valid edit
119: */
120: public final void onSubmit() {
121: // Construct a copy of the edited comment
122: final Comment comment = (Comment) getModelObject();
123: final Comment newComment = new Comment(comment);
124:
125: // Set date of comment to add
126: newComment.setDate(new Date());
127:
128: // Add the component we edited to the list of comments
129: commentList.add(0, newComment);
130:
131: // Clear out the text component
132: comment.setText("");
133: }
134: }
135:
136: /**
137: * Clears the comments.
138: */
139: public static void clear() {
140: commentList.clear();
141: }
142: }
|