- propertyevents_demo.zip
- article
- PropertyEvents.htm
- PropertyEvents
- checked.PNG
- decoupled.png
- mouseEnterFocused.PNG
- mouseEnterNotFocused.PNG
- mouseLeave.png
- propertyEvents.png
- traffic.PNG
- unchecked.PNG
- code
- MycroXaml
- MyXaml.MxHelpers
- PropertyEventsDemo1
- bin
- Debug
- demo.mycroxaml
- PropertyEventsDemo1.exe
- Demo.cs
- obj
- Debug
- PropertyEventsDemo1.exe
- PropertyEventsDemo1.projdata
- temp
- TempPE
- PropertyEventsDemo1.csproj
- PropertyEventsDemo1.csproj.user
- PropertyEventsDemo2
- bin
- Debug
- demo.mycroxaml
- PropertyEventsDemo2.exe
- Demo.cs
- obj
- Debug
- PropertyEventsDemo2.exe
- PropertyEventsDemo2.projdata
- temp
- TempPE
- PropertyEventsDemo2.csproj
- PropertyEventsDemo2.csproj.user
- PropertyEvents.sln
|
using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;
namespace MyXaml.MxHelpers
{
public class EventTargetNullException : ApplicationException
{
public EventTargetNullException(string msg) : base(msg) {}
}
public class EventBindingException : ApplicationException
{
public EventBindingException(string msg) : base(msg) {}
}
public class SetValueException : ApplicationException
{
public SetValueException(string msg) : base(msg) {}
}
public class NoPropertyException : ApplicationException
{
public NoPropertyException(string msg) : base(msg) {}
}
public interface ISetter
{
void Update(ref bool stop);
}
public interface ICondition
{
bool IsMet(object obj, bool stateValidated);
}
public class CheckState : ISetter
{
protected OnEvent onEvent;
public OnEvent OnEvent
{
get {return onEvent;}
set {onEvent=value;}
}
public void Update(ref bool stop)
{
onEvent.EventHandler(onEvent.Object, EventArgs.Empty);
}
}
public class Stop : ISetter
{
public void Update(ref bool stop)
{
stop=true;
}
}
public class Set : ISetter
{
protected string val;
protected object obj;
protected string property;
public string Value
{
get {return val;}
set {val=value;}
}
public object Object
{
get {return obj;}
set {obj=value;}
}
public string Property
{
get {return property;}
set {property=value;}
}
public void Update(ref bool stop)
{
PropertyInfo pi=obj.GetType().GetProperty(property);
if (pi==null)
{
throw(new NoPropertyException("Property "+property+" not found."));
}
TypeConverter tc=TypeDescriptor.GetConverter(pi.PropertyType);
object v=val;
if (tc.CanConvertFrom(typeof(string)))
{
v=tc.ConvertFromInvariantString(val);
}
try
{
pi.SetValue(obj, v, null);
}
catch(Exception e)
{
throw(new SetValueException(e.Message));
}
}
}
public class OnCondition : ICondition
{
protected object obj;
protected string val;
protected string property;
public object Object
{
get {return obj;}
set {obj=value;}
}
public string Value
{
get {return val;}
set {val=value;}
}
public string Property
{
get {return property;}
set {property=value;}
}
public bool IsMet(object obj, bool stateValidated)
{
// Use object if specified, otherwise, use the event object.
if (this.obj != null)
{
obj=this.obj;
}
PropertyInfo pi=obj.GetType().GetProperty(property);
if (pi==null)
{
throw(new NoPropertyException("Property "+property+" not found."));
}
TypeConverter tc=TypeDescriptor.GetConverter(pi.PropertyType);
object v=val;
if (tc.CanConvertFrom(typeof(string)))
{
v=tc.ConvertFromInvariantString(val);
}
bool ret=v.Equals(pi.GetValue(obj, null));
return ret;
}
}
public class OnOtherCondition : ICondition
{
public bool IsMet(object obj, bool stateValidated)
{
return !stateValidated;
}
}
public class OnAnyCondition : ICondition
{
public bool IsMet(object obj, bool stateValidated)
{
return true;
}
}
public class OnEvent : ISupportInitialize
{
protected object obj;
protected string ev;
protected ArrayList states;
public object Object
{
get {return obj;}
set {obj=value;}
}
public string Event
{
get {return ev;}
set {ev=value;}
}
public ArrayList States
{
get {return states;}
}
public OnEvent()
{
states=new ArrayList();
}
public void BeginInit()
{
}
public void EndInit()
{
if (obj==null)
{
throw(new EventTargetNullException("Event target is null."));
}
EventInfo ei=obj.GetType().GetEvent(ev, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
if (ei==null)
{
throw(new EventBindingException("Can't find event "+ev));
}
Delegate dlgt=null;
try
{
dlgt=Delegate.CreateDelegate(ei.EventHandlerType, this, "EventHandler");
}
catch {}
if (dlgt==null)
{
throw(new EventBindingException("Can't create delegate generic for event "+ev));
}
ei.AddEventHandler(obj, dlgt);
}
public void EventHandler(object sender, EventArgs ea)
{
Trace.WriteLine("Event");
bool stateValidated=false;
bool stop=false;
foreach(State state in states)
{
stateValidated|=state.Test(sender, stateValidated, ref stop);
if (stop)
{
break;
}
}
}
}
public class State
{
protected string description;
protected ArrayList conditions;
protected ArrayList setters;
public string Description
{
get {return description;}
set {description=value;}
}
public ArrayList Conditions
{
get {return conditions;}
}
public ArrayList Setters
{
get {return setters;}
}
public State()
{
conditions=new ArrayList();
setters=new ArrayList();
}
public bool Test(object obj, bool stateValidated, ref bool stop)
{
bool qualified=true;
foreach(ICondition cond in conditions)
{
bool ret=cond.IsMet(obj, stateValidated);
qualified&=ret;
if (!ret)
{
break;
}
}
if (qualified)
{
foreach(ISetter setter in setters)
{
setter.Update(ref stop);
if (stop)
{
break;
}
}
}
return qualified;
}
}
public class MxObjectStateManagement
{
protected ArrayList events;
public ArrayList Events
{
get {return events;}
}
public MxObjectStateManagement()
{
events=new ArrayList();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.