001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.ajax.form;
018:
019: import org.apache.wicket.Component;
020: import org.apache.wicket.Page;
021: import org.apache.wicket.ajax.AjaxEventBehavior;
022: import org.apache.wicket.ajax.AjaxRequestTarget;
023: import org.apache.wicket.markup.html.form.Form;
024: import org.apache.wicket.markup.html.form.IFormSubmittingComponent;
025: import org.apache.wicket.util.string.AppendingStringBuffer;
026:
027: /**
028: * Ajax event behavior that submits a form via ajax when the event it is
029: * attached to is invoked.
030: * <p>
031: * The form must have an id attribute in the markup or have MarkupIdSetter
032: * added.
033: *
034: * @see AjaxEventBehavior
035: *
036: * @since 1.2
037: *
038: * @author Igor Vaynberg (ivaynberg)
039: *
040: */
041: public abstract class AjaxFormSubmitBehavior extends AjaxEventBehavior {
042: private static final long serialVersionUID = 1L;
043:
044: private Form form;
045:
046: /**
047: * Constructor. This constructor can only be used when the component this
048: * behavior is attached to is inside a form.
049: *
050: * @param event
051: * javascript event this behavior is attached to, like onclick
052: */
053: public AjaxFormSubmitBehavior(String event) {
054: this (null, event);
055: }
056:
057: /**
058: * Construct.
059: *
060: * @param form
061: * form that will be submitted
062: * @param event
063: * javascript event this behavior is attached to, like onclick
064: */
065: public AjaxFormSubmitBehavior(Form form, String event) {
066: super (event);
067: this .form = form;
068:
069: if (form != null) {
070: form.setOutputMarkupId(true);
071: }
072: }
073:
074: private Form getForm() {
075: if (form == null) {
076: // try to find form in the hierarchy of owning component
077: Component cursor = getComponent();
078: while (cursor != null && !(cursor instanceof Form)) {
079: cursor = cursor.getParent();
080: }
081: if (cursor == null) {
082: throw new IllegalStateException(
083: "form was not specified in the constructor and cannot "
084: + "be found in the hierarchy of the component this behavior "
085: + "is attached to");
086: } else {
087: form = (Form) cursor;
088: }
089: }
090: return form;
091: }
092:
093: protected CharSequence getEventHandler() {
094: final String formId = getForm().getMarkupId();
095: final CharSequence url = getCallbackUrl();
096:
097: AppendingStringBuffer call = new AppendingStringBuffer(
098: "wicketSubmitFormById('").append(formId).append("', '")
099: .append(url).append("', ");
100:
101: if (getComponent() instanceof IFormSubmittingComponent) {
102: call.append("'").append(
103: ((IFormSubmittingComponent) getComponent())
104: .getInputName()).append("' ");
105: } else {
106: call.append("null");
107: }
108:
109: return generateCallbackScript(call) + ";";
110: }
111:
112: protected void onEvent(AjaxRequestTarget target) {
113: getForm().onFormSubmitted();
114: if (!getForm().hasError()) {
115: onSubmit(target);
116: }
117: if (form.findParent(Page.class) != null) {
118: /*
119: * there can be cases when a form is replaced with another component
120: * in the onsubmit() handler of this behavior. in that case form no
121: * longer has a page and so calling .hasError on it will cause an
122: * exception, thus the check above.
123: */
124: if (getForm().hasError()) {
125: onError(target);
126: }
127: }
128: }
129:
130: /**
131: * Listener method that is invoked after the form has ben submitted and
132: * processed without errors
133: *
134: * @param target
135: */
136: protected abstract void onSubmit(AjaxRequestTarget target);
137:
138: /**
139: * Listener method invoked when the form has been processed and errors
140: * occured
141: *
142: * @param target
143: *
144: * TODO 1.3: make abstract to be consistent with onsubmit()
145: *
146: */
147: protected void onError(AjaxRequestTarget target) {
148:
149: }
150:
151: }
|