|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| Checkbox.java | 100% | 93.8% | 66.7% | 91.3% |
|
||||||||||||||
| 1 | // Copyright 2004, 2005 The Apache Software Foundation | |
| 2 | // | |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 | // you may not use this file except in compliance with the License. | |
| 5 | // You may obtain a copy of the License at | |
| 6 | // | |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 | // | |
| 9 | // Unless required by applicable law or agreed to in writing, software | |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 | // See the License for the specific language governing permissions and | |
| 13 | // limitations under the License. | |
| 14 | ||
| 15 | package org.apache.tapestry.form; | |
| 16 | ||
| 17 | import org.apache.tapestry.IMarkupWriter; | |
| 18 | import org.apache.tapestry.IRequestCycle; | |
| 19 | import org.apache.tapestry.valid.ValidatorException; | |
| 20 | ||
| 21 | /** | |
| 22 | * Implements a component that manages an HTML <input type=checkbox> form element. [ <a | |
| 23 | * href="../../../../../ComponentReference/Checkbox.html">Component Reference </a>] | |
| 24 | * <p> | |
| 25 | * As of 4.0, this component can be validated. | |
| 26 | * | |
| 27 | * @author Howard Lewis Ship | |
| 28 | * @author Paul Ferraro | |
| 29 | */ | |
| 30 | public abstract class Checkbox extends AbstractFormComponent implements ValidatableField | |
| 31 | { | |
| 32 | /** | |
| 33 | * @see org.apache.tapestry.form.validator.AbstractRequirableField#renderRequirableFormComponent(org.apache.tapestry.IMarkupWriter, | |
| 34 | * org.apache.tapestry.IRequestCycle) | |
| 35 | */ | |
| 36 | 12 | protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle) |
| 37 | { | |
| 38 | 12 | writer.beginEmpty("input"); |
| 39 | 12 | writer.attribute("type", "checkbox"); |
| 40 | ||
| 41 | 12 | writer.attribute("name", getName()); |
| 42 | ||
| 43 | 12 | if (isDisabled()) |
| 44 | 3 | writer.attribute("disabled", "disabled"); |
| 45 | ||
| 46 | 12 | if (getValue()) |
| 47 | 9 | writer.attribute("checked", "checked"); |
| 48 | ||
| 49 | 12 | renderIdAttribute(writer, cycle); |
| 50 | ||
| 51 | 12 | renderInformalParameters(writer, cycle); |
| 52 | ||
| 53 | 12 | writer.closeTag(); |
| 54 | } | |
| 55 | ||
| 56 | /** | |
| 57 | * In traditional HTML, many checkboxes would have the same name but different values. Under | |
| 58 | * Tapestry, it makes more sense to have different names and a fixed value. For a checkbox, we | |
| 59 | * only care about whether the name appears as a request parameter. | |
| 60 | */ | |
| 61 | 9 | protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle) |
| 62 | { | |
| 63 | 9 | String value = cycle.getParameter(getName()); |
| 64 | ||
| 65 | 9 | try |
| 66 | { | |
| 67 | // This is atypical validation - since this component does not explicitly bind to an object | |
| 68 | 9 | getValidatableFieldSupport().validate(this, writer, cycle, value); |
| 69 | ||
| 70 | 6 | setValue(value != null); |
| 71 | } | |
| 72 | catch (ValidatorException e) | |
| 73 | { | |
| 74 | 3 | getForm().getDelegate().record(e); |
| 75 | } | |
| 76 | } | |
| 77 | ||
| 78 | public abstract boolean getValue(); | |
| 79 | ||
| 80 | public abstract void setValue(boolean selected); | |
| 81 | ||
| 82 | /** | |
| 83 | * Injected. | |
| 84 | */ | |
| 85 | public abstract ValidatableFieldSupport getValidatableFieldSupport(); | |
| 86 | ||
| 87 | /** | |
| 88 | * @see org.apache.tapestry.form.AbstractFormComponent#isRequired() | |
| 89 | */ | |
| 90 | 0 | public boolean isRequired() |
| 91 | { | |
| 92 | 0 | return getValidatableFieldSupport().isRequired(this); |
| 93 | } | |
| 94 | } |
|
||||||||||