sfxcode / sapphire-core

Scala - JavaFX Application Framework (CDI, Binding, Expressions)
https://sfxcode.github.io/sapphire-javafx
Apache License 2.0
8 stars 1 forks source link

Access the viewController right away in Fxml wrapper #2

Closed mmoyles87 closed 9 years ago

mmoyles87 commented 9 years ago

It seems that I can't access my view controller right away in the Fxml wrapper class

class ParametersController extends ViewController {

    val validationSupport = new ValidationSupport()

    def ui = fxml.asInstanceOf[ParametersFxml]

    override def didGainVisibility() {
        super.didGainVisibility()
        //println(ApplicationEnvironment.controllerMap)
    }

    def actionCancel() {
        applicationController.showDashboard()
    }

    def actionSave() {
        applicationController.showDashboard()
    }

    def registerValidatorForId[T](validator: Validator[T], fxid: String, parent: Node = null, required:Boolean = false) {
    val node = locateInternal(fxid, parent)
    registerValidator(validator, node, required)
  }

  def registerValidator[T](validator: Validator[T], node: Option[Node], required:Boolean = false) {
    node.foreach(c => {
      validationSupport.registerValidator(c.asInstanceOf[Control], required, validator)
    })
  }

    def applicationController():ApplicationController = {
    getBean[ApplicationController]()
  }

}

@sfxml
class ParametersFxml(viewController: ParametersController, private val txtBrineQMaxMin: TextField) {
  println(viewController) // prints null
  viewController.registerValidator(Validator.createEmptyValidator("Text is required"), Some(txtBrineQMaxMin.delegate.asInstanceOf[Control]), true)

  def actionSave() {
    viewController.actionCancel() //works fine
  }
}

Any ideas?

sfxcode commented 9 years ago

Hello, it is a problem of initialization order. Because of view controller will initialize the sfxml class, it is not available at instantiation of ParametersFxml.

I prefer to register the validator in the viewcontroller.

  override def didGainVisibilityFirstTime() {
    super.didGainVisibilityFirstTime()
  registerValidator(Validator.createEmptyValidator("Text is required"),Some(ui.txtBrineQMaxMin), true)
}

Greetings,

Tom

sfxcode commented 4 years ago

Good morning nova999, before i removed the direct dependency of scalafx in sapphire-core there was some experimental support of using sfxml. It was possible to integrate, but there were a lot of problems working with it. Code completion for properties in the IDE was gone, the code was harder to read and as main point sfxml had to little advantages. scalafx can operate without any boilerplate code because of implicit conversion from javafx to scalafx. Also using scala 2.13 and and Java 11+ makes the advantages of scalafx a little bit smaller. imho there is no really need of using sfxml. Have a nice coding time,

Tom