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 org.apache.wicket.markup.ComponentTag;
020import org.apache.wicket.markup.MarkupStream;
021import org.apache.wicket.markup.html.WebComponent;
022import org.apache.wicket.markup.parser.XmlTag.TagType;
023import org.apache.wicket.model.IModel;
024import org.apache.wicket.model.Model;
025import org.apache.wicket.util.string.Strings;
026
027/**
028 * A MultiLineLabel component replaces its body with the model object.
029 * <p>
030 * Unlike {@link Label}, {@link MultiLineLabel} shows text that spans multiple lines by inserting
031 * line breaks (<code>BR</code> tags) for newlines and paragraph markers (<code>P</code> tags) for
032 * sequences of more than one newline.
033 * 
034 * @author Jonathan Locke
035 */
036public class MultiLineLabel extends WebComponent
037{
038        private static final long serialVersionUID = 1L;
039
040        /**
041         * Constructor. Same as Label(String).
042         * 
043         * @param id
044         *            See Component
045         * @see Label#Label(String)
046         */
047        public MultiLineLabel(final String id)
048        {
049                super(id);
050        }
051
052        /**
053         * Convenience constructor. Same as MultiLineLabel(String, new Modell&lt;String&gt;(String))
054         * 
055         * @param id
056         *            See Component
057         * @param label
058         *            The label text
059         * 
060         * @see org.apache.wicket.Component#Component(String, IModel)
061         */
062        public MultiLineLabel(final String id, String label)
063        {
064                this(id, new Model<>(label));
065        }
066
067        /**
068         * @see org.apache.wicket.Component#Component(String, IModel)
069         */
070        public MultiLineLabel(final String id, IModel<?> model)
071        {
072                super(id, model);
073        }
074
075        @Override
076        public void onComponentTagBody(final MarkupStream markupStream, final ComponentTag openTag)
077        {
078                CharSequence body = Strings.toMultilineMarkup(getDefaultModelObjectAsString());
079                replaceComponentTagBody(markupStream, openTag, body);
080        }
081
082        @Override
083        protected void onComponentTag(ComponentTag tag)
084        {
085                super.onComponentTag(tag);
086
087                // always transform the tag to <span></span> so even labels defined as <span/> render
088                tag.setType(TagType.OPEN);
089        }
090}