A Scala/JavaFX RSS Reader GridPane example

As a note to self, this Scala/JavaFX example comes from my RssReader project. I’m just putting it here so I can find a JavaFX/GridPane example in the future.

Source code:

package com.alvinalexander.rssreader

import javafx.scene.layout.GridPane
import javafx.geometry.Insets
import javafx.scene.text.Text
import javafx.scene.text.Font
import javafx.scene.text.FontWeight
import javafx.scene.image.ImageView
import javafx.scene.image.Image
import javafx.geometry.VPos
import javafx.application.Application
import java.io.IOException
import javafx.stage.Stage
import javafx.scene.Group
import javafx.scene.Scene
import javafx.application.Application
import javafx.application.Platform
import javafx.scene.Group
import javafx.scene.Scene
import javafx.scene.control.ListView
import javafx.scene.layout.BorderPane
import javafx.scene.web.WebEngine
import javafx.scene.web.WebHistory
import javafx.scene.web.WebView
import javafx.stage.Screen
import javafx.stage.Stage
import javafx.beans.value.ChangeListener
import javafx.beans.value.ObservableValue
import javafx.collections.FXCollections
import javafx.collections.ObservableList
import javafx.geometry.Rectangle2D
import scala.collection.JavaConversions._
import scala.collection.mutable.ArrayBuffer
import java.io.BufferedReader
import java.io.FileReader

object GridPaneExample1 {
    def main(args: Array[String]) {
        Application.launch(classOf[GridPaneExample1], args: _*)
    }
}

/**
 *
 * I THINK THIS IS THE ORIGINAL EXAMPLE I GOT STARTED WITH.
 *
 */
class GridPaneExample1 extends Application {

    var webEngine: WebEngine = null

    @Override
    @throws(classOf[IOException])
    def start(primaryStage: Stage) {

        val rootGroup = new Group
        val scene = new Scene(rootGroup, initialWidth, initialHeight)
        
        val rssFeeds = getRssFeeds

        // LIST VIEW (RSS FEEDS)
        val listView: ListView[String]  = new ListView[String]()
        val data: ObservableList[String] = FXCollections.observableArrayList(rssFeeds: _*)
        listView.setItems(data)
        listView.setPrefWidth(150)

        val gridPane = new GridPane
        gridPane.setHgap(10)
        gridPane.setVgap(10)
        gridPane.setPadding(new Insets(0, 10, 0, 10));

        // ListView in Col 1
        gridPane.add(listView, 0, 0)
        
        // WebView in Col 2
        val webView = new WebView
        webEngine = webView.getEngine
        webEngine.load("http:://www.google.com")
        gridPane.add(webView, 0, 0)

//        val goodsPercent = new Text("Goods\n80%")
//        GridPane.setValignment(goodsPercent, VPos.BOTTOM)
//        grid.add(goodsPercent, 0, 2)
//
//        // BORDER PANE
//        val borderPane = new BorderPane
//        borderPane.setLeft(listView)
//        borderPane.setCenter(webView)

        
        rootGroup.getChildren().add(gridPane)
        
        primaryStage.setTitle("RSS Reader")
        primaryStage.setScene(scene)
        primaryStage.show()
        
    }
    
    def initialHeight = {
        val primaryScreenBounds: Rectangle2D = Screen.getPrimary.getVisualBounds
        primaryScreenBounds.getHeight*3/4
    }

    def initialWidth = {
        val primaryScreenBounds: Rectangle2D = Screen.getPrimary.getVisualBounds
        primaryScreenBounds.getWidth*3/4
    }
    
    def addGridPane(): GridPane = {
        val grid = new GridPane
        grid.setHgap(10)
        grid.setVgap(10)
        grid.setPadding(new Insets(0, 10, 0, 10));

        // Category in column 2, row 1
        val category = new Text("Sales:")
        category.setFont(Font.font("Arial", FontWeight.BOLD, 20))
        grid.add(category, 1, 0)

        // Title in column 3, row 1
        val chartTitle = new Text("Current Year")
        chartTitle.setFont(Font.font("Arial", FontWeight.BOLD, 20))
        grid.add(chartTitle, 2, 0)

        // Subtitle in columns 2-3, row 2
        val chartSubtitle = new Text("Goods and Services")
        grid.add(chartSubtitle, 1, 1, 2, 1)

        // House icon in column 1, rows 1-2
        //    val imageHouse = new ImageView(
        //      new Image(LayoutSample.class.getResourceAsStream("graphics/house.png")));
        //    grid.add(imageHouse, 0, 0, 1, 2);

        // Left label in column 1 (bottom), row 3
        val goodsPercent = new Text("Goods\n80%")
        GridPane.setValignment(goodsPercent, VPos.BOTTOM)
        grid.add(goodsPercent, 0, 2)

        // Chart in columns 2-3, row 3
        //    ImageView imageChart = new ImageView(
        //     new Image(LayoutSample.class.getResourceAsStream("graphics/piechart.png")));
        //    grid.add(imageChart, 1, 2, 2, 1);

        // Right label in column 4 (top), row 3
        val servicesPercent = new Text("Services\n20%");
        GridPane.setValignment(servicesPercent, VPos.TOP);
        grid.add(servicesPercent, 3, 2);

        return grid;
    }

    //TODO better way to handle this?
    def getRssFeeds: Array[String] = {
        val usersHomeDir = System.getProperty("user.home")
        val canonDataFile = usersHomeDir + "/" + "StockBrowser.data"
        readFileToStringArray(canonDataFile)
    }

    @throws(classOf[IOException])
    def readFileToStringArray(canonFilename: String): Array[String] = {
        val bufferedReader = new BufferedReader(new FileReader(canonFilename))
        val lines = new ArrayBuffer[String]()
        var line: String = null
        while ({line = bufferedReader.readLine; line != null}) {
            lines.add(line)
        }
        bufferedReader.close
        lines.toArray
    }

}