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.extensions.markup.html.basic;
018
019import org.apache.wicket.markup.ComponentTag;
020import org.apache.wicket.markup.MarkupStream;
021import org.apache.wicket.markup.html.basic.MultiLineLabel;
022import org.apache.wicket.model.IModel;
023import org.apache.wicket.model.Model;
024import org.apache.wicket.util.string.Strings;
025
026/**
027 * If you have email addresses or web URLs in the data that you are displaying, then you can
028 * automatically display those pieces of data as hyperlinks, you will not have to take any action to
029 * convert that data.
030 * <p>
031 * Email addresses will be wrapped with a &lt;a href="mailto:xxx"&gt;xxx&lt;/a&gt; tag, where "xxx"
032 * is the email address that was detected.
033 * <p>
034 * Web URLs will be wrapped with a &lt;a href="xxx"&gt;xxx&lt;/a&gt; tag, where "xxx" is the URL
035 * that was detected (it can be any valid URL type, http://, https://, ftp://, etc...)
036 * 
037 * @author Juergen Donnerstag
038 */
039public class SmartLinkMultiLineLabel extends MultiLineLabel
040{
041        private static final long serialVersionUID = 1L;
042
043        /**
044         * @see MultiLineLabel#MultiLineLabel(String, String)
045         */
046        public SmartLinkMultiLineLabel(final String id, final String label)
047        {
048                this(id, new Model<>(label));
049        }
050
051        /**
052         * Construct.
053         * 
054         * @param id
055         *            The non-null id of this component
056         * @param model
057         *            The component's model
058         */
059        public SmartLinkMultiLineLabel(final String id, final IModel<String> model)
060        {
061                super(id, model);
062        }
063
064        /**
065         * {@inheritDoc}
066         */
067        @Override
068        public void onComponentTagBody(final MarkupStream markupStream, final ComponentTag openTag)
069        {
070                final CharSequence body = getSmartLink(getDefaultModelObjectAsString());
071                replaceComponentTagBody(markupStream, openTag, Strings.toMultilineMarkup(body));
072        }
073
074        /**
075         * Get the link parser. You may subclass that methods to provide your own LinkParser.
076         * 
077         * @return ILinkParser
078         */
079        protected ILinkParser getLinkParser()
080        {
081                return new DefaultLinkParser();
082        }
083
084        /**
085         * Get the text after parsing by the link parser.
086         * 
087         * @param text
088         * @return smart link
089         */
090        protected CharSequence getSmartLink(final CharSequence text)
091        {
092                return getLinkParser().parse(text.toString());
093        }
094}