1 /* DomEvent.java --
2    Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
3 
4 This file is part of GNU Classpath.
5 
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.
10 
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.
15 
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.
20 
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.
25 
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. */
37 
38 package gnu.xml.dom;
39 
40 import org.w3c.dom.*;
41 import org.w3c.dom.events.*;
42 import org.w3c.dom.views.AbstractView;		// used by UIEvent
43 
44 /**
45  * "Event" implementation.  Events are
46  * created (through DocumentEvent interface methods on the document object),
47  * and are sent to any target node in the document.
48  *
49  * <p> Applications may define application specific event subclasses, but
50  * should otherwise use the <em>DocumentTraversal</em> interface to acquire
51  * event objects.
52  *
53  * @author David Brownell
54  */
55 public class DomEvent
56   implements Event
57 {
58 
59   String  type;  // init
60   EventTarget  target;
61   EventTarget  currentNode;
62   short  eventPhase;
63   boolean  bubbles; // init
64   boolean  cancelable; // init
65   long  timeStamp; // ?
66 
67   /** Returns the event's type (name) as initialized */
getType()68   public final String getType()
69   {
70     return type;
71   }
72 
73   /**
74    * Returns event's target; delivery of an event is initiated
75    * by a <em>target.dispatchEvent(event)</em> invocation.
76    */
getTarget()77   public final EventTarget getTarget()
78   {
79     return target;
80   }
81 
82   /**
83    * Returns the target to which events are currently being
84    * delivered.  When capturing or bubbling, this will not
85    * be what <em>getTarget</em> returns.
86    */
getCurrentTarget()87   public final EventTarget getCurrentTarget()
88   {
89     return currentNode;
90   }
91 
92   /**
93    * Returns CAPTURING_PHASE, AT_TARGET, or BUBBLING;
94    * only meaningful within EventListener.handleEvent
95    */
getEventPhase()96   public final short getEventPhase()
97   {
98     return eventPhase;
99   }
100 
101   /**
102    * Returns true if the news of the event bubbles to tree tops
103    * (as specified during initialization).
104    */
getBubbles()105   public final boolean getBubbles()
106   {
107     return bubbles;
108   }
109 
110   /**
111    * Returns true if the default handling may be canceled
112    * (as specified during initialization).
113    */
getCancelable()114   public final boolean getCancelable()
115   {
116     return cancelable;
117   }
118 
119   /**
120    * Returns the event's timestamp.
121    */
getTimeStamp()122   public final long getTimeStamp()
123   {
124     return timeStamp;
125   }
126 
127   boolean stop;
128   boolean doDefault;
129 
130   /**
131    * Requests the event no longer be captured or bubbled; only
132    * listeners on the event target will see the event, if they
133    * haven't yet been notified.
134    *
135    * <p> <em> Avoid using this </em> except for application-specific
136    * events, for which you the protocol explicitly "blesses" the use
137    * of this with some event types.  Otherwise, you are likely to break
138    * algorithms which depend on event notification either directly or
139    * through bubbling or capturing.  </p>
140    *
141    * <p> Note that this method is not final, specifically to enable
142    * enforcing of policies about events always propagating. </p>
143    */
stopPropagation()144   public void stopPropagation()
145   {
146     stop = true;
147   }
148 
149   /**
150    * Requests that whoever dispatched the event not perform their
151    * default processing when event delivery completes.  Initializes
152    * event timestamp.
153    */
preventDefault()154   public final void preventDefault()
155   {
156     doDefault = false;
157   }
158 
159   /** Initializes basic event state.  */
initEvent(String typeArg, boolean canBubbleArg, boolean cancelableArg)160   public void initEvent(String typeArg,
161                         boolean canBubbleArg,
162                         boolean cancelableArg)
163   {
164     eventPhase = 0;
165     type = typeArg;
166     bubbles = canBubbleArg;
167     cancelable = cancelableArg;
168     timeStamp = System.currentTimeMillis();
169   }
170 
171   /** Constructs, but does not initialize, an event. */
DomEvent(String type)172   public DomEvent(String type)
173   {
174     this.type = type;
175   }
176 
177   /**
178    * Returns a basic printable description of the event's type,
179    * state, and delivery conditions
180    */
toString()181   public String toString()
182   {
183     StringBuffer buf = new StringBuffer("[Event ");
184     buf.append(type);
185     switch (eventPhase)
186       {
187       case CAPTURING_PHASE:
188         buf.append(", CAPTURING");
189         break;
190       case AT_TARGET:
191         buf.append(", AT TARGET");
192         break;
193       case BUBBLING_PHASE:
194         buf.append(", BUBBLING");
195         break;
196       default:
197         buf.append(", (inactive)");
198         break;
199       }
200     if (bubbles && eventPhase != BUBBLING_PHASE)
201       {
202         buf.append(", bubbles");
203       }
204     if (cancelable)
205       {
206         buf.append(", can cancel");
207       }
208     // were we to provide subclass info, this's where it'd live
209     buf.append("]");
210     return buf.toString();
211   }
212 
213   /**
214    * "MutationEvent" implementation.
215    */
216   public static final class DomMutationEvent
217     extends DomEvent
218     implements MutationEvent
219   {
220 
221     // package private
222     Node   relatedNode; // init
223 
224     private String  prevValue; // init
225     private String  newValue; // init
226 
227     private String  attrName; // init
228     private short  attrChange; // init
229 
230     /** Returns any "related" node provided by this type of event */
getRelatedNode()231     public final Node getRelatedNode()
232     {
233       return relatedNode;
234     }
235 
236     /** Returns any "previous value" provided by this type of event */
getPrevValue()237     public final String getPrevValue()
238     {
239       return prevValue;
240     }
241 
242     /** Returns any "new value" provided by this type of event */
getNewValue()243     public final String getNewValue()
244     {
245       return newValue;
246     }
247 
248     /** For attribute change events, returns the attribute's name */
getAttrName()249     public final String getAttrName()
250     {
251       return attrName;
252     }
253 
254     /** For attribute change events, returns how the attribuet changed */
getAttrChange()255     public final short getAttrChange()
256     {
257       return attrChange;
258     }
259 
260     /** Initializes a mutation event */
initMutationEvent(String typeArg, boolean canBubbleArg, boolean cancelableArg, Node relatedNodeArg, String prevValueArg, String newValueArg, String attrNameArg, short attrChangeArg)261     public final void initMutationEvent(String typeArg,
262                                         boolean canBubbleArg,
263                                         boolean cancelableArg,
264                                         Node relatedNodeArg,
265                                         String prevValueArg,
266                                         String newValueArg,
267                                         String attrNameArg,
268                                         short attrChangeArg)
269     {
270       // super.initEvent is inlined here for speed
271       // (mutation events are issued on all DOM changes)
272       eventPhase = 0;
273       type = typeArg;
274       bubbles = canBubbleArg;
275       cancelable = cancelableArg;
276       timeStamp = System.currentTimeMillis();
277 
278       relatedNode = relatedNodeArg;
279       prevValue = prevValueArg;
280       newValue = newValueArg;
281       attrName = attrNameArg;
282       attrChange = attrChangeArg;
283     }
284 
285     // clear everything that should be GC-able
clear()286     void clear()
287     {
288       type = null;
289       target = null;
290       relatedNode = null;
291       currentNode = null;
292       prevValue = newValue = attrName = null;
293     }
294 
295     /** Constructs an uninitialized mutation event. */
DomMutationEvent(String type)296     public DomMutationEvent(String type)
297     {
298       super(type);
299     }
300 
301   }
302 
303   /**
304    * "UIEvent" implementation.
305    */
306   public static class DomUIEvent
307     extends DomEvent
308     implements UIEvent
309   {
310 
311     private AbstractView view;  // init
312     private int  detail;  // init
313 
314     /** Constructs an uninitialized User Interface (UI) event */
DomUIEvent(String type)315     public DomUIEvent (String type) { super (type); }
316 
getView()317     public final AbstractView getView () { return view; }
getDetail()318     public final int getDetail () { return detail; }
319 
320     /** Initializes a UI event */
initUIEvent(String typeArg, boolean canBubbleArg, boolean cancelableArg, AbstractView viewArg, int detailArg)321     public final void initUIEvent(String typeArg,
322                                   boolean canBubbleArg,
323                                   boolean cancelableArg,
324                                   AbstractView viewArg,
325                                   int detailArg)
326     {
327       super.initEvent(typeArg, canBubbleArg, cancelableArg);
328       view = viewArg;
329       detail = detailArg;
330     }
331 
332   }
333 
334     /*
335 
336     static final class DomMouseEvent extends DomUIEvent
337  implements MouseEvent
338     {
339  // another half dozen state variables/accessors
340     }
341 
342     */
343 
344 }
345 
346