Skip to content

Node inspector #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/jrubyfx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@
require_relative 'jrubyfx/fxml_application'
require_relative 'jrubyfx/fxml_controller'
require_relative 'jrubyfx/java_fx_impl'
require_relative 'jrubyfx/inspect'
4 changes: 4 additions & 0 deletions lib/jrubyfx/inspect.rb
Original file line number Diff line number Diff line change
@@ -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
59 changes: 59 additions & 0 deletions lib/jrubyfx/utils/inspector.rb
Original file line number Diff line number Diff line change
@@ -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
143 changes: 143 additions & 0 deletions spec/inspect_spec.rb
Original file line number Diff line number Diff line change
@@ -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 /#<Java::JavafxSceneShape::Arc:0x[0-9a-f]+/
subject.inspect.should match 'centerX:0.0'
subject.inspect.should match 'centerY:0.0'
subject.inspect.should match 'length:0.0'
subject.inspect.should match 'radiusX:0.0'
subject.inspect.should match 'radiusY:0.0'
subject.inspect.should match 'startAngle:0.0'
subject.inspect.should match 'type:OPEN'
end

it "javafx.scene.shape.Circle" do
subject = Java::JavafxSceneShape::Circle.new
subject.inspect.should match /#<Java::JavafxSceneShape::Circle:0x[0-9a-f]+/
subject.inspect.should match 'centerX:0.0'
subject.inspect.should match 'centerY:0.0'
subject.inspect.should match 'radius:0.0'
end

it "javafx.scene.shape.CubicCurve" do
subject = Java::JavafxSceneShape::CubicCurve.new
subject.inspect.should match /#<Java::JavafxSceneShape::CubicCurve:0x[0-9a-f]+/
subject.inspect.should match 'controlX1:0.0'
subject.inspect.should match 'controlX2:0.0'
subject.inspect.should match 'controlY1:0.0'
subject.inspect.should match 'controlY2:0.0'
subject.inspect.should match 'startX:0.0'
subject.inspect.should match 'startY:0.0'
subject.inspect.should match 'endX:0.0'
subject.inspect.should match 'endY:0.0'
end

it "javafx.scene.shape.Ellipse" do
subject = Java::JavafxSceneShape::Ellipse.new
subject.inspect.should match /#<Java::JavafxSceneShape::Ellipse:0x[0-9a-f]+/
subject.inspect.should match 'centerX:0.0'
subject.inspect.should match 'centerY:0.0'
subject.inspect.should match 'radiusX:0.0'
subject.inspect.should match 'radiusY:0.0'
end

it "javafx.scene.shape.Line" do
subject = Java::JavafxSceneShape::Line.new
subject.inspect.should match /#<Java::JavafxSceneShape::Line:0x[0-9a-f]+/
subject.inspect.should match 'endX:0.0'
subject.inspect.should match 'endY:0.0'
subject.inspect.should match 'startX:0.0'
subject.inspect.should match 'startY:0.0'
end

it "javafx.scene.shape.Path" do
subject = Java::JavafxSceneShape::Path.new
subject.inspect.should match /#<Java::JavafxSceneShape::Path:0x[0-9a-f]+/
subject.inspect.should match 'fillRule:NON_ZERO'
end

it "javafx.scene.shape.Polygon" do
subject = Java::JavafxSceneShape::Polygon.new
subject.inspect.should match /#<Java::JavafxSceneShape::Polygon:0x[0-9a-f]+>/
end

it "javafx.scene.shape.Polyline" do
subject = Java::JavafxSceneShape::Polyline.new
subject.inspect.should match /#<Java::JavafxSceneShape::Polyline:0x[0-9a-f]+>/
end

it "javafx.scene.shape.QuadCurve" do
subject = Java::JavafxSceneShape::QuadCurve.new
subject.inspect.should match /#<Java::JavafxSceneShape::QuadCurve:0x[0-9a-f]+/
subject.inspect.should match 'controlX:0.0'
subject.inspect.should match 'controlY:0.0'
subject.inspect.should match 'startX:0.0'
subject.inspect.should match 'startY:0.0'
subject.inspect.should match 'endX:0.0'
subject.inspect.should match 'endY:0.0'
end

it "javafx.scene.shape.Rectangle" do
subject = Java::JavafxSceneShape::Rectangle.new
subject.inspect.should match /#<Java::JavafxSceneShape::Rectangle:0x[0-9a-f]+/
subject.inspect.should match 'arcWidth:0.0'
subject.inspect.should match 'arcHeight:0.0'
subject.inspect.should match 'x:0.0'
subject.inspect.should match 'y:0.0'
subject.inspect.should match 'width:0.0'
subject.inspect.should match 'height:0.0'
end

it "javafx.scene.shape.SVGPath" do
subject = Java::JavafxSceneShape::SVGPath.new
subject.inspect.should match /#<Java::JavafxSceneShape::SVGPath:0x[0-9a-f]+/
subject.inspect.should match 'fillRule:NON_ZERO'
subject.inspect.should match 'content:'
end

it "javafx.scene.text.Text" do
subject = Java::JavafxSceneText::Text.new 'test'
subject.inspect.should match /#<Java::JavafxSceneText::Text:0x[0-9a-f]+/
subject.inspect.should match 'text:'
subject.inspect.should match 'textAlignment:LEFT'
subject.inspect.should match 'font:'
subject.inspect.should match 'underline:false'
subject.inspect.should match 'lineSpacing:0.0'
subject.inspect.should match 'x:0.0'
subject.inspect.should match 'y:0.0'
subject.inspect.should match 'textOrigin:BASELINE'
subject.inspect.should match 'boundsType:LOGICAL'
subject.inspect.should match 'wrappingWidth:0.0'
subject.inspect.should match 'strikethrough:false'
subject.inspect.should match 'fontSmoothingType:GRAY'
end

end

it "javafx.scene.canvas.Canvas" do
subject = Java::JavafxSceneCanvas::Canvas.new
subject.inspect.should match /#<Java::JavafxSceneCanvas::Canvas:0x[0-9a-f]+ width:0\.0 height:0\.0>/
end

it "javafx.scene.media.MediaView" do
subject = Java::JavafxSceneMedia::MediaView.new
subject.inspect.should match /#<Java::JavafxSceneMedia::MediaView:0x[0-9a-f]+/
subject.inspect.should match 'x:0.0'
subject.inspect.should match 'y:0.0'
subject.inspect.should match 'smooth:true'
subject.inspect.should match 'fitWidth:0.0'
subject.inspect.should match 'fitHeight:0.0'
subject.inspect.should match 'preserveRatio:true'
subject.inspect.should match 'viewport:'
subject.inspect.should match 'onError:'
subject.inspect.should match 'mediaPlayer:'
end

end
57 changes: 57 additions & 0 deletions spec/inspector_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require 'spec_helper'

require 'jrubyfx/utils/inspector'

# Inject the inspector into the base JavaFX class
Java::JavafxScene::Node.send :include, JRubyFX::Utils::Inspector

## Alternate Syntax
# class Java::JavafxSceneShape::Ellipse
# include JRubyFX::Utils::Inspector
# inspect_properties radiusX: :radiusX, radiusY: :radiusY
# end

describe JRubyFX::Utils::Inspector do

subject { Java::JavafxSceneShape::Ellipse.new 1.3, 2, 3.2, 42.0 }

# Clear the property cache
before do
subject.class.instance_variable_set '@inspect_properties', nil
end

it "without inspect_properties" do
subject.inspect.should match /#<Java::JavafxSceneShape::Ellipse:0x[0-9a-f]+ centerX:1\.3 centerY:2\.0 radiusX:3\.2 radiusY:42\.0>/
end

it "with blank inspect_properties" do
Java::JavafxSceneShape::Ellipse.inspect_properties
subject.inspect.should match /#<Java::JavafxSceneShape::Ellipse:0x[0-9a-f]+>/
end

it "with {} inspect_properties" do
Java::JavafxSceneShape::Ellipse.inspect_properties {}
subject.inspect.should match /#<Java::JavafxSceneShape::Ellipse:0x[0-9a-f]+>/
end

it "with nil inspect_properties" do
Java::JavafxSceneShape::Ellipse.inspect_properties nil
subject.inspect.should match /#<Java::JavafxSceneShape::Ellipse:0x[0-9a-f]+ centerX:1\.3 centerY:2\.0 radiusX:3\.2 radiusY:42\.0>/
end

it "with basic inspect_properties" do
Java::JavafxSceneShape::Ellipse.inspect_properties radiusX: :radiusX, radiusY: :radiusY
subject.inspect.should match /#<Java::JavafxSceneShape::Ellipse:0x[0-9a-f]+ radiusX:3\.2 radiusY:42\.0>/
end

it "with basic inspect_properties v2" do
Java::JavafxSceneShape::Ellipse.inspect_properties foo: :radiusX, bar: :radius_y
subject.inspect.should match /#<Java::JavafxSceneShape::Ellipse:0x[0-9a-f]+ foo:3\.2 bar:42\.0>/
end

it "with formatted inspect_properties" do
Java::JavafxSceneShape::Ellipse.inspect_properties radius: ["(%g,%g)",:radiusX,:radiusY]
subject.inspect.should match /#<Java::JavafxSceneShape::Ellipse:0x[0-9a-f]+ radius:\(3\.2,42\)>/
end

end