Refactor resource bundle handling

This commit is contained in:
Marvin Elsen 2024-09-26 23:10:40 +02:00
parent 9ac6f51743
commit 02257b7e6b
Signed by: marvinelsen
GPG Key ID: 820672408CC318C2
7 changed files with 33 additions and 22 deletions

View File

@ -30,7 +30,6 @@ class WillowApplication : Application() {
private const val FONT_SIZE = 12.0 private const val FONT_SIZE = 12.0
private const val JDBC_CONNECTION_STRING = "jdbc:sqlite:dictionary.db" private const val JDBC_CONNECTION_STRING = "jdbc:sqlite:dictionary.db"
val resourceBundle: ResourceBundle = ResourceBundle.getBundle("i18n/willow", Locale.US)
} }
override fun init() { override fun init() {
@ -49,7 +48,7 @@ class WillowApplication : Application() {
config.load() config.load()
val fxmlLoader = FXMLLoader() val fxmlLoader = FXMLLoader()
fxmlLoader.resources = resourceBundle fxmlLoader.resources = ResourceBundle.getBundle("i18n/willow", Locale.getDefault())
fxmlLoader.controllerFactory = Callback { type -> fxmlLoader.controllerFactory = Callback { type ->
when (type) { when (type) {
MainController::class.java -> MainController(model) MainController::class.java -> MainController(model)

View File

@ -9,10 +9,12 @@ import javafx.scene.control.ListView
import javafx.scene.layout.FlowPane import javafx.scene.layout.FlowPane
import javafx.scene.layout.VBox import javafx.scene.layout.VBox
import javafx.util.Callback import javafx.util.Callback
import java.util.ResourceBundle
class DictionaryEntryCellFactory : Callback<ListView<DictionaryEntryFx?>, ListCell<DictionaryEntryFx?>> { class DictionaryEntryCellFactory(private val resources: ResourceBundle) :
Callback<ListView<DictionaryEntryFx?>, ListCell<DictionaryEntryFx?>> {
override fun call(listView: ListView<DictionaryEntryFx?>): ListCell<DictionaryEntryFx?> { override fun call(listView: ListView<DictionaryEntryFx?>): ListCell<DictionaryEntryFx?> {
val entryCell = EntryCell() val entryCell = EntryCell(resources)
entryCell.prefWidthProperty().bind(listView.widthProperty().subtract(CELL_PADDING)) entryCell.prefWidthProperty().bind(listView.widthProperty().subtract(CELL_PADDING))
return entryCell return entryCell
} }
@ -22,7 +24,7 @@ class DictionaryEntryCellFactory : Callback<ListView<DictionaryEntryFx?>, ListCe
} }
} }
internal class EntryCell : ListCell<DictionaryEntryFx?>() { internal class EntryCell(private val resources: ResourceBundle) : ListCell<DictionaryEntryFx?>() {
private val labelHeadword = Label().apply { private val labelHeadword = Label().apply {
styleClass.add("list-view-entry") styleClass.add("list-view-entry")
} }
@ -58,7 +60,7 @@ internal class EntryCell : ListCell<DictionaryEntryFx?>() {
val definition = entry.definitions.joinToString(separator = " / ") { it.joinToString(separator = "; ") } val definition = entry.definitions.joinToString(separator = " / ") { it.joinToString(separator = "; ") }
labelDefinition.text = definition labelDefinition.text = definition
contextMenu = createContextMenuForEntry(entry) contextMenu = createContextMenuForEntry(entry, resources)
graphic = root graphic = root
} }
} }

View File

@ -3,6 +3,7 @@ package com.marvinelsen.willow.ui.controllers
import com.marvinelsen.willow.Config import com.marvinelsen.willow.Config
import com.marvinelsen.willow.Model import com.marvinelsen.willow.Model
import com.marvinelsen.willow.ui.DictionaryEntryFx import com.marvinelsen.willow.ui.DictionaryEntryFx
import com.marvinelsen.willow.ui.cells.DictionaryEntryCellFactory
import javafx.beans.binding.Bindings import javafx.beans.binding.Bindings
import javafx.fxml.FXML import javafx.fxml.FXML
import javafx.scene.control.Label import javafx.scene.control.Label
@ -15,8 +16,12 @@ import kotlinx.html.html
import kotlinx.html.li import kotlinx.html.li
import kotlinx.html.ol import kotlinx.html.ol
import kotlinx.html.stream.createHTML import kotlinx.html.stream.createHTML
import java.util.ResourceBundle
class DetailsController(private val model: Model, private val config: Config) {
@FXML
private lateinit var resources: ResourceBundle
class DetailsController(private val model: Model) {
@FXML @FXML
private lateinit var tabPaneDetails: TabPane private lateinit var tabPaneDetails: TabPane
@ -56,6 +61,7 @@ class DetailsController(private val model: Model) {
tabPaneDetails.disableProperty().bind(Bindings.isNull(model.selectedEntry)) tabPaneDetails.disableProperty().bind(Bindings.isNull(model.selectedEntry))
listViewWords.cellFactory = DictionaryEntryCellFactory(resources)
listViewWords.items = model.wordsContaining listViewWords.items = model.wordsContaining
tabPaneDetails.selectionModel.selectedItemProperty().addListener { _, _, selectedTab -> tabPaneDetails.selectionModel.selectedItemProperty().addListener { _, _, selectedTab ->
if (model.selectedEntry.value == null) return@addListener if (model.selectedEntry.value == null) return@addListener
@ -79,6 +85,13 @@ class DetailsController(private val model: Model) {
if (newValue == null) { if (newValue == null) {
return@addListener return@addListener
} }
when (tabPaneDetails.selectionModel.selectedItem.id) {
"tabWords" -> {
model.findWords()
}
else -> {}
}
webViewDefinition.engine.loadContent( webViewDefinition.engine.loadContent(
createHTML().html { createHTML().html {
body { body {

View File

@ -2,13 +2,18 @@ package com.marvinelsen.willow.ui.controllers
import com.marvinelsen.willow.Model import com.marvinelsen.willow.Model
import com.marvinelsen.willow.ui.DictionaryEntryFx import com.marvinelsen.willow.ui.DictionaryEntryFx
import com.marvinelsen.willow.ui.cells.DictionaryEntryCellFactory
import javafx.beans.binding.Bindings import javafx.beans.binding.Bindings
import javafx.fxml.FXML import javafx.fxml.FXML
import javafx.scene.control.Label import javafx.scene.control.Label
import javafx.scene.control.ListView import javafx.scene.control.ListView
import javafx.scene.control.ProgressIndicator import javafx.scene.control.ProgressIndicator
import java.util.ResourceBundle
class ListController(private val model: Model) { class ListController(private val model: Model) {
@FXML
private lateinit var resources: ResourceBundle
@FXML @FXML
private lateinit var progressIndicatorEntries: ProgressIndicator private lateinit var progressIndicatorEntries: ProgressIndicator
@ -22,6 +27,7 @@ class ListController(private val model: Model) {
@FXML @FXML
@Suppress("UnusedPrivateMember") @Suppress("UnusedPrivateMember")
private fun initialize() { private fun initialize() {
listViewSearchResults.cellFactory = DictionaryEntryCellFactory(resources)
listViewSearchResults.items = model.searchResults listViewSearchResults.items = model.searchResults
listViewSearchResults listViewSearchResults
.disableProperty() .disableProperty()

View File

@ -1,19 +1,19 @@
package com.marvinelsen.willow.ui.util package com.marvinelsen.willow.ui.util
import com.marvinelsen.willow.WillowApplication
import com.marvinelsen.willow.ui.DictionaryEntryFx import com.marvinelsen.willow.ui.DictionaryEntryFx
import javafx.event.EventHandler import javafx.event.EventHandler
import javafx.scene.control.ContextMenu import javafx.scene.control.ContextMenu
import javafx.scene.control.MenuItem import javafx.scene.control.MenuItem
import java.util.ResourceBundle
fun createContextMenuForEntry(entry: DictionaryEntryFx) = ContextMenu().apply { fun createContextMenuForEntry(entry: DictionaryEntryFx, resourceBundle: ResourceBundle) = ContextMenu().apply {
val menuItemCopyHeadword = val menuItemCopyHeadword =
MenuItem(WillowApplication.resourceBundle.getString("menubar.edit.copy.headword")).apply { MenuItem(resourceBundle.getString("menubar.edit.copy.headword")).apply {
onAction = EventHandler { ClipboardHelper.copyString(entry.traditionalProperty.value) } onAction = EventHandler { ClipboardHelper.copyString(entry.traditionalProperty.value) }
} }
val menuItemCopyPronunciation = val menuItemCopyPronunciation =
MenuItem(WillowApplication.resourceBundle.getString("menubar.edit.copy.pronunciation")).apply { MenuItem(resourceBundle.getString("menubar.edit.copy.pronunciation")).apply {
onAction = EventHandler { ClipboardHelper.copyString(entry.pinyinWithToneMarksProperty.value) } onAction = EventHandler { ClipboardHelper.copyString(entry.pinyinWithToneMarksProperty.value) }
} }

View File

@ -22,11 +22,7 @@
<ListView fx:id="listviewSentences"/> <ListView fx:id="listviewSentences"/>
</Tab> </Tab>
<Tab id="tabWords" closable="false" disable="false" text="%tab.words"> <Tab id="tabWords" closable="false" disable="false" text="%tab.words">
<ListView fx:id="listViewWords"> <ListView fx:id="listViewWords"/>
<cellFactory>
<DictionaryEntryCellFactory/>
</cellFactory>
</ListView>
</Tab> </Tab>
<Tab closable="false" disable="false" text="%tab.characters"> <Tab closable="false" disable="false" text="%tab.characters">
<ListView fx:id="listViewCharacters"/> <ListView fx:id="listViewCharacters"/>

View File

@ -1,6 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<?import com.marvinelsen.willow.ui.cells.DictionaryEntryCellFactory?>
<?import javafx.geometry.Insets?> <?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?> <?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?> <?import javafx.scene.control.ListView?>
@ -9,11 +8,7 @@
<StackPane xmlns="http://javafx.com/javafx/23" xmlns:fx="http://javafx.com/fxml/1" <StackPane xmlns="http://javafx.com/javafx/23" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="com.marvinelsen.willow.ui.controllers.ListController" fx:controller="com.marvinelsen.willow.ui.controllers.ListController"
stylesheets="/css/main.css"> stylesheets="/css/main.css">
<ListView fx:id="listViewSearchResults" disable="true"> <ListView fx:id="listViewSearchResults" disable="true"/>
<cellFactory>
<DictionaryEntryCellFactory/>
</cellFactory>
</ListView>
<Label fx:id="labelNoEntriesFound" text="%list.no_entries_found" textAlignment="CENTER" <Label fx:id="labelNoEntriesFound" text="%list.no_entries_found" textAlignment="CENTER"
visible="false" wrapText="true"> visible="false" wrapText="true">
<padding> <padding>