rubeus 0.0.1-java
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/swing_ext.rb +295 -0
- metadata +45 -0
data/lib/swing_ext.rb
ADDED
@@ -0,0 +1,295 @@
|
|
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
|
+
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
extensions: []
|
3
|
+
homepage: http://code.google.com/p/rubeus/
|
4
|
+
executables: []
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.0.1
|
7
|
+
post_install_message:
|
8
|
+
date: 2008-06-20 15:00:00 +00:00
|
9
|
+
files:
|
10
|
+
- lib/swing_ext.rb
|
11
|
+
rubygems_version: 1.1.0
|
12
|
+
rdoc_options: []
|
13
|
+
signing_key:
|
14
|
+
cert_chain: []
|
15
|
+
name: rubeus
|
16
|
+
has_rdoc: false
|
17
|
+
platform: java
|
18
|
+
summary: Rubeus provides you an easy access to Java objects from Ruby scripts on JRuby
|
19
|
+
default_executable:
|
20
|
+
bindir: bin
|
21
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
22
|
+
version:
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: !str 0
|
27
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
28
|
+
version:
|
29
|
+
requirements:
|
30
|
+
- - '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: !str 0
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
specification_version: 2
|
36
|
+
test_files: []
|
37
|
+
dependencies: []
|
38
|
+
description:
|
39
|
+
email: rubeus@googlegroups.com
|
40
|
+
authors:
|
41
|
+
- Takeshi Akima
|
42
|
+
extra_rdoc_files: []
|
43
|
+
requirements: []
|
44
|
+
rubyforge_project: rubybizcommons
|
45
|
+
autorequire:
|