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.basic; 018 019import java.io.Serializable; 020 021import org.apache.wicket.markup.ComponentTag; 022import org.apache.wicket.markup.MarkupStream; 023import org.apache.wicket.markup.html.WebComponent; 024import org.apache.wicket.markup.parser.XmlTag.TagType; 025import org.apache.wicket.model.IModel; 026import org.apache.wicket.model.Model; 027 028/** 029 * A Label component replaces its body with the String version of its model object returned by 030 * getModelObjectAsString(). 031 * <p> 032 * Exactly what is displayed as the body, depends on the model. The simplest case is a Label with a 033 * static String model, which can be constructed like this: 034 * 035 * <pre> 036 * add(new Label("myLabel", "the string to display")) 037 * </pre> 038 * 039 * A Label with a dynamic model can be created like this: 040 * 041 * <pre> 042 * 043 * add(new Label("myLabel", new PropertyModel(person, "name")); 044 * 045 * </pre> 046 * 047 * In this case, the Label component will replace the body of the tag it is attached to with the 048 * 'name' property of the given Person object, where Person might look like: 049 * 050 * <pre> 051 * public class Person 052 * { 053 * private String name; 054 * 055 * public String getName() 056 * { 057 * return name; 058 * } 059 * 060 * public void setName(String name) 061 * { 062 * this.name = name; 063 * } 064 * } 065 * </pre> 066 * 067 * @author Jonathan Locke 068 */ 069public class Label extends WebComponent 070{ 071 private static final long serialVersionUID = 1L; 072 073 /** 074 * Constructor 075 * 076 * @param id 077 * See Component 078 */ 079 public Label(final String id) 080 { 081 super(id); 082 } 083 084 /** 085 * Convenience constructor. Same as Label(String, Model.of(Serializable)) 086 * 087 * @param id 088 * See Component 089 * @param label 090 * The label text or object, converted to a string via the {@link org.apache.wicket.util.convert.IConverter}. 091 * 092 * @see org.apache.wicket.Component#Component(String, IModel) 093 */ 094 public Label(final String id, Serializable label) 095 { 096 this(id, Model.of(label)); 097 } 098 099 /** 100 * @param id 101 * @param model 102 * @see org.apache.wicket.Component#Component(String, IModel) 103 */ 104 public Label(final String id, IModel<?> model) 105 { 106 super(id, model); 107 } 108 109 /** 110 * {@inheritDoc} 111 */ 112 @Override 113 public void onComponentTagBody(final MarkupStream markupStream, final ComponentTag openTag) 114 { 115 replaceComponentTagBody(markupStream, openTag, getDefaultModelObjectAsString()); 116 } 117 118 /** 119 * {@inheritDoc} 120 */ 121 @Override 122 protected void onComponentTag(ComponentTag tag) 123 { 124 super.onComponentTag(tag); 125 126 if (tag.isOpenClose()) 127 { 128 // always transform the tag to <span></span> so even labels defined as <span/> render 129 tag.setType(TagType.OPEN); 130 } 131 } 132}