KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > bridge > FocusManager


1 /*
2
3    Copyright 2002-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.bridge;
19
20 import org.w3c.dom.Document JavaDoc;
21 import org.w3c.dom.Element JavaDoc;
22 import org.w3c.dom.events.DocumentEvent JavaDoc;
23 import org.w3c.dom.events.Event JavaDoc;
24 import org.w3c.dom.events.EventListener JavaDoc;
25 import org.w3c.dom.events.EventTarget JavaDoc;
26 import org.w3c.dom.events.MouseEvent JavaDoc;
27 import org.w3c.dom.events.UIEvent JavaDoc;
28
29 /**
30  * A class that manages focus on elements.
31  *
32  * @author <a HREF="mailto:Thierry.Kormann@sophia.inria.fr">Thierry Kormann</a>
33  * @version $Id: FocusManager.java,v 1.7 2004/08/18 07:12:31 vhardy Exp $
34  */

35 public class FocusManager {
36
37     /**
38      * The element that has the focus so far.
39      */

40     protected EventTarget JavaDoc lastFocusEventTarget;
41
42     /**
43      * The document.
44      */

45     protected Document JavaDoc document;
46
47     /**
48      * The EventListener that tracks 'mouseclick' events.
49      */

50     protected EventListener JavaDoc mouseclickListener;
51
52     /**
53      * The EventListener that tracks 'DOMFocusIn' events.
54      */

55     protected EventListener JavaDoc domFocusInListener;
56
57     /**
58      * The EventListener that tracks 'DOMFocusOut' events.
59      */

60     protected EventListener JavaDoc domFocusOutListener;
61
62     /**
63      * The EventListener that tracks 'mouseover' events.
64      */

65     protected EventListener JavaDoc mouseoverListener;
66
67     /**
68      * The EventListener that tracks 'mouseout' events.
69      */

70     protected EventListener JavaDoc mouseoutListener;
71
72     /**
73      * Constructs a new <tt>FocusManager</tt> for the specified document.
74      *
75      * @param doc the document
76      */

77     public FocusManager(Document JavaDoc doc) {
78         document = doc;
79         EventTarget JavaDoc target = (EventTarget JavaDoc)doc;
80
81         mouseclickListener = new MouseClickTacker();
82         target.addEventListener("click", mouseclickListener, true);
83
84         mouseoverListener = new MouseOverTacker();
85         target.addEventListener("mouseover", mouseoverListener, true);
86
87         mouseoutListener = new MouseOutTacker();
88         target.addEventListener("mouseout", mouseoutListener, true);
89
90         domFocusInListener = new DOMFocusInTracker();
91         target.addEventListener("DOMFocusIn", domFocusInListener, true);
92
93         domFocusOutListener = new DOMFocusOutTracker();
94         target.addEventListener("DOMFocusOut", domFocusOutListener, true);
95     }
96
97     /**
98      * Returns the current element that has the focus or null if any.
99      */

100     public EventTarget JavaDoc getCurrentEventTarget() {
101         return lastFocusEventTarget;
102     }
103
104     /**
105      * Removes all listeners attached to the document and that manage focus.
106      */

107     public void dispose() {
108         if (document == null) return;
109         EventTarget JavaDoc target = (EventTarget JavaDoc)document;
110         target.removeEventListener("click", mouseclickListener, true);
111         target.removeEventListener("mouseover", mouseoverListener, true);
112         target.removeEventListener("mouseout", mouseoutListener, true);
113         target.removeEventListener("DOMFocusIn", domFocusInListener, true);
114         target.removeEventListener("DOMFocusOut", domFocusOutListener, true);
115         lastFocusEventTarget = null;
116         document = null;
117     }
118
119     /**
120      * The class that is responsible for tracking 'mouseclick' changes.
121      */

122     protected class MouseClickTacker implements EventListener JavaDoc {
123
124         public void handleEvent(Event JavaDoc evt) {
125             MouseEvent JavaDoc mevt = (MouseEvent JavaDoc)evt;
126             fireDOMActivateEvent(evt.getTarget(), mevt.getDetail());
127         }
128     }
129
130     /**
131      * The class that is responsible for tracking 'DOMFocusIn' changes.
132      */

133     protected class DOMFocusInTracker implements EventListener JavaDoc {
134
135         public void handleEvent(Event JavaDoc evt) {
136             if (lastFocusEventTarget != null &&
137                 lastFocusEventTarget != evt.getTarget()) {
138                 fireDOMFocusOutEvent(lastFocusEventTarget);
139             }
140             lastFocusEventTarget = evt.getTarget();
141         }
142     }
143
144     /**
145      * The class that is responsible for tracking 'DOMFocusOut' changes.
146      */

147     protected class DOMFocusOutTracker implements EventListener JavaDoc {
148
149         public void handleEvent(Event JavaDoc evt) {
150             lastFocusEventTarget = null;
151         }
152     }
153
154     /**
155      * The class that is responsible to update the focus according to
156      * 'mouseover' event.
157      */

158     protected class MouseOverTacker implements EventListener JavaDoc {
159
160         public void handleEvent(Event JavaDoc evt) {
161             EventTarget JavaDoc target = evt.getTarget();
162             fireDOMFocusInEvent(target);
163         }
164     }
165
166     /**
167      * The class that is responsible to update the focus according to
168      * 'mouseout' event.
169      */

170     protected class MouseOutTacker implements EventListener JavaDoc {
171
172         public void handleEvent(Event JavaDoc evt) {
173             EventTarget JavaDoc target = evt.getTarget();
174             fireDOMFocusOutEvent(target);
175         }
176     }
177
178     /**
179      * Fires a 'DOMFocusIn' event to the specified target.
180      *
181      * @param target the event target
182      */

183     protected void fireDOMFocusInEvent(EventTarget JavaDoc target) {
184         DocumentEvent JavaDoc docEvt =
185             (DocumentEvent JavaDoc)((Element JavaDoc)target).getOwnerDocument();
186         UIEvent JavaDoc uiEvt = (UIEvent JavaDoc)docEvt.createEvent("UIEvents");
187         uiEvt.initUIEvent("DOMFocusIn", true, false, null, 0);
188         target.dispatchEvent(uiEvt);
189     }
190
191     /**
192      * Fires a 'DOMFocusOut' event to the specified target.
193      *
194      * @param target the event target
195      */

196     protected void fireDOMFocusOutEvent(EventTarget JavaDoc target) {
197         DocumentEvent JavaDoc docEvt =
198             (DocumentEvent JavaDoc)((Element JavaDoc)target).getOwnerDocument();
199         UIEvent JavaDoc uiEvt = (UIEvent JavaDoc)docEvt.createEvent("UIEvents");
200         uiEvt.initUIEvent("DOMFocusOut", true, false, null, 0);
201         target.dispatchEvent(uiEvt);
202     }
203     
204     /**
205      * Fires a 'DOMActivate' event to the specified target.
206      *
207      * @param target the event target
208      * @param detailArg the detailArg parameter of the event
209      */

210     protected void fireDOMActivateEvent(EventTarget JavaDoc target, int detailArg) {
211         DocumentEvent JavaDoc docEvt =
212             (DocumentEvent JavaDoc)((Element JavaDoc)target).getOwnerDocument();
213         UIEvent JavaDoc uiEvt = (UIEvent JavaDoc)docEvt.createEvent("UIEvents");
214         uiEvt.initUIEvent("DOMActivate", true, true, null, detailArg);
215         target.dispatchEvent(uiEvt);
216     }
217 }
218
Popular Tags