Skip to content

Commit

Permalink
code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
aress31 committed Dec 26, 2023
1 parent 2fcf977 commit ea79986
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,14 @@ private JMenu createHighlightMenu() {
private JMenuItem createHighlightMenuItem(Color color) {
JMenuItem menuItem = new JMenuItem();

CustomTableCellRenderer renderer = (CustomTableCellRenderer) table.getDefaultRenderer(Object.class);

menuItem.setOpaque(true);
menuItem.setBackground(color);
menuItem.setForeground(Color.BLACK);

CustomTableCellRenderer renderer = (CustomTableCellRenderer) table.getDefaultRenderer(Object.class);

menuItem.addActionListener(e -> processSelectedRows(index -> {
SwingUtilities.invokeLater(() -> renderer.setRowHighlightColor(index, color));
}));
menuItem.addActionListener(e -> processSelectedRows(
index -> SwingUtilities.invokeLater(() -> renderer.setRowHighlightColor(index, color))));

return menuItem;
}
Expand Down
68 changes: 28 additions & 40 deletions src/main/java/swurg/gui/components/menus/ParserContextMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,37 +47,28 @@ public ParserContextMenu(MontoyaApi montoyaApi, JTable table) {
}

private void initComponents() {
JMenuItem copyToClipboard = createCopyToClipboardMenuItem();

JMenuItem addToScope = createAddToScopeMenuItem();
JMenuItem addToSiteMap = createAddToSiteMapMenuItem();
JMenuItem sendToActiveScan = createSendToActiveScanMenuItem();
JMenuItem sendToPassiveScan = createSendToPassiveScanMenuItem();
JMenuItem sendToIntruder = createSendToIntruderMenuItem();
JMenuItem sendToRepeater = createSendToRepeaterMenuItem();
JMenuItem sendToOrganizer = createSendToOrganizerMenuItem();
JMenuItem sendToComparer = createSendToComparerMenuItem();
JMenu highlightMenu = createHighlightMenu();
JMenuItem clearItems = createClearItemsMenuItem();
JMenuItem clearAll = createClearAllMenuItem();

this.add(copyToClipboard);
this.add(createCopyToClipboardMenuItem());
this.add(new JSeparator());
this.add(addToScope);

this.add(createAddToScopeMenuItem());
this.add(new JSeparator());
this.add(sendToPassiveScan);
this.add(sendToActiveScan);

this.add(createSendToPassiveScanMenuItem());
this.add(createSendToActiveScanMenuItem());
this.add(new JSeparator());
this.add(sendToIntruder);
this.add(sendToRepeater);
this.add(sendToOrganizer);
this.add(sendToComparer);

this.add(createSendToIntruderMenuItem());
this.add(createSendToRepeaterMenuItem());
this.add(createSendToOrganizerMenuItem());
this.add(createSendToComparerMenuItem());
this.add(new JSeparator());
this.add(highlightMenu);
this.add(clearItems);
this.add(clearAll);

this.add(createHighlightMenu());
this.add(createClearItemsMenuItem());
this.add(createClearAllMenuItem());
this.add(new JSeparator());
this.add(addToSiteMap);

this.add(createAddToSiteMapMenuItem());
}

private HttpRequest getHttpRequestAt(int index) {
Expand All @@ -96,7 +87,7 @@ private void processSelectedRows(Consumer<Integer> action) {
private JMenuItem createCopyToClipboardMenuItem() {
JMenuItem copyToClipboard = new JMenuItem();

table.addMouseListener(new MouseAdapter() {
this.table.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
JTable source = (JTable) e.getSource();
Expand Down Expand Up @@ -199,6 +190,7 @@ private JMenuItem createSendToOrganizerMenuItem() {

private JMenuItem createSendToComparerMenuItem() {
JMenuItem sendToComparer = new JMenuItem("Send to Comparer (request)");

sendToComparer.addActionListener(e -> processSelectedRows(index -> {
HttpRequest httpRequest = getHttpRequestAt(index);
montoyaApi.comparer().sendToComparer(httpRequest.toByteArray());
Expand All @@ -223,15 +215,14 @@ private JMenu createHighlightMenu() {
private JMenuItem createHighlightMenuItem(Color color) {
JMenuItem menuItem = new JMenuItem();

CustomTableCellRenderer renderer = (CustomTableCellRenderer) table.getDefaultRenderer(Object.class);

menuItem.setOpaque(true);
menuItem.setBackground(color);
menuItem.setForeground(Color.BLACK);

CustomTableCellRenderer renderer = (CustomTableCellRenderer) table.getDefaultRenderer(Object.class);

menuItem.addActionListener(e -> processSelectedRows(index -> {
SwingUtilities.invokeLater(() -> renderer.setRowHighlightColor(index, color));
}));
menuItem.addActionListener(e -> processSelectedRows(
index -> SwingUtilities.invokeLater(() -> renderer.setRowHighlightColor(index, color))));

return menuItem;
}
Expand All @@ -251,7 +242,6 @@ private JMenuItem createClearItemsMenuItem() {
// Remove rows individually from the table.
SwingUtilities.invokeLater(() -> {
ParserTableModel tableModel = (ParserTableModel) table.getModel();

selectedRows.forEach(index -> {
int modelRow = table.convertRowIndexToModel(index);
tableModel.removeRow(modelRow);
Expand All @@ -272,13 +262,11 @@ private JMenuItem createClearAllMenuItem() {

CustomTableCellRenderer renderer = (CustomTableCellRenderer) table.getDefaultRenderer(Object.class);

clearAll.addActionListener(e -> {
SwingUtilities.invokeLater(() -> {
ParserTableModel tableModel = (ParserTableModel) table.getModel();
tableModel.clear();
renderer.clearAllRowHighlightColors();
});
});
clearAll.addActionListener(e -> SwingUtilities.invokeLater(() -> {
ParserTableModel tableModel = (ParserTableModel) table.getModel();
tableModel.clear();
renderer.clearAllRowHighlightColors();
}));

return clearAll;
}
Expand Down

0 comments on commit ea79986

Please sign in to comment.