diff --git a/lib/jrubyfx.rb b/lib/jrubyfx.rb index 7d0775c..c340abd 100644 --- a/lib/jrubyfx.rb +++ b/lib/jrubyfx.rb @@ -25,3 +25,4 @@ require_relative 'jrubyfx/fxml_application' require_relative 'jrubyfx/fxml_controller' require_relative 'jrubyfx/java_fx_impl' +require_relative 'jrubyfx/inspect' diff --git a/lib/jrubyfx/inspect.rb b/lib/jrubyfx/inspect.rb new file mode 100644 index 0000000..c2ed9ae --- /dev/null +++ b/lib/jrubyfx/inspect.rb @@ -0,0 +1,4 @@ +require 'jrubyfx/utils/inspector' + +# Inject the Inspector into all JavaFX Node descendants (most objects) +Java::JavafxScene::Node.send :include, JRubyFX::Utils::Inspector diff --git a/lib/jrubyfx/utils/inspector.rb b/lib/jrubyfx/utils/inspector.rb new file mode 100644 index 0000000..0e02841 --- /dev/null +++ b/lib/jrubyfx/utils/inspector.rb @@ -0,0 +1,59 @@ +=begin +JRubyFX - Write JavaFX and FXML in Ruby +Copyright (C) 2013 The JRubyFX Team + +Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +=end +module JRubyFX::Utils::Inspector + + class << self + def included(base) + base.extend ClassMethods + end + end + + module ClassMethods + def inspect_properties props = {} + @inspect_properties = props + end + end + + def inspect + props = ["#{self.class}:#{'0x%08x' % object_id}"] + props += (self.class.instance_variable_get('@inspect_properties')||discover_properties). + map do |key,val| + if val.is_a? Array + values = val.dup + format = values.shift + values.map! {|v|send v} + "#{key}:#{sprintf format, *values}" + else + "#{key}:#{send val}" + end + end + "#<#{props.join ' '}>" + end + +private + + # Use reflection to discover properties + def discover_properties + props = {} + (java_class.java_instance_methods - java_class.superclass.java_instance_methods). + select {|m|m.return_type and m.return_type.interfaces.include? Java::JavafxBeansProperty::Property.java_class and not m.name =~ /^impl_/}. + map {|m|send(m.name).getName}. + each {|p|props.merge! p => p} + self.class.instance_variable_set '@inspect_properties', props + end + +end diff --git a/spec/inspect_spec.rb b/spec/inspect_spec.rb new file mode 100644 index 0000000..b27a8e8 --- /dev/null +++ b/spec/inspect_spec.rb @@ -0,0 +1,143 @@ +require 'spec_helper' + +require 'jrubyfx/inspect' + +describe "JRubyFX Inspect" do + + context "javafx.scene.shape.Shape" do + + it "javafx.scene.shape.Arc" do + subject = Java::JavafxSceneShape::Arc.new + subject.inspect.should match /#/ + end + + it "javafx.scene.shape.Polyline" do + subject = Java::JavafxSceneShape::Polyline.new + subject.inspect.should match /#/ + end + + it "javafx.scene.shape.QuadCurve" do + subject = Java::JavafxSceneShape::QuadCurve.new + subject.inspect.should match /#/ + end + + it "javafx.scene.media.MediaView" do + subject = Java::JavafxSceneMedia::MediaView.new + subject.inspect.should match /#/ + end + + it "with blank inspect_properties" do + Java::JavafxSceneShape::Ellipse.inspect_properties + subject.inspect.should match /#/ + end + + it "with {} inspect_properties" do + Java::JavafxSceneShape::Ellipse.inspect_properties {} + subject.inspect.should match /#/ + end + + it "with nil inspect_properties" do + Java::JavafxSceneShape::Ellipse.inspect_properties nil + subject.inspect.should match /#/ + end + + it "with basic inspect_properties" do + Java::JavafxSceneShape::Ellipse.inspect_properties radiusX: :radiusX, radiusY: :radiusY + subject.inspect.should match /#/ + end + + it "with basic inspect_properties v2" do + Java::JavafxSceneShape::Ellipse.inspect_properties foo: :radiusX, bar: :radius_y + subject.inspect.should match /#/ + end + + it "with formatted inspect_properties" do + Java::JavafxSceneShape::Ellipse.inspect_properties radius: ["(%g,%g)",:radiusX,:radiusY] + subject.inspect.should match /#/ + end + +end