KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2007 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.Event;
20
21 /**
22  * A widget that contains arbitrary text, <i>not</i> interpreted as HTML.
23  *
24  * <h3>CSS Style Rules</h3>
25  * <ul class='css'>
26  * <li>.gwt-Label { }</li>
27  * </ul>
28  *
29  * <p>
30  * <h3>Example</h3> {@example com.google.gwt.examples.HTMLExample}
31  * </p>
32  */

33 public class Label extends Widget implements SourcesClickEvents,
34     SourcesMouseEvents, SourcesMouseWheelEvents,
35     HasHorizontalAlignment, HasText, HasWordWrap {
36
37   private ClickListenerCollection clickListeners;
38   private HorizontalAlignmentConstant horzAlign;
39   private MouseListenerCollection mouseListeners;
40   private MouseWheelListenerCollection mouseWheelListeners;
41
42   /**
43    * Creates an empty label.
44    */

45   public Label() {
46     setElement(DOM.createDiv());
47     sinkEvents(Event.ONCLICK | Event.MOUSEEVENTS | Event.ONMOUSEWHEEL);
48     setStyleName("gwt-Label");
49   }
50
51   /**
52    * Creates a label with the specified text.
53    *
54    * @param text the new label's text
55    */

56   public Label(String JavaDoc text) {
57     this();
58     setText(text);
59   }
60
61   /**
62    * Creates a label with the specified text.
63    *
64    * @param text the new label's text
65    * @param wordWrap <code>false</code> to disable word wrapping
66    */

67   public Label(String JavaDoc text, boolean wordWrap) {
68     this(text);
69     setWordWrap(wordWrap);
70   }
71
72   public void addClickListener(ClickListener listener) {
73     if (clickListeners == null) {
74       clickListeners = new ClickListenerCollection();
75     }
76     clickListeners.add(listener);
77   }
78
79   public void addMouseListener(MouseListener listener) {
80     if (mouseListeners == null) {
81       mouseListeners = new MouseListenerCollection();
82     }
83     mouseListeners.add(listener);
84   }
85
86   public void addMouseWheelListener(MouseWheelListener listener) {
87     if (mouseWheelListeners == null) {
88       mouseWheelListeners = new MouseWheelListenerCollection();
89     }
90     mouseWheelListeners.add(listener);
91   }
92
93   public HorizontalAlignmentConstant getHorizontalAlignment() {
94     return horzAlign;
95   }
96
97   public String JavaDoc getText() {
98     return DOM.getInnerText(getElement());
99   }
100
101   public boolean getWordWrap() {
102     return !DOM.getStyleAttribute(getElement(), "whiteSpace").equals("nowrap");
103   }
104
105   public void onBrowserEvent(Event event) {
106     switch (DOM.eventGetType(event)) {
107       case Event.ONCLICK:
108         if (clickListeners != null) {
109           clickListeners.fireClick(this);
110         }
111         break;
112
113       case Event.ONMOUSEDOWN:
114       case Event.ONMOUSEUP:
115       case Event.ONMOUSEMOVE:
116       case Event.ONMOUSEOVER:
117       case Event.ONMOUSEOUT:
118         if (mouseListeners != null) {
119           mouseListeners.fireMouseEvent(this, event);
120         }
121         break;
122
123       case Event.ONMOUSEWHEEL:
124         if (mouseWheelListeners != null) {
125           mouseWheelListeners.fireMouseWheelEvent(this, event);
126         }
127         break;
128     }
129   }
130
131   public void removeClickListener(ClickListener listener) {
132     if (clickListeners != null) {
133       clickListeners.remove(listener);
134     }
135   }
136
137   public void removeMouseListener(MouseListener listener) {
138     if (mouseListeners != null) {
139       mouseListeners.remove(listener);
140     }
141   }
142
143   public void removeMouseWheelListener(MouseWheelListener listener) {
144     if (mouseWheelListeners != null) {
145       mouseWheelListeners.remove(listener);
146     }
147   }
148
149   public void setHorizontalAlignment(HorizontalAlignmentConstant align) {
150     horzAlign = align;
151     DOM
152       .setStyleAttribute(getElement(), "textAlign", align.getTextAlignString());
153   }
154
155   public void setText(String JavaDoc text) {
156     DOM.setInnerText(getElement(), text);
157   }
158
159   public void setWordWrap(boolean wrap) {
160     DOM.setStyleAttribute(getElement(), "whiteSpace", wrap ? "normal"
161       : "nowrap");
162   }
163 }
164
Popular Tags