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.markup.html.form;
018:
019: import org.apache.wicket.ajax.AjaxRequestTarget;
020: import org.apache.wicket.ajax.IAjaxCallDecorator;
021: import org.apache.wicket.ajax.form.AjaxFormSubmitBehavior;
022: import org.apache.wicket.markup.html.form.Button;
023: import org.apache.wicket.markup.html.form.Form;
024: import org.apache.wicket.util.string.AppendingStringBuffer;
025:
026: /**
027: * A button that submits the form via ajax. Since this button takes the form as
028: * a constructor argument it does not need to be added to it unlike the
029: * {@link Button} component.
030: *
031: * @since 1.3
032: *
033: * @author Igor Vaynberg (ivaynberg)
034: */
035: public abstract class AjaxButton extends Button {
036: private static final long serialVersionUID = 1L;
037:
038: private final Form form;
039:
040: /**
041: * Construct.
042: *
043: * @param id
044: */
045: public AjaxButton(String id) {
046: this (id, null);
047: }
048:
049: /**
050: * Returns the form if it was set in constructor, otherwise returns the
051: * form nearest in parent hierarchy.
052: * @see org.apache.wicket.markup.html.form.FormComponent#getForm()
053: */
054: public Form getForm() {
055: if (form != null) {
056: return form;
057: } else {
058: return super .getForm();
059: }
060: }
061:
062: /**
063: * Construct.
064: *
065: * @param id
066: * @param form
067: */
068: public AjaxButton(String id, final Form form) {
069: super (id);
070: this .form = form;
071:
072: add(new AjaxFormSubmitBehavior(form, "onclick") {
073:
074: private static final long serialVersionUID = 1L;
075:
076: protected void onSubmit(AjaxRequestTarget target) {
077: AjaxButton.this .onSubmit(target, form);
078: }
079:
080: protected void onError(AjaxRequestTarget target) {
081: AjaxButton.this .onError(target, form);
082: }
083:
084: protected CharSequence getEventHandler() {
085: return new AppendingStringBuffer(super
086: .getEventHandler()).append("; return false;");
087: }
088:
089: protected IAjaxCallDecorator getAjaxCallDecorator() {
090: return AjaxButton.this .getAjaxCallDecorator();
091: }
092:
093: });
094:
095: }
096:
097: /**
098: * Returns the {@link IAjaxCallDecorator} that will be used to modify the
099: * generated javascript. This is the preferred way of changing the
100: * javascript in the onclick handler
101: *
102: * @return call decorator used to modify the generated javascript or null
103: * for none
104: */
105: protected IAjaxCallDecorator getAjaxCallDecorator() {
106: return null;
107: }
108:
109: /**
110: * Listener method invoked on form submit with no errors
111: *
112: * @param target
113: * @param form
114: */
115: protected abstract void onSubmit(AjaxRequestTarget target, Form form);
116:
117: /**
118: * Listener method invoked on form submit with errors
119: *
120: * @param target
121: * @param form
122: *
123: * TODO 1.3: Make abstract to be consistent with onSubmit()
124: */
125: protected void onError(AjaxRequestTarget target, Form form) {
126:
127: }
128:
129: }
|