Java tutorial
/* * Copyright 2011 Marcus Geiger. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.antbear.jee.wicket.guestbook.web; import java.io.Serializable; import java.util.GregorianCalendar; import org.antbear.jee.wicket.guestbook.model.Entry; import org.antbear.jee.wicket.guestbook.model.Guestbook; import org.antbear.jee.wicket.guestbook.persistence.GuestbookService; import org.apache.wicket.markup.html.form.Form; import org.apache.wicket.markup.html.form.FormComponentLabel; import org.apache.wicket.markup.html.form.RequiredTextField; import org.apache.wicket.markup.html.form.TextArea; import org.apache.wicket.markup.html.panel.FeedbackPanel; import org.apache.wicket.markup.html.panel.Panel; import org.apache.wicket.model.CompoundPropertyModel; import org.apache.wicket.model.Model; import org.apache.wicket.spring.injection.annot.SpringBean; import org.apache.wicket.validation.validator.EmailAddressValidator; /** * A wicket panel for adding entries to a guestbook. * * @author Marcus Geiger */ class AddEntryPanel extends Panel { private static final long serialVersionUID = 1L; /** * A simple observer to get notified when an entry has been added to a guestbook. */ public interface EntrySubmitObserver extends Serializable { void entrySubmitted(); } @SpringBean private GuestbookService guestbookService; private EntrySubmitObserver entrySubmitObserver; public AddEntryPanel(final String id, final Long guestbookId) { super(id); add(new FeedbackPanel("feedback")); Form<Entry> form = new Form<Entry>("form", new CompoundPropertyModel<Entry>(new Entry())) { private static final long serialVersionUID = 1L; @Override protected void onSubmit() { super.onSubmit(); Entry entry = getModel().getObject(); entry.setCreationDate(new GregorianCalendar(getLocale()).getTime()); // Persist entry by merging assert guestbookService != null; Guestbook guestbook = guestbookService.find(guestbookId); entry.setGuestbook(guestbook); guestbook.add(entry); guestbookService.merge(guestbook); // Create new fresh entry for next submit Entry freshEntry = new Entry(); getModel().setObject(freshEntry); info("Your comment has been added."); // Notify observer, if any if (entrySubmitObserver != null) entrySubmitObserver.entrySubmitted(); } }; add(form); RequiredTextField<String> name = new RequiredTextField<String>("authorName"); name.setLabel(new Model<String>("Name")); FormComponentLabel nameLabel = new FormComponentLabel("authorNameLabel", name); form.add(nameLabel); form.add(name); RequiredTextField<String> email = new RequiredTextField<String>("authorEmail"); email.add(EmailAddressValidator.getInstance()); email.setLabel(new Model<String>("Email")); FormComponentLabel emailLabel = new FormComponentLabel("authorEmailLabel", name); form.add(emailLabel); form.add(email); TextArea<String> text = new TextArea<String>("text"); text.setRequired(true).setLabel(new Model<String>("Text")); FormComponentLabel textLabel = new FormComponentLabel("textLabel", text); form.add(textLabel); form.add(text); } public EntrySubmitObserver getEntrySubmitObserver() { return entrySubmitObserver; } public void setEntrySubmitObserver(EntrySubmitObserver entrySubmitObserver) { this.entrySubmitObserver = entrySubmitObserver; } }