| /* |
| * Copyright 2008 Google Inc. |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| * use this file except in compliance with the License. You may obtain a copy of |
| * the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| * License for the specific language governing permissions and limitations under |
| * the License. |
| */ |
| package com.google.gwt.user.client.ui; |
| |
| import com.google.gwt.dom.client.Document; |
| import com.google.gwt.dom.client.Element; |
| import com.google.gwt.event.dom.client.ClickEvent; |
| import com.google.gwt.event.dom.client.ClickHandler; |
| import com.google.gwt.event.dom.client.HasAllMouseHandlers; |
| import com.google.gwt.event.dom.client.HasClickHandlers; |
| import com.google.gwt.event.dom.client.MouseDownEvent; |
| import com.google.gwt.event.dom.client.MouseDownHandler; |
| import com.google.gwt.event.dom.client.MouseMoveEvent; |
| import com.google.gwt.event.dom.client.MouseMoveHandler; |
| import com.google.gwt.event.dom.client.MouseOutEvent; |
| import com.google.gwt.event.dom.client.MouseOutHandler; |
| import com.google.gwt.event.dom.client.MouseOverEvent; |
| import com.google.gwt.event.dom.client.MouseOverHandler; |
| import com.google.gwt.event.dom.client.MouseUpEvent; |
| import com.google.gwt.event.dom.client.MouseUpHandler; |
| import com.google.gwt.event.dom.client.MouseWheelEvent; |
| import com.google.gwt.event.dom.client.MouseWheelHandler; |
| import com.google.gwt.event.shared.HandlerRegistration; |
| import com.google.gwt.i18n.client.BidiUtils; |
| import com.google.gwt.i18n.client.HasDirection; |
| |
| /** |
| * A widget that contains arbitrary text, <i>not</i> interpreted as HTML. |
| * |
| * This widget uses a <div> element, causing it to be displayed with block |
| * layout. |
| * |
| * <h3>CSS Style Rules</h3> |
| * <ul class='css'> |
| * <li>.gwt-Label { }</li> |
| * </ul> |
| * |
| * <p> |
| * <h3>Example</h3> |
| * {@example com.google.gwt.examples.HTMLExample} |
| * </p> |
| */ |
| @SuppressWarnings("deprecation") |
| public class Label extends Widget implements HasHorizontalAlignment, HasText, |
| HasWordWrap, HasDirection, HasClickHandlers, SourcesClickEvents, |
| SourcesMouseEvents, HasAllMouseHandlers { |
| |
| /** |
| * Creates a Label widget that wraps an existing <div> or <span> |
| * element. |
| * |
| * This element must already be attached to the document. If the element is |
| * removed from the document, you must call |
| * {@link RootPanel#detachNow(Widget)}. |
| * |
| * @param element the element to be wrapped |
| */ |
| public static Label wrap(Element element) { |
| // Assert that the element is attached. |
| assert Document.get().getBody().isOrHasChild(element); |
| |
| Label label = new Label(element); |
| |
| // Mark it attached and remember it for cleanup. |
| label.onAttach(); |
| RootPanel.detachOnWindowClose(label); |
| |
| return label; |
| } |
| |
| private HorizontalAlignmentConstant horzAlign; |
| |
| /** |
| * Creates an empty label. |
| */ |
| public Label() { |
| setElement(Document.get().createDivElement()); |
| setStyleName("gwt-Label"); |
| } |
| |
| /** |
| * Creates a label with the specified text. |
| * |
| * @param text the new label's text |
| */ |
| public Label(String text) { |
| this(); |
| setText(text); |
| } |
| |
| /** |
| * Creates a label with the specified text. |
| * |
| * @param text the new label's text |
| * @param wordWrap <code>false</code> to disable word wrapping |
| */ |
| public Label(String text, boolean wordWrap) { |
| this(text); |
| setWordWrap(wordWrap); |
| } |
| |
| /** |
| * This constructor may be used by subclasses to explicitly use an existing |
| * element. This element must be either a <div> or <span> element. |
| * |
| * @param element the element to be used |
| */ |
| protected Label(Element element) { |
| setElement(element); |
| assert element.getTagName().equalsIgnoreCase("div") |
| || element.getTagName().equalsIgnoreCase("span"); |
| } |
| |
| public HandlerRegistration addClickHandler(ClickHandler handler) { |
| return addDomHandler(handler, ClickEvent.getType()); |
| } |
| |
| /** |
| * @deprecated Use {@link #addClickHandler} instead |
| */ |
| @Deprecated |
| public void addClickListener(ClickListener listener) { |
| ListenerWrapper.WrappedClickListener.add(this, listener); |
| } |
| |
| public HandlerRegistration addMouseDownHandler(MouseDownHandler handler) { |
| return addDomHandler(handler, MouseDownEvent.getType()); |
| } |
| |
| /** |
| * @deprecated Use {@link #addMouseOverHandler} {@link |
| * #addMouseMoveHandler}, {@link #addMouseDownHandler}, {@link |
| * #addMouseUpHandler} and {@link #addMouseOutHandler} instead |
| */ |
| @Deprecated |
| public void addMouseListener(MouseListener listener) { |
| ListenerWrapper.WrappedMouseListener.add(this, listener); |
| } |
| |
| public HandlerRegistration addMouseMoveHandler(MouseMoveHandler handler) { |
| return addDomHandler(handler, MouseMoveEvent.getType()); |
| } |
| |
| public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) { |
| return addDomHandler(handler, MouseOutEvent.getType()); |
| } |
| |
| public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) { |
| return addDomHandler(handler, MouseOverEvent.getType()); |
| } |
| |
| public HandlerRegistration addMouseUpHandler(MouseUpHandler handler) { |
| return addDomHandler(handler, MouseUpEvent.getType()); |
| } |
| |
| public HandlerRegistration addMouseWheelHandler(MouseWheelHandler handler) { |
| return addDomHandler(handler, MouseWheelEvent.getType()); |
| } |
| |
| /** |
| * @deprecated Use {@link #addMouseWheelHandler} instead |
| */ |
| @Deprecated |
| public void addMouseWheelListener(MouseWheelListener listener) { |
| ListenerWrapper.WrappedMouseWheelListener.add(this, listener); |
| } |
| |
| public Direction getDirection() { |
| return BidiUtils.getDirectionOnElement(getElement()); |
| } |
| |
| public HorizontalAlignmentConstant getHorizontalAlignment() { |
| return horzAlign; |
| } |
| |
| public String getText() { |
| return getElement().getInnerText(); |
| } |
| |
| public boolean getWordWrap() { |
| return !getElement().getStyle().getProperty("whiteSpace").equals("nowrap"); |
| } |
| |
| /** |
| * @deprecated Use the {@link HandlerRegistration#removeHandler} method on |
| * the object returned by {@link #addClickHandler} instead |
| */ |
| @Deprecated |
| public void removeClickListener(ClickListener listener) { |
| ListenerWrapper.WrappedClickListener.remove(this, listener); |
| } |
| |
| /** |
| * @deprecated Use the {@link HandlerRegistration#removeHandler} |
| * method on the object returned by an add*Handler method instead |
| */ |
| @Deprecated |
| public void removeMouseListener(MouseListener listener) { |
| ListenerWrapper.WrappedMouseListener.remove(this, listener); |
| } |
| |
| /** |
| * @deprecated Use the {@link HandlerRegistration#removeHandler} |
| * method on the object returned by {@link #addMouseWheelHandler} instead |
| */ |
| @Deprecated |
| public void removeMouseWheelListener(MouseWheelListener listener) { |
| ListenerWrapper.WrappedMouseWheelListener.remove(this, listener); |
| } |
| |
| public void setDirection(Direction direction) { |
| BidiUtils.setDirectionOnElement(getElement(), direction); |
| } |
| |
| public void setHorizontalAlignment(HorizontalAlignmentConstant align) { |
| horzAlign = align; |
| getElement().getStyle().setProperty("textAlign", align.getTextAlignString()); |
| } |
| |
| public void setText(String text) { |
| getElement().setInnerText(text); |
| } |
| |
| public void setWordWrap(boolean wrap) { |
| getElement().getStyle().setProperty("whiteSpace", |
| wrap ? "normal" : "nowrap"); |
| } |
| } |