001 /*
002 * Copyright 2006 Mat Gessel <mat.gessel@gmail.com>
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005 * use this file except in compliance with the License. You may obtain a copy of
006 * the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013 * License for the specific language governing permissions and limitations under
014 * the License.
015 */
016 package asquare.gwt.tk.client.ui;
017
018 import asquare.gwt.tk.client.util.DomUtil;
019
020 import com.google.gwt.user.client.DOM;
021 import com.google.gwt.user.client.ui.HasHTML;
022 import com.google.gwt.user.client.ui.HasText;
023 import com.google.gwt.user.client.ui.Widget;
024
025 /**
026 * An anchor linking to a page (or resource) external to the application.
027 * Clicking the hyperlink will result in the GWT application being
028 * unloaded unless you specify a target frame, window or "_blank".
029 *
030 * <h3>Example usage</h3>
031 * <p>
032 * <pre>
033 * new ExternalHyperLink("Google", "http://www.google.com");
034 * new ExternalHyperLink("Go to <b>Google</b>", true, "http://www.google.com", "contentFrame");
035 * new ExternalHyperLink("More info", false, "moreInfo.html", ExternalHyperLink.TARGET_BLANK);
036 * new ExternalHyperLink("Email us for more info", "mailto:sales@example.com");
037 * </pre>
038 * </p>
039 *
040 * <h3>CSS Style Rules</h3>
041 * <ul class='css'>
042 * <li>.tk-ExternalHyperLink { }</li>
043 * </ul>
044 */
045 public class ExternalHyperLink extends Widget implements HasText, HasHTML
046 {
047 /**
048 * Specify this target to open the linked location in a new window.
049 */
050 public static final String TARGET_BLANK = "_blank";
051
052 /**
053 * Specify this target to open the linked location in this frame's parent frame.
054 */
055 public static final String TARGET_PARENT = "_parent";
056
057 /**
058 * Specify this target to open the linked location in this frame.
059 */
060 public static final String TARGET_SELF = "_self";
061
062 /**
063 * Specify this target to open the linked location in the root frame.
064 */
065 public static final String TARGET_TOP = "_top";
066
067 /**
068 * Constructs a new ExternalHyperLink
069 *
070 * @param text a String to display in the link
071 * @param url the url the link will open
072 */
073 public ExternalHyperLink(String text, String url)
074 {
075 this(text, false, url, null);
076 }
077
078 /**
079 * Constructs a new ExternalHyperLink
080 *
081 * @param text a String to display in the link
082 * @param asHtml <code>true</code> to treat <code>text</code> as HTML,
083 * <code>false</code> to treat <code>text</code> as plain
084 * text
085 * @param url the url the link will open
086 * @param target the case-sensitive name of a target frame or window, or one
087 * of the reserved target constants
088 */
089 public ExternalHyperLink(String text, boolean asHtml, String url, String target)
090 {
091 setElement(DOM.createAnchor());
092 setStyleName("tk-ExternalHyperLink");
093
094 if (text != null)
095 {
096 if (asHtml)
097 {
098 setHTML(text);
099 }
100 else
101 {
102 setText(text);
103 }
104 }
105
106 if (url != null)
107 {
108 setUrl(url);
109 }
110
111 if (target != null)
112 {
113 setTarget(target);
114 }
115 }
116
117 /**
118 * Get the url which the link will open.
119 */
120 public String getUrl()
121 {
122 return DomUtil.getAttribute(this, "href");
123 }
124
125 /**
126 * Set the url which the link will open.
127 */
128 public void setUrl(String url)
129 {
130 DomUtil.setAttribute(this, "href", url);
131 }
132
133 /**
134 * Get the target frame or window in which the link will open.
135 */
136 public String getTarget()
137 {
138 return DomUtil.getAttribute(this, "target");
139 }
140
141 /**
142 * Set the target frame or window in which the link will open.
143 *
144 * @param target the case-sensitive name of a target frame or window, or one
145 * of the reserved target constants
146 */
147 public void setTarget(String target)
148 {
149 DomUtil.setAttribute(this, "target", target);
150 }
151
152 // HasText methods
153 public String getText()
154 {
155 return DOM.getInnerText(getElement());
156 }
157
158 public void setText(String text)
159 {
160 DOM.setInnerText(getElement(), text);
161 }
162
163 // HasHTML methods
164 public String getHTML()
165 {
166 return DOM.getInnerHTML(getElement());
167 }
168
169 public void setHTML(String html)
170 {
171 DOM.setInnerHTML(getElement(), html);
172 }
173 }