Skip to content

Commit

Permalink
Make new filters go on top and fix layout issues
Browse files Browse the repository at this point in the history
  • Loading branch information
waleedyaseen committed Oct 2, 2024
1 parent 2e79c00 commit 7ad3b0f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
22 changes: 15 additions & 7 deletions gui/proxy-tool/src/main/kotlin/net/rsprox/gui/FiltersSidePanel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ public class FiltersSidePanel(
private val checkboxes = hashMapOf<PropertyFilter, JCheckBox>()
private val incomingPanel = FiltersPanel(StreamDirection.SERVER_TO_CLIENT)
private val outgoingPanel = FiltersPanel(StreamDirection.CLIENT_TO_SERVER)
private val regexPanel = JPanel()
private val regexPanel = JPanel().apply {
layout = BorderLayout()
border = BorderFactory.createEmptyBorder(0, 0, 0, 0)
}

private val searchBox = JTextField(SEARCH)

init {
Expand Down Expand Up @@ -186,7 +190,7 @@ public class FiltersSidePanel(
val tabbedGroup = FlatTabbedPane()
tabbedGroup.addTab("Incoming", incomingPanel.wrapWithBorderlessScrollPane())
tabbedGroup.addTab("Outgoing", outgoingPanel.wrapWithBorderlessScrollPane())
tabbedGroup.addTab("Regex", regexPanel.wrapWithBorderlessScrollPane(verticalPolicy = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED))
tabbedGroup.addTab("Regex", regexPanel.wrapWithBorderlessScrollPane())

tabbedGroup.addMouseListener(
object : MouseAdapter() {
Expand Down Expand Up @@ -216,12 +220,10 @@ public class FiltersSidePanel(
}

private fun buildRegexPanel() {
regexPanel.layout = BorderLayout()
regexPanel.border = BorderFactory.createEmptyBorder(0, 0, 0, 0)
regexPanel.removeAll()

val regexFiltersContainer = JPanel()
regexFiltersContainer.layout = MigLayout("ins 5, hidemode 3", "[grow, fill]", "[]")
regexFiltersContainer.layout = MigLayout("ins 5", "[grow, fill]", "[]")
regexFiltersContainer.border = BorderFactory.createEmptyBorder(0, 0, 0, 0)
regexPanel.add(regexFiltersContainer, BorderLayout.CENTER)

Expand All @@ -232,6 +234,7 @@ public class FiltersSidePanel(
val regexLabel = FlatLabel().apply {
text = "Regex Filters"
labelType = FlatLabel.LabelType.large
isEnabled = presetsBox.selectedIndex != 0
}
actionsPanel.add(regexLabel, BorderLayout.WEST)

Expand All @@ -244,7 +247,11 @@ public class FiltersSidePanel(
val regexFilter = RegexFilter("prot_name", Regex(""), true)
filterStore.addRegexFilter(regexFilter)
regexFiltersContainer.addRegexFilterPanel(regexFilter)

regexPanel.revalidate()
regexPanel.repaint()
}
isEnabled = presetsBox.selectedIndex != 0
}, BorderLayout.EAST)
regexPanel.add(actionsPanel, BorderLayout.NORTH)

Expand All @@ -253,13 +260,14 @@ public class FiltersSidePanel(
active.getRegexFilters().forEach { regexFilter ->
regexFiltersContainer.addRegexFilterPanel(regexFilter)
}

revalidate()
}

private fun JPanel.addRegexFilterPanel(filter: RegexFilter) {
val filterStore = proxyService.filterSetStore.getActive()
val regexFilterPanel = RegexFilterPanel(filterStore, filter)
add(regexFilterPanel, "wrap")
regexPanel.revalidate()
add(regexFilterPanel, "wrap", 0)
}

private fun updateFilterState() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import net.rsprox.shared.filters.PropertyFilterSet
import net.rsprox.shared.filters.RegexFilter
import java.awt.event.FocusAdapter
import java.awt.event.FocusEvent
import java.awt.event.KeyAdapter
import java.awt.event.KeyEvent
import java.awt.event.MouseAdapter
import java.awt.event.MouseEvent
import java.util.regex.PatternSyntaxException
Expand All @@ -21,6 +23,7 @@ import javax.swing.JSeparator
import javax.swing.JTextField
import javax.swing.event.DocumentEvent
import javax.swing.event.DocumentListener
import javax.swing.undo.UndoManager

public class RegexFilterPanel(
private val filterSet: PropertyFilterSet,
Expand All @@ -32,14 +35,14 @@ public class RegexFilterPanel(
private val perLineCheckbox = FlatCheckBox()

init {
layout = MigLayout("fill, gap 5", "[grow][]", "[][][]")
layout = MigLayout("gap 5", "[grow, fill][]", "[][][]")
putClientProperty(FlatClientProperties.STYLE, "background: darken(${'$'}Panel.background,2%)")

// Add a label for the filter name.
protNameLabel.text = currentFilter.protName
protNameLabel.toolTipText = "The name of the filter."
installNameEditor(protNameLabel)
add(protNameLabel, "growx")
add(protNameLabel)

// Add a delete button to remove the filter.
add(FlatButton().apply {
Expand All @@ -61,7 +64,7 @@ public class RegexFilterPanel(
}
}, "wrap")

add(JSeparator(), "span, growx, wrap")
add(JSeparator(), "span, wrap")

// Add a text field for the regular expression.
regexTextField.toolTipText = "The regular expression to match."
Expand All @@ -80,8 +83,16 @@ public class RegexFilterPanel(
regexUpdated()
}
})
regexTextField.columns = 15
add(regexTextField, "growx")

// Add undo/redo support to the text field.
val undoManager = UndoManager()
regexTextField.document.addUndoableEditListener(undoManager)
regexTextField.addKeyListener(UndoRedoKeyListener(undoManager))

// Set the columns to 1 to prevent the text field from growing and resizing the parent.
regexTextField.columns = 1

add(regexTextField)

// Add a checkbox for matching per line.
perLineCheckbox.toolTipText = "Whether to match per line of output."
Expand All @@ -107,7 +118,7 @@ public class RegexFilterPanel(
editor.selectAll()

remove(label)
add(editor, "growx", 0)
add(editor, 0)

revalidate()
repaint()
Expand Down Expand Up @@ -147,7 +158,7 @@ public class RegexFilterPanel(

// Remove the editor and add the label back.
remove(editor)
add(label, "growx", 0)
add(label, 0)

// Revalidate and repaint the panel.
revalidate()
Expand All @@ -173,4 +184,23 @@ public class RegexFilterPanel(
}
}
}

private class UndoRedoKeyListener(private val undoManager: UndoManager) : KeyAdapter() {
override fun keyPressed(e: KeyEvent) {
if (e.isControlDown) {
when {
e.keyCode == KeyEvent.VK_Z -> {
if (undoManager.canUndo()) {
undoManager.undo()
}
}
e.keyCode == KeyEvent.VK_Y -> {
if (undoManager.canRedo()) {
undoManager.redo()
}
}
}
}
}
}
}

0 comments on commit 7ad3b0f

Please sign in to comment.