sweetgui 0.0.3 → 0.0.4

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.
Binary file
Binary file
@@ -1,3 +1,6 @@
1
+ # This file is an application of the SWT module. JRubyUtils::RubyObjectBrowser may be
2
+ # used to show the contents of an object.
3
+ #--
1
4
  # The contents of this file are subject to the Mozilla Public License
2
5
  # Version 1.1 (the "License"); you may not use this file except in
3
6
  # compliance with the License. You may obtain a copy of the License at
@@ -15,9 +18,8 @@
15
18
  # Contributor(s):
16
19
  # Johannes Rudolph <johannes_rudolph@gmail.com>
17
20
 
18
- #require '../swt'
19
-
20
21
  class Method
22
+ # get the name of the method
21
23
  def name
22
24
  method_field=Java::JavaClass.for_name('org.jruby.RubyMethod').declared_field('methodName')
23
25
  method_field.accessible=true
@@ -36,9 +38,14 @@ class ::SWT::TreeBuilder
36
38
  end
37
39
  end
38
40
 
41
+ # some JRuby utilities
39
42
  module JRubyUtils
43
+ # A Browser of objects for JRuby
44
+ #
45
+ # link:../docs/object-tree.jpg
40
46
  class RubyObjectBrowser
41
47
  class << self
48
+ # Builds a tree which shows the structure of the given object
42
49
  def object_tree(object)
43
50
  SWT::Builder.go do
44
51
  shell "JRuby Object Viewer by JR - visit http://virtual-void.net" do
data/lib/swt.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # SWT::Builder and SWT::TreeBuilder implementation
2
+ #--
1
3
  # The contents of this file are subject to the Mozilla Public License
2
4
  # Version 1.1 (the "License"); you may not use this file except in
3
5
  # compliance with the License. You may obtain a copy of the License at
@@ -18,10 +20,12 @@
18
20
  require 'java'
19
21
 
20
22
  module SWT
23
+ # see JRubyUtils::RubyObjectBrowser#object_tree
21
24
  def self.object_tree(o)
22
25
  require 'apps/objecttree'
23
26
  JRubyUtils::RubyObjectBrowser.object_tree o
24
27
  end
28
+
25
29
  class TreeCommand
26
30
  def initialize(parent)
27
31
  @parent=parent
@@ -165,28 +169,8 @@ module SWT
165
169
  end
166
170
  def invoke(stack)
167
171
  lastobject=stack.last.getData("object")
168
- #puts "Laststack: #{stack.last} id: #{stack.last.hashCode} object: #{lastobject} id: #{lastobject.hashCode}"
169
172
  newobject=@exp.call(lastobject)
170
- #class <<newobject
171
- # def last
172
- # @last
173
- # end
174
- # def last=(last)
175
- # @last=last
176
- # end
177
- # alias :old_dup :dup
178
- # def dup
179
- # new=old_dup
180
- # new.last=@last
181
- # puts "Object gets dupd: #{self} #{self.id}"
182
- # end
183
- #end
184
- #newobject.last=lastobject
185
- #test=Java.java_to_ruby(newobject)
186
173
  stack.last.setData("object",newobject)
187
- #test2=stack.last.getData("object")
188
- #puts "Test: #{test}, Test2: #{test2} test1==test2: #{test==test2} test1===test2 #{test===test2} id:#{test.id} lasttree: #{stack.last.hashCode}"
189
- # puts "ObjectCommand.invoke lastobject: #{lastobject} newobject: #{newobject} newobject.last: #{newobject.last}"
190
174
  end
191
175
  end
192
176
  class ChildrenCommand<ContainerTreeCommand
@@ -221,18 +205,56 @@ module SWT
221
205
  super if condition
222
206
  end
223
207
  end
208
+
209
+ # Used to describe Trees
210
+ #
211
+ # Example:
212
+ # SWT::Builder.go do
213
+ # shell "Example" do
214
+ # tree :horizontalFill=>true,:verticalFill=>true do
215
+ # child "Father" do
216
+ # child "Peter"
217
+ # child "Paul"
218
+ # child "Mary"
219
+ # end
220
+ # end
221
+ # end
222
+ # end
223
+ #
224
+ # But most of it's power lies in its 'children' and 'link' constructs:
225
+ #
226
+ # kids=%w[Peter Paul Mary] # define some children
227
+ # SWT::Builder.go do
228
+ # shell "Example" do
229
+ # tree :horizontalFill=>true,:verticalFill=>true do # some display styles
230
+ # object {"Fritz"}
231
+ # @father=child do # safe this tree command for later
232
+ # label {|object| "Father #{object}"} # we'll call him Father X
233
+ # children lambda{kids} do # children will evaluate the given lambda when appropriate ...
234
+ # label {|child| "#{child} (age:#{child.length})"} # ... and execute every child command for each child
235
+ # link{@father} # since every child is a maybe-father, link back to father command, see what happens...
236
+ # end # ... yeah, recursive reproduction
237
+ # end
238
+ # end
239
+ # end
240
+ # end
241
+ #
242
+ # Since every command is evaluated lazily, it's possible to construct
243
+ # such infinite trees easily.
244
+ #
245
+ # See JRubyUtils::RubyObjectBrowser#object_tree for a more complex example.
224
246
  class TreeBuilder
225
247
  def initialize(&block)
226
248
  @current=ContainerTreeCommand.new nil
227
249
  instance_eval(&block)
228
250
  end
251
+ # Command to create a new tree column in a tree table
229
252
  def column(text)
230
253
  cmd=SimpleCommand.new @current
231
254
  @current.accept cmd
232
255
  class <<cmd
233
256
  include_class 'org.eclipse.swt.widgets.TreeColumn'
234
257
  def invoke(stack)
235
- # puts "column invoke stack:#{stack}, name:#{@name}"
236
258
  stack.last.setHeaderVisible true
237
259
  tc=TreeColumn.new stack.last,Builder::RealSWT::NONE
238
260
  tc.setText @name
@@ -244,6 +266,7 @@ module SWT
244
266
  end
245
267
  cmd.name=text
246
268
  end
269
+ # Command which creates a new child if invoked
247
270
  def child(label=nil,&block)
248
271
  cmd=ChildCommand.new @current
249
272
  @current and @current.accept cmd
@@ -254,24 +277,37 @@ module SWT
254
277
  @current=old
255
278
  cmd
256
279
  end
280
+ # Command which will set the text of a specific column
281
+ # when invoked
257
282
  def cell(i,&block)
258
283
  @current.accept(LabelCommand.new(@current,block,i))
259
284
  end
285
+ # Command which will set the label of the item
260
286
  def label(labelExp=nil,&block)
261
287
  block=lambda{labelExp} if labelExp
262
288
  @current.accept(LabelCommand.new(@current,block))
263
289
  end
290
+ # Command to set the object property of the item
264
291
  def object(&block)
265
292
  @current.accept(ObjectCommand.new(@current,block))
266
293
  end
294
+ # Command to set the image of a tree item
267
295
  def image(&block)
268
296
  @current.accept(ImageCommand.new(@current,block))
269
297
  end
298
+ # Link back to command used before, if the link command is
299
+ # invoked it simply delegates to the cmd given
270
300
  def link(cmd=nil,&block)
271
301
  cmd=block unless cmd
272
302
  #puts "Creating link with cmd=#{cmd}"
273
303
  @current.accept(SimpleCommand.new(@current, cmd))
274
304
  end
305
+ # Command which expands child items from a collection
306
+ #
307
+ # The collection has to be given as lambda (exp).
308
+ # It will be invoked when the children command is invoked.
309
+ # You may define child commands in a block, these will
310
+ # be executed for each child of the computed collection/array.
275
311
  def children(exp,&block)
276
312
  cmd=ChildrenCommand.new @current,exp
277
313
  @current.accept cmd
@@ -281,11 +317,14 @@ module SWT
281
317
  @current=old
282
318
  cmd
283
319
  end
320
+ # A shortcut command for a child without children
284
321
  def leaf(&block)
285
322
  child do
286
323
  label &block
287
324
  end
288
325
  end
326
+ # Command which acts as a container for other commands.
327
+ # It has no semantics on its own but just invokes it child commands.
289
328
  def container(&block)
290
329
  cmd=ContainerTreeCommand.new @current
291
330
  @current and @current.accept cmd
@@ -295,6 +334,8 @@ module SWT
295
334
  @current=old
296
335
  cmd
297
336
  end
337
+ # Command that will execute only if the condition given as 'exp' evaluates
338
+ # to a non-nil value. It will then invoke its child commands given in a block.
298
339
  def conditional(exp,&block)
299
340
  cmd=ConditionalCommand.new @current,exp
300
341
  @current.accept cmd
@@ -311,16 +352,12 @@ module SWT
311
352
  @current
312
353
  end
313
354
  end
314
- class Builder
315
- #class <<self
316
- # alias :old_include_class :include_class
317
- #def include_class (name,&block)
318
- # cl=old_include_class name,&block
319
- # JavaUtilities.get_proxy_class(name).compile
320
- #end
321
- #end
322
355
 
356
+ # The heart of the SWT module: You may build GUIs with the Builder class
357
+ #
358
+ class Builder
323
359
  include_class 'org.eclipse.swt.widgets.Shell'
360
+ include_class 'org.eclipse.swt.events.SelectionListener'
324
361
  include_class 'org.eclipse.swt.widgets.Group'
325
362
  include_class 'org.eclipse.swt.widgets.Text'
326
363
  include_class 'org.eclipse.swt.widgets.Label'
@@ -335,10 +372,7 @@ module SWT
335
372
  'RealSWT'
336
373
  end
337
374
 
338
- # def initialize#(&block)
339
-
340
- # end
341
-
375
+ # takes a block of Builder language code, builds the screen and shows it
342
376
  def self.go(&block)
343
377
  shell=Builder.new.instance_eval(&block)
344
378
  shell.pack
@@ -347,6 +381,7 @@ module SWT
347
381
  run shell
348
382
  end
349
383
 
384
+ private
350
385
  def init_control(c,opts={})
351
386
  if c.getParent.getLayout.class==GridLayout
352
387
  gd=GridData.new
@@ -376,6 +411,8 @@ module SWT
376
411
  end
377
412
  end
378
413
 
414
+ public
415
+ # Create a shell (a toplevel window)
379
416
  def shell(title,columns=1,style=RealSWT::SHELL_TRIM,&block)
380
417
  res=Shell.new(Display.getDefault,style)
381
418
  res.setText title
@@ -383,15 +420,13 @@ module SWT
383
420
  fl.marginHeight=5
384
421
  fl.marginWidth=5
385
422
  res.setLayout fl
386
- # oldparent=@parent
387
423
  @parent=res
388
- # instance_eval(&block) if block_given?
389
- # @parent=oldparent
390
424
  container(columns,&block)
391
- #init_composite res,columns
392
425
  return res
393
426
  end
394
427
 
428
+ # Create an empty container (does not show anything). It's
429
+ # only supposed for layouting elements
395
430
  def container(columns=1,opts={},&block)
396
431
  opts[:style] or opts[:style]=RealSWT::NONE
397
432
  res=Composite.new(@parent,opts[:style])
@@ -399,6 +434,7 @@ module SWT
399
434
  return res
400
435
  end
401
436
 
437
+ # Create a group element with the given title
402
438
  def group(title,columns=1,style=RealSWT::NONE,&block)
403
439
  res=Group.new(@parent,style)
404
440
  res.setText title
@@ -406,14 +442,16 @@ module SWT
406
442
  return res
407
443
  end
408
444
 
445
+ # Create a text box. Sets the text if supplied
409
446
  def text(text=nil,style=RealSWT::BORDER)
410
- #puts "Text: #{text}"
411
447
  res=Text.new(@parent,style)
412
448
  text and res.setText text
413
449
  init_control res
414
450
  return res
415
451
  end
416
452
 
453
+ # Create a label and show the text supplied.
454
+ # You may use any form of 'labeledText', 'labeledButton'...
417
455
  def label(text,style=RealSWT::NONE)
418
456
  res=Label.new(@parent,style)
419
457
  res.setText text
@@ -421,19 +459,33 @@ module SWT
421
459
  return res
422
460
  end
423
461
 
424
- def button(text,style=RealSWT::NONE)
462
+ # Create a push button with the specified text
463
+ # If block is given this block will be called if button is pressed
464
+ def button(text,style=RealSWT::NONE,&block)
425
465
  res=Button.new(@parent,style)
426
466
  res.setText text
427
467
  init_control res
468
+ if block_given?
469
+ lis=SelectionListener.new
470
+ lis.instance_eval do
471
+ def widgetDefaultSelected(event)
472
+ end
473
+ def widgetSelected (event)
474
+ @block.call event
475
+ end
476
+ @block=block
477
+ end
478
+ res.addSelectionListener lis
479
+ end
428
480
  return res
429
481
  end
430
482
 
483
+ # Create Tree if block is given it is evaluated as TreeBuilder commands
431
484
  def tree(opts={:style=>RealSWT::BORDER},&block)
432
485
  opts[:style] or opts[:style]=RealSWT::BORDER
433
486
  res=Tree.new(@parent,opts[:style])
434
487
  init_control res,opts
435
488
  builder=TreeBuilder.new &block
436
- #puts builder.current
437
489
  builder.run res
438
490
  res.pack
439
491
  return res
@@ -443,16 +495,17 @@ module SWT
443
495
  alias :old_method_missing method_missing
444
496
  end
445
497
 
446
- def method_missing(id,*args)
447
- #puts "ID:#{id.class.inspect}\t\targs:#{args.join ','}\t#{id=~/labeled(\w+)/}"
498
+ def method_missing(id,*args,&block)
448
499
  if id.to_s=~/labeled(\w+)/
449
500
  label args.shift
450
- method($1.downcase).call *args
501
+ method($1.downcase).call *args,&block
451
502
  else
452
- old_method_missing id,args
503
+ old_method_missing id,args,&block
453
504
  end
454
505
  end
455
506
 
507
+ # A helper function for dispatching SWT events.
508
+ # Blocks as long as the given shell is not disposed.
456
509
  def Builder.run(shell)
457
510
  d=shell.getDisplay
458
511
  while(!shell.isDisposed&&shell.isVisible) do
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: sweetgui
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.0.3
7
- date: 2006-09-19 00:00:00 +02:00
6
+ version: 0.0.4
7
+ date: 2006-10-12 00:00:00 +02:00
8
8
  summary: "SWeeTgui: Fast building of SWT GUI screens for JRuby"
9
9
  require_paths:
10
10
  - lib
@@ -31,8 +31,8 @@ files:
31
31
  - tests/ts_swt.rb
32
32
  - lib/apps
33
33
  - lib/swt.rb
34
- - lib/test2.class
35
34
  - lib/apps/objecttree.rb
35
+ - docs/object-tree.jpg
36
36
  - icons/action_back.gif
37
37
  - icons/action_forward.gif
38
38
  - icons/action_go.gif
Binary file