morinted / Bozzy

Capstone project for fourth year software engineering
GNU General Public License v3.0
2 stars 3 forks source link

Resource bundle #58

Closed sofa13 closed 8 years ago

sofa13 commented 8 years ago
morinted commented 8 years ago

Some adjustments (that you can apply with git apply

diff --git a/src/main/scala/bozzy/Bozzy.scala b/src/main/scala/bozzy/Bozzy.scala
index 2650c7f..c74ae67 100644
--- a/src/main/scala/bozzy/Bozzy.scala
+++ b/src/main/scala/bozzy/Bozzy.scala
@@ -9,10 +9,9 @@ import scalafx.Includes._
 import scalafx.application.JFXApp
 import scalafx.application.JFXApp.PrimaryStage
 import scalafx.scene.Scene
-import scalafx.scene.input.{TransferMode, DragEvent}
+import scalafx.scene.input.{DragEvent, TransferMode}
 import scalafx.scene.text.Font
-import scalafxml.core.{NoDependencyResolver, FXMLView, FXMLLoader}
-import javafx.{scene => jfxs}
+import scalafxml.core.{FXMLLoader, NoDependencyResolver}

 /**
   * Created by ted on 2016-04-06.
@@ -23,20 +22,21 @@ object Bozzy extends JFXApp {
     14
   )

-  val resource = getClass.getResource("/view/Main.fxml")
-  val bundle = new PropertyResourceBundle(getClass.getResource("/bundle/Resources.properties").openStream)
-  if (resource == null) {
+  val main = getClass.getResource("/view/Main.fxml")
+  val i18n = new PropertyResourceBundle(getClass.getResource("/bundle/Resources.properties").openStream)
+  if (main == null) {
     throw new FileNotFoundException("Cannot load resource: /view/Main.fxml")
   }
-  if (bundle == null) {
+  if (i18n == null) {
     throw new FileNotFoundException("Cannot load resource: /bundle/Resources.properties")
   }
-  val loader: FXMLLoader = new FXMLLoader(resource, NoDependencyResolver)
-  loader.setResources(bundle)
-  val root = loader.load[jfxs.Parent]
+  val loader = new FXMLLoader(main, NoDependencyResolver) {
+    setResources(i18n)
+  }
+  val root = loader.load[javafx.scene.Parent]

   stage = new PrimaryStage() {
-    title = bundle.getString("applicationTitle")
+    title = i18n.getString("applicationTitle")
     scene = new Scene(root) {
       stylesheets = List(getClass.getResource("/css/style.css").toExternalForm)
       onDragOver = (event: DragEvent) => {
diff --git a/src/main/scala/bozzy/controllers/DictionaryListController.scala b/src/main/scala/bozzy/controllers/DictionaryListController.scala
index e727d33..f967eba 100644
--- a/src/main/scala/bozzy/controllers/DictionaryListController.scala
+++ b/src/main/scala/bozzy/controllers/DictionaryListController.scala
@@ -7,23 +7,21 @@ package bozzy.controllers

 import java.util.PropertyResourceBundle

+import bozzy.Bozzy
+
 import scalafx.event.ActionEvent
 import scalafx.scene.control.ListView
 import scalafx.stage.FileChooser
 import scalafx.stage.FileChooser.ExtensionFilter
 import scalafxml.core.macros.sfxml
-
 import bozzy.steno.StenoDictionary

 @sfxml
 class DictionaryListController (private val dictionary_list: ListView[StenoDictionary]) {
-
-  val bundle = new PropertyResourceBundle(getClass.getResource("/bundle/Resources.properties").openStream)
-
   dictionary_list.items = MainDictionary.openDictionaries
   def handleAdd (event: ActionEvent) {
     val fileChooser = new FileChooser {
-      title = bundle.getString("openResourceFileTitle")
+      title = Bozzy.i18n.getString("openResourceFileTitle")
       extensionFilters.add(
         new ExtensionFilter("Steno Dictionaries", Seq("*.json", "*.rtf"))
       )
diff --git a/src/main/scala/bozzy/controllers/FilterPaneController.scala b/src/main/scala/bozzy/controllers/FilterPaneController.scala
index ad79356..909bafe 100644
--- a/src/main/scala/bozzy/controllers/FilterPaneController.scala
+++ b/src/main/scala/bozzy/controllers/FilterPaneController.scala
@@ -2,6 +2,8 @@ package bozzy.controllers

 import java.util.PropertyResourceBundle

+import bozzy.Bozzy
+
 import scala.math.Ordering.StringOrdering
 import scalafx.collections.ObservableBuffer
 import scalafx.collections.transformation.SortedBuffer
@@ -10,8 +12,7 @@ import scalafx.scene.control.SpinnerValueFactory.ListSpinnerValueFactory
 import scalafx.scene.control._
 import scalafx.util.StringConverter
 import scalafxml.core.macros.sfxml
-
-import bozzy.steno.{StenoDictionary, DictionaryEntry}
+import bozzy.steno.{DictionaryEntry, StenoDictionary}

 /**
  * Created by Ian on 2/10/2016.
@@ -25,14 +26,13 @@ class FilterPaneController (private val filterLabel: Label,
                             private val wordCount: Spinner[String],
                             private val collisions_checkbox: CheckBox,
                             private val hide_duplicates_checkbox: CheckBox) {
-  val bundle = new PropertyResourceBundle(getClass.getResource("/bundle/Resources.properties").openStream)
   dictionary_box.items = new SortedBuffer[String](MainDictionary.dictionaryFilterChoices) {
     comparator = new StringOrdering {
-      val anyOption = bundle.getString("anyDictionaryOption")
+      val anyOption = Bozzy.i18n.getString("anyDictionaryOption")
       override def compare(x: String, y: String) =
         (x, y) match {
-          case (anyOption, _) => -1
-          case (_, anyOption) => 1
+          case (`anyOption`, _) => -1
+          case (_, `anyOption`) => 1
           case _ => super.compare(x, y)
         }
       }
@@ -43,7 +43,7 @@ class FilterPaneController (private val filterLabel: Label,
   chordCount.setValueFactory(new ListSpinnerValueFactory[String](choicesWithoutZero))
   wordCount.setValueFactory(new ListSpinnerValueFactory[String](choicesWithZero))

-  val filterTitle = bundle.getString("filterTitle")
+  val filterTitle = Bozzy.i18n.getString("filterTitle")
   MainDictionary.filteredEntries.onChange((source, changes) =>
     filterLabel.text = filterTitle + s" (${source.size}/${MainDictionary.allEntries.size})"
   )
diff --git a/src/main/scala/bozzy/controllers/MainController.scala b/src/main/scala/bozzy/controllers/MainController.scala
index fafe0bf..4970613 100644
--- a/src/main/scala/bozzy/controllers/MainController.scala
+++ b/src/main/scala/bozzy/controllers/MainController.scala
@@ -4,7 +4,9 @@

 package bozzy.controllers

-import java.util.{PropertyResourceBundle}
+import java.util.PropertyResourceBundle
+
+import bozzy.Bozzy

 import scala.collection.mutable.ListBuffer
 import scalafx.collections.ObservableBuffer
@@ -14,19 +16,17 @@ import scalafx.stage.FileChooser
 import scalafx.stage.FileChooser.ExtensionFilter
 import scalafx.application.Platform
 import scalafxml.core.macros.sfxml
-
-import bozzy.steno.{DictionaryEntry, StenoDictionary, DictionaryFormat}
+import bozzy.steno.{DictionaryEntry, DictionaryFormat, StenoDictionary}

 @sfxml
 class MainController {
-  val bundle = new PropertyResourceBundle(getClass.getResource("/bundle/Resources.properties").openStream)

   def handleButtonPress(event: ActionEvent) {
   }

   def handleOpen(event: ActionEvent) {
     val fileChooser = new FileChooser {
-      title = bundle.getString("openResourceFileTitle")
+      title = Bozzy.i18n.getString("openResourceFileTitle")
       extensionFilters.add(
         new ExtensionFilter("Steno Dictionaries", Seq("*.json", "*.rtf"))
       )
@@ -46,10 +46,9 @@ class MainController {
 }

 object MainDictionary {
-  val bundle = new PropertyResourceBundle(getClass.getResource("/bundle/Resources.properties").openStream)
   val openDictionaryNames = new ObservableBuffer[String]
   val dictionaryFilterChoices = new ObservableBuffer[String]
-  dictionaryFilterChoices add bundle.getString("anyDictionaryOption")
+  dictionaryFilterChoices add Bozzy.i18n.getString("anyDictionaryOption")

   val openDictionaries = new ObservableBuffer[StenoDictionary]
   val allEntries = new ObservableBuffer[DictionaryEntry]
sofa13 commented 8 years ago

Thanks Ted, your changes have been applied

sofa13 commented 8 years ago

Running into an issue with the test, for example, the last filter test, it makes the following call:

MainDictionary.add(absolutepath)

where in the MainDictionary object, the following code returns a null pointer since Bozzy was never initilized:

dictionaryFilterChoices add Bozzy.i18n.getString("anyDictionaryOption")

I could do the following fix, where I import bozzy.Bozzy into the test class, change i18n from val to var, and then initialize i18n in the test class

Bozzy.i18n = new PropertyResourceBundle(getClass.getResource("/bundle/Resources.properties").openStream)

Not sure if this is the best workaround @morinted .. but it works

morinted commented 8 years ago

Maybe you could put i18n in its own class, and load it the first time it is accessed, then memoize the resource.

sofa13 commented 8 years ago

Okay, all tests should pass now

sofa13 commented 8 years ago

Should be good now

morinted commented 8 years ago

Want to squash this into one commit? Use resource strings instead of hardcoded strings

sofa13 commented 8 years ago

squash complete

sofa13 commented 8 years ago

fixes #4