rubeus 0.0.1-java → 0.0.2-java
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rubeus/awt/attributes.rb +51 -0
- data/lib/rubeus/awt/event.rb +133 -0
- data/lib/rubeus/awt/extensions/component.rb +14 -0
- data/lib/rubeus/awt/extensions/container.rb +6 -0
- data/lib/rubeus/awt/extensions/dimension.rb +24 -0
- data/lib/rubeus/awt/extensions.rb +4 -0
- data/lib/rubeus/awt/nestable.rb +88 -0
- data/lib/rubeus/awt/setters.rb +26 -0
- data/lib/rubeus/awt.rb +14 -0
- data/lib/rubeus/component_loader.rb +103 -0
- data/lib/rubeus/swing/extensions/box_layout.rb +28 -0
- data/lib/rubeus/swing/extensions/j_applet.rb +7 -0
- data/lib/rubeus/swing/extensions/j_component.rb +19 -0
- data/lib/rubeus/swing/extensions/j_frame.rb +27 -0
- data/lib/rubeus/swing/extensions/j_panel.rb +9 -0
- data/lib/rubeus/swing/extensions/j_scroll_pane.rb +7 -0
- data/lib/rubeus/swing/extensions/j_split_pane.rb +15 -0
- data/lib/rubeus/swing/extensions/j_text_field.rb +5 -0
- data/lib/rubeus/swing/extensions/j_window.rb +3 -0
- data/lib/rubeus/swing/extensions.rb +4 -0
- data/lib/rubeus/swing.rb +42 -0
- data/lib/rubeus.rb +10 -0
- metadata +40 -5
- data/lib/swing_ext.rb +0 -295
@@ -0,0 +1,51 @@
|
|
1
|
+
module Rubeus::Awt
|
2
|
+
module Attributes
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
base.instance_eval do
|
6
|
+
alias :new_without_attributes :new
|
7
|
+
alias :new :new_with_attributes
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def new_with_attributes(*args, &block)
|
13
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
14
|
+
result = self.new_without_attributes(*args, &block)
|
15
|
+
attributes = ((@class_to_default_attributes || {})[self.java_class.name] || {}).merge(options)
|
16
|
+
if respond_to?(:update_attributes)
|
17
|
+
update_attributes(result, attributes)
|
18
|
+
else
|
19
|
+
attributes.each do |attr, value|
|
20
|
+
begin
|
21
|
+
result.send("#{attr.to_s}=", value)
|
22
|
+
rescue
|
23
|
+
raise ArgumentError, "Failed setting #{value.inspect} to #{attr.inspect} cause of #{$!.to_s}"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
result
|
28
|
+
end
|
29
|
+
|
30
|
+
def default_attributes(attributes = nil)
|
31
|
+
self.default_attributes = attributes if attributes
|
32
|
+
result = {}
|
33
|
+
@class_to_default_attributes ||= {}
|
34
|
+
classes = self.ancestors.select{|klass|
|
35
|
+
!klass.java_class.interface? and klass.respond_to?(:default_attributes)}
|
36
|
+
classes.reverse.each do |klass|
|
37
|
+
if attrs = @class_to_default_attributes[klass.java_class.name]
|
38
|
+
result.update(attrs)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
result
|
42
|
+
end
|
43
|
+
|
44
|
+
def default_attributes=(attributes)
|
45
|
+
@class_to_default_attributes ||= {}
|
46
|
+
@class_to_default_attributes[self.java_class.name] ||= (attributes || {})
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,133 @@
|
|
1
|
+
module Rubeus::Awt
|
2
|
+
module Event
|
3
|
+
def self.included(klass)
|
4
|
+
klass.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def uncapitalize(str)
|
9
|
+
str[0..0].downcase + str[1..-1]
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def find_java_method(method_name, &block)
|
14
|
+
klass = self.java_class
|
15
|
+
while klass
|
16
|
+
method = klass.declared_instance_methods.detect do |m|
|
17
|
+
(m.name == method_name) and (block_given? ? yield(m) : true)
|
18
|
+
end
|
19
|
+
return method if method
|
20
|
+
klass = klass.superclass
|
21
|
+
end
|
22
|
+
nil
|
23
|
+
end
|
24
|
+
|
25
|
+
def events
|
26
|
+
self.event_types.inject({}){|dest, event_type|
|
27
|
+
dest[event_type] = event_methods(event_type); dest}
|
28
|
+
end
|
29
|
+
|
30
|
+
def event_types
|
31
|
+
self.methods.map{|m| /^add\_(.*?)\_listener$/ =~ m ? $1 : nil }.compact.sort
|
32
|
+
end
|
33
|
+
|
34
|
+
def event_methods(event_type, *methods)
|
35
|
+
listener_interface = listener_interface(event_type)
|
36
|
+
listener_interface.declared_instance_methods.map{|method|
|
37
|
+
method.name.underscore}
|
38
|
+
end
|
39
|
+
|
40
|
+
NULL_METHOD = Proc.new{}
|
41
|
+
|
42
|
+
private
|
43
|
+
def build_hash_comparision(options, option_key, method, inverse, comparision_options = nil)
|
44
|
+
comparision_options ||= options[option_key]
|
45
|
+
return nil unless comparision_options
|
46
|
+
"#{option_key.inspect} option must be a hash" unless comparision_options.is_a?(Hash)
|
47
|
+
Proc.new do |event|
|
48
|
+
matched = comparision_options.send(method){|key, value| event.send(key) == value}
|
49
|
+
inverse ? !matched : matched
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def build_listener_filters(options)
|
54
|
+
filters = []
|
55
|
+
[:if, :unless].each do |condition|
|
56
|
+
[:any, :all].each do |joiner|
|
57
|
+
filters << build_hash_comparision(options,
|
58
|
+
"#{condition.to_s}_#{joiner.to_s}".to_sym,
|
59
|
+
"#{joiner.to_s}?", condition == :unless)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
if filters.empty?
|
63
|
+
filters << build_hash_comparision(options, :if, :all?, false) if options[:if]
|
64
|
+
filters << build_hash_comparision(options, :unless, :all?, true) if options[:unless]
|
65
|
+
end
|
66
|
+
unless filters.empty? and options.empty?
|
67
|
+
filters << build_hash_comparision(nil, :if, :all?, false, options)
|
68
|
+
end
|
69
|
+
filters.compact!
|
70
|
+
filters
|
71
|
+
end
|
72
|
+
|
73
|
+
public
|
74
|
+
def listen(event_type, *methods, &block)
|
75
|
+
options = methods.last.is_a?(Hash) ? methods.pop : {}
|
76
|
+
filters = build_listener_filters(options)
|
77
|
+
listener_block = filters.empty? ? block :
|
78
|
+
Proc.new do |event|
|
79
|
+
block.call(event) if filters.all?{|filter| filter.call(event)}
|
80
|
+
end
|
81
|
+
|
82
|
+
listener_interface = listener_interface(event_type)
|
83
|
+
lister_methods = listener_interface.declared_instance_methods.map{|method| method.name}
|
84
|
+
handling_methods = methods.empty? ?
|
85
|
+
lister_methods :
|
86
|
+
methods.map{|method| invokable_method(lister_methods, event_type, method)}.compact
|
87
|
+
mod = Module.new do
|
88
|
+
lister_methods.each do |listener_method|
|
89
|
+
if handling_methods.include?(listener_method)
|
90
|
+
define_method(listener_method){|*args| listener_block.call(*args)}
|
91
|
+
else
|
92
|
+
define_method(listener_method, &NULL_METHOD)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
method_name = "add#{event_type.to_s.camelize}Listener"
|
97
|
+
listener = Object.new
|
98
|
+
listener.extend(mod)
|
99
|
+
send(method_name, listener)
|
100
|
+
listener
|
101
|
+
end
|
102
|
+
|
103
|
+
private
|
104
|
+
def listener_interface(event_type)
|
105
|
+
java_event_name = event_type.to_s.camelize
|
106
|
+
method_name = "add#{java_event_name}Listener"
|
107
|
+
java_method =
|
108
|
+
find_java_method(method_name){|m|m.parameter_types.length == 1} ||
|
109
|
+
find_java_method(method_name)
|
110
|
+
raise "unsupported event '#{java_event_name}' for #{self.class.name}" unless java_method
|
111
|
+
if java_method.parameter_types.length != 1
|
112
|
+
method_name = "%s(%s)" % [java_method.name, java_method.parameter_types.map{|t|t.name}.join(',')]
|
113
|
+
raise "unsupported evnet method #{method_name} for #{java_method.declaring_class.name}"
|
114
|
+
end
|
115
|
+
java_method.parameter_types.first
|
116
|
+
end
|
117
|
+
|
118
|
+
def invokable_method(java_methods, base_event_type, base_name)
|
119
|
+
base_name = base_name.to_s
|
120
|
+
return base_name if java_methods.include?(base_name)
|
121
|
+
s = self.class.uncapitalize(base_name)
|
122
|
+
return s if java_methods.include?(s)
|
123
|
+
s = self.class.uncapitalize(base_name.camelize)
|
124
|
+
return s if java_methods.include?(s)
|
125
|
+
even_type = self.class.uncapitalize(base_event_type.to_s.camelize)
|
126
|
+
s = "#{even_type}#{base_name}"
|
127
|
+
return s if java_methods.include?(s)
|
128
|
+
s = "#{even_type}#{base_name.camelize}"
|
129
|
+
return s if java_methods.include?(s)
|
130
|
+
return nil
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
Rubeus::Awt.depend_on("Dimension")
|
2
|
+
|
3
|
+
module Rubeus::Awt::Extensions
|
4
|
+
module Component
|
5
|
+
def self.included(base)
|
6
|
+
base.module_eval do
|
7
|
+
include Rubeus::Awt::Attributes
|
8
|
+
include Rubeus::Awt::Nestable
|
9
|
+
include Rubeus::Awt::Event
|
10
|
+
include Rubeus::Awt::Setters
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Rubeus::Awt::Extensions
|
2
|
+
module Dimension
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def create(*args)
|
9
|
+
values = args
|
10
|
+
if args.length == 1
|
11
|
+
if args.first.is_a?(Array)
|
12
|
+
return create(*args.first)
|
13
|
+
elsif args.first.is_a?(Rubeus::Awt::Dimension)
|
14
|
+
return args.first
|
15
|
+
else
|
16
|
+
values = args.first.to_s.split("x", 2)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
new(*values.map{|s|s.to_i})
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
@@ -0,0 +1,88 @@
|
|
1
|
+
module Rubeus::Awt
|
2
|
+
module Nestable
|
3
|
+
def self.included(klass)
|
4
|
+
klass.extend(ClassMethods)
|
5
|
+
klass.instance_eval do
|
6
|
+
alias :new_without_nestable :new
|
7
|
+
alias :new :new_with_nestable
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class Context
|
12
|
+
def self.container_class_names
|
13
|
+
@container_class_names ||= []
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.container_class_names=(*class_names)
|
17
|
+
@container_class_names = class_names
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.register_as_container(*class_names)
|
21
|
+
self.container_class_names.concat(class_names)
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.add_component_if_container_exist(component, &block)
|
25
|
+
@container ||= nil
|
26
|
+
@container.send(@action, component) if @container
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.add_new_component_to(container, action = :add, block_argument = nil, &block)
|
30
|
+
former_container = @container
|
31
|
+
former_action = @action
|
32
|
+
@container = container
|
33
|
+
@action = action
|
34
|
+
begin
|
35
|
+
yield(block_argument || container)
|
36
|
+
ensure
|
37
|
+
@container = former_container
|
38
|
+
@action = former_action
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.container
|
43
|
+
@container
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
module ClassMethods
|
49
|
+
def new_with_nestable(*args, &block)
|
50
|
+
object = self.new_without_nestable(*args)
|
51
|
+
Context.add_component_if_container_exist(object)
|
52
|
+
return object unless block_given?
|
53
|
+
process_block_for_new(object, &block)
|
54
|
+
return object
|
55
|
+
end
|
56
|
+
|
57
|
+
def constianer?
|
58
|
+
Context.container_class_names.include?(self.java_class.name)
|
59
|
+
end
|
60
|
+
|
61
|
+
def perform_as_container
|
62
|
+
Context.container_class_names << self.java_class.name
|
63
|
+
end
|
64
|
+
|
65
|
+
def perform_as_containee
|
66
|
+
Context.container_class_names.delete(self.java_class.name)
|
67
|
+
end
|
68
|
+
|
69
|
+
def process_block_for_new(object, &block)
|
70
|
+
if self.constianer?
|
71
|
+
self.add_new_component_to(object, &block)
|
72
|
+
elsif object.respond_to?(:listen)
|
73
|
+
object.listen(*self.default_event_type, &block)
|
74
|
+
else
|
75
|
+
raise "#{self.java_class.name} doesn't support process_block_for_new"
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def add_new_component_to(object, &block)
|
80
|
+
Context.add_new_component_to(object, &block)
|
81
|
+
end
|
82
|
+
|
83
|
+
def default_event_type
|
84
|
+
:action
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
Rubeus::Awt.depend_on("Dimension")
|
2
|
+
|
3
|
+
module Rubeus::Awt
|
4
|
+
module Setters
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.module_eval do
|
8
|
+
alias_method :set_preferred_size_without_rubeus, :set_preferred_size
|
9
|
+
alias_method :set_preferred_size, :set_preferred_size_with_rubeus
|
10
|
+
alias_method :preferred_size=, :set_preferred_size_with_rubeus
|
11
|
+
|
12
|
+
alias_method :set_size_without_rubeus, :set_size
|
13
|
+
alias_method :set_size, :set_size_with_rubeus
|
14
|
+
alias_method :size=, :set_size_with_rubeus
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def set_preferred_size_with_rubeus(*args)
|
19
|
+
set_preferred_size_without_rubeus(Rubeus::Awt::Dimension.create(*args))
|
20
|
+
end
|
21
|
+
|
22
|
+
def set_size_with_rubeus(*args)
|
23
|
+
set_size_without_rubeus(Rubeus::Awt::Dimension.create(*args))
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/rubeus/awt.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require "rubeus/component_loader"
|
2
|
+
|
3
|
+
module Rubeus
|
4
|
+
extensions_path = File.join(File.dirname(__FILE__), "awt", "extensions")
|
5
|
+
Awt = ComponentLoader.new("java.awt", extensions_path) do
|
6
|
+
base_path = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
7
|
+
Dir.glob(File.join(base_path, 'rubeus', 'awt', '*.rb')) do |file|
|
8
|
+
rel_path = file.gsub("#{base_path}/", '')
|
9
|
+
autoload(File.basename(rel_path, '.*').camelize, rel_path.gsub(/\.rb$/, ''))
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
require "rubeus/awt/extensions"
|
@@ -0,0 +1,103 @@
|
|
1
|
+
module Rubeus
|
2
|
+
class ComponentLoader < Module
|
3
|
+
attr_reader :java_package, :ruby_module_path, :base_path
|
4
|
+
|
5
|
+
def self.find_base_path(path)
|
6
|
+
path = File.expand_path(path)
|
7
|
+
$LOAD_PATH.detect{|load_path| path.include?(File.expand_path(load_path))}
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(java_package, ruby_module_path, base_path = nil, &block)
|
11
|
+
@java_package, @ruby_module_path, @base_path =
|
12
|
+
java_package, ruby_module_path, base_path
|
13
|
+
@base_path ||= ComponentLoader.find_base_path(ruby_module_path)
|
14
|
+
@ruby_module_holder_name = File.basename(ruby_module_path).camelize
|
15
|
+
super(&block)
|
16
|
+
end
|
17
|
+
|
18
|
+
def ruby_module_holder
|
19
|
+
@ruby_module_holder ||= (self.const_get(@ruby_module_holder_name) rescue nil)
|
20
|
+
unless @ruby_module_holder
|
21
|
+
self.const_set(@ruby_module_holder_name, Module.new)
|
22
|
+
@ruby_module_holder = self.const_get(@ruby_module_holder_name)
|
23
|
+
end
|
24
|
+
@ruby_module_holder
|
25
|
+
end
|
26
|
+
|
27
|
+
def extended(object)
|
28
|
+
mod = Module.new do
|
29
|
+
def const_missing(java_class_name)
|
30
|
+
@loader.const_get(java_class_name)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
object.instance_variable_set("@loader", self)
|
34
|
+
object.extend(mod)
|
35
|
+
end
|
36
|
+
|
37
|
+
def autolodings
|
38
|
+
@autolodings ||= {}
|
39
|
+
end
|
40
|
+
|
41
|
+
def autoload(const_name, feature = nil)
|
42
|
+
autolodings[const_name.to_s] = feature ||"#{self.name}::#{java_class_name.to_s}".underscore
|
43
|
+
end
|
44
|
+
|
45
|
+
def autoload?(const_name)
|
46
|
+
autolodings[const_name.to_s]
|
47
|
+
end
|
48
|
+
|
49
|
+
def const_missing(java_class_name)
|
50
|
+
if autoload?(java_class_name)
|
51
|
+
feature = autolodings.delete(java_class_name.to_s)
|
52
|
+
require(feature)
|
53
|
+
return const_get(java_class_name)
|
54
|
+
end
|
55
|
+
if java_class_name.to_s == @ruby_module_holder_name.to_s
|
56
|
+
raise NameError, java_class_name
|
57
|
+
end
|
58
|
+
if java_class_name.to_s.include?('::')
|
59
|
+
names = java_class_name.to_s.split('::', 2)
|
60
|
+
package = names.first
|
61
|
+
mod = ComponentLoader.new(
|
62
|
+
[@java_package, package.underscore].join('.'),
|
63
|
+
File.join(@ruby_module_path, package.underscore),
|
64
|
+
@base_path)
|
65
|
+
self.const_set(package.camelize, mod)
|
66
|
+
mod.const_get(names.last)
|
67
|
+
else
|
68
|
+
begin
|
69
|
+
require(File.join(@ruby_module_path, java_class_name.to_s.underscore))
|
70
|
+
rescue LoadError => e
|
71
|
+
# puts "warning: #{e}"
|
72
|
+
end
|
73
|
+
java_fqn = to_java_fqn(java_class_name)
|
74
|
+
extension = nil
|
75
|
+
begin
|
76
|
+
extension = self.ruby_module_holder.const_get(java_class_name)
|
77
|
+
rescue
|
78
|
+
# puts "warning: #{$!.inspect}"
|
79
|
+
end
|
80
|
+
if extension
|
81
|
+
JavaUtilities.extend_proxy(java_fqn) do
|
82
|
+
include extension
|
83
|
+
end
|
84
|
+
end
|
85
|
+
result = instance_eval(java_fqn)
|
86
|
+
self.const_set(java_class_name, result)
|
87
|
+
result
|
88
|
+
end
|
89
|
+
rescue
|
90
|
+
puts $!
|
91
|
+
puts $!.backtrace.join("\n ")
|
92
|
+
super
|
93
|
+
end
|
94
|
+
|
95
|
+
def to_java_fqn(java_class_name)
|
96
|
+
"#{java_package}.#{java_class_name}"
|
97
|
+
end
|
98
|
+
|
99
|
+
def depend_on(*java_class_names)
|
100
|
+
java_class_names.each{|java_class_name| self.const_get(java_class_name)}
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Rubeus::Swing::Extensions
|
2
|
+
module BoxLayout
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
base.instance_eval do
|
6
|
+
alias :new_without_nestable :new
|
7
|
+
alias :new :new_with_nestable
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def new_with_nestable(*args, &block)
|
13
|
+
if args.length == 1
|
14
|
+
container = Rubeus::Awt::Nestable::Context.container
|
15
|
+
raise ArgumentError, "No container! you must specify a Container and an axis" unless container
|
16
|
+
args.unshift(container.respond_to?(:content_pane) ? container.content_pane : container)
|
17
|
+
end
|
18
|
+
|
19
|
+
unless args.last.is_a?(Integer)
|
20
|
+
value = args.pop
|
21
|
+
args << const_get(value.to_s)
|
22
|
+
end
|
23
|
+
|
24
|
+
new_without_nestable(*args, &block)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
Rubeus::Awt.depend_on("Container")
|
2
|
+
|
3
|
+
module Rubeus::Swing::Extensions
|
4
|
+
module JComponent
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.module_eval do
|
8
|
+
alias_method :set_preferred_size_without_rubeus, :set_preferred_size
|
9
|
+
alias_method :set_preferred_size, :set_preferred_size_with_rubeus
|
10
|
+
alias_method :preferred_size=, :set_preferred_size_with_rubeus
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def set_preferred_size_with_rubeus(*args)
|
15
|
+
set_preferred_size_without_rubeus(Rubeus::Awt::Dimension.create(*args))
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
Rubeus::Awt.depend_on('Container', 'Window', 'Frame')
|
2
|
+
Rubeus::Swing.depend_on('JPanel')
|
3
|
+
|
4
|
+
module Rubeus::Swing::Extensions
|
5
|
+
module JFrame
|
6
|
+
def self.included(base)
|
7
|
+
base.perform_as_container
|
8
|
+
base.default_attributes = {
|
9
|
+
:size => [400, 300],
|
10
|
+
:default_close_operation => javax.swing.JFrame::EXIT_ON_CLOSE
|
11
|
+
}
|
12
|
+
if ENV_JAVA["java.specification.version"] == "1.6"
|
13
|
+
base.module_eval do
|
14
|
+
alias_method :set_size_without_rubeus, :set_size
|
15
|
+
alias_method :set_size, :set_size_with_rubeus
|
16
|
+
alias_method :size=, :set_size_with_rubeus
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
if ENV_JAVA["java.specification.version"] == "1.6"
|
22
|
+
def set_size_with_rubeus(*args)
|
23
|
+
set_size_without_rubeus(Rubeus::Awt::Dimension.create(*args))
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
JavaUtilities.extend_proxy('javax.swing.JSplitPane') do
|
2
|
+
perform_as_container
|
3
|
+
|
4
|
+
def self.add_new_component_to(object, &block)
|
5
|
+
Rubeus::Awt::Nestable::Context.add_new_component_to(object, :append_component, &block)
|
6
|
+
end
|
7
|
+
|
8
|
+
def append_component(component)
|
9
|
+
append_method =
|
10
|
+
(self.orientation == javax.swing.JSplitPane::VERTICAL_SPLIT) ?
|
11
|
+
(top_component ? :set_bottom_component : :set_top_component) :
|
12
|
+
(left_component ? :set_right_component : :set_left_component)
|
13
|
+
send(append_method, component)
|
14
|
+
end
|
15
|
+
end
|
data/lib/rubeus/swing.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require "rubeus/component_loader"
|
2
|
+
|
3
|
+
module Rubeus
|
4
|
+
extensions_path = File.join(File.dirname(__FILE__), "swing", "extensions")
|
5
|
+
Swing = ComponentLoader.new("javax.swing", extensions_path) do
|
6
|
+
|
7
|
+
def self.irb
|
8
|
+
Object.send(:extend, self)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
require "rubeus/swing/extensions"
|
14
|
+
|
15
|
+
require "rubeus/awt/attributes"
|
16
|
+
require "rubeus/awt/nestable"
|
17
|
+
require "rubeus/awt/event"
|
18
|
+
|
19
|
+
|
20
|
+
=begin
|
21
|
+
import "javax.swing.JFrame"
|
22
|
+
|
23
|
+
require "swing_ext"
|
24
|
+
|
25
|
+
frame = JFrame.new("JDBC Query")
|
26
|
+
frame.set_size(400, 300)
|
27
|
+
frame.default_close_operation = JFrame::EXIT_ON_CLOSE
|
28
|
+
frame.events
|
29
|
+
frame.event_methods(:key)
|
30
|
+
frame.event_methods(:key, :typed)
|
31
|
+
frame.event_methods(:key, "typed")
|
32
|
+
frame.event_methods(:key, :keyTyped)
|
33
|
+
frame.event_methods(:key, :keyTyped)
|
34
|
+
frame.event_methods(:key, "key_typed")
|
35
|
+
frame.event_methods(:key, "key_Typed")
|
36
|
+
|
37
|
+
frame.events.each
|
38
|
+
|
39
|
+
frame.visible = true
|
40
|
+
|
41
|
+
=end
|
42
|
+
|
data/lib/rubeus.rb
ADDED
metadata
CHANGED
@@ -3,12 +3,38 @@ extensions: []
|
|
3
3
|
homepage: http://code.google.com/p/rubeus/
|
4
4
|
executables: []
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.0.
|
6
|
+
version: 0.0.2
|
7
7
|
post_install_message:
|
8
|
-
date: 2008-06-
|
8
|
+
date: 2008-06-28 15:00:00 +00:00
|
9
9
|
files:
|
10
|
-
- lib/
|
11
|
-
|
10
|
+
- lib/rubeus
|
11
|
+
- lib/rubeus.rb
|
12
|
+
- lib/rubeus/awt
|
13
|
+
- lib/rubeus/awt.rb
|
14
|
+
- lib/rubeus/component_loader.rb
|
15
|
+
- lib/rubeus/swing
|
16
|
+
- lib/rubeus/swing.rb
|
17
|
+
- lib/rubeus/awt/attributes.rb
|
18
|
+
- lib/rubeus/awt/event.rb
|
19
|
+
- lib/rubeus/awt/extensions
|
20
|
+
- lib/rubeus/awt/extensions.rb
|
21
|
+
- lib/rubeus/awt/nestable.rb
|
22
|
+
- lib/rubeus/awt/setters.rb
|
23
|
+
- lib/rubeus/awt/extensions/component.rb
|
24
|
+
- lib/rubeus/awt/extensions/container.rb
|
25
|
+
- lib/rubeus/awt/extensions/dimension.rb
|
26
|
+
- lib/rubeus/swing/extensions
|
27
|
+
- lib/rubeus/swing/extensions.rb
|
28
|
+
- lib/rubeus/swing/extensions/box_layout.rb
|
29
|
+
- lib/rubeus/swing/extensions/j_applet.rb
|
30
|
+
- lib/rubeus/swing/extensions/j_component.rb
|
31
|
+
- lib/rubeus/swing/extensions/j_frame.rb
|
32
|
+
- lib/rubeus/swing/extensions/j_panel.rb
|
33
|
+
- lib/rubeus/swing/extensions/j_scroll_pane.rb
|
34
|
+
- lib/rubeus/swing/extensions/j_split_pane.rb
|
35
|
+
- lib/rubeus/swing/extensions/j_text_field.rb
|
36
|
+
- lib/rubeus/swing/extensions/j_window.rb
|
37
|
+
rubygems_version: 1.1.1
|
12
38
|
rdoc_options: []
|
13
39
|
signing_key:
|
14
40
|
cert_chain: []
|
@@ -34,7 +60,16 @@ require_paths:
|
|
34
60
|
- lib
|
35
61
|
specification_version: 2
|
36
62
|
test_files: []
|
37
|
-
dependencies:
|
63
|
+
dependencies:
|
64
|
+
- !ruby/object:Gem::Dependency
|
65
|
+
name: activesupport
|
66
|
+
version_requirement:
|
67
|
+
version_requirements: !ruby/object:Gem::Requirement
|
68
|
+
version:
|
69
|
+
requirements:
|
70
|
+
- - '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: 2.0.2
|
38
73
|
description:
|
39
74
|
email: rubeus@googlegroups.com
|
40
75
|
authors:
|
data/lib/swing_ext.rb
DELETED
@@ -1,295 +0,0 @@
|
|
1
|
-
class << java.swing
|
2
|
-
def container_class_names
|
3
|
-
@container_class_names ||= []
|
4
|
-
end
|
5
|
-
|
6
|
-
def container_class_names=(*class_names)
|
7
|
-
@container_class_names = class_names
|
8
|
-
end
|
9
|
-
|
10
|
-
def register_as_container(*class_names)
|
11
|
-
self.container_class_names.concat(class_names)
|
12
|
-
end
|
13
|
-
|
14
|
-
def add_component_if_container_exist(component, &block)
|
15
|
-
@container ||= nil
|
16
|
-
@container.send(@action, component) if @container
|
17
|
-
end
|
18
|
-
|
19
|
-
def add_new_component_to(container, action = :add, block_argument = nil, &block)
|
20
|
-
former_container = @container
|
21
|
-
former_action = @action
|
22
|
-
@container = container
|
23
|
-
@action = action
|
24
|
-
begin
|
25
|
-
yield(block_argument || container)
|
26
|
-
ensure
|
27
|
-
@container = former_container
|
28
|
-
@action = former_action
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
|
34
|
-
java.swing.register_as_container(
|
35
|
-
'javax.swing.JApplet',
|
36
|
-
'javax.swing.JFrame',
|
37
|
-
'javax.swing.JPanel',
|
38
|
-
'javax.swing.JScrollPane',
|
39
|
-
'javax.swing.JSplitPane',
|
40
|
-
'javax.swing.JWindow'
|
41
|
-
)
|
42
|
-
|
43
|
-
JavaUtilities.extend_proxy('javax.swing.JComponent') do
|
44
|
-
def set_preferred_size_with_rubeus(*args)
|
45
|
-
values = args
|
46
|
-
if args.length == 1
|
47
|
-
if args.first.is_a?(java.awt.Dimension)
|
48
|
-
return set_preferred_size_without_rubeus(*args)
|
49
|
-
else
|
50
|
-
values = args.first.to_s.split("x", 2)
|
51
|
-
end
|
52
|
-
end
|
53
|
-
set_preferred_size_without_rubeus(java.awt.Dimension.new(*values.map{|s|s.to_i}))
|
54
|
-
end
|
55
|
-
|
56
|
-
alias_method :set_preferred_size_without_rubeus, :set_preferred_size
|
57
|
-
alias_method :set_preferred_size, :set_preferred_size_with_rubeus
|
58
|
-
end
|
59
|
-
|
60
|
-
|
61
|
-
JavaUtilities.extend_proxy('java.awt.Component') do
|
62
|
-
def self.new_with_nestable(*args, &block)
|
63
|
-
object = self.new_without_nestable(*args)
|
64
|
-
java.swing.add_component_if_container_exist(object)
|
65
|
-
return object unless block_given?
|
66
|
-
initial_nest(object, &block)
|
67
|
-
return object
|
68
|
-
end
|
69
|
-
|
70
|
-
self.instance_eval do
|
71
|
-
alias :new_without_nestable :new
|
72
|
-
alias :new :new_with_nestable
|
73
|
-
end
|
74
|
-
|
75
|
-
def self.constianer?
|
76
|
-
java.swing.container_class_names.include?(self.java_class.name)
|
77
|
-
end
|
78
|
-
|
79
|
-
def self.perform_as_container
|
80
|
-
@as_container = true
|
81
|
-
end
|
82
|
-
|
83
|
-
def self.perform_as_containee
|
84
|
-
@as_container = false
|
85
|
-
end
|
86
|
-
|
87
|
-
def self.initial_nest(object, &block)
|
88
|
-
if self.constianer?
|
89
|
-
self.add_new_component_to(object, &block)
|
90
|
-
elsif object.respond_to?(:listen)
|
91
|
-
object.listen(*self.default_event_type, &block)
|
92
|
-
else
|
93
|
-
raise "#{self.java_class.name} doesn't support initial_nest"
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
def self.add_new_component_to(object, &block)
|
98
|
-
java.swing.add_new_component_to(object, &block)
|
99
|
-
end
|
100
|
-
|
101
|
-
def self.default_event_type
|
102
|
-
:action
|
103
|
-
end
|
104
|
-
|
105
|
-
def self.camelize(str)
|
106
|
-
parts = str.to_s.split('_')
|
107
|
-
parts.map{|part| part[0..0].upcase + part[1..-1].downcase}.join
|
108
|
-
end
|
109
|
-
|
110
|
-
def self.underscore(camel_cased_word)
|
111
|
-
camel_cased_word.to_s.gsub(/::/, '/').
|
112
|
-
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
113
|
-
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
114
|
-
tr("-", "_").
|
115
|
-
downcase
|
116
|
-
end
|
117
|
-
|
118
|
-
def self.uncapitalize(str)
|
119
|
-
str[0..0].downcase + str[1..-1]
|
120
|
-
end
|
121
|
-
|
122
|
-
def find_java_method(method_name, &block)
|
123
|
-
klass = self.java_class
|
124
|
-
while klass
|
125
|
-
method = klass.declared_instance_methods.detect do |m|
|
126
|
-
(m.name == method_name) and (block_given? ? yield(m) : true)
|
127
|
-
end
|
128
|
-
return method if method
|
129
|
-
klass = klass.superclass
|
130
|
-
end
|
131
|
-
nil
|
132
|
-
end
|
133
|
-
|
134
|
-
def events
|
135
|
-
self.event_types.inject({}){|dest, event_type|
|
136
|
-
dest[event_type] = event_methods(event_type); dest}
|
137
|
-
end
|
138
|
-
|
139
|
-
def event_types
|
140
|
-
self.methods.map{|m| /^add\_(.*?)\_listener$/ =~ m ? $1 : nil }.compact.sort
|
141
|
-
end
|
142
|
-
|
143
|
-
def event_methods(event_type, *methods)
|
144
|
-
listener_interface = listener_interface(event_type)
|
145
|
-
listener_interface.declared_instance_methods.map{|method|
|
146
|
-
self.class.underscore(method.name)}
|
147
|
-
end
|
148
|
-
|
149
|
-
NULL_METHOD = Proc.new{}
|
150
|
-
|
151
|
-
private
|
152
|
-
def build_hash_comparision(options, option_key, method, inverse, comparision_options = nil)
|
153
|
-
comparision_options ||= options[option_key]
|
154
|
-
return nil unless comparision_options
|
155
|
-
"#{option_key.inspect} option must be a hash" unless comparision_options.is_a?(Hash)
|
156
|
-
Proc.new do |event|
|
157
|
-
matched = comparision_options.send(method){|key, value| event.send(key) == value}
|
158
|
-
inverse ? !matched : matched
|
159
|
-
end
|
160
|
-
end
|
161
|
-
|
162
|
-
def build_listener_filters(options)
|
163
|
-
filters = []
|
164
|
-
[:if, :unless].each do |condition|
|
165
|
-
[:any, :all].each do |joiner|
|
166
|
-
filters << build_hash_comparision(options,
|
167
|
-
"#{condition.to_s}_#{joiner.to_s}".to_sym,
|
168
|
-
"#{joiner.to_s}?", condition == :unless)
|
169
|
-
end
|
170
|
-
end
|
171
|
-
if filters.empty?
|
172
|
-
filters << build_hash_comparision(options, :if, :all?, false) if options[:if]
|
173
|
-
filters << build_hash_comparision(options, :unless, :all?, true) if options[:unless]
|
174
|
-
end
|
175
|
-
unless filters.empty? and options.empty?
|
176
|
-
filters << build_hash_comparision(nil, :if, :all?, false, options)
|
177
|
-
end
|
178
|
-
filters.compact!
|
179
|
-
filters
|
180
|
-
end
|
181
|
-
|
182
|
-
public
|
183
|
-
def listen(event_type, *methods, &block)
|
184
|
-
options = methods.last.is_a?(Hash) ? methods.pop : {}
|
185
|
-
filters = build_listener_filters(options)
|
186
|
-
listener_block = filters.empty? ? block :
|
187
|
-
Proc.new do |event|
|
188
|
-
block.call(event) if filters.all?{|filter| filter.call(event)}
|
189
|
-
end
|
190
|
-
|
191
|
-
listener_interface = listener_interface(event_type)
|
192
|
-
lister_methods = listener_interface.declared_instance_methods.map{|method| method.name}
|
193
|
-
handling_methods = methods.empty? ?
|
194
|
-
lister_methods :
|
195
|
-
methods.map{|method| invokable_method(lister_methods, event_type, method)}.compact
|
196
|
-
mod = Module.new do
|
197
|
-
lister_methods.each do |listener_method|
|
198
|
-
if handling_methods.include?(listener_method)
|
199
|
-
define_method(listener_method){|*args| listener_block.call(*args)}
|
200
|
-
else
|
201
|
-
define_method(listener_method, &NULL_METHOD)
|
202
|
-
end
|
203
|
-
end
|
204
|
-
end
|
205
|
-
method_name = "add#{self.class.camelize(event_type)}Listener"
|
206
|
-
listener = Object.new
|
207
|
-
listener.extend(mod)
|
208
|
-
send(method_name, listener)
|
209
|
-
listener
|
210
|
-
end
|
211
|
-
|
212
|
-
private
|
213
|
-
def listener_interface(event_type)
|
214
|
-
java_event_name = self.class.camelize(event_type)
|
215
|
-
method_name = "add#{java_event_name}Listener"
|
216
|
-
java_method =
|
217
|
-
find_java_method(method_name){|m|m.parameter_types.length == 1} ||
|
218
|
-
find_java_method(method_name)
|
219
|
-
raise "unsupported event '#{java_event_name}' for #{self.class.name}" unless java_method
|
220
|
-
if java_method.parameter_types.length != 1
|
221
|
-
method_name = "%s(%s)" % [java_method.name, java_method.parameter_types.map{|t|t.name}.join(',')]
|
222
|
-
raise "unsupported evnet method #{method_name} for #{java_method.declaring_class.name}"
|
223
|
-
end
|
224
|
-
java_method.parameter_types.first
|
225
|
-
end
|
226
|
-
|
227
|
-
def invokable_method(java_methods, base_event_type, base_name)
|
228
|
-
base_name = base_name.to_s
|
229
|
-
return base_name if java_methods.include?(base_name)
|
230
|
-
s = self.class.uncapitalize(base_name)
|
231
|
-
return s if java_methods.include?(s)
|
232
|
-
s = self.class.uncapitalize(self.class.camelize(base_name))
|
233
|
-
return s if java_methods.include?(s)
|
234
|
-
even_type = self.class.uncapitalize(self.class.camelize(base_event_type))
|
235
|
-
s = "#{even_type}#{base_name}"
|
236
|
-
return s if java_methods.include?(s)
|
237
|
-
camelized = self.class.camelize(base_name)
|
238
|
-
s = "#{even_type}#{camelized}"
|
239
|
-
return s if java_methods.include?(s)
|
240
|
-
return nil
|
241
|
-
end
|
242
|
-
end
|
243
|
-
|
244
|
-
JavaUtilities.extend_proxy("javax.swing.JTextField") do
|
245
|
-
def self.default_event_type
|
246
|
-
return :key, :pressed
|
247
|
-
end
|
248
|
-
end
|
249
|
-
|
250
|
-
JavaUtilities.extend_proxy('javax.swing.JScrollPane') do
|
251
|
-
def self.add_new_component_to(object, &block)
|
252
|
-
java.swing.add_new_component_to(object.viewport, :set_view, object, &block)
|
253
|
-
end
|
254
|
-
end
|
255
|
-
|
256
|
-
JavaUtilities.extend_proxy('javax.swing.JSplitPane') do
|
257
|
-
def self.add_new_component_to(object, &block)
|
258
|
-
java.swing.add_new_component_to(object, :append_component, &block)
|
259
|
-
end
|
260
|
-
|
261
|
-
def append_component(component)
|
262
|
-
append_method =
|
263
|
-
(self.orientation == javax.swing.JSplitPane::VERTICAL_SPLIT) ?
|
264
|
-
(top_component ? :set_bottom_component : :set_top_component) :
|
265
|
-
(left_component ? :set_right_component : :set_left_component)
|
266
|
-
send(append_method, component)
|
267
|
-
end
|
268
|
-
|
269
|
-
end
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
=begin
|
274
|
-
import "javax.swing.JFrame"
|
275
|
-
|
276
|
-
require "swing_ext"
|
277
|
-
|
278
|
-
frame = JFrame.new("JDBC Query")
|
279
|
-
frame.set_size(400, 300)
|
280
|
-
frame.default_close_operation = JFrame::EXIT_ON_CLOSE
|
281
|
-
frame.events
|
282
|
-
frame.event_methods(:key)
|
283
|
-
frame.event_methods(:key, :typed)
|
284
|
-
frame.event_methods(:key, "typed")
|
285
|
-
frame.event_methods(:key, :keyTyped)
|
286
|
-
frame.event_methods(:key, :keyTyped)
|
287
|
-
frame.event_methods(:key, "key_typed")
|
288
|
-
frame.event_methods(:key, "key_Typed")
|
289
|
-
|
290
|
-
frame.events.each
|
291
|
-
|
292
|
-
frame.visible = true
|
293
|
-
|
294
|
-
=end
|
295
|
-
|