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 */ 017package org.apache.wicket.markup.html.form; 018 019import java.util.Locale; 020 021import org.apache.wicket.markup.ComponentTag; 022import org.apache.wicket.model.IModel; 023import org.apache.wicket.util.convert.IConverter; 024 025/** 026 * HTML checkbox input component. 027 * <p> 028 * Java: 029 * 030 * <pre> 031 * form.add(new CheckBox("bool")); 032 * </pre> 033 * 034 * HTML: 035 * 036 * <pre> 037 * <input type="checkbox" wicket:id="bool" /> 038 * </pre> 039 * 040 * </p> 041 * <p> 042 * A CheckBox always has a valid therefore values from methods 043 * {@link FormComponent#setRequired(boolean)} and {@link FormComponent#isRequired()} are not taken 044 * into account. 045 * </p> 046 * 047 * @author Jonathan Locke 048 */ 049public class CheckBox extends FormComponent<Boolean> 050{ 051 private static final long serialVersionUID = 1L; 052 053 /** 054 * @see org.apache.wicket.Component#Component(String) 055 */ 056 public CheckBox(final String id) 057 { 058 this(id, null); 059 } 060 061 /** 062 * @param id 063 * @param model 064 * @see org.apache.wicket.Component#Component(String, IModel) 065 */ 066 public CheckBox(final String id, IModel<Boolean> model) 067 { 068 super(id, model); 069 setType(Boolean.class); 070 } 071 072 /** 073 * Processes the component tag. 074 * 075 * @param tag 076 * Tag to modify 077 * @see org.apache.wicket.Component#onComponentTag(ComponentTag) 078 */ 079 @Override 080 protected void onComponentTag(final ComponentTag tag) 081 { 082 checkComponentTag(tag, "input"); 083 checkComponentTagAttribute(tag, "type", "checkbox"); 084 085 final String value = getValue(); 086 final IConverter<Boolean> converter = getConverter(Boolean.class); 087 final Boolean checked = converter.convertToObject(value, getLocale()); 088 089 if (Boolean.TRUE.equals(checked)) 090 { 091 tag.put("checked", "checked"); 092 } 093 else 094 { 095 // In case the attribute was added at design time 096 tag.remove("checked"); 097 } 098 099 // remove value attribute, because it overrides the browser's submitted value, eg a [input 100 // type="checkbox" value=""] will always submit as false 101 tag.remove("value"); 102 103 super.onComponentTag(tag); 104 } 105 106 @Override 107 protected IConverter<?> createConverter(Class<?> type) 108 { 109 if (Boolean.class.equals(type)) 110 { 111 return CheckBoxConverter.INSTANCE; 112 } 113 return null; 114 } 115 116 /** 117 * Converter specific to the check box 118 * 119 * @author igor.vaynberg 120 */ 121 private static class CheckBoxConverter implements IConverter<Boolean> 122 { 123 private static final long serialVersionUID = 1L; 124 125 private static final IConverter<Boolean> INSTANCE = new CheckBoxConverter(); 126 127 /** 128 * Constructor 129 */ 130 private CheckBoxConverter() 131 { 132 133 } 134 135 /** 136 * @see org.apache.wicket.util.convert.IConverter#convertToObject(java.lang.String, 137 * java.util.Locale) 138 */ 139 @Override 140 public Boolean convertToObject(String value, Locale locale) 141 { 142 if ("on".equals(value) || "true".equals(value)) 143 { 144 return Boolean.TRUE; 145 } 146 else 147 { 148 return Boolean.FALSE; 149 } 150 } 151 152 /** 153 * @see org.apache.wicket.util.convert.IConverter#convertToString(java.lang.Object, 154 * java.util.Locale) 155 */ 156 @Override 157 public String convertToString(Boolean value, Locale locale) 158 { 159 return value.toString(); 160 } 161 } 162 163 @Override 164 public boolean checkRequired() 165 { 166 // a checkbox always has a value so this check always passes 167 return true; 168 } 169 170}