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