KickJava   Java API By Example, From Geeks To Geeks.

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


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.Element;
20 import com.google.gwt.user.client.Event;
21 import com.google.gwt.user.client.ui.impl.FocusImpl;
22
23 /**
24  * Abstract base class for most widgets that can receive keyboard focus.
25  */

26 public abstract class FocusWidget extends Widget implements SourcesClickEvents,
27     SourcesFocusEvents, HasFocus {
28
29   private static final FocusImpl impl = FocusImpl.getFocusImplForWidget();
30
31   /**
32    * Gets the FocusImpl instance.
33    *
34    * @return impl
35    */

36   protected static FocusImpl getFocusImpl() {
37     return impl;
38   }
39
40   private ClickListenerCollection clickListeners;
41   private FocusListenerCollection focusListeners;
42   private KeyboardListenerCollection keyboardListeners;
43
44   /**
45    * Creates a new focus widget with no element. {@link #setElement(Element)}
46    * must be called before any other methods.
47    */

48   protected FocusWidget() {
49   }
50
51   /**
52    * Creates a new focus widget that wraps the specified browser element.
53    *
54    * @param elem the element to be wrapped
55    */

56   protected FocusWidget(Element elem) {
57     setElement(elem);
58   }
59
60   public void addClickListener(ClickListener listener) {
61     if (clickListeners == null) {
62       clickListeners = new ClickListenerCollection();
63     }
64     clickListeners.add(listener);
65   }
66
67   public void addFocusListener(FocusListener listener) {
68     if (focusListeners == null) {
69       focusListeners = new FocusListenerCollection();
70     }
71     focusListeners.add(listener);
72   }
73
74   public void addKeyboardListener(KeyboardListener listener) {
75     if (keyboardListeners == null) {
76       keyboardListeners = new KeyboardListenerCollection();
77     }
78     keyboardListeners.add(listener);
79   }
80
81   public int getTabIndex() {
82     return impl.getTabIndex(getElement());
83   }
84
85   /**
86    * Gets whether this widget is enabled.
87    *
88    * @return <code>true</code> if the widget is enabled
89    */

90   public boolean isEnabled() {
91     return !DOM.getElementPropertyBoolean(getElement(), "disabled");
92   }
93
94   public void onBrowserEvent(Event event) {
95     switch (DOM.eventGetType(event)) {
96       case Event.ONCLICK:
97         if (clickListeners != null) {
98           clickListeners.fireClick(this);
99         }
100         break;
101
102       case Event.ONBLUR:
103       case Event.ONFOCUS:
104         if (focusListeners != null) {
105           focusListeners.fireFocusEvent(this, event);
106         }
107         break;
108
109       case Event.ONKEYDOWN:
110       case Event.ONKEYUP:
111       case Event.ONKEYPRESS:
112         if (keyboardListeners != null) {
113           keyboardListeners.fireKeyboardEvent(this, event);
114         }
115         break;
116     }
117   }
118
119   public void removeClickListener(ClickListener listener) {
120     if (clickListeners != null) {
121       clickListeners.remove(listener);
122     }
123   }
124
125   public void removeFocusListener(FocusListener listener) {
126     if (focusListeners != null) {
127       focusListeners.remove(listener);
128     }
129   }
130
131   public void removeKeyboardListener(KeyboardListener listener) {
132     if (keyboardListeners != null) {
133       keyboardListeners.remove(listener);
134     }
135   }
136
137   public void setAccessKey(char key) {
138     DOM.setElementProperty(getElement(), "accessKey", "" + key);
139   }
140
141   /**
142    * Sets whether this widget is enabled.
143    *
144    * @param enabled <code>true</code> to enable the widget, <code>false</code>
145    * to disable it
146    */

147   public void setEnabled(boolean enabled) {
148     DOM.setElementPropertyBoolean(getElement(), "disabled", !enabled);
149   }
150
151   public void setFocus(boolean focused) {
152     if (focused) {
153       impl.focus(getElement());
154     } else {
155       impl.blur(getElement());
156     }
157   }
158
159   public void setTabIndex(int index) {
160     impl.setTabIndex(getElement(), index);
161   }
162
163   protected void setElement(Element elem) {
164     super.setElement(elem);
165     sinkEvents(Event.ONCLICK | Event.FOCUSEVENTS | Event.KEYEVENTS);
166   }
167
168   /**
169    * Fire all current {@link ClickListener}.
170    */

171   void fireClickListeners() {
172     /*
173      * Implementation note: PushButton needs to fire click listeners manually.
174      * Exposing this method so it can do so.
175      */

176     if (clickListeners != null) {
177       clickListeners.fireClick(this);
178     }
179   }
180 }
181
Popular Tags