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.markup.ComponentTag; 020import org.apache.wicket.markup.MarkupStream; 021 022/** 023 * A form component label that replaces its body with the contents of 024 * {@link LabeledWebMarkupContainer#getLabel() labelProvider's label} 025 * 026 * @author Igor Vaynberg (ivaynberg) 027 */ 028public class SimpleFormComponentLabel extends FormComponentLabel 029{ 030 private static final long serialVersionUID = 1L; 031 032 /** 033 * Construct. 034 * 035 * @param id 036 * component id 037 * @param labelProvider 038 * label provider (e.g. FormComponent, Radio, Check) 039 */ 040 public SimpleFormComponentLabel(String id, LabeledWebMarkupContainer labelProvider) 041 { 042 super(id, labelProvider); 043 if (labelProvider.getLabel() == null) 044 { 045 throw new IllegalStateException("Provided form component does not have a label set. " 046 + "Use FormComponent.setLabel(IModel) to set the model " 047 + "that will feed this label"); 048 } 049 setDefaultModel(labelProvider.getLabel()); 050 } 051 052 /** 053 * @see org.apache.wicket.Component#onComponentTagBody(org.apache.wicket.markup.MarkupStream, 054 * org.apache.wicket.markup.ComponentTag) 055 */ 056 @Override 057 public void onComponentTagBody(final MarkupStream markupStream, final ComponentTag openTag) 058 { 059 replaceComponentTagBody(markupStream, openTag, getDefaultModelObjectAsString()); 060 } 061 062}