Module: JRubyFX::Controller::ClassMethods

Includes:
DSL
Defined in:
lib/jrubyfx/controller.rb

Overview

class methods for FXML controllers

Constant Summary

Constants included from DSL

DSL::NAME_TO_CLASSES, DSL::NAME_TO_CLASS_NAME

Constants included from FXImports

FXImports::JFX_CLASS_HIERARCHY, FXImports::LOCAL_NAME_MAP

Constants included from JRubyFX

VERSION

Instance Method Summary collapse

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, write_event_method

Methods included from FXImports

#const_missing

Methods included from JRubyFX

#build, included, load_fx, #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_javaObject

decorator to force becoming java class


171
172
173
# File 'lib/jrubyfx/controller.rb', line 171

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


176
177
178
179
180
181
# File 'lib/jrubyfx/controller.rb', line 176

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


84
85
86
87
88
# File 'lib/jrubyfx/controller.rb', line 84

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();

108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/jrubyfx/controller.rb', line 108

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


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/jrubyfx/controller.rb', line 141

def new(*args, &block)
  if @preparsed && @preparsed.length > 0
    return @preparsed.pop.finish_initialization(*args, &block)
  end
  # 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");
}

220
221
222
223
224
225
226
227
# File 'lib/jrubyfx/controller.rb', line 220

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

#preparse_new(num = 3) ⇒ Object


159
160
161
162
163
164
165
166
167
168
# File 'lib/jrubyfx/controller.rb', line 159

def preparse_new(num=3)
  become_java! if @filename
  @preparsed ||= []
  num.times do
    ctrl = allocate
    ctrl.pre_initialize_controller(DEFAULT_SETTINGS.merge({root_dir: @fxml_root_dir || fxml_root,
          filename: @filename})) if @filename
    @preparsed << ctrl
  end
end