Module: Rubeus::Awt::Event

Defined in:
lib/rubeus/awt/event.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

NULL_METHOD =
Proc.new{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



3
4
5
# File 'lib/rubeus/awt/event.rb', line 3

def self.included(klass)
  klass.extend(ClassMethods)
end

Instance Method Details

#event_methods(event_type, *methods) ⇒ Object



34
35
36
37
38
# File 'lib/rubeus/awt/event.rb', line 34

def event_methods(event_type, *methods)
  listener_interface = listener_interface(event_type)
  listener_interface.declared_instance_methods.map{|method|
    method.name.underscore}
end

#event_typesObject



30
31
32
# File 'lib/rubeus/awt/event.rb', line 30

def event_types
  self.methods.map{|m| /^add\_(.*?)\_listener$/ =~ m ? $1 : nil }.compact.sort
end

#eventsObject



25
26
27
28
# File 'lib/rubeus/awt/event.rb', line 25

def events
  self.event_types.inject({}){|dest, event_type|
    dest[event_type] = event_methods(event_type); dest}
end

#find_java_method(method_name, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rubeus/awt/event.rb', line 13

def find_java_method(method_name, &block)
  klass = self.java_class
  while klass
    method = klass.declared_instance_methods.detect do |m|
      (m.name == method_name) and (block_given? ? yield(m) : true)
    end
    return method if method
    klass = klass.superclass
  end
  nil
end

#listen(event_type, *methods, &block) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rubeus/awt/event.rb', line 74

def listen(event_type, *methods, &block)
  options = methods.last.is_a?(Hash) ? methods.pop : {}
  filters = build_listener_filters(options)
  listener_block = filters.empty? ? block :
    Proc.new do |event|
      block.call(event) if filters.all?{|filter| filter.call(event)}
    end

  listener_interface = listener_interface(event_type)
  lister_methods = listener_interface.declared_instance_methods.map{|method| method.name}
  handling_methods = methods.empty? ?
    lister_methods :
    methods.map{|method| invokable_method(lister_methods, event_type, method)}.compact
  mod = Module.new do
    lister_methods.each do |listener_method|
      if handling_methods.include?(listener_method)
        define_method(listener_method){|*args| listener_block.call(*args)}
      else
        define_method(listener_method, &NULL_METHOD)
      end
    end
  end
  method_name = "add#{event_type.to_s.camelize}Listener"
  listener = Object.new
  listener.extend(mod)
  send(method_name, listener)
  listener
end