001 /*
002 * Copyright 2006 Google Inc.
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 com.google.gwt.user.client.ui;
017
018 import com.google.gwt.user.client.DOM;
019 import com.google.gwt.user.client.Element;
020 import com.google.gwt.user.client.Event;
021 import com.google.gwt.user.client.History;
022
023 /**
024 * A widget that serves as an "internal" hyperlink. That is, it is a link to
025 * another state of the running application. When clicked, it will create a new
026 * history frame using {@link com.google.gwt.user.client.History#newItem}, but
027 * without reloading the page.
028 *
029 * <p>
030 * Being a true hyperlink, it is also possible for the user to "right-click,
031 * open link in new window", which will cause the application to be loaded in a
032 * new window at the state specified by the hyperlink.
033 * </p>
034 *
035 * <p>
036 * <img class='gallery' src='Hyperlink.png'/>
037 * </p>
038 *
039 * <h3>CSS Style Rules</h3>
040 * <ul class='css'>
041 * <li>.gwt-Hyperlink { }</li>
042 * </ul>
043 *
044 * <h3>Example</h3>
045 * {@link com.google.gwt.examples.HistoryExample code}
046 */
047 public class Hyperlink extends Widget implements HasHTML, SourcesClickEvents {
048
049 private Element anchorElem;
050 private ClickListenerCollection fClickListeners;
051 private String targetHistoryToken;
052
053 /**
054 * Creates an empty hyperlink.
055 */
056 public Hyperlink() {
057 setElement(DOM.createDiv());
058 DOM.appendChild(getElement(), anchorElem = DOM.createAnchor());
059 sinkEvents(Event.ONCLICK);
060 }
061
062 /**
063 * Creates a hyperlink with its text and target history token specified.
064 *
065 * @param text the hyperlink's text
066 * @param asHTML <code>true</code> to treat the specified text as html
067 * @param targetHistoryToken the history token to which it will link
068 * @see #setTargetHistoryToken
069 */
070 public Hyperlink(String text, boolean asHTML, String targetHistoryToken) {
071 this();
072 if (asHTML)
073 setHTML(text);
074 else
075 setText(text);
076 setTargetHistoryToken(targetHistoryToken);
077 }
078
079 /**
080 * Creates a hyperlink with its text and target history token specified.
081 *
082 * @param text the hyperlink's text
083 * @param targetHistoryToken the history token to which it will link
084 */
085 public Hyperlink(String text, String targetHistoryToken) {
086 this();
087 setText(text);
088 setTargetHistoryToken(targetHistoryToken);
089 }
090
091 public void addClickListener(ClickListener listener) {
092 if (fClickListeners == null)
093 fClickListeners = new ClickListenerCollection();
094 fClickListeners.add(listener);
095 }
096
097 public String getHTML() {
098 return DOM.getInnerHTML(anchorElem);
099 }
100
101 /**
102 * Gets the history token referenced by this hyperlink.
103 *
104 * @return the target history token
105 * @see #setTargetHistoryToken
106 */
107 public String getTargetHistoryToken() {
108 return targetHistoryToken;
109 }
110
111 public String getText() {
112 return DOM.getInnerText(anchorElem);
113 }
114
115 public void onBrowserEvent(Event event) {
116 if (DOM.eventGetType(event) == Event.ONCLICK) {
117 if (fClickListeners != null)
118 fClickListeners.fireClick(this);
119 History.newItem(targetHistoryToken);
120 DOM.eventPreventDefault(event);
121 }
122 }
123
124 public void removeClickListener(ClickListener listener) {
125 if (fClickListeners == null)
126 fClickListeners.remove(listener);
127 }
128
129 public void setHTML(String html) {
130 DOM.setInnerHTML(anchorElem, html);
131 }
132
133 /**
134 * Sets the history token referenced by this hyperlink. This is the history
135 * token that will be passed to {@link History#newItem} when this link is
136 * clicked.
137 *
138 * @param targetHistoryToken the new target history token
139 */
140 public void setTargetHistoryToken(String targetHistoryToken) {
141 this.targetHistoryToken = targetHistoryToken;
142 DOM.setAttribute(anchorElem, "href", "#" + targetHistoryToken);
143 }
144
145 public void setText(String text) {
146 DOM.setInnerHTML(anchorElem, text);
147 }
148 }