Class: ScriptEventHandler

Inherits:
Object
  • Object
show all
Includes:
EventHandler
Defined in:
lib/fxmlloader/elts.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(script, scriptEngine) ⇒ ScriptEventHandler

Returns a new instance of ScriptEventHandler.



534
535
536
537
# File 'lib/fxmlloader/elts.rb', line 534

def initialize(script, scriptEngine)
  @script = script;
  @scriptEngine = scriptEngine;
end

Instance Attribute Details

#scriptObject (readonly)

Returns the value of attribute script.



533
534
535
# File 'lib/fxmlloader/elts.rb', line 533

def script
  @script
end

#scriptEngineObject (readonly)

Returns the value of attribute scriptEngine.



533
534
535
# File 'lib/fxmlloader/elts.rb', line 533

def scriptEngine
  @scriptEngine
end

Instance Method Details

#handle(event) ⇒ Object



539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# File 'lib/fxmlloader/elts.rb', line 539

def handle(event)
  # Don't pollute the page namespace with values defined in the script
  engineBindings = @scriptEngine.getBindings(ScriptContext::ENGINE_SCOPE);
  #localBindings = @scriptEngine.createBindings(); # TODO: this causes errors with nashorn in jdk8 by creating a different kind of 
  # script object that doesn't respect the magic nashorn.global object
  localBindings = Java::JavaxScript::SimpleBindings.new
  localBindings.put_all(engineBindings)
  localBindings.put(FXL::EVENT_KEY, event);
  @scriptEngine.setBindings(localBindings, ScriptContext::ENGINE_SCOPE);

  # Execute the script
  begin
    @scriptEngine.eval(@script);
  rescue ScriptException => exception
    raise exception
  end

  # Restore the original bindings
  @scriptEngine.setBindings(engineBindings, ScriptContext::ENGINE_SCOPE);
end