# SWT::Builder and SWT::TreeBuilder implementation #-- # The contents of this file are subject to the Mozilla Public License # Version 1.1 (the "License"); you may not use this file except in # compliance with the License. You may obtain a copy of the License at # http://www.mozilla.org/MPL/ # # Software distributed under the License is distributed on an "AS IS" # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the # License for the specific language governing rights and limitations # under the License. # # The Initial Developer of the Original Code is Johannes Rudolph. # Portions created by the Initial Developer are Copyright (C) 2006 # the Initial Developer. All Rights Reserved. # # Contributor(s): # Johannes Rudolph require 'java' module SWT # see JRubyUtils::RubyObjectBrowser#object_tree def self.object_tree(o) require 'apps/objecttree' JRubyUtils::RubyObjectBrowser.object_tree o end class TreeCommand def initialize(parent) @parent=parent end def accept(child) child end def stacktrace "in #{self.class}\n#{@parent.stacktrace if @parent}" end end class SimpleCommand0 res.getParent.removeTreeListener li end end parent.getParent.addTreeListener li end end class LabelCommandtrue,: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. class TreeBuilder def initialize(&block) @current=ContainerTreeCommand.new nil instance_eval(&block) end # Command to create a new tree column in a tree table def column(text) cmd=SimpleCommand.new @current @current.accept cmd class <RealSWT::BORDER},&block) opts[:style] or opts[:style]=RealSWT::BORDER res=Tree.new(@parent,opts[:style]) init_control res,opts builder=TreeBuilder.new &block builder.run res res.pack return res end unless Builder.methods.include? :method_missing alias :old_method_missing method_missing end def method_missing(id,*args,&block) if id.to_s=~/labeled(\w+)/ label args.shift method($1.downcase).call *args,&block else old_method_missing id,args,&block end end # A helper function for dispatching SWT events. # Blocks as long as the given shell is not disposed. def Builder.run(shell) d=shell.getDisplay while(!shell.isDisposed&&shell.isVisible) do while(d.readAndDispatch) do end d.sleep end end end end