raising events in chaining classes - events

say I have three classes: class1, control1 and form1; form1 instantiate contorl. and control1 instantiate class1, the later produces some event that I need to 'bypass' to form1, to achieve that I have made an intermediate function as shown below:
public delegate void TestHandler(String^ str);
public ref Class class1
{
event TestHandler^ TestHappen;
void someFunction()
{
TestHappen("test string");
}
};
public ref Class control1
{
event TestHandler^ TestHappen;
class1^ class1Obj;
control1()
{
class1Obj= gcnew class1();
class1Obj->TestHappen+= gcnew TestHandler(this,&control1::onTest);
}
void onTest(String^ str)
{
TestHappen(str);
}
};
public ref Class form1
{
control1^ control1Obj;
form1()
{
control1Obj= gcenw control1();
control1Obj->TestHappen+= gcnew TestHandler(this,&form1::onTest);
}
void onTest(String^ str)
{
//do something with the string...
}
};
I don't want to use class1 in form1, are there a way to remove the intermediate onTest() function.

Yes, if you use a custom event, you can write its add-handler and remove-handler functions so that they add and remove the delegate directly from another object's event.
For example:
public ref class control1 // in "ref class", class is lowercase!
{
class1 class1Obj; // stack-semantics syntax, locks class1Obj lifetime to be same as the containing control1 instance
public:
event TestHandler^ TestHappen {
void add(TestHandler^ handler) { class1Obj.TestHappen += handler; }
void remove(TestHandler^ handler) { class1Obj.TestHappen -= handler; }
}
};

Related

Delegate in Xamarin.iOS

I have a problem in creating class for xamarin.ios delegate.
In iOS we use protocols to implement the delegate but here I can not implement Interface as a delegate.
Let me clear this concept.
I have an interface in one file like:
public interface SendBackDelegate
{
void sendBackData();
}
public class SelectList
{
}
In other file I have main class like this:
public class ShowList: SendBackDelegate
{
public ShowList()
{
SelectList obj = new SelectList();
obj.delegate = this;
}
void sendBackData()
{
Console.WriteLine("Send Back DATA");
}
}
Now Can you please tell me how this interface be implemented in SelectList class?
Thanks
you can use Interface like protocol, only you have to make reference object of interfaces it can own the class object which already implemented the Interface methods. it will be clear with your example as well.
public interface SendBackDelegate
{
void sendBackData();
}
public class SelectList
{
ShowList showListObj = new ShowList();
SendBackDelegate delegate = showListObj;
delegate.sendBackData();
//this will call the method sendBackData() of class ShowList.
}
public class ShowList: SendBackDelegate
{
public ShowList()
{
SelectList obj = new SelectList();
obj.delegate = this;
}
void sendBackData()
{
Console.WriteLine("Send Back DATA");
}
}
Please let me know if you still have questions.
I am not using delegates anymore on Xamarin.iOS. I prefer to use actions though.
class MainClass
{
public static void Main (string[] args)
{
var x = new ShowList ();
}
}
public interface SendBackDelegate
{
void sendBackData ();
}
public class ShowList : SendBackDelegate
{
public ShowList ()
{
SelectList obj = new SelectList (sendBackData);
}
public void sendBackData ()
{
Console.WriteLine ("Send Back DATA");
}
}
public class SelectList
{
Action _callback;
public SelectList (Action callback)
{
_callback = callback;
Ticker ();
}
private void Ticker ()
{
_callback ();
}
}

JavaFX. Register eventHandler in custom class

I try register eventHandler in my custom class. I don't know what interface or methods I have to implement for having addEventHandler method in my custom class. For this reason my Model class extends Rectangle (Rectangle class has addEventHandler mechanism).
Also I don't know why assigned source object not working (please see comment in Controller class).
Creating custom events I make by this tutorial: https://stackoverflow.com/a/27423430/3102393.
Project Structure
Controller
package sample;
import javafx.event.Event;
public class Controller {
private Model model;
public Controller() {
model = new Model();
model.addEventHandler(MyEvent.ROOT_EVENT, this::handler);
}
private void handler(MyEvent event) {
if(event.getEventType().equals(MyEvent.INSTANCE_CREATED)) {
// Why is event.getSource() instence of Rectangle and not instance of assigned MyObject?
Object obj = event.getSource();
System.out.println(event.getMyObject().getText());
}
}
public void clickedCreate(Event event) {
model.makeEvent();
}
}
Model
package sample;
import javafx.scene.shape.Rectangle;
import java.util.ArrayList;
public class Model extends Rectangle {
private ArrayList<MyObject> objects = new ArrayList<>();
private Integer counter = 0;
public void makeEvent() {
MyObject object = new MyObject((++counter).toString() + "!");
objects.add(object);
fireEvent(new MyEvent(object, null, MyEvent.INSTANCE_CREATED));
}
}
Custom event MyEvent
package sample;
import javafx.event.Event;
import javafx.event.EventTarget;
import javafx.event.EventType;
public class MyEvent extends Event {
public static final EventType<MyEvent> ROOT_EVENT = new EventType<>(Event.ANY, "ROOT_EVENT");
public static final EventType<MyEvent> INSTANCE_CREATED = new EventType<>(ROOT_EVENT, "INSTANCE_CREATED ");
public static final EventType<MyEvent> INSTANCE_DELETED = new EventType<>(ROOT_EVENT, "INSTANCE_DELETED");
private MyObject object;
public MyEvent(MyObject source, EventTarget target, EventType<MyEvent> eventType) {
super(source, target, eventType);
object = source;
}
public MyObject getMyObject() {
return object;
}
}
And finally MyObject
package sample;
public class MyObject {
private String text;
MyObject(String text) {
this.text = text;
}
public String getText() {
return text;
}
}
Note (and question): I also tried using a ObservableList of instances of MyObjects, but I think that there is no notify for updating instance attribute.
Basics of Events
Events are fired using Event.fireEvent which works in 2 steps:
Build the EventDispatchChain using EventTarget.buildEventDispatchChain.
Pass the Event to the first EventDispatcher in the resulting EventDispatchChain.
This code snippet demonstrates the behaviour:
EventTarget target = new EventTarget() {
#Override
public EventDispatchChain buildEventDispatchChain(EventDispatchChain tail) {
return tail.append(new EventDispatcher() {
#Override
public Event dispatchEvent(Event event, EventDispatchChain tail) {
System.out.println("Dispatch 1");
tail.dispatchEvent(event);
return event;
}
}).append(new EventDispatcher() {
#Override
public Event dispatchEvent(Event event, EventDispatchChain tail) {
System.out.println("Dispatch 2");
tail.dispatchEvent(event);
return event;
}
});
}
};
Event.fireEvent(target, new Event(EventType.ROOT));
It prints
Dispatch 1
Dispatch 2
As you can see, the way the EventTarget constructs the EventDispatchChain is totally up to the EventTarget.
This explains why you have to implement addEventHandler ect. yourself.
How it's done for Nodes
This is described in detail in the article JavaFX: Handling Events - 1 Processing Events on the Oracle website.
The important details are:
Different source objects are used during the event handling.
EventHandlers / EventFilters are used during the event dispatching (2.).
This explains why the source value is unexpected.
How to implement addEventHandler
It's not that hard to do this, if you leave out the event capturing and bubbling. You just need to store the EventHandlers by type in a Map<EventType, Collection>> and call the EventHandlers for each type in the EventType hierarchy:
public class EventHandlerTarget implements EventTarget {
private final Map<EventType, Collection<EventHandler>> handlers = new HashMap<>();
public final <T extends Event> void addEventHandler(EventType<T> eventType, EventHandler<? super T> eventHandler) {
handlers.computeIfAbsent(eventType, (k) -> new ArrayList<>())
.add(eventHandler);
}
public final <T extends Event> void removeEventHandler(EventType<T> eventType, EventHandler<? super T> eventHandler) {
handlers.computeIfPresent(eventType, (k, v) -> {
v.remove(eventHandler);
return v.isEmpty() ? null : v;
});
}
#Override
public final EventDispatchChain buildEventDispatchChain(EventDispatchChain tail) {
return tail.prepend(this::dispatchEvent);
}
private void handleEvent(Event event, Collection<EventHandler> handlers) {
if (handlers != null) {
handlers.forEach(handler -> handler.handle(event));
}
}
private Event dispatchEvent(Event event, EventDispatchChain tail) {
// go through type hierarchy and trigger all handlers
EventType type = event.getEventType();
while (type != Event.ANY) {
handleEvent(event, handlers.get(type));
type = type.getSuperType();
}
handleEvent(event, handlers.get(Event.ANY));
return event;
}
public void fireEvent(Event event) {
Event.fireEvent(this, event);
}
}

Events: Modify value on Form 1 -> set Global Variable and raise event -> update value on Form 2

I'm spinning my wheels with what I think is a basic concept: receiving a 'data changed' notification from a GlobalData class.
I have Form1 with a single control on it (textBox1) which, when the number is changed, I set a corresponding property on 'global variable' class.
Now, when the property is set, I want to raise an event that Form2 can subscribe to and updates its indicator accordingly.
The basic structure is:
Form1 has textBox1 and button1, Form2 has textBox2, and GlobalClass has testValue.
Form1 stuff....
public partial class Form1 : Form {
public GlobalClass myGlobals= new GlobalClass();
private void button1_Click(object sender, EventArgs e) {
myGlobals.testValue= Convert.ToInt32(textBox1.Text);
}
}
GlobalClass stuff...
public class GlobalClass{
private int mynum;
public int testValue{
get{
return mynum;}
set{
mynum = value;
// NEED TO RAISE MY 'HEY YOUR testVALUE HAS CHANGED!!' EVENT HERE?
}
}
}
Form2 stuff
public partial class Form2 : Form {
public GlobalClass myGlobals2;
//NEED TO RECEIVE NOTIFICATION THAT myGlobals2.testValue HAS BEEN UPDATED?
//THEN I CAN DO textBox2.Text = myGlobals2.testValue.ToString();
}
Thank you in advance for the help.
You need to read on .NET events and delegates.
You have to make the GlobalClass instance a true global variable:
public class GlobalClass
{
public static GlobalClass Instance = new GlobalClass();
public event EventHandler ValueChanged;
private int mynum;
public int testValue{
get{
return mynum;}
set{
if (mynum != value)
{
mynum = value;
// NEED TO RAISE MY 'HEY YOUR testVALUE HAS CHANGED!!' EVENT HERE?
if (ValueChanged != null)
{
EventArgs e = new EventArgs();
ValueChanged(this, e);
}
}
}
}
}
and address it like this:
GlobalClass.Instance.testValue = 3;
in Form2:
protected void OnValueChanged(Object sender, EventArgs e)
{
<your code>
}
In Form2 constructor:
GlobalClass.Instance.ValueChanged += new System.EventHandler(this.OnValueChanged);

Demultiplex / delegate for GWT event system, using enums

I have an enum, say Fruits { Apple, Banana, Cherry }. I want to write a event subsystem for my application so that I can have the following pattern :
class AppleListener implements HasFruitPriceChangeListener<Apple> {
onFruitPriceChange(int newPrice) {
// do stuff
}
}
and a single listener, that can delegate tasks in the following format:
class FruitPriceListener {
public void onPriceChange(EnumMap<Fruits, Integer> pricePoints) {
// for each fruit, throw the event as
// FruitPriceChange.fire(Apple, newPrice);
}
}
Is there a way to do it in the above manner ? I would probably like to use ValueChangeEvent, but creating another 1 event and handler is also fine too. What I do not want to do is have event/class definitions for each item, like AppleFruitPriceChangeEvent, and so on.
You can use the EventBus for this things, which google suggested ( http://www.youtube.com/watch?v=PDuhR18-EdM ) Here how to use it.
Your globl Eventbus
public static SimpleEventBus bus = new SimpleEventBus();
Your change event:
import com.google.gwt.event.shared.GwtEvent;
import eyeweb.client.gwtMessages.JSPollingEntry;
public class EventModified extends GwtEvent<EventModifiedHandler> {
public final static Type<EventModifiedHandler> TYPE = new Type<EventModifiedHandler>();
private final Fruits fruits;
public final JSPollingEntry getPollingMessage(){
return fruits;
}
public EventModified(Fruits fruits) {
this.fruits = fruits;
}
#Override
public com.google.gwt.event.shared.GwtEvent.Type<EventModifiedHandler> getAssociatedType() {
return TYPE;
}
#Override
protected void dispatch(EventModifiedHandler handler) {
handler.onUpdateRecivde(this);
}
}
the handler for the event
package eyeweb.client.eventbus;
import com.google.gwt.event.shared.EventHandler;
public interface EventModifiedHandler extends EventHandler {
public void onUpdateRecivde(EventModified handler);
}
The event when something changes
EventBus.bus.fireEvent(new EventModified(fruite));
and the handler which gets the event
EventBus.bus.addHandler(EventModified .TYPE, new EventModifiedHandler() {
#Override
public void onMessageSend(EventSendData e) {
//... do stuff }
});
Well that sould be all ;)
Regards,
Stefan
So the solution I came up with was:
Create the enum, and associate a GwtEvent.Type with them:
enum Fruits {
Apple, Banana, Cherry;
public GwtEvent.Type getGwtEventType() {
return new GwtEvent.Type();
}
}
Create a new event.
class FruitPriceChangeEvent extends GwtEvent<?> {
private final Fruit fruit;
FruitPriceChangeEvent(Fruit fruitEnum) {
this.fruit = fruitEnum;
}
#Override
public GwtEvent.Type<?> getAssociatedType() {
return fruit.getGwtEventType();
}
// ... other stuff...
}
And then pass it through the whole event handler loop as #Stefan has mentioned. The beauty/hack of this approach is that the SimpleEventBus maintains a HashMap<GwtEvent.Type, List<HasHandlers>> from which to get the events, and everytime you create a new GwtEvent.Type it generates a unique hashcode (check the implementation for more details).
References:
http://grepcode.com/file/repo1.maven.org/maven2/com.google.gwt/gwt-servlet/2.1.1-rc1/com/google/gwt/event/shared/GwtEvent.java?av=f
http://grepcode.com/file/repo1.maven.org/maven2/com.google.gwt/gwt-servlet/2.1.1-rc1/com/google/gwt/event/shared/SimpleEventBus.java?av=f

Contravariance problems with event properties

Suppose I have a simple EventArgs subclass:
class MyArgs : EventArgs { }
Consider I have two classes with events:
class MyData {
public event EventHandler<MyArgs> Method;
}
class MyObject {
public event EventHandler Method;
}
And a simple program that uses them:
static void Main(string[] args){
MyObject o = new MyObject();
o.Method += MyMethod;
MyData data = new MyData();
data.Method += MyMethod;
}
static void MyMethod(object sender, EventArgs e) { }
Thanks to Contravariance, MyMethod counts as both an EventHandler and an EventHandler<MyArgs>. However, if I change MyObject's event handler into a property that forwards the method to a MyData:
class MyObject {
MyData data = new MyData();
public event EventHandler Method {
add { data.Method += value; }
remove { data.Method += value; }
}
}
The event property is unable to forward the EventHandler to the EventHandler. This seems strange to me because it seems to fall into the contravariance category - a handler with a weaker signature (base classes) should be able to accept arguments with a stronger signature (subclasses).
Why won't C# let me do this? Is there a way to tunnel a generic EventHandler down through an event property into an EventHandler? Is there some sort of legal cast that can be performed on the delegates?
There is no implicit conversion from EventHandler to EventHandler<T>. However since both are of compatible types with each other, you could just pass the EventHandler to the constructor to "convert" it.
public event EventHandler Method
{
add { data.Method += new EventHandler<MyArgs>(value); }
remove { data.Method -= new EventHandler<MyArgs>(value); }
}

Resources