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



200
201
202
# File 'lib/jrubyfx/controller.rb', line 200

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



205
206
207
208
209
210
# File 'lib/jrubyfx/controller.rb', line 205

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



93
94
95
96
97
# File 'lib/jrubyfx/controller.rb', line 93

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


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/jrubyfx/controller.rb', line 117

def load_into(stage, settings={})
  # Inherit from default settings
  settings = DEFAULT_SETTINGS.merge({root_dir: (self.instance_variable_get("@fxml_root_dir") || fxml_root),
      get_resources: get_fxml_resource_class_loader,
      fxml_class_loader: get_fxml_classes_class_loader,
      filename: self.instance_variable_get("@filename")}).merge settings
      
  raise "JRubyFX 1.x style class loader argument passed into 'load_into'. Replace 'class_loader' with either 'get_resources' (likely) or 'fxml_class_loader' (less likely)" if settings.has_key? :class_loader
      
  unless @built
    ## Use the temporary loader to reformat AController
    JRubyFX::FxmlHelper.transform self, Controller.get_fxml_location(settings[:root_dir], settings[:filename], settings[:get_resources])
  
    # Custom controls don't always need to be pure java, but oh well...
    become_java!
    @built = true
  end

  # 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], settings[:get_resources], settings[:fxml_class_loader]).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



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/jrubyfx/controller.rb', line 160

def new(*args, &block)
  if @preparsed && @preparsed.length > 0
    return @preparsed.pop.finish_initialization(*args, &block)
  end
  
  settings = DEFAULT_SETTINGS.merge({root_dir: @fxml_root_dir || fxml_root, get_resources: get_fxml_resource_class_loader,
      fxml_class_loader: get_fxml_classes_class_loader, filename: @filename})
  
  # Custom controls don't always need to be pure java, but oh well...
  if @filename && !@built
    @built = true # TODO: move later if no new :bake call
    ## Use the temporary loader to reformat AController
    JRubyFX::FxmlHelper.transform self, Controller.get_fxml_location(settings[:root_dir], settings[:filename], settings[:get_resources])
  
    # Custom controls don't always need to be pure java, but oh well...
    become_java!
  end

  # like new, without initialize
  ctrl = allocate

  ctrl.initialize_controller(settings,
    *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");
}


249
250
251
252
253
254
255
256
# File 'lib/jrubyfx/controller.rb', line 249

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



188
189
190
191
192
193
194
195
196
197
# File 'lib/jrubyfx/controller.rb', line 188

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