diff --git a/CSharpTranslator/tests/Tester/Main.cs b/CSharpTranslator/tests/Tester/Main.cs index aee0f3c..de099f6 100644 --- a/CSharpTranslator/tests/Tester/Main.cs +++ b/CSharpTranslator/tests/Tester/Main.cs @@ -1,25 +1,26 @@ -using System; -namespace Tester -{ - public class MainClass - { - public static void Main (string[] args) - { - - //Tester.DelegateUser.DelegateTest.DelMain(args); - - //Tester.RefOut.RefOutTest.RefOutMain(args); - - //Tester.PartialUser.PartialMain(args); - - //Tester.Delegates.EventHandler.Sample.EHMain(); - - // Tester.Misc.RenameTest.RNMain("hello world"); - //Tester.Misc.Sorter.Example.SorterMain(); +using System; +namespace Tester +{ + public class MainClass + { + public static void Main (string[] args) + { + + //Tester.DelegateUser.DelegateTest.DelMain(args); + + //Tester.RefOut.RefOutTest.RefOutMain(args); + + //Tester.PartialUser.PartialMain(args); + + //Tester.Delegates.EventHandler.Sample.EHMain(); + + // Tester.Misc.RenameTest.RNMain("hello world"); + //Tester.Misc.Sorter.Example.SorterMain(); //Tester.Misc.RegexTest.RegexMain(); Twiglet.Sample.Delegate.DelegateSampler.DelegateSamplerMain(); - Console.WriteLine("Finished"); - } - } -} - + Twiglet.Sample.Event.EventSampler.EventSamplerMain(args); + Console.WriteLine("Finished"); + } + } +} + diff --git a/CSharpTranslator/tests/Tester/Tester.csproj b/CSharpTranslator/tests/Tester/Tester.csproj index 13ab7f4..20b3b8b 100644 --- a/CSharpTranslator/tests/Tester/Tester.csproj +++ b/CSharpTranslator/tests/Tester/Tester.csproj @@ -61,6 +61,7 @@ + diff --git a/CSharpTranslator/tests/Tester/WebEvents/EventSampler.cs b/CSharpTranslator/tests/Tester/WebEvents/EventSampler.cs new file mode 100755 index 0000000..118eff3 --- /dev/null +++ b/CSharpTranslator/tests/Tester/WebEvents/EventSampler.cs @@ -0,0 +1,158 @@ +// Copyright 2011 Kevin Glynn (http://www.twigletsoftware.com) +// +// The MIT License (MIT) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of this software +// and associated documentation files (the "Software"), to deal in the Software without restriction, +// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all copies or substantial +// portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT +// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +using System; +using System.Collections.Generic; + +namespace Twiglet.Sample.Event +{ + + /// + /// Sample class to show off CS2J's translations for events + /// + public class StorePublisher + { + private List store = null; + + // The events raised by this Store Publisher + public event EventHandler RaiseClearedEvent; + public event EventHandler RaiseStoredEvent; + + // raise the Clear event + protected virtual void OnRaiseClearedEvent(ClearEventArgs e) + { + if (RaiseClearedEvent != null) + { + // invoke the waiting subscribers. + RaiseClearedEvent(this, e); + } + } + + // raise the Store event + protected virtual void OnRaiseStoredEvent(StoreEventArgs e) + { + if (RaiseStoredEvent != null) + { + // invoke the waiting subscribers. + RaiseStoredEvent(this, e); + } + } + + public void Clear(string requestor) + { + store = new List(); + OnRaiseClearedEvent(new ClearEventArgs(requestor)); + } + + public void Add(string requestor, string data) + { + if (store == null) + { + Clear("Add, needed to service " + requestor); + } + store.Add(data); + OnRaiseStoredEvent(new StoreEventArgs(requestor, data)); + } + } + + + /// + /// EventArgs derived class to pass info about a request to clear store + /// + public class ClearEventArgs : EventArgs + { + public ClearEventArgs(string req) { this.Requestor = req; } + public string Requestor { get; set; } + } + + /// + /// EventArgs derived class to pass info about a request to add data to store + /// + public class StoreEventArgs : EventArgs + { + public StoreEventArgs(string req, string data) { this.Requestor = req; this.Data = data; } + public string Requestor { get; set; } + public string Data { get; set; } + } + + /// + /// Info about the type of subscription a subscriber requested + /// + public enum SubscriptionType { NewSubscriber, ClearRequest, StoreData } + + /// + /// EventArgs derived class to pass info about a new subscriber + /// + public class SubscribeEventArgs : EventArgs + { + public SubscribeEventArgs(string sub, SubscriptionType ty) { this.Subscriber = sub; this.SubnType = ty; } + public string Subscriber { get; set; } + public SubscriptionType SubnType { get; set; } + } + + public class StoreSubscriber + { + public string Name { get; set; } + + public StoreSubscriber(string n) + { + Name = n; + } + + public void SubscribeToStore(StorePublisher store) { + store.RaiseClearedEvent += new EventHandler(OnClearEvent); + store.RaiseStoredEvent += new EventHandler(OnStoreEvent); + } + + private void OnClearEvent(object sender, ClearEventArgs e) + { + Console.WriteLine(Name + ": " + e.Requestor + " cleared the Store"); + } + private void OnStoreEvent(object sender, StoreEventArgs e) + { + Console.WriteLine(Name + ": " + e.Requestor + " added " + e.Data + " to the Store"); + } + } + + public static class EventSampler + { + public static string MYID = "MAIN"; + + public static void EventSamplerMain(string[] args) + { + + StorePublisher s = new StorePublisher(); + s.Add(MYID, "Store Ham"); + + // Add two subscribers + StoreSubscriber sub1 = new StoreSubscriber("Subscriber A"); + sub1.SubscribeToStore(s); + + s.Add(MYID, "Store Eggs"); + + StoreSubscriber sub2 = new StoreSubscriber("Subscriber B"); + sub2.SubscribeToStore(s); + s.Add(MYID, "Store Milk"); + + s.Add("Main", "Hello World"); + } + + } + +}