EventTarget.cs :  » GUI » SharpVectorGraphics » SharpVectors » Dom » Events » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » GUI » SharpVectorGraphics 
SharpVectorGraphics » SharpVectors » Dom » Events » EventTarget.cs
using System;
using System.Collections;
using System.Xml;

using SharpVectors.Dom.Events;
using SharpVectors.Dom.Svg;

#if TEST
using NUnit.Framework;
#endif

namespace SharpVectors.Dom.Events{
  /// <summary>
  /// Summary description for EventTarget.
  /// </summary>
  public struct EventTarget
    : IEventTargetSupport
  {
    #region Private Fields
    
    private IEventTargetSupport eventTarget;
    
    private EventListenerMap captureMap;
    private EventListenerMap bubbleMap;
    private ArrayList ancestors;
    
    #endregion
    
    #region Constructors
    
    public EventTarget(
      IEventTargetSupport eventTarget)
    {
      this.eventTarget = eventTarget;
      captureMap = new EventListenerMap();
      bubbleMap = new EventListenerMap();
      ancestors = new ArrayList();
    }
    
    #endregion
    
    #region IEventTarget interface
    
    #region Methods
    
    #region DOM Level 2
    
    public void AddEventListener(
      string type,
      EventListener listener,
      bool useCapture)
    {
      if (useCapture)
      {
        captureMap.AddEventListener(null, type, null, listener);
      }
      else
      {
        bubbleMap.AddEventListener(null, type, null, listener);
      }
    }
    
    public void RemoveEventListener(
      string type,
      EventListener listener,
      bool useCapture)
    {
      if (useCapture)
      {
        captureMap.RemoveEventListener(null, type, listener);
      }
      else
      {
        bubbleMap.RemoveEventListener(null, type, listener);
      }
    }
    
    public bool DispatchEvent(
      IEvent @event)
    {
      if (@event.Type == null || @event.Type == "")
      {
        throw new EventException(EventExceptionCode.UnspecifiedEventTypeErr);
      }
      try
      {
        // The actual target may be an SvgElement or an SvgElementInstance from 
        // a conceptual tree <see href="http://www.w3.org/TR/SVG/struct.html#UseElement"/>
        XmlNode currNode = null;    
        ISvgElementInstance currInstance = null;
        
        if (this.eventTarget is ISvgElementInstance)
          currInstance = (ISvgElementInstance)this.eventTarget;
        else
          currNode = (XmlNode)this.eventTarget;
        
        // We can't use an XPath ancestor axe because we must account for 
        // conceptual nodes
        ancestors.Clear();
        
        // Buid the ancestors from the conceptual tree
        if (currInstance != null) 
        {
          while (currInstance.ParentNode != null) 
          {
            currInstance = currInstance.ParentNode;
            ancestors.Add(currInstance);
          }
          currNode = (XmlNode)currInstance.CorrespondingUseElement;
          ancestors.Add(currNode);
        } 
          
        // Build actual ancestors
        while (currNode != null && currNode.ParentNode != null) 
        {
          currNode = currNode.ParentNode;
          ancestors.Add(currNode);
        }
        

        Event realEvent = (Event)@event;        
        realEvent.eventTarget = this.eventTarget;
        
        if (!realEvent.stopped)
        {
          realEvent.eventPhase = EventPhase.CapturingPhase;

          for (int i = ancestors.Count-1; i >= 0; i--)
          {
            if (realEvent.stopped)
            {
              break;
            }
            
            IEventTarget ancestor = ancestors[i] as IEventTarget;
            
            if (ancestor != null)
            {
              realEvent.currentTarget = ancestor;
              
              if (ancestor is IEventTargetSupport)
              {
                ((IEventTargetSupport)ancestor).FireEvent(realEvent);
              }
            }
          }
        }
        
        if (!realEvent.stopped)
        {
          realEvent.eventPhase = EventPhase.AtTarget;
          realEvent.currentTarget = this.eventTarget;
          this.eventTarget.FireEvent(realEvent);
        }
        
        if (!realEvent.stopped)
        {
          realEvent.eventPhase = EventPhase.BubblingPhase;
          
          for (int i = 0; i < ancestors.Count; i++)
          {
            if (realEvent.stopped)
            {
              break;
            }
            
            IEventTarget ancestor = ancestors[i] as IEventTarget;
            
            if (ancestor != null)
            {
              realEvent.currentTarget = ancestor;
              ((IEventTargetSupport)ancestor).FireEvent(realEvent);
            }
          }
        }
        
        return realEvent.stopped;
      }
      catch (InvalidCastException)
      {
        throw new DomException(DomExceptionType.WrongDocumentErr);
      }
    }
    
    #endregion
    
    #region DOM Level 3 Experimental
    
    public void AddEventListenerNs(
      string namespaceUri,
      string type,
      EventListener listener,
      bool useCapture,
      object evtGroup)
    {
      if (useCapture)
      {
        captureMap.AddEventListener(namespaceUri, type, evtGroup, listener);
      }
      else
      {
        bubbleMap.AddEventListener(namespaceUri, type, evtGroup, listener);
      }
    }
    
    public void RemoveEventListenerNs(
      string namespaceUri,
      string type,
      EventListener listener,
      bool useCapture)
    {
      if (useCapture)
      {
        captureMap.RemoveEventListener(namespaceUri, type, listener);
      }
      else
      {
        bubbleMap.RemoveEventListener(namespaceUri, type, listener);
      }
    }
    
    public bool WillTriggerNs(
      string namespaceUri,
      string type)
    {
      XmlNode node = (XmlNode)this.eventTarget;
      XmlNodeList ancestors = node.SelectNodes("ancestor::node()");
      
      for (int i = 0; i < ancestors.Count; i++)
      {
        IEventTarget ancestor = ancestors[i] as IEventTarget;
        
        if (ancestor.HasEventListenerNs(namespaceUri, type))
        {
          return true;
        }
      }
      
      return false;
    }
    
    public bool HasEventListenerNs(
      string namespaceUri,
      string eventType)
    {
      return captureMap.HasEventListenerNs(namespaceUri, eventType) ||
        bubbleMap.HasEventListenerNs(namespaceUri, eventType);
    }
    
    #endregion
    
    #endregion
    
    #region NON-DOM
    
    public void FireEvent(
      IEvent @event)
    {
      switch (@event.EventPhase)
      {
        case EventPhase.AtTarget:
        case EventPhase.BubblingPhase:
          bubbleMap.Lock();
          bubbleMap.FireEvent(@event);
          bubbleMap.Unlock();
          break;
        case EventPhase.CapturingPhase:
          captureMap.Lock();
          captureMap.FireEvent(@event);
          captureMap.Unlock();
          break;
      }
    }
    
    public event EventListener OnMouseMove
    {
      add
      {
        throw new NotImplementedException();
      }
      remove
      {
        throw new NotImplementedException();
      }
    }
    
    #endregion
    
    #endregion
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.