Created
October 30, 2008 19:32
-
-
Save bakkdoor/21114 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module RSwing | |
module Components | |
module Events | |
module FocusEvents | |
FocusListener = java.awt.event.FocusListener | |
def on_focus(&block) | |
self.add_focus_listener(Listener.create(FocusListener, :focusGained ,&block)) | |
end | |
# i want to be able to do something like this instead of declaring all those | |
# methods by hand all the time, since their body is so similar: | |
# Events.has_event self => :on_focus, FocusListener => :focusGained | |
# this should simply create a method "on_focus" in this module and should take a block | |
# and pass it on to the has_event code which then would pass it on to the Listener.create method. | |
def on_focus_lost(&block) | |
self.add_focus_listener(Listener.create(FocusListener, :focusLost ,&block)) | |
end | |
end | |
end | |
end | |
end | |
# the Events.has_event method looks like this: | |
module RSwing | |
module Components | |
module Events | |
def self.has_event(listener_class_method_hash) | |
module_with_event = listener_class_method_hash.keys.first | |
event_name = listener_class_method_hash.values.first | |
event_listener_class = listener_class_method_hash.keys.last | |
java_method_name = listener_class_method_hash.values.last | |
# this doesnt seem to work. i don't know how to pass a &block to the Listener.create call... | |
module_with_event.module_eval do | |
define_method("#{event_name}") do | |
self.send("add#{event_listener_class}", Listener.create(event_listener_class, java_method_name, &block)) | |
end | |
end | |
#define_method("#{event_name}") do | |
# self.send("add#{event_listener_class}", Listener.create(event_listener_class, java_method_name)) | |
#end | |
end | |
end | |
end | |
end | |
# then, the Listener module looks like this: | |
module RSwing | |
module Components | |
class Listener | |
def self.create(listener_interface, methodname, &block) | |
listener_class = Class.new() do | |
include listener_interface | |
implement listener_interface | |
def initialize(methodname, &block) | |
self.class.instance_eval do | |
define_method(methodname, &block) | |
end | |
end | |
end | |
# neue instanz zurückgeben | |
listener_class.new(methodname, &block) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment