Skip to content

Commit

Permalink
Make Dialog list for intellij sorted + add search bar (#1034)
Browse files Browse the repository at this point in the history
  • Loading branch information
infiniteregrets authored Feb 9, 2023
1 parent b00f02e commit 5458036
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how

## [Unreleased]

### Changed

- IntelliJ-ext: change the dialog to provide a sorted list and make it searchable, resolves [#1031](https://github.com/metalbear-co/mirrord/issues/1031).

## 3.23.0

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,32 @@ import com.intellij.ui.components.JBScrollPane
import java.awt.*
import javax.swing.*
import javax.swing.border.EmptyBorder
import javax.swing.event.DocumentEvent
import javax.swing.event.DocumentListener


object MirrordExecDialog {
private const val dialogHeading: String = "mirrord"
private const val targetLabel = "Select Target"

fun selectTargetDialog(targets: List<String>): String? {
val jbTargets = targets.asJBList()
val result = DialogBuilder(). apply {
val jbTargets = targets.sorted().asJBList()
val searchField = JTextField()
searchField.document.addDocumentListener(object : DocumentListener {
override fun insertUpdate(e: DocumentEvent) = updateList()
override fun removeUpdate(e: DocumentEvent) = updateList()
override fun changedUpdate(e: DocumentEvent) = updateList()

private fun updateList() {
val searchTerm = searchField.text
val filteredTargets = targets.filter { it.contains(searchTerm, true) }.sorted()
jbTargets.setListData(filteredTargets.toTypedArray())
}

})
val result = DialogBuilder().apply {
setCenterPanel(JPanel(BorderLayout()).apply {
add(createSelectionDialog(targetLabel, jbTargets), BorderLayout.WEST)
add(createSelectionDialog(targetLabel, jbTargets, searchField), BorderLayout.WEST)
})
setTitle(dialogHeading)
}.show()
Expand All @@ -32,14 +46,19 @@ object MirrordExecDialog {
selectionMode = ListSelectionModel.SINGLE_SELECTION
}

private fun createSelectionDialog(label: String, items: JBList<String>): JPanel =
private fun createSelectionDialog(label: String, items: JBList<String>, searchField: JTextField): JPanel =
JPanel().apply {
layout = BoxLayout(this, BoxLayout.Y_AXIS)
border = EmptyBorder(10, 5, 10, 5)
add(JLabel(label).apply {
alignmentX = JLabel.LEFT_ALIGNMENT
})
add(Box.createRigidArea(Dimension(0, 5)))
add(searchField.apply {
alignmentX = JBScrollPane.LEFT_ALIGNMENT
preferredSize = Dimension(250, 30)
size = Dimension(250, 30)
})
add(JBScrollPane(items).apply {
alignmentX = JBScrollPane.LEFT_ALIGNMENT
preferredSize = Dimension(250, 350)
Expand Down

0 comments on commit 5458036

Please sign in to comment.