Search in ListSpeedSearch with CheckBoxList works incorrect

Answered

Hello.

Here two videos: Upload id: 2021_03_13_Bu4t7ZTuXLyUPY7z (files: 01.webm, 02.webm)

1) How search works with ListSpeedSearch  + JBList. It`s ok.

2) How search works with ListSpeedSearch  + CheckBoxList. Here the problem. Search works strange. I can`t find any pattern to describe.

 

Code of my DialogWrapper:

abstract class SearchableListDialog<T>(
project: Project,
dialogTitle: String,
private val items: List<T>,
private val multipleSelection: Boolean = false,
private var initialSelectedIndex: Int,
) : DialogWrapper(project) {

protected abstract val itemNameMapper: (T) -> String
private lateinit var selectionListener: (List<Int>) -> Unit

private val itemsNames = items.map { itemNameMapper(it) }.toTypedArray()

private var selectedIndex = initialSelectedIndex.coerceAtLeast(0)
private val checkedIndexes: MutableMap<Int, Boolean> =
(0..itemsNames.lastIndex).map { it to false }.toMap().toMutableMap()

init {
title = dialogTitle
setOKButtonText("Select")
super.init()
}

fun onSelected(listener: (List<Int>) -> Unit) {
selectionListener = listener
}

override fun createCenterPanel(): JComponent {
val list = if (multipleSelection) {
getMultipleSelectionList()
} else {
getSingleSelectionList()
}

list.addListSelectionListener {
selectedIndex = list.selectedIndex
}

return ListSpeedSearch(list).apply {
findAndSelectElement(itemNameMapper(items[initialSelectedIndex]))
}.component
}

private fun getSingleSelectionList(): JBList<*> =
JBList(*itemsNames).apply {
selectionMode = ListSelectionModel.SINGLE_SELECTION
}

private fun getMultipleSelectionList(): JBList<*> =
CheckBoxList<String>().apply {
setStringItems(itemsNames.map { it to false }.toMap())

setCheckBoxListListener { index, isChecked ->
checkedIndexes[index] = isChecked
}
}
}
2 comments
Comment actions Permalink

In Preferences | Editor | General | Gutter Icons, this is used:

new ListSpeedSearch<>(myList, JCheckBox::getText);
0
Comment actions Permalink

Thank you!

0

Please sign in to leave a comment.