1 18 package org.apache.batik.bridge; 19 20 import org.w3c.dom.Document ; 21 import org.w3c.dom.Element ; 22 import org.w3c.dom.events.DocumentEvent ; 23 import org.w3c.dom.events.Event ; 24 import org.w3c.dom.events.EventListener ; 25 import org.w3c.dom.events.EventTarget ; 26 import org.w3c.dom.events.MouseEvent ; 27 import org.w3c.dom.events.UIEvent ; 28 29 35 public class FocusManager { 36 37 40 protected EventTarget lastFocusEventTarget; 41 42 45 protected Document document; 46 47 50 protected EventListener mouseclickListener; 51 52 55 protected EventListener domFocusInListener; 56 57 60 protected EventListener domFocusOutListener; 61 62 65 protected EventListener mouseoverListener; 66 67 70 protected EventListener mouseoutListener; 71 72 77 public FocusManager(Document doc) { 78 document = doc; 79 EventTarget target = (EventTarget )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 100 public EventTarget getCurrentEventTarget() { 101 return lastFocusEventTarget; 102 } 103 104 107 public void dispose() { 108 if (document == null) return; 109 EventTarget target = (EventTarget )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 122 protected class MouseClickTacker implements EventListener { 123 124 public void handleEvent(Event evt) { 125 MouseEvent mevt = (MouseEvent )evt; 126 fireDOMActivateEvent(evt.getTarget(), mevt.getDetail()); 127 } 128 } 129 130 133 protected class DOMFocusInTracker implements EventListener { 134 135 public void handleEvent(Event evt) { 136 if (lastFocusEventTarget != null && 137 lastFocusEventTarget != evt.getTarget()) { 138 fireDOMFocusOutEvent(lastFocusEventTarget); 139 } 140 lastFocusEventTarget = evt.getTarget(); 141 } 142 } 143 144 147 protected class DOMFocusOutTracker implements EventListener { 148 149 public void handleEvent(Event evt) { 150 lastFocusEventTarget = null; 151 } 152 } 153 154 158 protected class MouseOverTacker implements EventListener { 159 160 public void handleEvent(Event evt) { 161 EventTarget target = evt.getTarget(); 162 fireDOMFocusInEvent(target); 163 } 164 } 165 166 170 protected class MouseOutTacker implements EventListener { 171 172 public void handleEvent(Event evt) { 173 EventTarget target = evt.getTarget(); 174 fireDOMFocusOutEvent(target); 175 } 176 } 177 178 183 protected void fireDOMFocusInEvent(EventTarget target) { 184 DocumentEvent docEvt = 185 (DocumentEvent )((Element )target).getOwnerDocument(); 186 UIEvent uiEvt = (UIEvent )docEvt.createEvent("UIEvents"); 187 uiEvt.initUIEvent("DOMFocusIn", true, false, null, 0); 188 target.dispatchEvent(uiEvt); 189 } 190 191 196 protected void fireDOMFocusOutEvent(EventTarget target) { 197 DocumentEvent docEvt = 198 (DocumentEvent )((Element )target).getOwnerDocument(); 199 UIEvent uiEvt = (UIEvent )docEvt.createEvent("UIEvents"); 200 uiEvt.initUIEvent("DOMFocusOut", true, false, null, 0); 201 target.dispatchEvent(uiEvt); 202 } 203 204 210 protected void fireDOMActivateEvent(EventTarget target, int detailArg) { 211 DocumentEvent docEvt = 212 (DocumentEvent )((Element )target).getOwnerDocument(); 213 UIEvent uiEvt = (UIEvent )docEvt.createEvent("UIEvents"); 214 uiEvt.initUIEvent("DOMActivate", true, true, null, detailArg); 215 target.dispatchEvent(uiEvt); 216 } 217 } 218 | Popular Tags |