Mapping events to java

9 views
Skip to first unread message

Rodrigo B. de Oliveira

unread,
Oct 27, 2007, 8:04:40 PM10/27/07
to boo...@googlegroups.com
How would you java people expect the following example to be translated to java?

// the 'callable' below defines a function type that takes two arguments
callable ClickEventHandler(sender as object)

class Button:
event Click as ClickEventHandler

def whatever():
Click(self) // triggers the event

button = Button()
button.Click += do: // subscribe closure to event
print "clicked"
button.Click += do: // subscribe another closure to the event
print "once more"
button.whatever()


Scroll to the end of the message after you have given it some though.


// callable ClickEventHandler(sender as object)
public interface ClickEventHandler extends Callable<object> {
void call(sender as object);
}

class Button {

public void whatever() {
triggerClick(this); // Click(self)
}

// begin 'Click as ClickEventHandler'
protected MulticastCallable<ClickEventHandler> _click;

public void addClickListener(ClickEventHandler handler) {
_click = MulticastCallable<ClickEventHandler>.add(_click, handler);
}

public void removeClickListener(ClickEventHandler handler) {
_click =
MulticastCallable<ClickEventHandler>.remove(_click, handler);
}

protected void triggerClick(Object sender) {
if (_click == null) return;
for (ClickEventHandler handler : _click) {
handler.call(sender);
}
}
// end 'Click as ClickEventHandler'
}

public class Module {

public static void main(String[] argv) {
Button button = new Button();
button.addClickListener(new ClickEventHandler() {
public void call(sender as object) {
System.out.println("clicked");
}
});
button.addClickListener(new ClickEventHandler() {
public void call(sender as object) {
System.out.println("once more");
}
});
button.whatever();
}
}


Thoughts?

--
bamboo

Ricardo Andere de Mello

unread,
Oct 28, 2007, 1:22:05 PM10/28/07
to boo...@googlegroups.com
On 10/28/07, Rodrigo B. de Oliveira <rodrig...@gmail.com> wrote:
 
>How would you java people expect the following example to be translated to java?
>Thoughts?
 
since sneer is involved, maybe something like:
 
button.onclick(Omnivore<Boolean> callable);
 
:)))))
 
hugs, gandhi

dramage

unread,
Oct 28, 2007, 4:48:59 PM10/28/07
to boojay
Yup, I'd support the intuition that your translation is pretty much
the standard (and, sadly, most straightforward) way of implementing
that functionality in pure Java.

dramage

On Oct 27, 5:04 pm, "Rodrigo B. de Oliveira" <rodrigobam...@gmail.com>
wrote:

Reply all
Reply to author
Forward
0 new messages