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.markup.html.form;
018:
019: import org.apache.wicket.markup.ComponentTag;
020: import org.apache.wicket.model.IModel;
021: import org.apache.wicket.version.undo.Change;
022:
023: /**
024: * A form button.
025: * <p>
026: * Within a form, you can nest Button components. Note that you don't have to do
027: * this to let the form work (a simple <input type="submit".. suffices), but
028: * if you want to have different kinds of submit behavior it might be a good
029: * idea to use Buttons.
030: * </p>
031: * <p>
032: * The model property is used to set the "value" attribute. It will
033: * thus be the label of the button that shows up for end users. If you want the
034: * attribute to keep it's markup attribute value, don't provide a model, or let
035: * it return an empty string.
036: * </p>
037: * <p>
038: * When you add a Wicket Button to a form, and that button is clicked, by
039: * default the button's onSubmit method is called first, and after that the
040: * form's onSubmit method is called. If you want to change this (e.g. you don't
041: * want to call the form's onSubmit method, or you want it called before the
042: * button's onSubmit method), you can override Form.delegateSubmit.
043: * </p>
044: * <p>
045: * One other option you should know of is the 'defaultFormProcessing' property
046: * of Button components. When you set this to false (default is true), all
047: * validation and formupdating is bypassed and the onSubmit method of that
048: * button is called directly, and the onSubmit method of the parent form is not
049: * called. A common use for this is to create a cancel button.
050: * </p>
051: *
052: * @author Jonathan Locke
053: * @author Eelco Hillenius
054: */
055: public class Button extends FormComponent implements
056: IFormSubmittingComponent {
057: private static final long serialVersionUID = 1L;
058:
059: /**
060: * If false, all standard processing like validating and model updating is
061: * skipped.
062: */
063: private boolean defaultFormProcessing = true;
064:
065: /**
066: * Constructor without a model. Buttons without models leave the markup
067: * attribute "value". Provide a model if you want to set the
068: * button's label dynamically.
069: *
070: * @see org.apache.wicket.Component#Component(String)
071: */
072: public Button(String id) {
073: super (id);
074: setVersioned(true);
075: setOutputMarkupId(true);
076: }
077:
078: /**
079: * Constructor taking an model for rendering the 'label' of the button (the
080: * value attribute of the input/button tag). Use a
081: * {@link org.apache.wicket.model.StringResourceModel} for a localized value.
082: *
083: * @param id
084: * Component id
085: * @param model
086: * The model property is used to set the "value"
087: * attribute. It will thus be the label of the button that shows
088: * up for end users. If you want the attribute to keep it's
089: * markup attribute value, don't provide a model, or let it
090: * return an empty string.
091: */
092: public Button(final String id, final IModel model) {
093: super (id, model);
094: setVersioned(true);
095: setOutputMarkupId(true);
096: }
097:
098: /**
099: * Override of the default initModel behaviour. This component <strong>will
100: * not</strong> use any compound model a parent, but only a model that is
101: * explicitly set.
102: *
103: * @see org.apache.wicket.Component#initModel()
104: */
105: protected IModel initModel() {
106: return null;
107: }
108:
109: /**
110: * Gets the defaultFormProcessing property. When false (default is true),
111: * all validation and formupdating is bypassed and the onSubmit method of
112: * that button is called directly, and the onSubmit method of the parent
113: * form is not called. A common use for this is to create a cancel button.
114: *
115: * @return defaultFormProcessing
116: */
117: public final boolean getDefaultFormProcessing() {
118: return defaultFormProcessing;
119: }
120:
121: /**
122: * Sets the defaultFormProcessing property. When false (default is true),
123: * all validation and form updating is bypassed and the onSubmit method of
124: * that button is called directly, and the onSubmit method of the parent
125: * form is not called. A common use for this is to create a cancel button.
126: *
127: * @param defaultFormProcessing
128: * defaultFormProcessing
129: * @return This
130: */
131: public final Button setDefaultFormProcessing(
132: boolean defaultFormProcessing) {
133: if (this .defaultFormProcessing != defaultFormProcessing) {
134: addStateChange(new Change() {
135: private static final long serialVersionUID = 1L;
136:
137: boolean formerValue = Button.this .defaultFormProcessing;
138:
139: public void undo() {
140: Button.this .defaultFormProcessing = formerValue;
141: }
142:
143: public String toString() {
144: return "DefaultFormProcessingChange[component: "
145: + getPath() + ", default processing: "
146: + formerValue + "]";
147: }
148: });
149: }
150:
151: this .defaultFormProcessing = defaultFormProcessing;
152: return this ;
153: }
154:
155: /**
156: * This method does nothing, as any model of a button is only used to
157: * display the button's label (by setting it's markup attribute
158: * "value").
159: *
160: * @see org.apache.wicket.markup.html.form.FormComponent#updateModel()
161: */
162: public void updateModel() {
163: }
164:
165: /**
166: * Gets any script that should rendered as the "onclick" attribute
167: * of the button. Returns null by default, override this method to provide
168: * any script.
169: *
170: * @return Any onClick JavaScript that should be used, returns null by
171: * default
172: */
173: protected String getOnClickScript() {
174: return null;
175: }
176:
177: /**
178: * Processes the component tag. A <tt>value</tt> attribute is added with
179: * the value of the model object, if available. An <tt>onclick</tt>
180: * attribute is added if the subclass specified javascript.
181: *
182: * <p>
183: * <b>NOTE</b>. For a <tt><button></tt> the <tt>value</tt>
184: * attribute is not rendered, markup needs to be added within the button
185: * to display the button's label.
186: * </p>
187: *
188: * @param tag
189: * Tag to modify
190: * @see org.apache.wicket.Component#onComponentTag(ComponentTag)
191: */
192: protected void onComponentTag(final ComponentTag tag) {
193: // Default handling for component tag
194: super .onComponentTag(tag);
195:
196: String name = tag.getName();
197:
198: try {
199: String value = getModelObjectAsString();
200: if (value != null && !"".equals(value)) {
201: tag.put("value", value);
202: }
203: } catch (Exception e) {
204: // ignore.
205: }
206:
207: // If the subclass specified javascript, use that
208: final String onClickJavaScript = getOnClickScript();
209: if (onClickJavaScript != null) {
210: tag.put("onclick", onClickJavaScript);
211: }
212: }
213:
214: /**
215: * Override this method to provide special submit handling in a multi-button
216: * form. It is called whenever the user clicks this particular button.
217: */
218: public void onSubmit() {
219: }
220: }
|