Class: SWT::TreeBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/swt.rb,
lib/apps/objecttree.rb

Overview

Used to describe Trees

Example:

SWT::Builder.go do
  shell "Example" do
    tree :horizontalFill=>true,:verticalFill=>true do
      child "Father" do
        child "Peter"
        child "Paul"
        child "Mary"
      end
    end
  end
end

But most of it's power lies in its 'children' and 'link' constructs:

kids=%w[Peter Paul Mary] # define some children
SWT::Builder.go do
  shell "Example" do
    tree :horizontalFill=>true,:verticalFill=>true do # some display styles
      object {"Fritz"}
      @father=child do  # safe this tree command for later
        label {|object| "Father #{object}"} # we'll call him Father X
        children lambda{kids} do # children will evaluate the given lambda when appropriate ...
          label {|child| "#{child} (age:#{child.length})"} # ... and execute every child command for each child
          link{@father} # since every child is a maybe-father, link back to father command, see what happens...
        end # ... yeah, recursive reproduction
      end
    end
  end
end

Since every command is evaluated lazily, it's possible to construct such infinite trees easily.

See JRubyUtils::RubyObjectBrowser#object_tree for a more complex example.

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ TreeBuilder


247
248
249
250
# File 'lib/swt.rb', line 247

def initialize(&block)
  @current=ContainerTreeCommand.new nil
  instance_eval(&block)
end

Instance Method Details

#cell(i, &block) ⇒ Object

Command which will set the text of a specific column when invoked


282
283
284
# File 'lib/swt.rb', line 282

def cell(i,&block)
  @current.accept(LabelCommand.new(@current,block,i))
end

#child(label = nil, &block) ⇒ Object

Command which creates a new child if invoked


270
271
272
273
274
275
276
277
278
279
# File 'lib/swt.rb', line 270

def child(label=nil,&block)
  cmd=ChildCommand.new @current
  @current and @current.accept cmd
  old=@current
  @current=cmd
  label(label) if label
  instance_eval(&block) if block
  @current=old
  cmd
end

#children(exp, &block) ⇒ Object

Command which expands child items from a collection

The collection has to be given as lambda (exp). It will be invoked when the children command is invoked. You may define child commands in a block, these will be executed for each child of the computed collection/array.


311
312
313
314
315
316
317
318
319
# File 'lib/swt.rb', line 311

def children(exp,&block)
  cmd=ChildrenCommand.new @current,exp
  @current.accept cmd
  old=@current
  @current=cmd
  instance_eval(&block) if block
  @current=old
  cmd
end

#column(text) ⇒ Object

Command to create a new tree column in a tree table


252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/swt.rb', line 252

def column(text)
  cmd=SimpleCommand.new @current
  @current.accept cmd
  class <<cmd
    include_class 'org.eclipse.swt.widgets.TreeColumn'
    def invoke(stack)
      stack.last.setHeaderVisible true
      tc=TreeColumn.new stack.last,Builder::RealSWT::NONE
      tc.setText @name
      tc.pack
    end
    def name=(name)
      @name=name
    end
  end
  cmd.name=text
end

#conditional(exp, &block) ⇒ Object

Command that will execute only if the condition given as 'exp' evaluates to a non-nil value. It will then invoke its child commands given in a block.


339
340
341
342
343
344
345
346
347
# File 'lib/swt.rb', line 339

def conditional(exp,&block)
  cmd=ConditionalCommand.new @current,exp
  @current.accept cmd
  old=@current
  @current=cmd
  instance_eval(&block) if block
  @current=old
  cmd
end

#container(&block) ⇒ Object

Command which acts as a container for other commands. It has no semantics on its own but just invokes it child commands.


328
329
330
331
332
333
334
335
336
# File 'lib/swt.rb', line 328

def container(&block)
  cmd=ContainerTreeCommand.new @current
  @current and @current.accept cmd
  old=@current
  @current=cmd
  instance_eval(&block)if block
  @current=old
  cmd
end

#currentObject


351
352
353
# File 'lib/swt.rb', line 351

def current
  @current
end

#i(o) ⇒ Object


33
34
35
36
37
38
# File 'lib/apps/objecttree.rb', line 33

def i(o)
  @@image_cache={} unless defined? @@image_cache
  return @@image_cache[o] if @@image_cache.has_key? o
  res=File.expand_path(File.dirname(__FILE__)+"/../../"+o)
  Image.new Display.getCurrent,res
end

#image(&block) ⇒ Object

Command to set the image of a tree item


295
296
297
# File 'lib/swt.rb', line 295

def image(&block)
  @current.accept(ImageCommand.new(@current,block))
end

#label(labelExp = nil, &block) ⇒ Object

Command which will set the label of the item


286
287
288
289
# File 'lib/swt.rb', line 286

def label(labelExp=nil,&block)
  block=lambda{labelExp} if labelExp
  @current.accept(LabelCommand.new(@current,block))
end

#leaf(&block) ⇒ Object

A shortcut command for a child without children


321
322
323
324
325
# File 'lib/swt.rb', line 321

def leaf(&block)
  child do
    label &block
  end
end

Link back to command used before, if the link command is invoked it simply delegates to the cmd given


300
301
302
303
304
# File 'lib/swt.rb', line 300

def link(cmd=nil,&block)
  cmd=block unless cmd
  #puts "Creating link with cmd=#{cmd}"
  @current.accept(SimpleCommand.new(@current, cmd))
end

#object(&block) ⇒ Object

Command to set the object property of the item


291
292
293
# File 'lib/swt.rb', line 291

def object(&block)
  @current.accept(ObjectCommand.new(@current,block))
end

#run(tree) ⇒ Object


348
349
350
# File 'lib/swt.rb', line 348

def run(tree)
  @current.invoke [tree]
end