root / sweetgui / trunk / lib / apps / objecttree.rb

Revision 64, 5.6 kB (checked in by johannes, 2 years ago)

* fix the wrong email address ;)

Line 
1# This file is an application of the SWT module. JRubyUtils::RubyObjectBrowser may be
2# used to show the contents of an object.
3#--
4#   The contents of this file are subject to the Mozilla Public License
5#   Version 1.1 (the "License"); you may not use this file except in
6#   compliance with the License. You may obtain a copy of the License at
7#   http://www.mozilla.org/MPL/
8#
9#   Software distributed under the License is distributed on an "AS IS"
10#   basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11#   License for the specific language governing rights and limitations
12#   under the License.
13#
14#   The Initial Developer of the Original Code is Johannes Rudolph.
15#   Portions created by the Initial Developer are Copyright (C) 2006
16#   the Initial Developer. All Rights Reserved.
17#
18#   Contributor(s):
19#      Johannes Rudolph <johannes.rudolph@gmail.com>
20
21class Method
22  # get the name of the method
23  def name
24    method_field=Java::JavaClass.for_name('org.jruby.RubyMethod').declared_field('methodName')
25    method_field.accessible=true
26    Java::java_to_ruby(method_field.value(Java::ruby_to_java(self)))
27  end
28end
29
30class ::SWT::TreeBuilder
31  include_class 'org.eclipse.swt.graphics.Image'
32  include_class 'org.eclipse.swt.widgets.Display'
33  def i(o)
34    @@image_cache={} unless defined? @@image_cache
35    return @@image_cache[o] if @@image_cache.has_key? o
36    res=File.expand_path(File.dirname(__FILE__)+"/../../"+o)
37    Image.new Display.getCurrent,res
38  end
39end
40
41# some JRuby utilities
42module JRubyUtils
43  # A Browser of objects for JRuby
44  #
45  # link:../docs/object-tree.jpg
46  class RubyObjectBrowser
47    class << self
48      # Builds a tree which shows the structure of the given object
49      def object_tree(object)
50        SWT::Builder.go do
51          shell "JRuby Object Viewer by JR - visit http://virtual-void.net" do
52            tree(:verticalFill=>true,:horizontalFill=>true) do
53              child do
54                object {object}
55                @object=container do
56                  label {|o|o.inspect}
57                  image {i("icons/arrow_right.gif")}
58
59                  # instance variables node
60                  child "instance_variables" do
61                    image {i("icons/flag_blue.gif")}
62                    children(lambda{|o|o.instance_variables.sort.collect! {|x|{:object=>o,:name=>x}  }}) do
63                      image {i("icons/flag_blue.gif")}
64                      label {|v|v[:name]}
65                      child do
66                        object {|ar|
67                          #puts "Object: #{ar}"
68                          #name.last.instance_variable_get name
69                          ar[:object].instance_variable_get ar[:name]
70                        }
71                        label {|o|o.to_s}
72                        link {@object}
73                      end
74                    end
75                  end
76
77                  # methods node
78                  child "methods" do
79                    image {i("icons/page_text.gif")}
80                    children(lambda {|o|
81                               o.methods.sort.collect! do |name|
82                                 {:method=>name,:object=>o}
83                               end
84                             }) do
85                      image {i("icons/page_text.gif")}
86                      label {|h|h[:method]}
87
88                      # execute method if it has no arguments
89                      conditional lambda{|h|
90                        (-1..0).include? h[:object].method(h[:method]).arity
91                      } do
92                        child do
93                          object do |h|
94                            begin
95                              o=h[:object].dup
96                              o.send(h[:method])
97                            rescue Exception=>exc
98                              exc
99                            end
100                          end
101                          link {@object}
102                        end
103                      end
104                    end
105                  end
106
107                  # class node
108                  child "class" do
109                    object {|o|o.class}
110                    @class=container do
111                      image {i("icons/icon_component.gif")}
112
113                      child do
114                        image {i("icons/icon_component.gif")}
115                        label{|c|c.to_s}
116                        child "class_variables" do
117                          image{i("icons/flag_red.gif")}
118                          children(lambda{|c|c.class_variables.sort.collect! { |x|{:class=>c,:name=>x}}}) do
119                            image{i("icons/flag_red.gif")}
120                            label{|horizontalFill|h[:name]}
121                            child do
122                              image{i("icons/arrow_right.gif")}
123                              label{|horizontalFill|h[:class].class_eval h[:name]}
124                            end
125                          end
126                        end
127                        link{@object}
128                      end
129
130                      # superclass node if appropriate
131                      conditional lambda{|c|c.superclass}do
132                        child "superclass" do
133                          image {i("icons/icon_component.gif")}
134                          object{|c|c.superclass}
135                          link{@class}
136                        end
137                      end
138                    end
139                  end
140                end
141              end
142            end
143          end
144        end
145      end
146    end
147  end
148end
Note: See TracBrowser for help on using the browser.