KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > org > w3c > dom > events > EventTarget

org.w3c.dom.events
Interface EventTarget

See Also:
Top Examples, Source Code

void addEventListener(String type,
                      EventListener listener,
                      boolean useCapture)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1595]An utility can be attached to any DOM tree and output stream
By Anonymous on 2005/11/04 20:23:08  Rate
import java.io.*; 
 import java.util.Date; 
 import org.w3c.dom.*; 
 import org.w3c.dom.events.*; 
  
  
 /** 
 This utility can be attached to any DOM tree and output stream: it will 
 catch DOM2 Mutation Events for node insertion and removal and attribute 
 modification and report on them in a simple text format. 
 */
 
 public class SampleEventListener 
     implements EventListener 
  {  
     /** 
     Pass the root of the target tree; thanks to event capturing the SampleEventListener 
     will be notified of changes on all descendants. The constructor registers 
     the SampleEventListener as a listener for "DOMNodeInserted", 
     "DOMNodeRemoved", and "DOMAttrModified" events. 
      ( Note that we capture the output stream as a field 
      (  { @link #out }  ) , but have no need for further reference to the source node. 
     */
 
     public SampleEventListener  ( EventTarget root, OutputStream out )  
      {  
         root.addEventListener  ( "DOMNodeInserted", this, true ) ; 
         root.addEventListener  ( "DOMNodeRemoved", this, true ) ; 
         root.addEventListener  ( "DOMAttrModified", this, true ) ; 
  
  
         this.out = new PrintWriter  ( out ) ; 
      }  
  
  
     /** 
     Write a simple text report of the event. 
     */
 
     public void handleEvent  ( Event ev )  
      {  
         out.print  ( new Date  ( ev.getTimeStamp  (  )  ) .toString  (  )  ) ; 
         out.print  ( ": " ) ; 
         out.print  ( ev.getType  (  )  ) ; 
         out.print  ( " -- " ) ; 
  
  
         Node target =  ( Node )  ev.getTarget  (  ) ; 
         out.print  ( target.getNodeName  (  )  ) ; 
  
  
         if  ( target.getNodeType  (  )  == Node.ELEMENT_NODE )  
          {  
             if  ( target.getFirstChild  (  )  instanceof Text )  
              {  
                 out.print  ( "  ( " ) ; 
                 out.print  (  (  ( Text )  target.getFirstChild  (  )  ) .getData  (  )  ) ; 
                 out.print  ( " ) " ) ; 
              }  
          }  
         else 
          {  
             out.print  ( "  ( " ) ; 
             out.print  ( target.getNodeValue  (  )  ) ; 
             out.print  ( " ) " ) ; 
          }  
  
  
         if  ( ev.getType  (  ) .equals  ( "DOMAttrModified" )  )  
          {  
             out.print  ( ' ' ) ; 
             Attr attr =  ( Attr )   (  ( MutationEvent )  ev ) .getRelatedNode  (  ) ; 
             out.print  ( attr.getName  (  )  ) ; 
             out.print  ( '=' ) ; 
             out.print  ( attr.getValue  (  )  ) ; 
          }  
  
  
         out.println  (  ) ; 
         out.flush  (  ) ; 
      }  
  
  
     PrintWriter out; 
  }  
  
  
 


boolean dispatchEvent(Event evt)
                      throws EventException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void removeEventListener(String type,
                         EventListener listener,
                         boolean useCapture)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags