001// Copyright 2006-2013 The Apache Software Foundation 002// 003// Licensed under the Apache License, Version 2.0 (the "License"); 004// you may not use this file except in compliance with the License. 005// You may obtain a copy of the License at 006// 007// http://www.apache.org/licenses/LICENSE-2.0 008// 009// Unless required by applicable law or agreed to in writing, software 010// distributed under the License is distributed on an "AS IS" BASIS, 011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012// See the License for the specific language governing permissions and 013// limitations under the License. 014 015package org.apache.tapestry5.corelib.components; 016 017import org.apache.tapestry5.MarkupWriter; 018import org.apache.tapestry5.annotations.AfterRender; 019import org.apache.tapestry5.annotations.BeginRender; 020import org.apache.tapestry5.annotations.Mixin; 021import org.apache.tapestry5.annotations.Parameter; 022import org.apache.tapestry5.corelib.base.AbstractField; 023import org.apache.tapestry5.corelib.mixins.RenderDisabled; 024 025/** 026 * A Checkbox component is simply a <input type="checkbox">. 027 * 028 * @tapestrydoc 029 */ 030public class Checkbox extends AbstractField 031{ 032 /** 033 * The value to be read or updated. If not bound, the Checkbox will attempt to edit a property of its container 034 * whose name matches the component's id. 035 */ 036 @Parameter(required = true, autoconnect = true) 037 private boolean value; 038 039 @SuppressWarnings("unused") 040 @Mixin 041 private RenderDisabled renderDisabled; 042 043 @BeginRender 044 void begin(MarkupWriter writer) 045 { 046 String asSubmitted = validationTracker.getInput(this); 047 048 boolean checked = asSubmitted != null ? Boolean.parseBoolean(asSubmitted) : value; 049 050 writer.element("input", "type", "checkbox", 051 "name", getControlName(), 052 "id", getClientId(), 053 "checked", checked ? "checked" : null); 054 055 resources.renderInformalParameters(writer); 056 057 decorateInsideField(); 058 } 059 060 @AfterRender 061 void after(MarkupWriter writer) 062 { 063 writer.end(); // input 064 } 065 066 @Override 067 protected void processSubmission(String controlName) 068 { 069 String postedValue = request.getParameter(controlName); 070 071 // record as "true" or "false" 072 073 validationTracker.recordInput(this, Boolean.toString(postedValue != null)); 074 075 value = postedValue != null; 076 } 077}