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.model.IModel;
025: import org.apache.wicket.util.string.AppendingStringBuffer;
026:
027: /**
028: * An ajax submit button that will degrade to a normal request if ajax is not
029: * available or javascript is disabled.
030: *
031: * @since 1.3
032: *
033: * @author Jeremy Thomerson (jthomerson)
034: * @author Alastair Maw
035: */
036: public abstract class AjaxFallbackButton extends Button {
037:
038: private static final long serialVersionUID = 1L;
039:
040: private final Form mForm;
041:
042: /**
043: * Construct.
044: *
045: * @param id
046: * @param form
047: */
048: public AjaxFallbackButton(String id, Form form) {
049: this (id, null, form);
050: }
051:
052: /**
053: * Construct.
054: *
055: * @param id
056: * @param model
057: * @param form
058: */
059: public AjaxFallbackButton(String id, IModel model, Form form) {
060: super (id, model);
061: mForm = form;
062:
063: add(new AjaxFormSubmitBehavior(form, "onclick") {
064:
065: private static final long serialVersionUID = 1L;
066:
067: protected void onSubmit(AjaxRequestTarget target) {
068: AjaxFallbackButton.this .onSubmit(target, mForm);
069: }
070:
071: protected void onError(AjaxRequestTarget target) {
072: AjaxFallbackButton.this .onError(target, mForm);
073: }
074:
075: protected CharSequence getEventHandler() {
076: return new AppendingStringBuffer(super
077: .getEventHandler()).append("; return false;");
078: }
079:
080: protected IAjaxCallDecorator getAjaxCallDecorator() {
081: return AjaxFallbackButton.this .getAjaxCallDecorator();
082: }
083:
084: });
085: }
086:
087: /**
088: * Listener method invoked on form submit with errors
089: *
090: * @param target
091: * @param form
092: *
093: * TODO 1.3: Make abstract to be consistent with onsubmit()
094: */
095: protected void onError(AjaxRequestTarget target, Form form) {
096: // created to override
097: }
098:
099: /**
100: * @see org.apache.wicket.markup.html.form.IFormSubmittingComponent#onSubmit()
101: */
102: public void onSubmit() {
103: if (!(getRequestCycle().getRequestTarget() instanceof AjaxRequestTarget)) {
104: onSubmit(null, getForm());
105: }
106: }
107:
108: public Form getForm() {
109: return mForm == null ? super .getForm() : mForm;
110: }
111:
112: /**
113: * Callback for the onClick event. If ajax failed and this event was
114: * generated via a normal submission, the target argument will be null
115: *
116: * @param target
117: * ajax target if this linked was invoked using ajax, null
118: * otherwise
119: * @param form
120: */
121: protected abstract void onSubmit(final AjaxRequestTarget target,
122: final Form form);
123:
124: protected IAjaxCallDecorator getAjaxCallDecorator() {
125: return null;
126: }
127:
128: /**
129: * Helper methods that both checks whether the link is enabled and whether
130: * the action ENABLE is allowed.
131: *
132: * @return whether the link should be rendered as enabled
133: */
134: protected final boolean isButtonEnabled() {
135: return isEnabled() && isEnableAllowed();
136: }
137: }
|