Skip to content

Commit

Permalink
add a hit indicator to the search field
Browse files Browse the repository at this point in the history
  • Loading branch information
aress31 committed Dec 24, 2023
1 parent 84b4f2e commit 323ea06
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 51 deletions.
51 changes: 36 additions & 15 deletions src/main/java/swurg/gui/components/FilterPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,56 @@
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.TableRowSorter;

public class FilterPanel extends JPanel {
private final JTextField filterTextField;
private final JTextField filterTextField = new JTextField(32);
private final TableRowSorter<?> tableRowSorter;

public FilterPanel(JTextField filterTextField, TableRowSorter<?> tableRowSorter) {
this.filterTextField = filterTextField;
private JLabel eastLabel = new JLabel("0 hits");

public FilterPanel(TableRowSorter<?> tableRowSorter) {
this.tableRowSorter = tableRowSorter;

this.add(new JLabel("Filter (regex, case-sensitive):"));
// Prevents JTextField from collapsing on resizes...
initComponents();

addFilterTextFieldListener();
}

public void initComponents() {
JLabel westLabel = new JLabel("Filter (regex, case-sensitive):");

// Ensures JTextField size stability during resizing...
this.filterTextField.setMinimumSize(new Dimension(this.filterTextField.getPreferredSize()));
this.add(this.filterTextField);

this.setUpFilterTextField();
this.add(westLabel);
this.add(this.filterTextField);
this.add(this.eastLabel);
}

private void setUpFilterTextField() {
private void addFilterTextFieldListener() {
this.filterTextField.getDocument().addDocumentListener(new DocumentListener() {
private void updateFilter() {
String regex = filterTextField.getText();
try {
tableRowSorter.setRowFilter(regex.isEmpty() ? null : RowFilter.regexFilter(regex));
} catch (PatternSyntaxException e) {
// Show an error message if the regex pattern is invalid.
}
SwingUtilities.invokeLater(() -> {
String regex = filterTextField.getText();

try {
if (regex.isBlank()) {
tableRowSorter.setRowFilter(null);
eastLabel.setText("0 hits");
} else {
tableRowSorter.setRowFilter(RowFilter.regexFilter(regex));

int rowCount = tableRowSorter.getViewRowCount();
eastLabel.setText(String.format("%d hit%s", rowCount, (rowCount != 1 ? "s" : "")));
}
} catch (PatternSyntaxException e) {
// Show an error message if the regex pattern is invalid.
}
});
}

@Override
Expand All @@ -50,7 +71,7 @@ public void removeUpdate(DocumentEvent e) {

@Override
public void changedUpdate(DocumentEvent e) {
// No action needed
// TODO Auto-generated method stub
}
});
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/swurg/gui/components/StatusPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ public class StatusPanel extends JPanel {

public StatusPanel() {
statusLabel = new JLabel(COPYRIGHT);

initComponents();
}

private void initComponents() {
statusLabel.putClientProperty("html.disable", null);
add(statusLabel);
this.add(statusLabel);
}

public void updateStatus(String status, Color color) {
Expand Down
50 changes: 28 additions & 22 deletions src/main/java/swurg/gui/components/tables/TablePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
Expand All @@ -31,36 +30,43 @@
@Data
public class TablePanel extends JPanel {

private final TableModel tableModel;
private final TableCellRenderer cellRenderer;
private final HttpRequestEditor requestViewer;

private JTable table;
private TableRowSorter<TableModel> tableRowSorter;
private JTextField filterTextField;

public TablePanel(TableModel tableModel, TableCellRenderer cellRenderer) {
this(tableModel, cellRenderer, null);
}

public TablePanel(TableModel tableModel, TableCellRenderer cellRenderer, HttpRequestEditor requestViewer) {
this.tableModel = tableModel;
this.cellRenderer = cellRenderer;
this.requestViewer = requestViewer;

initComponents();
}

public void initComponents() {
this.setLayout(new GridBagLayout());

filterTextField = new JTextField(32);
table = createTable(tableModel, cellRenderer, requestViewer);

JPanel filterPanel = new FilterPanel(filterTextField, tableRowSorter);
JScrollPane scrollPane = new JScrollPane(table);

GridBagConstraints filterPanelConstraints = new GridBagConstraints();
filterPanelConstraints.gridx = 0;
filterPanelConstraints.gridy = 0;
filterPanelConstraints.anchor = GridBagConstraints.NORTHWEST;
this.add(filterPanel, filterPanelConstraints);

GridBagConstraints tableConstraints = new GridBagConstraints();
tableConstraints.gridx = 0;
tableConstraints.gridy = 1;
tableConstraints.fill = GridBagConstraints.BOTH;
tableConstraints.weightx = 1.0;
tableConstraints.weighty = 1.0;
this.add(scrollPane, tableConstraints);
this.table = createTable(this.tableModel, this.cellRenderer, this.requestViewer);
JScrollPane scrollPane = new JScrollPane(this.table);

JPanel filterPanel = new FilterPanel(this.tableRowSorter);

GridBagConstraints gbc = new GridBagConstraints();

gbc.anchor = GridBagConstraints.NORTHWEST;
this.add(filterPanel, gbc);

gbc.fill = GridBagConstraints.BOTH;
gbc.gridy = 1;
gbc.weightx = 1;
gbc.weighty = 1;
this.add(scrollPane, gbc);
}

private JTable createTable(TableModel tableModel, TableCellRenderer cellRenderer, HttpRequestEditor requestViewer) {
Expand All @@ -83,7 +89,7 @@ public void changeSelection(int row, int col, boolean toggle, boolean extend) {
table.setDefaultRenderer(Object.class, cellRenderer);
table.setAutoCreateRowSorter(true);

tableRowSorter = new TableRowSorter<>(table.getModel());
this.tableRowSorter = new TableRowSorter<>(table.getModel());
table.setRowSorter(tableRowSorter);

return table;
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/swurg/gui/views/AboutPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public AboutPanel() {
}

private void initComponents() {
setLayout(new BorderLayout());
this.setLayout(new BorderLayout());

JPanel svgPanel = createSvgCanvas();
svgPanel.setPreferredSize(new Dimension(192, 192));
Expand All @@ -44,9 +44,9 @@ private void initComponents() {
JPanel contentPanel = createContentPanel();
contentPanel.setBorder(new EmptyBorder(0, 16, 0, 16));

add(svgPanel, BorderLayout.NORTH);
add(contentPanel, BorderLayout.CENTER);
add(new StatusPanel(), BorderLayout.SOUTH);
this.add(svgPanel, BorderLayout.NORTH);
this.add(contentPanel, BorderLayout.CENTER);
this.add(new StatusPanel(), BorderLayout.SOUTH);
}

private JPanel createSvgCanvas() {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/swurg/gui/views/ParametersPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void onRequestWithMetadatasUpdate() {
}

private void initComponents() {
setLayout(new BorderLayout());
this.setLayout(new BorderLayout());

JPanel northPanel = createNorthPanel();
TablePanel tablePanel = new TablePanel(parametersTableModel, new CustomTableCellRenderer());
Expand All @@ -83,12 +83,12 @@ private void initComponents() {
JPanel eastPanel = createEastPanel();
JPanel southPanel = new StatusPanel();

add(northPanel, BorderLayout.NORTH);
add(southPanel, BorderLayout.SOUTH);
this.add(northPanel, BorderLayout.NORTH);
this.add(southPanel, BorderLayout.SOUTH);

// add a nested JPanel with a GridBagLayout to the CENTER of the main container
JPanel centerContainer = new JPanel(new GridBagLayout());
add(centerContainer, BorderLayout.CENTER);
this.add(centerContainer, BorderLayout.CENTER);

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/swurg/gui/views/ParserPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ public void changedUpdate(DocumentEvent e) {
private void initComponents() {
setLayout(new BorderLayout());

add(createNorthPanel(), BorderLayout.NORTH);
add(createSplitPane(), BorderLayout.CENTER);
add(statusPanel, BorderLayout.SOUTH);
this.add(createNorthPanel(), BorderLayout.NORTH);
this.add(createSplitPane(), BorderLayout.CENTER);
this.add(this.statusPanel, BorderLayout.SOUTH);
}

private Icon resizeIcon(Icon originalIcon, int iconWidth) {
Expand Down Expand Up @@ -149,13 +149,13 @@ public JPanel createNorthPanel() {
resourcePanel.add(new JLabel("Parse from local file or URL:"), gbc);

gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
gbc.gridx = 1;
gbc.weightx = 1;
gbc.insets = new Insets(0, 0, 0, 0);
resourcePanel.add(resourceTextField, gbc);

gbc.weightx = 0;
gbc.gridx = 2;
gbc.weightx = 0;
gbc.insets = new Insets(0, 0, 0, 5);
resourcePanel.add(eastPanel, gbc);

Expand Down

0 comments on commit 323ea06

Please sign in to comment.