001: /*
002: * Copyright 2007 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: * <p>
045: * <h3>Example</h3> {@example com.google.gwt.examples.HistoryExample}
046: * </p>
047: */
048: public class Hyperlink extends Widget implements HasHTML,
049: SourcesClickEvents {
050:
051: private Element anchorElem;
052: private ClickListenerCollection clickListeners;
053: private String targetHistoryToken;
054:
055: /**
056: * Creates an empty hyperlink.
057: */
058: public Hyperlink() {
059: setElement(DOM.createDiv());
060: DOM.appendChild(getElement(), anchorElem = DOM.createAnchor());
061: sinkEvents(Event.ONCLICK);
062: setStyleName("gwt-Hyperlink");
063: }
064:
065: /**
066: * Creates a hyperlink with its text and target history token specified.
067: *
068: * @param text the hyperlink's text
069: * @param asHTML <code>true</code> to treat the specified text as html
070: * @param targetHistoryToken the history token to which it will link
071: * @see #setTargetHistoryToken
072: */
073: public Hyperlink(String text, boolean asHTML,
074: String targetHistoryToken) {
075: this ();
076: if (asHTML) {
077: setHTML(text);
078: } else {
079: setText(text);
080: }
081: setTargetHistoryToken(targetHistoryToken);
082: }
083:
084: /**
085: * Creates a hyperlink with its text and target history token specified.
086: *
087: * @param text the hyperlink's text
088: * @param targetHistoryToken the history token to which it will link
089: */
090: public Hyperlink(String text, String targetHistoryToken) {
091: this ();
092: setText(text);
093: setTargetHistoryToken(targetHistoryToken);
094: }
095:
096: public void addClickListener(ClickListener listener) {
097: if (clickListeners == null) {
098: clickListeners = new ClickListenerCollection();
099: }
100: clickListeners.add(listener);
101: }
102:
103: public String getHTML() {
104: return DOM.getInnerHTML(anchorElem);
105: }
106:
107: /**
108: * Gets the history token referenced by this hyperlink.
109: *
110: * @return the target history token
111: * @see #setTargetHistoryToken
112: */
113: public String getTargetHistoryToken() {
114: return targetHistoryToken;
115: }
116:
117: public String getText() {
118: return DOM.getInnerText(anchorElem);
119: }
120:
121: @Override
122: public void onBrowserEvent(Event event) {
123: if (DOM.eventGetType(event) == Event.ONCLICK) {
124: if (clickListeners != null) {
125: clickListeners.fireClick(this );
126: }
127: History.newItem(targetHistoryToken);
128: DOM.eventPreventDefault(event);
129: }
130: }
131:
132: public void removeClickListener(ClickListener listener) {
133: if (clickListeners != null) {
134: clickListeners.remove(listener);
135: }
136: }
137:
138: public void setHTML(String html) {
139: DOM.setInnerHTML(anchorElem, html);
140: }
141:
142: /**
143: * Sets the history token referenced by this hyperlink. This is the history
144: * token that will be passed to {@link History#newItem} when this link is
145: * clicked.
146: *
147: * @param targetHistoryToken the new target history token
148: */
149: public void setTargetHistoryToken(String targetHistoryToken) {
150: this .targetHistoryToken = targetHistoryToken;
151: DOM.setElementProperty(anchorElem, "href", "#"
152: + targetHistoryToken);
153: }
154:
155: public void setText(String text) {
156: DOM.setInnerText(anchorElem, text);
157: }
158: }
|