Add "Copy sentence" context menu for example sentences

This commit is contained in:
Marvin Elsen 2024-11-03 11:01:55 +01:00
parent cfff342a3a
commit b8194f3ca6
Signed by: marvinelsen
GPG Key ID: 820672408CC318C2
9 changed files with 41 additions and 8 deletions

View File

@ -15,8 +15,11 @@ import javafx.scene.layout.VBox
import javafx.util.Callback import javafx.util.Callback
import java.util.ResourceBundle import java.util.ResourceBundle
class DictionaryEntryCellFactory(private val resources: ResourceBundle, private val config: Config) : class DictionaryEntryCellFactory(
Callback<ListView<DictionaryEntryFx?>, ListCell<DictionaryEntryFx?>> { private val resources: ResourceBundle,
private val config: Config,
) : Callback<ListView<DictionaryEntryFx?>, ListCell<DictionaryEntryFx?>> {
override fun call(listView: ListView<DictionaryEntryFx?>): ListCell<DictionaryEntryFx?> { override fun call(listView: ListView<DictionaryEntryFx?>): ListCell<DictionaryEntryFx?> {
val entryCell = EntryCell(resources, config) val entryCell = EntryCell(resources, config)
entryCell.prefWidthProperty().bind(listView.widthProperty().subtract(CELL_PADDING)) entryCell.prefWidthProperty().bind(listView.widthProperty().subtract(CELL_PADDING))
@ -28,8 +31,11 @@ class DictionaryEntryCellFactory(private val resources: ResourceBundle, private
} }
} }
private class EntryCell(private val resources: ResourceBundle, private val config: Config) : private class EntryCell(
ListCell<DictionaryEntryFx?>() { private val resources: ResourceBundle,
private val config: Config,
) : ListCell<DictionaryEntryFx?>() {
private val labelHeadword = Label().apply { private val labelHeadword = Label().apply {
styleClass.add("headword") styleClass.add("headword")
styleProperty().bind( styleProperty().bind(
@ -118,9 +124,11 @@ private class EntryCell(private val resources: ResourceBundle, private val confi
entry.crossStraitsDefinitions.isNotEmpty() -> entry.crossStraitsDefinitions.joinToString( entry.crossStraitsDefinitions.isNotEmpty() -> entry.crossStraitsDefinitions.joinToString(
separator = " / " separator = " / "
) { it.definition } ) { it.definition }
entry.moedictDefinitions.isNotEmpty() -> entry.moedictDefinitions.joinToString( entry.moedictDefinitions.isNotEmpty() -> entry.moedictDefinitions.joinToString(
separator = " / " separator = " / "
) { it.definition } ) { it.definition }
else -> error("No definition for entry") else -> error("No definition for entry")
} }
labelDefinition.text = definition labelDefinition.text = definition

View File

@ -3,17 +3,22 @@ package com.marvinelsen.willow.ui.cells
import com.marvinelsen.willow.config.Config import com.marvinelsen.willow.config.Config
import com.marvinelsen.willow.config.Script import com.marvinelsen.willow.config.Script
import com.marvinelsen.willow.ui.SentenceFx import com.marvinelsen.willow.ui.SentenceFx
import com.marvinelsen.willow.ui.util.createContextMenuForSentence
import javafx.beans.binding.Bindings import javafx.beans.binding.Bindings
import javafx.scene.control.Label import javafx.scene.control.Label
import javafx.scene.control.ListCell import javafx.scene.control.ListCell
import javafx.scene.control.ListView import javafx.scene.control.ListView
import javafx.scene.layout.VBox import javafx.scene.layout.VBox
import javafx.util.Callback import javafx.util.Callback
import java.util.ResourceBundle
class SentenceCellFactory(private val config: Config) : Callback<ListView<SentenceFx?>, ListCell<SentenceFx?>> { class SentenceCellFactory(
private val resources: ResourceBundle,
private val config: Config,
) : Callback<ListView<SentenceFx?>, ListCell<SentenceFx?>> {
override fun call(listView: ListView<SentenceFx?>): ListCell<SentenceFx?> { override fun call(listView: ListView<SentenceFx?>): ListCell<SentenceFx?> {
val sentenceCell = SentenceCell(config) val sentenceCell = SentenceCell(resources, config)
sentenceCell.prefWidthProperty().bind(listView.widthProperty().subtract(CELL_PADDING)) sentenceCell.prefWidthProperty().bind(listView.widthProperty().subtract(CELL_PADDING))
return sentenceCell return sentenceCell
} }
@ -23,7 +28,11 @@ class SentenceCellFactory(private val config: Config) : Callback<ListView<Senten
} }
} }
private class SentenceCell(private val config: Config) : ListCell<SentenceFx?>() { private class SentenceCell(
private val resources: ResourceBundle,
private val config: Config,
) : ListCell<SentenceFx?>() {
private val labelSentence = Label().apply { private val labelSentence = Label().apply {
styleClass.add("sentence") styleClass.add("sentence")
isWrapText = true isWrapText = true
@ -51,6 +60,7 @@ private class SentenceCell(private val config: Config) : ListCell<SentenceFx?>()
) )
) )
contextMenu = createContextMenuForSentence(sentence, resources)
graphic = root graphic = root
} }
} }

View File

@ -192,7 +192,7 @@ class DetailsController(private val model: Model, private val config: Config) {
private fun initializeListViewSentences() { private fun initializeListViewSentences() {
listViewSentences.apply { listViewSentences.apply {
cellFactory = SentenceCellFactory(config) cellFactory = SentenceCellFactory(resources, config)
items = model.sentences items = model.sentences
disableProperty().bind(Bindings.or(model.isFindingSentences, Bindings.isEmpty(model.sentences))) disableProperty().bind(Bindings.or(model.isFindingSentences, Bindings.isEmpty(model.sentences)))

View File

@ -1,6 +1,7 @@
package com.marvinelsen.willow.ui.util package com.marvinelsen.willow.ui.util
import com.marvinelsen.willow.ui.DictionaryEntryFx import com.marvinelsen.willow.ui.DictionaryEntryFx
import com.marvinelsen.willow.ui.SentenceFx
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
@ -19,3 +20,12 @@ fun createContextMenuForEntry(entry: DictionaryEntryFx, resourceBundle: Resource
items.addAll(menuItemCopyHeadword, menuItemCopyPronunciation) items.addAll(menuItemCopyHeadword, menuItemCopyPronunciation)
} }
fun createContextMenuForSentence(sentence: SentenceFx, resourceBundle: ResourceBundle) = ContextMenu().apply {
val menuItemCopySentence =
MenuItem(resourceBundle.getString("menubar.edit.copy.sentence")).apply {
onAction = EventHandler { ClipboardHelper.copyString(sentence.traditionalProperty.value) }
}
items.addAll(menuItemCopySentence)
}

View File

@ -25,6 +25,7 @@ menubar.file.preferences=_Preferences…
menubar.edit=_Edit menubar.edit=_Edit
menubar.edit.copy.headword=Copy Headword menubar.edit.copy.headword=Copy Headword
menubar.edit.copy.pronunciation=Copy Pronunciation menubar.edit.copy.pronunciation=Copy Pronunciation
menubar.edit.copy.sentence=Copy Sentence
menubar.help=_Help menubar.help=_Help
menubar.help.about=_About… menubar.help.about=_About…

View File

@ -25,6 +25,7 @@ menubar.file.preferences=_Einstellungen…
menubar.edit=_Bearbeiten menubar.edit=_Bearbeiten
menubar.edit.copy.headword=Kopiere Wort menubar.edit.copy.headword=Kopiere Wort
menubar.edit.copy.pronunciation=Kopiere Aussprache menubar.edit.copy.pronunciation=Kopiere Aussprache
menubar.edit.copy.sentence=Copy Sentence
menubar.help=_Hilfe menubar.help=_Hilfe
menubar.help.about=_Über… menubar.help.about=_Über…

View File

@ -25,6 +25,7 @@ menubar.file.preferences=_Preferences…
menubar.edit=_Edit menubar.edit=_Edit
menubar.edit.copy.headword=Copy Headword menubar.edit.copy.headword=Copy Headword
menubar.edit.copy.pronunciation=Copy Pronunciation menubar.edit.copy.pronunciation=Copy Pronunciation
menubar.edit.copy.sentence=Copy Sentence
menubar.help=_Help menubar.help=_Help
menubar.help.about=_About… menubar.help.about=_About…

View File

@ -25,6 +25,7 @@ menubar.file.preferences=_設定…
menubar.edit=_編輯 menubar.edit=_編輯
menubar.edit.copy.headword=複製 Wort menubar.edit.copy.headword=複製 Wort
menubar.edit.copy.pronunciation=複製 Aussprache menubar.edit.copy.pronunciation=複製 Aussprache
menubar.edit.copy.sentence=Copy Sentence
menubar.help=_說明 menubar.help=_說明
menubar.help.about=_關於 Willow… menubar.help.about=_關於 Willow…

View File

@ -25,6 +25,7 @@ menubar.file.preferences=_設定…
menubar.edit=_編輯 menubar.edit=_編輯
menubar.edit.copy.headword=複製 Wort menubar.edit.copy.headword=複製 Wort
menubar.edit.copy.pronunciation=複製 Aussprache menubar.edit.copy.pronunciation=複製 Aussprache
menubar.edit.copy.sentence=Copy Sentence
menubar.help=_說明 menubar.help=_說明
menubar.help.about=_關於 Willow… menubar.help.about=_關於 Willow…