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: */
017: package org.apache.wicket.markup.html.link;
018:
019: import org.apache.wicket.IPageMap;
020: import org.apache.wicket.RequestCycle;
021: import org.apache.wicket.markup.ComponentTag;
022: import org.apache.wicket.markup.MarkupStream;
023: import org.apache.wicket.markup.html.WebMarkupContainer;
024: import org.apache.wicket.model.IModel;
025: import org.apache.wicket.model.Model;
026: import org.apache.wicket.util.string.Strings;
027:
028: /**
029: * A simple anchor link (<a href="http://url">) pointing to any URL.
030: * Usually this is used for links to destinations outside of Wicket.
031: *
032: * @author Juergen Donnerstag
033: */
034: public class ExternalLink extends WebMarkupContainer {
035: private static final long serialVersionUID = 1L;
036:
037: /** the href attribute. */
038: private final IModel href;
039:
040: /** this links' label. */
041: private final IModel label;
042:
043: private boolean contextRelative = false;
044:
045: /**
046: * The popup specification. If not-null, a javascript on-click event handler
047: * will be generated that opens a new window using the popup properties.
048: */
049: private PopupSettings popupSettings = null;
050:
051: /**
052: * Constructor.
053: *
054: * @param id
055: * See Component
056: * @param href
057: * the href attribute to set
058: * @param label
059: * the label (body)
060: */
061: public ExternalLink(final String id, final String href,
062: final String label) {
063: super (id);
064:
065: this .href = (href != null ? new Model(href) : null);
066: this .label = (label != null ? new Model(label) : null);
067: }
068:
069: /**
070: * Constructor.
071: *
072: * @param id
073: * The name of this component
074: * @param href
075: * the href attribute to set
076: */
077: public ExternalLink(final String id, final String href) {
078: this (id, href, null);
079: }
080:
081: /**
082: * Constructor.
083: *
084: * @param id
085: * See Component
086: * @param href
087: * the href attribute to set
088: * @param label
089: * the label (body)
090: */
091: public ExternalLink(final String id, final IModel href,
092: final IModel label) {
093: super (id);
094:
095: this .href = wrap(href);
096: this .label = wrap(label);
097: }
098:
099: /**
100: * Constructor.
101: *
102: * @param id
103: * The name of this component
104: * @param href
105: * the href attribute to set
106: */
107: public ExternalLink(final String id, final IModel href) {
108: this (id, href, null);
109: }
110:
111: /**
112: * Gets the popup specification. If not-null, a javascript on-click event
113: * handler will be generated that opens a new window using the popup
114: * properties.
115: *
116: * @return the popup specification.
117: */
118: public final PopupSettings getPopupSettings() {
119: return popupSettings;
120: }
121:
122: /**
123: * Sets the popup specification. If not-null, a javascript on-click event
124: * handler will be generated that opens a new window using the popup
125: * properties.
126: *
127: * @param popupSettings
128: * the popup specification.
129: * @return This
130: */
131: public final ExternalLink setPopupSettings(
132: final PopupSettings popupSettings) {
133: this .popupSettings = popupSettings;
134: return this ;
135: }
136:
137: /**
138: * Processes the component tag.
139: *
140: * @param tag
141: * Tag to modify
142: * @see org.apache.wicket.Component#onComponentTag(org.apache.wicket.markup.ComponentTag)
143: */
144: protected void onComponentTag(ComponentTag tag) {
145: super .onComponentTag(tag);
146: if (href != null) {
147: Object hrefValue = href.getObject();
148: if (hrefValue != null) {
149: String url = hrefValue.toString();
150:
151: if (contextRelative) {
152: if (url.length() > 0 && url.charAt(0) == '/') {
153: url = url.substring(1);
154: }
155: url = RequestCycle.get().getRequest()
156: .getRelativePathPrefixToContextRoot()
157: + url;
158: }
159:
160: // if the tag is an anchor proper
161: if (tag.getName().equalsIgnoreCase("a")
162: || tag.getName().equalsIgnoreCase("link")
163: || tag.getName().equalsIgnoreCase("area")) {
164: // generate the href attribute
165: tag.put("href", Strings.replaceAll(url, "&",
166: "&"));
167:
168: // Add any popup script
169: if (popupSettings != null) {
170: // NOTE: don't encode to HTML as that is not valid
171: // JavaScript
172: tag.put("onclick", popupSettings
173: .getPopupJavaScript());
174: }
175: } else {
176: // generate a popup script by asking popup settings for one
177: if (popupSettings != null) {
178: popupSettings.setTarget("'" + url + "'");
179: String popupScript = popupSettings
180: .getPopupJavaScript();
181: tag.put("onclick", popupScript);
182: } else {
183: // or generate an onclick JS handler directly
184: tag.put("onclick", "window.location.href='"
185: + url + "';");
186: }
187: }
188: }
189:
190: if (popupSettings != null) {
191: IPageMap popupPageMap = popupSettings.getPageMap(this );
192: if (popupPageMap != null) {
193: tag.put("target", popupPageMap.getName());
194: }
195: }
196: }
197: }
198:
199: /**
200: * Handle the container's body.
201: *
202: * @param markupStream
203: * The markup stream
204: * @param openTag
205: * The open tag for the body
206: * @see org.apache.wicket.Component#onComponentTagBody(org.apache.wicket.markup.MarkupStream,
207: * org.apache.wicket.markup.ComponentTag)
208: */
209: protected void onComponentTagBody(MarkupStream markupStream,
210: ComponentTag openTag) {
211: if ((label != null) && (label.getObject() != null)) {
212: replaceComponentTagBody(markupStream, openTag,
213: getModelObjectAsString(label.getObject()));
214: } else {
215: super .onComponentTagBody(markupStream, openTag);
216: }
217: }
218:
219: /**
220: * @return True if this link is automatically prepended with ../ to make it
221: * relative to the context root.
222: */
223: public boolean isContextRelative() {
224: return contextRelative;
225: }
226:
227: /**
228: * Set to true if this link should be automatically prepended with ../ to
229: * make it relative to the context root.
230: *
231: * @param contextRelative
232: * @return This for chaining
233: */
234: public ExternalLink setContextRelative(boolean contextRelative) {
235: this.contextRelative = contextRelative;
236: return this;
237: }
238: }
|