swing4rb 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/swing4rb.rb +280 -0
  2. metadata +47 -0
@@ -0,0 +1,280 @@
1
+ #Copyright (c) 2007 Toby Ho
2
+ #
3
+ #Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ #of this software and associated documentation files (the "Software"), to deal
5
+ #in the Software without restriction, including without limitation the rights
6
+ #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ #copies of the Software, and to permit persons to whom the Software is
8
+ #furnished to do so, subject to the following conditions:
9
+ #
10
+ #The above copyright notice and this permission notice shall be included in
11
+ #all copies or substantial portions of the Software.
12
+ #
13
+ #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ #THE SOFTWARE.
20
+
21
+
22
+ require 'java'
23
+ module Swing
24
+ include_package 'java.awt'
25
+ include_package 'javax.swing'
26
+ end
27
+ module AwtEvent
28
+ include_package 'java.awt.event'
29
+ end
30
+
31
+ def camelcase str
32
+ str.gsub!(/[^a-zA-Z_\- ]/," ")
33
+ str = " " + str.split('_').join(" ")
34
+ str.gsub!(/ (.)/) { $1.upcase }
35
+ end
36
+
37
+ #AbstractButton
38
+ #JComboBox
39
+ #JFileChooser
40
+ #JTextField
41
+ #Timer
42
+
43
+ def pretty_action_listener *syms
44
+ syms.each{|sym|
45
+ eval(
46
+ %Q|class Java::JavaxSwing::#{camelcase(sym.to_s)}
47
+ alias original_add_action_listener add_action_listener
48
+ def add_action_listener &func
49
+ l = AwtEvent::ActionListener.new
50
+ l.instance_eval {
51
+ @func = func
52
+ }
53
+ def l.actionPerformed(event)
54
+ @func.call(event)
55
+ end
56
+ l
57
+ original_add_action_listener l
58
+ end
59
+ end
60
+ |)
61
+ }
62
+ end
63
+
64
+ pretty_action_listener :abstract_button, :j_combo_box, :j_file_chooser, :j_text_field, :timer
65
+
66
+ def border_fact_method *syms
67
+ syms.each{|sym|
68
+ eval %Q|
69
+ def #{sym.to_s} *args
70
+ Java::JavaxSwing::BorderFactory.create#{camelcase(sym.to_s)} *args
71
+ end
72
+ |
73
+ }
74
+ end
75
+
76
+ border_fact_method :line_border, :bevel_border, :compound_border, :etched_border
77
+ border_fact_method :matte_border, :raised_bevel_border, :titled_border
78
+
79
+ class Java::JavaAwt::Component
80
+ alias original_add_mouse_listener add_mouse_listener
81
+ alias original_add_key_listener add_key_listener
82
+ def add_mouse_listener map
83
+ l = AwtEvent::MouseListener.new
84
+ [:clicked, :entered, :exited, :pressed, :released].each{|key|
85
+ value = map[key]
86
+ if value.nil? then
87
+ value = proc{|event| }
88
+ end
89
+ eval %Q|l.instance_eval {
90
+ @#{key} = value
91
+ }|
92
+ eval %Q|
93
+ def l.mouse#{key.to_s.capitalize}(event)
94
+ @#{key}.call(event)
95
+ end
96
+ |
97
+ }
98
+ original_add_mouse_listener l
99
+ end
100
+
101
+ def add_key_listener map
102
+ l = AwtEvent::KeyListener.new
103
+ [:pressed, :released, :typed].each{|key|
104
+ value = map[key]
105
+ if value.nil? then
106
+ value = proc{|event| }
107
+ end
108
+ eval %Q|l.instance_eval {
109
+ @#{key} = value
110
+ }|
111
+ eval %Q|
112
+ def l.key#{key.to_s.capitalize}(event)
113
+ @#{key}.call(event)
114
+ end
115
+ |
116
+ }
117
+ original_add_key_listener l
118
+ end
119
+ end
120
+
121
+ class Java::JavaAwtEvent::ActionEvent
122
+ alias action_command getActionCommand
123
+ end
124
+
125
+ class Java::JavaxSwing::JPanel
126
+ alias layout getLayout
127
+ end
128
+
129
+ class Java::JavaxSwing::JFrame
130
+ def setup_menubar
131
+ bar = Java::JavaxSwing::JMenuBar.new
132
+ if block_given?
133
+ yield bar
134
+ end
135
+ self.setJMenuBar bar
136
+ bar
137
+ end
138
+ end
139
+
140
+ class Java::JavaxSwing::JMenuBar
141
+ def menu name
142
+ menu = Java::JavaxSwing::JMenu.new name
143
+ if block_given?
144
+ yield menu
145
+ end
146
+ self.add menu
147
+ menu
148
+ end
149
+ end
150
+
151
+ class Java::JavaxSwing::JMenu
152
+ def item name, shortcut = nil, clazz=Java::JavaxSwing::JMenuItem, &action_listener
153
+ shortcut = shortcut[0] unless shortcut.nil? or shortcut.length == 0
154
+ item = clazz.new name, shortcut
155
+ if block_given?
156
+ item.add_action_listener &action_listener
157
+ end
158
+ self.add item
159
+ item
160
+ end
161
+
162
+ def check_box_item name, shortcut = nil, &block
163
+ self.item name, shortcut, Java::JavaxSwing::JCheckBoxMenuItem, &block
164
+ end
165
+
166
+ def radio_button_item name, shortcut = nil, &block
167
+ self.item name, shortcut, Java::JavaxSwing::JRadioButtonMenuItem, &block
168
+ end
169
+
170
+ def submenu name
171
+ menu = Java::JavaxSwing::JMenu.new name
172
+ if block_given?
173
+ yield menu
174
+ end
175
+ self.add menu
176
+ menu
177
+ end
178
+ end
179
+
180
+ def fact_method_j *syms
181
+ syms.each {|sym|
182
+ name = sym.to_s
183
+ eval %Q|
184
+ def #{name} *args
185
+ #{name} = Swing::J#{camelcase(name)}.new *args
186
+ if block_given?
187
+ yield #{name}
188
+ end
189
+ #{name}
190
+ end
191
+ |
192
+ }
193
+ end
194
+
195
+ def fact_method *syms
196
+ syms.each {|sym|
197
+ name = sym.to_s
198
+ eval %Q|
199
+ def #{name} *args
200
+ #{name} = Swing::#{camelcase(name)}.new *args
201
+ if block_given?
202
+ yield #{name}
203
+ end
204
+ #{name}
205
+ end
206
+ |
207
+ }
208
+ end
209
+
210
+ fact_method_j :frame, :panel, :button, :text_area, :scroll_pane, :text_field, :label, :tool_bar
211
+ fact_method_j :text_field, :password_field, :progress_bar
212
+ fact_method :border_layout, :flow_layout, :box_layout, :card_layout, :dimension, :point
213
+ fact_method :grid_layout
214
+
215
+ def constant clazz, const
216
+ eval "Swing::#{clazz.to_s}::#{const.to_s.upcase}"
217
+ end
218
+
219
+
220
+
221
+ def border_panel map
222
+ panel {|p|
223
+ p.layout = border_layout
224
+ map.each {|key, value|
225
+ p.add(value, constant(:BorderLayout, key))
226
+ }
227
+ if block_given?
228
+ yield p
229
+ end
230
+ }
231
+ end
232
+
233
+ def flow_panel *components
234
+ panel {|p|
235
+ p.layout = flow_layout
236
+ components.each{|c|
237
+ p.add(c)
238
+ }
239
+ if block_given?
240
+ yield p
241
+ end
242
+ }
243
+ end
244
+
245
+ def box_panel axis, *components
246
+ panel {|p|
247
+ p.layout = box_layout(p, constant(:BoxLayout, axis))
248
+ components.each{|c|
249
+ p.add(c)
250
+ }
251
+ if block_given?
252
+ yield p
253
+ end
254
+ }
255
+ end
256
+
257
+ def card_panel map
258
+ panel {|p|
259
+ p.layout = card_layout
260
+ map.each {|key, value|
261
+ p.add(value, key.to_s)
262
+ }
263
+ if block_given?
264
+ yield p
265
+ end
266
+ }
267
+ end
268
+
269
+ def grid_panel columns, rows, *components
270
+ panel {|p|
271
+ p.layout = grid_layout columns, rows
272
+ components.each{|c|
273
+ p.add c
274
+ }
275
+ if block_given?
276
+ yield p
277
+ end
278
+ }
279
+ end
280
+
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
4
+ name: swing4rb
5
+ version: !ruby/object:Gem::Version
6
+ version: "0.1"
7
+ date: 2007-07-18 00:00:00 -04:00
8
+ summary: Declarative style Swing GUI API.
9
+ require_paths:
10
+ - lib
11
+ email: airportyh@gmail.com
12
+ homepage:
13
+ rubyforge_project:
14
+ description: Declarative style Swing GUI API.
15
+ autorequire: swing4rb
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - TobyHo
31
+ files:
32
+ - lib/swing4rb.rb
33
+ test_files: []
34
+
35
+ rdoc_options:
36
+ - --title
37
+ - swing4rb -- Declarative style Swing GUI API.
38
+ extra_rdoc_files: []
39
+
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ requirements: []
45
+
46
+ dependencies: []
47
+