Skip to content

Commit

Permalink
Introduce ListFilterPanel to avoid repetitive class impls.
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshlha committed Sep 18, 2023
1 parent c36703a commit 55125fc
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 124 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.github.inductiveautomation.kindling.core

import io.github.inductiveautomation.kindling.utils.Column
import io.github.inductiveautomation.kindling.utils.FilterList
import io.github.inductiveautomation.kindling.utils.FlatScrollPane
import io.github.inductiveautomation.kindling.utils.Stringifier
import io.github.inductiveautomation.kindling.utils.getAll
import net.miginfocom.swing.MigLayout
import javax.swing.JPanel
import javax.swing.JPopupMenu

abstract class LIstFilterPanel<T>(
override val tabName: String,
toStringFn: Stringifier = { it?.toString() },
) : FilterPanel<T>() {
val filterList = FilterList(toStringFn = toStringFn)

private val sortButtons = filterList.createSortButtons()

override val component = JPanel(MigLayout("fill, gap 5")).apply {
val sortGroupEnumeration = sortButtons.elements
add(sortGroupEnumeration.nextElement(), "split ${sortButtons.buttonCount}, flowx")
for (element in sortGroupEnumeration) {
add(element, "gapx 2")
}
add(FlatScrollPane(filterList), "newline, push, grow")
}

init {
filterList.checkBoxListSelectionModel.addListSelectionListener { e ->
if (!e.valueIsAdjusting) {
listeners.getAll<FilterChangeListener>().forEach(FilterChangeListener::filterChanged)
}
}
}

override fun isFilterApplied() = filterList.checkBoxListSelectedValues.size != filterList.model.size - 1

override fun reset() = filterList.selectAll()

override fun customizePopupMenu(menu: JPopupMenu, column: Column<out T, *>, event: T) = Unit
}
Original file line number Diff line number Diff line change
@@ -1,50 +1,20 @@
package io.github.inductiveautomation.kindling.log

import io.github.inductiveautomation.kindling.core.FilterChangeListener
import io.github.inductiveautomation.kindling.core.FilterPanel
import io.github.inductiveautomation.kindling.core.LIstFilterPanel
import io.github.inductiveautomation.kindling.utils.Action
import io.github.inductiveautomation.kindling.utils.Column
import io.github.inductiveautomation.kindling.utils.FilterList
import io.github.inductiveautomation.kindling.utils.FilterModel
import io.github.inductiveautomation.kindling.utils.FlatScrollPane
import io.github.inductiveautomation.kindling.utils.getAll
import net.miginfocom.swing.MigLayout
import javax.swing.ButtonGroup
import javax.swing.JPanel
import javax.swing.JPopupMenu
import javax.swing.JToggleButton

internal class ThreadPanel(events: List<LogEvent>) : FilterPanel<LogEvent>() {
private val filterList = FilterList().apply {
setModel(FilterModel(events.groupingBy { (it as SystemLogEvent).thread }.eachCount()))
}

override val component = JPanel(MigLayout("ins 0, fill"))
override val tabName: String = "Thread"

internal class ThreadPanel(events: List<LogEvent>) : LIstFilterPanel<LogEvent>("Thread") {
init {
val bg = ButtonGroup()
for (sortAction in filterList.sortActions) {
val sortToggle = JToggleButton(sortAction)
bg.add(sortToggle)
component.add(sortToggle, "split, gapx 2")
}

component.add(FlatScrollPane(filterList), "newline, push, grow")

filterList.selectAll()
filterList.checkBoxListSelectionModel.addListSelectionListener { event ->
if (!event.valueIsAdjusting) {
listeners.getAll<FilterChangeListener>().forEach(FilterChangeListener::filterChanged)
}
filterList.apply {
setModel(FilterModel(events.groupingBy { (it as SystemLogEvent).thread }.eachCount()))
selectAll()
}
}

override fun isFilterApplied(): Boolean = filterList.checkBoxListSelectedIndices.size < filterList.model.size - 1

override fun filter(item: LogEvent): Boolean {
return (item as SystemLogEvent).thread in filterList.checkBoxListSelectedValues
}
override fun filter(item: LogEvent) = (item as SystemLogEvent).thread in filterList.checkBoxListSelectedValues

override fun customizePopupMenu(
menu: JPopupMenu,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ class MultiThreadView(
val allThreads = value.flatten().filterNotNull()
if (allThreads.isNotEmpty()) {
statePanel.stateList.setModel(FilterModel(allThreads.groupingBy { it.state.name }.eachCount()))
systemPanel.systemList.setModel(FilterModel(allThreads.groupingBy(Thread::system).eachCount()))
poolPanel.poolList.setModel(FilterModel(allThreads.groupingBy(Thread::pool).eachCount()))
systemPanel.filterList.setModel(FilterModel(allThreads.groupingBy(Thread::system).eachCount()))
poolPanel.filterList.setModel(FilterModel(allThreads.groupingBy(Thread::pool).eachCount()))
}
if (initialized) {
updateData()
Expand Down Expand Up @@ -165,14 +165,14 @@ class MultiThreadView(
SingleThreadColumns.system, MultiThreadColumns.system -> {
val system = model[selectedRowIndex, model.columns.system]
if (system != null) {
systemPanel.systemList.select(system)
systemPanel.filterList.select(system)
}
}

SingleThreadColumns.pool, MultiThreadColumns.pool -> {
val pool = model[selectedRowIndex, model.columns.pool]
if (pool != null) {
poolPanel.poolList.select(pool)
poolPanel.filterList.select(pool)
}
}
}
Expand Down Expand Up @@ -322,9 +322,9 @@ class MultiThreadView(

toolTipText = paths.joinToString("\n", transform = Path::name)

poolPanel.poolList.selectAll()
poolPanel.filterList.selectAll()
statePanel.stateList.selectAll()
systemPanel.systemList.selectAll()
systemPanel.filterList.selectAll()

sidebar.filterPanels.forEach { panel ->
panel.addFilterChangeListener {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,11 @@
package io.github.inductiveautomation.kindling.thread

import io.github.inductiveautomation.kindling.core.FilterChangeListener
import io.github.inductiveautomation.kindling.core.FilterPanel
import io.github.inductiveautomation.kindling.core.LIstFilterPanel
import io.github.inductiveautomation.kindling.thread.model.Thread
import io.github.inductiveautomation.kindling.utils.Column
import io.github.inductiveautomation.kindling.utils.FilterList
import io.github.inductiveautomation.kindling.utils.FlatScrollPane
import io.github.inductiveautomation.kindling.utils.getAll
import net.miginfocom.swing.MigLayout
import javax.swing.JPanel
import javax.swing.JPopupMenu

class PoolPanel : FilterPanel<Thread?>() {
override val tabName = "Pool"

val poolList = FilterList { it?.toString() ?: "(No Pool)" }

private val sortButtons = poolList.createSortButtons()

override val component = JPanel(MigLayout("fill, gap 5")).apply {
val sortGroupEnumeration = sortButtons.elements
add(sortGroupEnumeration.nextElement(), "split ${sortButtons.buttonCount}, flowx")
for (element in sortGroupEnumeration) {
add(element, "gapx 2")
}
add(FlatScrollPane(poolList), "newline, push, grow")
}

init {
poolList.checkBoxListSelectionModel.addListSelectionListener { e ->
if (!e.valueIsAdjusting) {
listeners.getAll<FilterChangeListener>().forEach(FilterChangeListener::filterChanged)
}
}
}

override fun isFilterApplied(): Boolean = poolList.checkBoxListSelectedValues.size != poolList.model.size - 1

override fun reset() = poolList.selectAll()

override fun filter(item: Thread?): Boolean = item?.pool in poolList.checkBoxListSelectedValues

override fun customizePopupMenu(menu: JPopupMenu, column: Column<out Thread?, *>, event: Thread?) = Unit
class PoolPanel : LIstFilterPanel<Thread?>(
tabName = "Pool",
toStringFn = { it?.toString() ?: "(No Pool)" },
) {
override fun filter(item: Thread?) = item?.pool in filterList.checkBoxListSelectedValues
}
Original file line number Diff line number Diff line change
@@ -1,47 +1,11 @@
package io.github.inductiveautomation.kindling.thread

import io.github.inductiveautomation.kindling.core.FilterChangeListener
import io.github.inductiveautomation.kindling.core.FilterPanel
import io.github.inductiveautomation.kindling.core.LIstFilterPanel
import io.github.inductiveautomation.kindling.thread.model.Thread
import io.github.inductiveautomation.kindling.utils.Column
import io.github.inductiveautomation.kindling.utils.FilterList
import io.github.inductiveautomation.kindling.utils.FlatScrollPane
import io.github.inductiveautomation.kindling.utils.getAll
import net.miginfocom.swing.MigLayout
import javax.swing.JPanel
import javax.swing.JPopupMenu

class SystemPanel : FilterPanel<Thread?>() {
override val tabName = "System"

val systemList = FilterList { it?.toString() ?: "Unassigned" }

private val sortButtons = systemList.createSortButtons()

override val component = JPanel(MigLayout("fill, gap 5")).apply {
val sortGroupEnumeration = sortButtons.elements
add(sortGroupEnumeration.nextElement(), "split ${sortButtons.buttonCount}, flowx")
for (element in sortGroupEnumeration) {
add(element, "gapx 2")
}
add(FlatScrollPane(systemList), "newline, push, grow")
}

init {
systemList.selectAll()

systemList.checkBoxListSelectionModel.addListSelectionListener { e ->
if (!e.valueIsAdjusting) {
listeners.getAll<FilterChangeListener>().forEach(FilterChangeListener::filterChanged)
}
}
}

override fun isFilterApplied(): Boolean = systemList.checkBoxListSelectedValues.size != systemList.model.size - 1

override fun reset() = systemList.selectAll()

override fun filter(item: Thread?): Boolean = item?.system in systemList.checkBoxListSelectedValues

override fun customizePopupMenu(menu: JPopupMenu, column: Column<out Thread?, *>, event: Thread?) = Unit
class SystemPanel : LIstFilterPanel<Thread?>(
tabName = "System",
toStringFn = { it?.toString() ?: "Unassigned" },
) {
override fun filter(item: Thread?): Boolean = item?.system in filterList.checkBoxListSelectedValues
}

0 comments on commit 55125fc

Please sign in to comment.