01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.wicket.extensions.markup.html.basic;
18:
19: import org.apache.wicket.markup.ComponentTag;
20: import org.apache.wicket.markup.MarkupStream;
21: import org.apache.wicket.markup.html.basic.Label;
22: import org.apache.wicket.model.IModel;
23:
24: /**
25: * If you have email addresses or web URLs in the data that you are displaying,
26: * then you can automatically display those pieces of data as hyperlinks, you
27: * will not have to take any action to convert that data.
28: * <p>
29: * Email addresses will be wrapped with a <a
30: * href="mailto:xxx">xxx</a> tag, where "xxx" is the email address that
31: * was detected.
32: * <p>
33: * Web URLs will be wrapped with a <a href="xxx">xxx</a> tag, where
34: * "xxx" is the URL that was detected (it can be any valid URL type, http://,
35: * https://, ftp://, etc...)
36: *
37: * @author Juergen Donnerstag
38: */
39: public class SmartLinkLabel extends Label {
40: private static final long serialVersionUID = 1L;
41:
42: /**
43: * @see Label#Label(String, String)
44: */
45: public SmartLinkLabel(String name, String label) {
46: super (name, label);
47: }
48:
49: /**
50: * @see Label#Label(String, IModel)
51: */
52: public SmartLinkLabel(String name, IModel model) {
53: super (name, model);
54: }
55:
56: /**
57: * @see Label#Label(String)
58: */
59: public SmartLinkLabel(String name) {
60: super (name);
61: }
62:
63: /**
64: * @see org.apache.wicket.Component#onComponentTagBody(org.apache.wicket.markup.MarkupStream,
65: * org.apache.wicket.markup.ComponentTag)
66: */
67: protected void onComponentTagBody(final MarkupStream markupStream,
68: final ComponentTag openTag) {
69: replaceComponentTagBody(markupStream, openTag,
70: getSmartLink(getModelObjectAsString()));
71: }
72:
73: protected ILinkParser getLinkParser() {
74: return new DefaultLinkParser();
75: }
76:
77: /**
78: * Replace all email and URL addresses
79: *
80: * @param text
81: * Text to be modified
82: * @return Modified Text
83: */
84: protected final CharSequence getSmartLink(final CharSequence text) {
85: return getLinkParser().parse(text.toString());
86: }
87: }
|