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.Event;
022 import com.google.gwt.user.client.ui.*;
023
024 /**
025 * Just a simple anchor. No DIV, no history tokens showing up in the browser URL.
026 *
027 * <h3>CSS Style Rules</h3>
028 * <ul class='css'>
029 * <li>.tk-SimpleHyperLink { }</li>
030 * </ul>
031 */
032 public class SimpleHyperLink extends Widget implements HasText, HasHTML, SourcesClickEvents
033 {
034 private ClickListenerCollection m_listeners;
035
036 public SimpleHyperLink()
037 {
038 this(null, null);
039 }
040
041 public SimpleHyperLink(String text)
042 {
043 this(text, null);
044 }
045
046 /**
047 * Constructs a new SimpleHyperLink
048 *
049 * @param text a String or null
050 * @param clickListener a ClickListener or null
051 */
052 public SimpleHyperLink(String text, ClickListener clickListener)
053 {
054 setElement(DOM.createAnchor());
055
056 // prevents text selection by double-click
057 DomUtil.setAttribute(this, "href", "#");
058
059 sinkEvents(Event.ONCLICK);
060
061 setStyleName("tk-SimpleHyperLink");
062
063 if (text != null)
064 {
065 setText(text);
066 }
067
068 if (clickListener != null)
069 {
070 addClickListener(clickListener);
071 }
072 }
073
074 public void onBrowserEvent(Event event)
075 {
076 if (DOM.eventGetType(event) == Event.ONCLICK)
077 {
078 if (m_listeners != null)
079 {
080 m_listeners.fireClick(this);
081 }
082 // keep '#' out of the location bar
083 DOM.eventPreventDefault(event);
084 }
085 }
086
087 // HasText methods
088 public void setText(String text)
089 {
090 DOM.setInnerText(getElement(), text);
091 }
092
093 public String getText()
094 {
095 return DOM.getInnerText(getElement());
096 }
097
098 // HasHTML mehtods
099 public String getHTML()
100 {
101 return DOM.getInnerHTML(getElement());
102 }
103
104 public void setHTML(String html)
105 {
106 DOM.setInnerHTML(getElement(), html);
107 }
108
109 // SourcesClickEvents methods
110 public void addClickListener(ClickListener listener)
111 {
112 if (m_listeners == null)
113 {
114 m_listeners = new ClickListenerCollection();
115 }
116 m_listeners.add(listener);
117 }
118
119 public void removeClickListener(ClickListener listener)
120 {
121 if (m_listeners != null)
122 {
123 m_listeners.remove(listener);
124 }
125 }
126 }