Tutorial Application

Application object

A sapphire application must contain an Application object that extends SFXApplication.

All startup code is inside this SFXApplication pattern.

sourcepackage com.sfxcode.sapphire.javafx.demo.tutorial

import com.sfxcode.sapphire.javafx.application.SFXApplication
import com.sfxcode.sapphire.javafx.controller.SFXApplicationController

object Application extends SFXApplication {

  val applicationController: SFXApplicationController = new ApplicationController

  override def height: Int = 600

  override def width: Int = 800

  override def forceMaxWidth: Boolean = true

  override def forceMaxHeight: Boolean = true
}

SFXApplicationController

Application controller is used for startup purposes.

Normally the main scene content is replaced by a SFXViewController.

Here we will use a MainViewController and later we connect a Navigation-, Workspace- and StatusBarController.

sourcepackage com.sfxcode.sapphire.javafx.demo.tutorial

import java.util.{ Locale, ResourceBundle }
import com.sfxcode.sapphire.javafx.application.SFXApplicationEnvironment
import com.sfxcode.sapphire.javafx.controller.SFXApplicationController
import com.sfxcode.sapphire.javafx.demo.tutorial.controller.app.MainViewController

class ApplicationController extends SFXApplicationController {

  var mainViewController: MainViewController = _

  def applicationDidLaunch() {
    logger.info("start " + this)
    // #Resources
    SFXApplicationEnvironment.loadResourceBundle("bundles/application")
    // #Resources
    reload()
  }

  def reload(): Unit = {
    // Styling
    reloadStyles()
    // Resources
    SFXApplicationEnvironment.clearResourceBundleCache()
    SFXApplicationEnvironment.loadResourceBundle("bundles/application")
    // FXML
    mainViewController = getController[MainViewController]()
    replaceSceneContent(mainViewController)
    // do some other stuff
  }

  def applicationName: ApplicationName =
    ApplicationName(configStringValue("application.name"))

  // #CustomBundle
  // only example values ...
  override def resourceBundleForView(viewPath: String): ResourceBundle =
    if (viewPath.contains("mySpecialViewName")) {
      val path = "myCustomResourcePath"
      val classLoader = Thread.currentThread().getContextClassLoader
      ResourceBundle.getBundle(path, Locale.getDefault(), classLoader)
    } else
      super.resourceBundleForView(viewPath) // =  applicationEnvironment.resourceBundle

  // #CustomBundle

}

case class ApplicationName(name: String)

MainViewController

The following code snippet loads the MainViewController by the FXMLoader from the CDI managed ApplicationController Bean.

  lazy val mainViewController = getController[MainViewController]()

This pattern for Controller-Loading is commonly used in sapphire-javafx Framework.