DomEvent.java 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /* DomEvent.java --
  2. Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.xml.dom;
  32. import gnu.java.lang.CPStringBuilder;
  33. import org.w3c.dom.Node;
  34. import org.w3c.dom.events.Event;
  35. import org.w3c.dom.events.EventTarget;
  36. import org.w3c.dom.events.MutationEvent;
  37. import org.w3c.dom.events.UIEvent;
  38. import org.w3c.dom.views.AbstractView; // used by UIEvent
  39. /**
  40. * "Event" implementation. Events are
  41. * created (through DocumentEvent interface methods on the document object),
  42. * and are sent to any target node in the document.
  43. *
  44. * <p> Applications may define application specific event subclasses, but
  45. * should otherwise use the <em>DocumentTraversal</em> interface to acquire
  46. * event objects.
  47. *
  48. * @author David Brownell
  49. */
  50. public class DomEvent
  51. implements Event
  52. {
  53. String type; // init
  54. EventTarget target;
  55. EventTarget currentNode;
  56. short eventPhase;
  57. boolean bubbles; // init
  58. boolean cancelable; // init
  59. long timeStamp; // ?
  60. /** Returns the event's type (name) as initialized */
  61. public final String getType()
  62. {
  63. return type;
  64. }
  65. /**
  66. * Returns event's target; delivery of an event is initiated
  67. * by a <em>target.dispatchEvent(event)</em> invocation.
  68. */
  69. public final EventTarget getTarget()
  70. {
  71. return target;
  72. }
  73. /**
  74. * Returns the target to which events are currently being
  75. * delivered. When capturing or bubbling, this will not
  76. * be what <em>getTarget</em> returns.
  77. */
  78. public final EventTarget getCurrentTarget()
  79. {
  80. return currentNode;
  81. }
  82. /**
  83. * Returns CAPTURING_PHASE, AT_TARGET, or BUBBLING;
  84. * only meaningful within EventListener.handleEvent
  85. */
  86. public final short getEventPhase()
  87. {
  88. return eventPhase;
  89. }
  90. /**
  91. * Returns true if the news of the event bubbles to tree tops
  92. * (as specified during initialization).
  93. */
  94. public final boolean getBubbles()
  95. {
  96. return bubbles;
  97. }
  98. /**
  99. * Returns true if the default handling may be canceled
  100. * (as specified during initialization).
  101. */
  102. public final boolean getCancelable()
  103. {
  104. return cancelable;
  105. }
  106. /**
  107. * Returns the event's timestamp.
  108. */
  109. public final long getTimeStamp()
  110. {
  111. return timeStamp;
  112. }
  113. boolean stop;
  114. boolean doDefault;
  115. /**
  116. * Requests the event no longer be captured or bubbled; only
  117. * listeners on the event target will see the event, if they
  118. * haven't yet been notified.
  119. *
  120. * <p> <em> Avoid using this </em> except for application-specific
  121. * events, for which you the protocol explicitly "blesses" the use
  122. * of this with some event types. Otherwise, you are likely to break
  123. * algorithms which depend on event notification either directly or
  124. * through bubbling or capturing. </p>
  125. *
  126. * <p> Note that this method is not final, specifically to enable
  127. * enforcing of policies about events always propagating. </p>
  128. */
  129. public void stopPropagation()
  130. {
  131. stop = true;
  132. }
  133. /**
  134. * Requests that whoever dispatched the event not perform their
  135. * default processing when event delivery completes. Initializes
  136. * event timestamp.
  137. */
  138. public final void preventDefault()
  139. {
  140. doDefault = false;
  141. }
  142. /** Initializes basic event state. */
  143. public void initEvent(String typeArg,
  144. boolean canBubbleArg,
  145. boolean cancelableArg)
  146. {
  147. eventPhase = 0;
  148. type = typeArg;
  149. bubbles = canBubbleArg;
  150. cancelable = cancelableArg;
  151. timeStamp = System.currentTimeMillis();
  152. }
  153. /** Constructs, but does not initialize, an event. */
  154. public DomEvent(String type)
  155. {
  156. this.type = type;
  157. }
  158. /**
  159. * Returns a basic printable description of the event's type,
  160. * state, and delivery conditions
  161. */
  162. public String toString()
  163. {
  164. CPStringBuilder buf = new CPStringBuilder("[Event ");
  165. buf.append(type);
  166. switch (eventPhase)
  167. {
  168. case CAPTURING_PHASE:
  169. buf.append(", CAPTURING");
  170. break;
  171. case AT_TARGET:
  172. buf.append(", AT TARGET");
  173. break;
  174. case BUBBLING_PHASE:
  175. buf.append(", BUBBLING");
  176. break;
  177. default:
  178. buf.append(", (inactive)");
  179. break;
  180. }
  181. if (bubbles && eventPhase != BUBBLING_PHASE)
  182. {
  183. buf.append(", bubbles");
  184. }
  185. if (cancelable)
  186. {
  187. buf.append(", can cancel");
  188. }
  189. // were we to provide subclass info, this's where it'd live
  190. buf.append("]");
  191. return buf.toString();
  192. }
  193. /**
  194. * "MutationEvent" implementation.
  195. */
  196. public static final class DomMutationEvent
  197. extends DomEvent
  198. implements MutationEvent
  199. {
  200. // package private
  201. Node relatedNode; // init
  202. private String prevValue; // init
  203. private String newValue; // init
  204. private String attrName; // init
  205. private short attrChange; // init
  206. /** Returns any "related" node provided by this type of event */
  207. public final Node getRelatedNode()
  208. {
  209. return relatedNode;
  210. }
  211. /** Returns any "previous value" provided by this type of event */
  212. public final String getPrevValue()
  213. {
  214. return prevValue;
  215. }
  216. /** Returns any "new value" provided by this type of event */
  217. public final String getNewValue()
  218. {
  219. return newValue;
  220. }
  221. /** For attribute change events, returns the attribute's name */
  222. public final String getAttrName()
  223. {
  224. return attrName;
  225. }
  226. /** For attribute change events, returns how the attribuet changed */
  227. public final short getAttrChange()
  228. {
  229. return attrChange;
  230. }
  231. /** Initializes a mutation event */
  232. public final void initMutationEvent(String typeArg,
  233. boolean canBubbleArg,
  234. boolean cancelableArg,
  235. Node relatedNodeArg,
  236. String prevValueArg,
  237. String newValueArg,
  238. String attrNameArg,
  239. short attrChangeArg)
  240. {
  241. // super.initEvent is inlined here for speed
  242. // (mutation events are issued on all DOM changes)
  243. eventPhase = 0;
  244. type = typeArg;
  245. bubbles = canBubbleArg;
  246. cancelable = cancelableArg;
  247. timeStamp = System.currentTimeMillis();
  248. relatedNode = relatedNodeArg;
  249. prevValue = prevValueArg;
  250. newValue = newValueArg;
  251. attrName = attrNameArg;
  252. attrChange = attrChangeArg;
  253. }
  254. // clear everything that should be GC-able
  255. void clear()
  256. {
  257. type = null;
  258. target = null;
  259. relatedNode = null;
  260. currentNode = null;
  261. prevValue = newValue = attrName = null;
  262. }
  263. /** Constructs an uninitialized mutation event. */
  264. public DomMutationEvent(String type)
  265. {
  266. super(type);
  267. }
  268. }
  269. /**
  270. * "UIEvent" implementation.
  271. */
  272. public static class DomUIEvent
  273. extends DomEvent
  274. implements UIEvent
  275. {
  276. private AbstractView view; // init
  277. private int detail; // init
  278. /** Constructs an uninitialized User Interface (UI) event */
  279. public DomUIEvent (String type) { super (type); }
  280. public final AbstractView getView () { return view; }
  281. public final int getDetail () { return detail; }
  282. /** Initializes a UI event */
  283. public final void initUIEvent(String typeArg,
  284. boolean canBubbleArg,
  285. boolean cancelableArg,
  286. AbstractView viewArg,
  287. int detailArg)
  288. {
  289. super.initEvent(typeArg, canBubbleArg, cancelableArg);
  290. view = viewArg;
  291. detail = detailArg;
  292. }
  293. }
  294. /*
  295. static final class DomMouseEvent extends DomUIEvent
  296. implements MouseEvent
  297. {
  298. // another half dozen state variables/accessors
  299. }
  300. */
  301. }