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 org.apache.wicket.core.util.string.CssUtils; 020import org.apache.wicket.markup.ComponentTag; 021import org.apache.wicket.markup.html.WebMarkupContainer; 022import org.apache.wicket.markup.parser.XmlTag.TagType; 023import org.apache.wicket.util.lang.Args; 024 025/** 026 * A component that represents HTML <label> tag. This component will automatically make the 027 * form component output an <em>id</em> attribute and link its <em>for</em> attribute with that 028 * value. 029 * 030 * @author Igor Vaynberg (ivaynberg) 031 */ 032public class FormComponentLabel extends WebMarkupContainer 033{ 034 private static final long serialVersionUID = 1L; 035 036 public static final String REQUIRED_CSS_CLASS_KEY = CssUtils.key(FormComponentLabel.class, 037 "required"); 038 039 public static final String INVALID_CSS_CLASS_KEY = CssUtils.key(FormComponentLabel.class, 040 "invalid"); 041 042 public static final String DISABLED_CSS_CLASS_KEY = CssUtils.key(FormComponentLabel.class, 043 "disabled"); 044 045 private final LabeledWebMarkupContainer component; 046 047 /** 048 * Constructor 049 * 050 * @param id 051 * component id 052 * @param component 053 * component that this label is linked to 054 */ 055 public FormComponentLabel(String id, LabeledWebMarkupContainer component) 056 { 057 super(id); 058 059 this.component = Args.notNull(component, "component"); 060 component.setOutputMarkupId(true); 061 } 062 063 /** 064 * 065 * @see org.apache.wicket.Component#onComponentTag(org.apache.wicket.markup.ComponentTag) 066 */ 067 @Override 068 protected void onComponentTag(ComponentTag tag) 069 { 070 super.onComponentTag(tag); 071 072 checkComponentTag(tag, "label"); 073 074 LabeledWebMarkupContainer formComponent = getFormComponent(); 075 076 tag.put("for", formComponent.getMarkupId()); 077 078 if (formComponent instanceof FormComponent<?>) 079 { 080 FormComponent<?> fc = (FormComponent<?>) formComponent; 081 082 if (fc.isRequired()) 083 { 084 tag.append("class", getString(REQUIRED_CSS_CLASS_KEY), " "); 085 } 086 if (fc.isValid() == false) 087 { 088 tag.append("class", getString(INVALID_CSS_CLASS_KEY), " "); 089 } 090 } 091 092 if (formComponent.isEnabledInHierarchy() == false) 093 { 094 tag.append("class", getString(DISABLED_CSS_CLASS_KEY), " "); 095 } 096 097 // always transform the tag to <span></span> so even labels defined as <span/> render 098 tag.setType(TagType.OPEN); 099 } 100 101 /** 102 * Returns LabeledWebMarkupContainer bound to this label. This will be a FormComponent, a Radio 103 * or a Check. 104 * 105 * @return form component 106 */ 107 public LabeledWebMarkupContainer getFormComponent() 108 { 109 return component; 110 } 111}