KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > user > client > ui > Hyperlink


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.user.client.ui;
17
18 import com.google.gwt.user.client.DOM;
19 import com.google.gwt.user.client.Element;
20 import com.google.gwt.user.client.Event;
21 import com.google.gwt.user.client.History;
22
23 /**
24  * A widget that serves as an "internal" hyperlink. That is, it is a link to
25  * another state of the running application. When clicked, it will create a new
26  * history frame using {@link com.google.gwt.user.client.History#newItem}, but
27  * without reloading the page.
28  *
29  * <p>
30  * Being a true hyperlink, it is also possible for the user to "right-click,
31  * open link in new window", which will cause the application to be loaded in a
32  * new window at the state specified by the hyperlink.
33  * </p>
34  *
35  * <p>
36  * <img class='gallery' SRC='Hyperlink.png'/>
37  * </p>
38  *
39  * <h3>CSS Style Rules</h3>
40  * <ul class='css'>
41  * <li>.gwt-Hyperlink { }</li>
42  * </ul>
43  *
44  * <p>
45  * <h3>Example</h3> {@example com.google.gwt.examples.HistoryExample}
46  * </p>
47  */

48 public class Hyperlink extends Widget implements HasHTML, SourcesClickEvents {
49
50   private Element anchorElem;
51   private ClickListenerCollection clickListeners;
52   private String JavaDoc targetHistoryToken;
53
54   /**
55    * Creates an empty hyperlink.
56    */

57   public Hyperlink() {
58     setElement(DOM.createDiv());
59     DOM.appendChild(getElement(), anchorElem = DOM.createAnchor());
60     sinkEvents(Event.ONCLICK);
61     setStyleName("gwt-Hyperlink");
62   }
63
64   /**
65    * Creates a hyperlink with its text and target history token specified.
66    *
67    * @param text the hyperlink's text
68    * @param asHTML <code>true</code> to treat the specified text as html
69    * @param targetHistoryToken the history token to which it will link
70    * @see #setTargetHistoryToken
71    */

72   public Hyperlink(String JavaDoc text, boolean asHTML, String JavaDoc targetHistoryToken) {
73     this();
74     if (asHTML) {
75       setHTML(text);
76     } else {
77       setText(text);
78     }
79     setTargetHistoryToken(targetHistoryToken);
80   }
81
82   /**
83    * Creates a hyperlink with its text and target history token specified.
84    *
85    * @param text the hyperlink's text
86    * @param targetHistoryToken the history token to which it will link
87    */

88   public Hyperlink(String JavaDoc text, String JavaDoc targetHistoryToken) {
89     this();
90     setText(text);
91     setTargetHistoryToken(targetHistoryToken);
92   }
93
94   public void addClickListener(ClickListener listener) {
95     if (clickListeners == null) {
96       clickListeners = new ClickListenerCollection();
97     }
98     clickListeners.add(listener);
99   }
100
101   public String JavaDoc getHTML() {
102     return DOM.getInnerHTML(anchorElem);
103   }
104
105   /**
106    * Gets the history token referenced by this hyperlink.
107    *
108    * @return the target history token
109    * @see #setTargetHistoryToken
110    */

111   public String JavaDoc getTargetHistoryToken() {
112     return targetHistoryToken;
113   }
114
115   public String JavaDoc getText() {
116     return DOM.getInnerText(anchorElem);
117   }
118
119   public void onBrowserEvent(Event event) {
120     if (DOM.eventGetType(event) == Event.ONCLICK) {
121       if (clickListeners != null) {
122         clickListeners.fireClick(this);
123       }
124       History.newItem(targetHistoryToken);
125       DOM.eventPreventDefault(event);
126     }
127   }
128
129   public void removeClickListener(ClickListener listener) {
130     if (clickListeners != null) {
131       clickListeners.remove(listener);
132     }
133   }
134
135   public void setHTML(String JavaDoc html) {
136     DOM.setInnerHTML(anchorElem, html);
137   }
138
139   /**
140    * Sets the history token referenced by this hyperlink. This is the history
141    * token that will be passed to {@link History#newItem} when this link is
142    * clicked.
143    *
144    * @param targetHistoryToken the new target history token
145    */

146   public void setTargetHistoryToken(String JavaDoc targetHistoryToken) {
147     this.targetHistoryToken = targetHistoryToken;
148     DOM.setElementProperty(anchorElem, "href", "#" + targetHistoryToken);
149   }
150
151   public void setText(String JavaDoc text) {
152     DOM.setInnerText(anchorElem, text);
153   }
154 }
155
Popular Tags