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.Event;
020:
021: /**
022: * A widget that contains arbitrary text, <i>not</i> interpreted as HTML.
023: *
024: * <h3>CSS Style Rules</h3>
025: * <ul class='css'>
026: * <li>.gwt-Label { }</li>
027: * </ul>
028: *
029: * <p>
030: * <h3>Example</h3> {@example com.google.gwt.examples.HTMLExample}
031: * </p>
032: */
033: public class Label extends Widget implements SourcesClickEvents,
034: SourcesMouseEvents, SourcesMouseWheelEvents,
035: HasHorizontalAlignment, HasText, HasWordWrap {
036:
037: private ClickListenerCollection clickListeners;
038: private HorizontalAlignmentConstant horzAlign;
039: private MouseListenerCollection mouseListeners;
040: private MouseWheelListenerCollection mouseWheelListeners;
041:
042: /**
043: * Creates an empty label.
044: */
045: public Label() {
046: setElement(DOM.createDiv());
047: sinkEvents(Event.ONCLICK | Event.MOUSEEVENTS
048: | Event.ONMOUSEWHEEL);
049: setStyleName("gwt-Label");
050: }
051:
052: /**
053: * Creates a label with the specified text.
054: *
055: * @param text the new label's text
056: */
057: public Label(String text) {
058: this ();
059: setText(text);
060: }
061:
062: /**
063: * Creates a label with the specified text.
064: *
065: * @param text the new label's text
066: * @param wordWrap <code>false</code> to disable word wrapping
067: */
068: public Label(String text, boolean wordWrap) {
069: this (text);
070: setWordWrap(wordWrap);
071: }
072:
073: public void addClickListener(ClickListener listener) {
074: if (clickListeners == null) {
075: clickListeners = new ClickListenerCollection();
076: }
077: clickListeners.add(listener);
078: }
079:
080: public void addMouseListener(MouseListener listener) {
081: if (mouseListeners == null) {
082: mouseListeners = new MouseListenerCollection();
083: }
084: mouseListeners.add(listener);
085: }
086:
087: public void addMouseWheelListener(MouseWheelListener listener) {
088: if (mouseWheelListeners == null) {
089: mouseWheelListeners = new MouseWheelListenerCollection();
090: }
091: mouseWheelListeners.add(listener);
092: }
093:
094: public HorizontalAlignmentConstant getHorizontalAlignment() {
095: return horzAlign;
096: }
097:
098: public String getText() {
099: return DOM.getInnerText(getElement());
100: }
101:
102: public boolean getWordWrap() {
103: return !DOM.getStyleAttribute(getElement(), "whiteSpace")
104: .equals("nowrap");
105: }
106:
107: @Override
108: public void onBrowserEvent(Event event) {
109: switch (DOM.eventGetType(event)) {
110: case Event.ONCLICK:
111: if (clickListeners != null) {
112: clickListeners.fireClick(this );
113: }
114: break;
115:
116: case Event.ONMOUSEDOWN:
117: case Event.ONMOUSEUP:
118: case Event.ONMOUSEMOVE:
119: case Event.ONMOUSEOVER:
120: case Event.ONMOUSEOUT:
121: if (mouseListeners != null) {
122: mouseListeners.fireMouseEvent(this , event);
123: }
124: break;
125:
126: case Event.ONMOUSEWHEEL:
127: if (mouseWheelListeners != null) {
128: mouseWheelListeners.fireMouseWheelEvent(this , event);
129: }
130: break;
131: }
132: }
133:
134: public void removeClickListener(ClickListener listener) {
135: if (clickListeners != null) {
136: clickListeners.remove(listener);
137: }
138: }
139:
140: public void removeMouseListener(MouseListener listener) {
141: if (mouseListeners != null) {
142: mouseListeners.remove(listener);
143: }
144: }
145:
146: public void removeMouseWheelListener(MouseWheelListener listener) {
147: if (mouseWheelListeners != null) {
148: mouseWheelListeners.remove(listener);
149: }
150: }
151:
152: public void setHorizontalAlignment(HorizontalAlignmentConstant align) {
153: horzAlign = align;
154: DOM.setStyleAttribute(getElement(), "textAlign", align
155: .getTextAlignString());
156: }
157:
158: public void setText(String text) {
159: DOM.setInnerText(getElement(), text);
160: }
161:
162: public void setWordWrap(boolean wrap) {
163: DOM.setStyleAttribute(getElement(), "whiteSpace",
164: wrap ? "normal" : "nowrap");
165: }
166: }
|