Refactor listview to its own FXML file and controller

This commit is contained in:
Marvin Elsen 2024-09-26 09:18:40 +02:00
parent 49fe0f65bd
commit 9508412427
Signed by: marvinelsen
GPG Key ID: 820672408CC318C2
5 changed files with 67 additions and 47 deletions

View File

@ -2,6 +2,7 @@ package com.marvinelsen.willow
import com.marvinelsen.willow.domain.SqliteDictionary import com.marvinelsen.willow.domain.SqliteDictionary
import com.marvinelsen.willow.ui.controllers.DetailsController import com.marvinelsen.willow.ui.controllers.DetailsController
import com.marvinelsen.willow.ui.controllers.ListController
import com.marvinelsen.willow.ui.controllers.MainController import com.marvinelsen.willow.ui.controllers.MainController
import com.marvinelsen.willow.ui.controllers.MenuController import com.marvinelsen.willow.ui.controllers.MenuController
import com.marvinelsen.willow.ui.controllers.SearchController import com.marvinelsen.willow.ui.controllers.SearchController
@ -53,6 +54,7 @@ class WillowApplication : Application() {
MenuController::class.java -> MenuController(model) MenuController::class.java -> MenuController(model)
DetailsController::class.java -> DetailsController(model) DetailsController::class.java -> DetailsController(model)
SearchController::class.java -> SearchController(model) SearchController::class.java -> SearchController(model)
ListController::class.java -> ListController(model)
else -> error("Trying to instantiate unknown controller type $type") else -> error("Trying to instantiate unknown controller type $type")
} }
} }

View File

@ -0,0 +1,39 @@
package com.marvinelsen.willow.ui.controllers
import com.marvinelsen.willow.Model
import com.marvinelsen.willow.ui.DictionaryEntryFx
import javafx.beans.binding.Bindings
import javafx.fxml.FXML
import javafx.scene.control.Label
import javafx.scene.control.ListView
import javafx.scene.control.ProgressIndicator
class ListController(private val model: Model) {
@FXML
private lateinit var progressIndicatorEntries: ProgressIndicator
@FXML
@Suppress("UnusedPrivateProperty")
private lateinit var labelNoEntriesFound: Label
@FXML
private lateinit var listViewSearchResults: ListView<DictionaryEntryFx>
@FXML
@Suppress("UnusedPrivateMember")
private fun initialize() {
listViewSearchResults.items = model.searchResults
listViewSearchResults
.disableProperty()
.bind(Bindings.or(model.isSearching, Bindings.isEmpty(model.searchResults)))
progressIndicatorEntries.visibleProperty().bind(model.isSearching)
listViewSearchResults.selectionModel.selectedItemProperty().addListener { _, _, newValue: DictionaryEntryFx? ->
if (newValue == null) {
return@addListener
}
model.selectEntry(newValue)
}
}
}

View File

@ -1,39 +1,11 @@
package com.marvinelsen.willow.ui.controllers package com.marvinelsen.willow.ui.controllers
import com.marvinelsen.willow.Model import com.marvinelsen.willow.Model
import com.marvinelsen.willow.ui.DictionaryEntryFx
import javafx.beans.binding.Bindings
import javafx.fxml.FXML import javafx.fxml.FXML
import javafx.scene.control.Label
import javafx.scene.control.ListView
import javafx.scene.control.ProgressIndicator
class MainController(private val model: Model) { class MainController(private val model: Model) {
@FXML
private lateinit var progressIndicatorEntries: ProgressIndicator
@FXML
@Suppress("UnusedPrivateProperty")
private lateinit var labelNoEntriesFound: Label
@FXML
private lateinit var listViewSearchResults: ListView<DictionaryEntryFx>
@FXML @FXML
@Suppress("UnusedPrivateMember") @Suppress("UnusedPrivateMember")
private fun initialize() { private fun initialize() {
listViewSearchResults.items = model.searchResults
listViewSearchResults
.disableProperty()
.bind(Bindings.or(model.isSearching, Bindings.isEmpty(model.searchResults)))
progressIndicatorEntries.visibleProperty().bind(model.isSearching)
listViewSearchResults.selectionModel.selectedItemProperty().addListener { _, _, newValue: DictionaryEntryFx? ->
if (newValue == null) {
return@addListener
}
model.selectEntry(newValue)
}
} }
} }

View File

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

View File

@ -1,10 +1,6 @@
<?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.ListView?>
<?import javafx.scene.control.ProgressIndicator?>
<?import javafx.scene.control.SplitPane?> <?import javafx.scene.control.SplitPane?>
<?import javafx.scene.layout.*?> <?import javafx.scene.layout.*?>
<BorderPane xmlns="http://javafx.com/javafx/23" xmlns:fx="http://javafx.com/fxml/1" <BorderPane xmlns="http://javafx.com/javafx/23" xmlns:fx="http://javafx.com/fxml/1"
@ -20,20 +16,7 @@
</BorderPane.margin> </BorderPane.margin>
<fx:include source="/fxml/search.fxml"/> <fx:include source="/fxml/search.fxml"/>
<SplitPane dividerPositions="0.33" VBox.vgrow="ALWAYS"> <SplitPane dividerPositions="0.33" VBox.vgrow="ALWAYS">
<StackPane> <fx:include source="/fxml/list.fxml"/>
<ListView fx:id="listViewSearchResults" disable="true">
<cellFactory>
<DictionaryEntryCellFactory/>
</cellFactory>
</ListView>
<Label fx:id="labelNoEntriesFound" text="%list.no_entries_found" textAlignment="CENTER"
visible="false" wrapText="true">
<padding>
<Insets bottom="8.0" left="8.0" right="8.0" top="8.0"/>
</padding>
</Label>
<ProgressIndicator fx:id="progressIndicatorEntries" visible="false"/>
</StackPane>
<fx:include source="/fxml/details.fxml"/> <fx:include source="/fxml/details.fxml"/>
</SplitPane> </SplitPane>
</VBox> </VBox>