001: /*
002: * $Id: AjaxSubmitButton.java 5125 2006-03-25 11:42:10 -0800 (Sat, 25 Mar 2006)
003: * ivaynberg $ $Revision: 5155 $ $Date: 2006-03-25 11:42:10 -0800 (Sat, 25 Mar
004: * 2006) $
005: *
006: * ==============================================================================
007: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
008: * use this file except in compliance with the License. You may obtain a copy of
009: * the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
015: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
016: * License for the specific language governing permissions and limitations under
017: * the License.
018: */
019: package wicket.ajax.markup.html.form;
020:
021: import wicket.ajax.AjaxRequestTarget;
022: import wicket.ajax.IAjaxCallDecorator;
023: import wicket.ajax.form.AjaxFormSubmitBehavior;
024: import wicket.markup.ComponentTag;
025: import wicket.markup.html.WebMarkupContainer;
026: import wicket.markup.html.form.Form;
027: import wicket.util.string.AppendingStringBuffer;
028:
029: /**
030: * A link that submits a form via ajax. Since this link takes the form as a
031: * constructor argument it does not need to be inside form's component
032: * hierarchy.
033: *
034: * @since 1.2
035: *
036: * @author Igor Vaynberg (ivaynberg)
037: */
038: public abstract class AjaxSubmitLink extends WebMarkupContainer {
039: private static final long serialVersionUID = 1L;
040:
041: /**
042: * Construct.
043: *
044: * @param id
045: * @param form
046: */
047: public AjaxSubmitLink(String id, final Form form) {
048: super (id);
049:
050: form.setOutputMarkupId(true);
051:
052: add(new AjaxFormSubmitBehavior(form, "onclick") {
053:
054: private static final long serialVersionUID = 1L;
055:
056: protected void onSubmit(AjaxRequestTarget target) {
057: AjaxSubmitLink.this .onSubmit(target, form);
058: }
059:
060: protected void onError(AjaxRequestTarget target) {
061: AjaxSubmitLink.this .onError(target, form);
062: }
063:
064: protected CharSequence getEventHandler() {
065: return new AppendingStringBuffer(super
066: .getEventHandler()).append("; return false;");
067: }
068:
069: protected IAjaxCallDecorator getAjaxCallDecorator() {
070: return AjaxSubmitLink.this .getAjaxCallDecorator();
071: }
072: });
073:
074: }
075:
076: /**
077: * Returns the {@link IAjaxCallDecorator} that will be used to modify the
078: * generated javascript. This is the preferred way of changing the
079: * javascript in the onclick handler
080: *
081: * @return call decorator used to modify the generated javascript or null
082: * for none
083: */
084: protected IAjaxCallDecorator getAjaxCallDecorator() {
085: return null;
086: }
087:
088: protected void onComponentTag(ComponentTag tag) {
089: super .onComponentTag(tag);
090: checkComponentTag(tag, "a");
091: tag.put("href", "#");
092: }
093:
094: /**
095: * Listener method invoked on form submit
096: *
097: * @param target
098: * @param form
099: */
100: protected abstract void onSubmit(AjaxRequestTarget target, Form form);
101:
102: /**
103: * Listener method invoked on form submit with errors
104: *
105: * @param target
106: * @param form
107: *
108: * TODO 1.3: Make abstract to be consistent with onsubmit()
109: */
110: protected void onError(AjaxRequestTarget target, Form form) {
111:
112: }
113:
114: }
|