Module: JRubyFX::Controller::ClassMethods
- Includes:
- DSL
- Defined in:
- lib/jrubyfx/controller.rb
Overview
class methods for FXML controllers
Constant Summary
Constants included from DSL
Constants included from JRubyFX
Constants included from FXImports
FXImports::JFX_CLASS_HIERARCHY
Instance Method Summary collapse
-
#become_java ⇒ Object
decorator to force becoming java class.
-
#fxml(fxml = nil, name = nil, root_dir = nil) ⇒ Object
Set the filename of the fxml this control is part of.
-
#included(base) ⇒ Object
nested including, TODO: don’t duplicate this.
-
#load_into(stage, settings = {}) ⇒ Object
Load given fxml file onto the given stage.
-
#new(*args, &block) ⇒ Object
This is the default override for custom controls Normal FXML controllers will use Control#new.
-
#on(names, &block) ⇒ Object
call-seq: on(callback, …) { |event_info| block } => Method.
Methods included from DSL
compile_dsl, included, load_dsl, #logical_lookup, #method_missing, #self_test_lookup, write_color_method_converter, write_enum_converter, write_enum_method_converter
Methods included from JRubyFX
#build, included, #run_later, #with
Methods included from Utils::CommonUtils
#attempt_conversion, #populate_properties, #split_args_from_properties
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class JRubyFX::DSL
Instance Method Details
#become_java ⇒ Object
decorator to force becoming java class
133 134 135 |
# File 'lib/jrubyfx/controller.rb', line 133 def become_java @force_java = true end |
#fxml(fxml = nil, name = nil, root_dir = nil) ⇒ Object
Set the filename of the fxml this control is part of
138 139 140 141 142 143 |
# File 'lib/jrubyfx/controller.rb', line 138 def fxml(fxml=nil, name = nil, root_dir = nil) @filename = fxml # snag the filename from the caller @fxml_root_dir = root_dir register_type(self, name) if name end |
#included(base) ⇒ Object
nested including, TODO: don’t duplicate this
60 61 62 63 64 |
# File 'lib/jrubyfx/controller.rb', line 60 def included(base) base.extend(JRubyFX::Controller::ClassMethods) # register ourselves as a control. overridable with custom_fxml_control JRubyFX::DSL::ClassUtils.register_type base if base.is_a? Class end |
#load_into(stage, settings = {}) ⇒ Object
Load given fxml file onto the given stage. settings is an optional hash of:
-
:initialize => [array of arguments to pass to the initialize function]
-
:width => Default width of the Scene
-
:height => Default height of the Scene
-
:fill => Fill color of the Scene’s background
-
:depth_buffer => JavaFX Scene DepthBuffer argument (look it up)
-
:root_dir => filename search for fxml realtive to this file
Examples
controller = MyFXController.new "Demo.fxml", stage
Equivalent Java
Parent root = FXMLLoader.load(getClass().getResource("Demo.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
controller = root.getController();
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/jrubyfx/controller.rb', line 84 def load_into(stage, settings={}) # Inherit from default settings settings = DEFAULT_SETTINGS.merge({root_dir: (self.instance_variable_get("@fxml_root_dir") || fxml_root), filename: self.instance_variable_get("@filename")}).merge settings # Custom controls don't always need to be pure java, but oh well... become_java! # like new, without initialize ctrl = allocate # Set the stage so we can reference it if needed later ctrl.stage = stage # load the FXML file root = Controller.get_fxml_loader(settings[:filename], ctrl, settings[:root_dir]).load # Unless the FXML root node is a scene, wrap that node in a scene if root.is_a? Scene scene = root else scene = Scene.new root, settings[:width], settings[:height], settings[:depth_buffer] scene.fill = settings[:fill] end # set the controller and stage scene ctrl.scene = stage.scene = scene ctrl.finish_initialization *settings[:initialize].to_a end |
#new(*args, &block) ⇒ Object
This is the default override for custom controls Normal FXML controllers will use Control#new
117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/jrubyfx/controller.rb', line 117 def new(*args, &block) # Custom controls don't always need to be pure java, but oh well... become_java! if @filename # like new, without initialize ctrl = allocate ctrl.initialize_controller(DEFAULT_SETTINGS.merge({root_dir: @fxml_root_dir || fxml_root, filename: @filename}), *args, &block) if @filename # return the controller ctrl end |
#on(names, &block) ⇒ Object
call-seq:
on(callback, ...) { |event_info| block } => Method
Registers a function of name name for a FXML defined event with the body in the block. Note you can also just use normal methods
Examples
on :click do
puts "button clicked"
end
on :moved, :pressed do |event|
puts "Mouse Moved or Key Pressed"
p event
end
Equivalent Java
@FXML
private void click(ActionEvent event) {
System.out.println("button clicked");
}
@FXML
private void moved(MouseEvent event) {
System.out.println("Mouse Moved or Key Pressed");
}
@FXML
private void keypress(KeyEvent event) {
System.out.println("Key Pressed or Key Pressed");
}
182 183 184 185 186 187 188 189 |
# File 'lib/jrubyfx/controller.rb', line 182 def on(names, &block) [names].flatten.each do |name| class_eval do # must define this way so block executes in class scope, not static scope define_method name, block end end end |