Skip to content
forked from vigoo/scalafxml

Bridging the gap between scalafx and FXML with generated proxies

Notifications You must be signed in to change notification settings

jpsacha/scalafxml

 
 

Repository files navigation

scalafxml

Build Status

The scalafx library is a great UI DSL that wraps JavaFX classes and provides a nice syntax to work with them from Scala.

This library bridges FXML and scalafx by automatically building proxy classes, enabling a more clear controller syntax.

Status

The main branch contains the initial implementation of the compile time proxy generator, which uses macro annotations. This requires the addition of the macro paradise compiler plugin, but has no runtime dependencies. It depends on ScalaFX 8 and JavaFX 8.

The SFX-2 branch is the compile time proxy generator for ScalaFX 2.2 using JavaFX 2.

On the dynamic branch there is the first version of the proxy generator which executes runtime. This has a disadvantage of having scala-compiler.jar as a dependency, but has no special compile-time dependencies.

The latest published version is 0.3. To use it in SBT add:

addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full)

libraryDependencies += "org.scalafx" %% "scalafxml-core-sfx8" % "0.3"

Example

The controller's, referenced from the FXML's through the fx:controller attribute, can be implemented as simple Scala classes, getting all the bound controls though the constructor:

import scalafx.scene.control.TextField
import scalafx.scene.control.Button
import scalafx.scene.control.ListView
import scalafx.event.ActionEvent
import scalafxml.core.macros.sfxml

@sfxml
class TestController(
    private val input: TextField,
    private val create: Button,
    private val recentInputs: ListView[String],
    private val dep: AnAdditionalDependency) {

	// event handlers are simple public methods:
	def onCreate(event: ActionEvent) {
		// ...
	}
}

Beside the JavaFX controls, additional dependencies can be injected to the controller as well. This injection process is extensible.

The following example uses SubCut for injecting the additional dependency:

object SubCutDemo extends JFXApp {

  implicit val bindingModule = newBindingModule(module => {
    import module._

    bind [AnAdditionalDependency] toSingle(new AnAdditionalDependency("subcut dependency"))
  })

  stage = new JFXApp.PrimaryStage() {
    title = "Test window"
    scene = new Scene(
		FXMLView(
			getClass.getResource("test.fxml"),
			new SubCutDependencyResolver()))
  }
}

but it is also possible to simply give the dependencies by their type or by their name:

object SimpleDemo extends JFXApp {

  stage = new JFXApp.PrimaryStage() {
    title = "Test window"
    scene = new Scene(
		FXMLView(getClass.getResource("test.fxml"),
		    new DependenciesByType(Map(
		      typeOf[AnAdditionalDependency] -> new AnAdditionalDependency("dependency by type"))))

  }
}

Requirements

  • sbt 0.13 is required

Related

About

Bridging the gap between scalafx and FXML with generated proxies

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Scala 100.0%