/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.wicket.markup.html.form;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.model.IModel;
import org.apache.wicket.version.undo.Change;
/**
* A form button.
* <p>
* Within a form, you can nest Button components. Note that you don't have to do
* this to let the form work (a simple <input type="submit".. suffices), but
* if you want to have different kinds of submit behavior it might be a good
* idea to use Buttons.
* </p>
* <p>
* The model property is used to set the "value" attribute. It will
* thus be the label of the button that shows up for end users. If you want the
* attribute to keep it's markup attribute value, don't provide a model, or let
* it return an empty string.
* </p>
* <p>
* When you add a Wicket Button to a form, and that button is clicked, by
* default the button's onSubmit method is called first, and after that the
* form's onSubmit method is called. If you want to change this (e.g. you don't
* want to call the form's onSubmit method, or you want it called before the
* button's onSubmit method), you can override Form.delegateSubmit.
* </p>
* <p>
* One other option you should know of is the 'defaultFormProcessing' property
* of Button components. When you set this to false (default is true), all
* validation and formupdating is bypassed and the onSubmit method of that
* button is called directly, and the onSubmit method of the parent form is not
* called. A common use for this is to create a cancel button.
* </p>
*
* @author Jonathan Locke
* @author Eelco Hillenius
*/
public class Button extends FormComponent implements IFormSubmittingComponent
{
private static final long serialVersionUID = 1L;
/**
* If false, all standard processing like validating and model updating is
* skipped.
*/
private boolean defaultFormProcessing = true;
/**
* Constructor without a model. Buttons without models leave the markup
* attribute "value". Provide a model if you want to set the
* button's label dynamically.
*
* @see org.apache.wicket.Component#Component(String)
*/
public Button(String id)
{
super(id);
setVersioned(true);
}
/**
* Constructor taking an model for rendering the 'label' of the button (the
* value attribute of the input/button tag). Use a
* {@link org.apache.wicket.model.StringResourceModel} for a localized value.
*
* @param id
* Component id
* @param model
* The model property is used to set the "value"
* attribute. It will thus be the label of the button that shows
* up for end users. If you want the attribute to keep it's
* markup attribute value, don't provide a model, or let it
* return an empty string.
*/
public Button(final String id, final IModel model)
{
super(id, model);
setVersioned(true);
}
/**
* Override of the default initModel behaviour. This component <strong>will
* not</strong> use any compound model a parent, but only a model that is
* explicitly set.
*
* @see org.apache.wicket.Component#initModel()
*/
protected IModel initModel()
{
return null;
}
/**
* Gets the defaultFormProcessing property. When false (default is true),
* all validation and formupdating is bypassed and the onSubmit method of
* that button is called directly, and the onSubmit method of the parent
* form is not called. A common use for this is to create a cancel button.
*
* @return defaultFormProcessing
*/
public final boolean getDefaultFormProcessing()
{
return defaultFormProcessing;
}
/**
* Sets the defaultFormProcessing property. When false (default is true),
* all validation and form updating is bypassed and the onSubmit method of
* that button is called directly, and the onSubmit method of the parent
* form is not called. A common use for this is to create a cancel button.
*
* @param defaultFormProcessing
* defaultFormProcessing
* @return This
*/
public final Button setDefaultFormProcessing(boolean defaultFormProcessing)
{
if (this.defaultFormProcessing != defaultFormProcessing)
{
addStateChange(new Change()
{
private static final long serialVersionUID = 1L;
boolean formerValue = Button.this.defaultFormProcessing;
public void undo()
{
Button.this.defaultFormProcessing = formerValue;
}
public String toString()
{
return "DefaultFormProcessingChange[component: " + getPath()
+ ", default processing: " + formerValue + "]";
}
});
}
this.defaultFormProcessing = defaultFormProcessing;
return this;
}
/**
* This method does nothing, as any model of a button is only used to
* display the button's label (by setting it's markup attribute
* "value").
*
* @see org.apache.wicket.markup.html.form.FormComponent#updateModel()
*/
public void updateModel()
{
}
/**
* Gets any script that should rendered as the "onclick" attribute
* of the button. Returns null by default, override this method to provide
* any script.
*
* @return Any onClick JavaScript that should be used, returns null by
* default
*/
protected String getOnClickScript()
{
return null;
}
/**
* Processes the component tag. A <tt>value</tt> attribute is added with
* the value of the model object, if available. An <tt>onclick</tt>
* attribute is added if the subclass specified javascript.
*
* <p>
* <b>NOTE</b>. For a <tt><button></tt> the <tt>value</tt>
* attribute is not rendered, markup needs to be added within the button
* to display the button's label.
* </p>
*
* @param tag
* Tag to modify
* @see org.apache.wicket.Component#onComponentTag(ComponentTag)
*/
protected void onComponentTag(final ComponentTag tag)
{
// Default handling for component tag
super.onComponentTag(tag);
String name = tag.getName();
try
{
String value = getModelObjectAsString();
if (value != null && !"".equals(value))
{
tag.put("value", value);
}
}
catch (Exception e)
{
// ignore.
}
// If the subclass specified javascript, use that
final String onClickJavaScript = getOnClickScript();
if (onClickJavaScript != null)
{
tag.put("onclick", onClickJavaScript);
}
}
/**
* Override this method to provide special submit handling in a multi-button
* form. It is called whenever the user clicks this particular button.
*/
public void onSubmit()
{
}
}