Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve tables models #44

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,42 +25,27 @@ public int getRowCount() {

@Override
public int getColumnCount() {
return 3;
return Column.getSize();
}

@Override
public String getColumnName(int columnIndex) {
return switch (columnIndex) {
case 0 -> getLocaleString("common-url");
case 1 -> getLocaleString("common-regex");
case 2 -> getLocaleString("common-match");
default -> "";
};
}

public String getColumnNameFormatted(int columnIndex) {
return switch (columnIndex) {
case 0 -> "url";
case 1 -> "regex";
case 2 -> "match";
default -> "";
};
return getLocaleString(Column.getById(columnIndex).localeKey);
}

@Override
public Class<?> getColumnClass(int columnIndex) {
return String.class;
return Column.getById(columnIndex).columnType;
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
LogEntity logEntity = logEntries.get(rowIndex);

return switch (columnIndex) {
case 0 -> logEntity.getRequestResponse().finalRequest().url();
case 1 -> logEntity.getRegexEntity().getDescription() + " - " + logEntity.getRegexEntity().getRegex();
case 2 -> logEntity.getMatch();
default -> "";
return switch (Column.getById(columnIndex)) {
case URL -> logEntity.getRequestResponse().finalRequest().url();
case REGEX -> logEntity.getRegexEntity().getDescription() + " - " + logEntity.getRegexEntity().getRegex();
case MATCH -> logEntity.getMatch();
};
}

Expand All @@ -71,4 +56,46 @@ public void addNewRow(int row) {
public void clear() {
fireTableDataChanged();
}

/**
* Enum representing the columns of the table model for logs
*/
public enum Column {
URL("common-url", "url", String.class),
REGEX("common-regex", "regex", String.class),
MATCH("common-match", "match", String.class);

private static final List<Column> columns = List.of(URL, REGEX, MATCH);

private final String localeKey;
private final String formattedName;
private final Class<?> columnType;

Column(String localeKey, String formattedName, Class<?> columnType) {
this.localeKey = localeKey;
this.formattedName = formattedName;
this.columnType = columnType;
}

public static Column getById(int index) {
return columns.get(index);
}

/**
* Returns the number of elements in this enum
*
* @return the number of elements in this enum
*/
public static int getSize() {
return columns.size();
}

public String getNameFormatted() {
return formattedName;
}

public int getIndex() {
return columns.indexOf(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,43 +24,33 @@ public int getRowCount() {

@Override
public int getColumnCount() {
return 4;
return Column.getSize();
}

@Override
public String getColumnName(int columnIndex) {
return switch (columnIndex) {
case 0 -> getLocaleString("common-active");
case 1 -> getLocaleString("common-regex");
case 2 -> getLocaleString("common-description");
case 3 -> getLocaleString("common-sections");
default -> "";
};
return getLocaleString(Column.getById(columnIndex).localeKey);
}

@Override
public Class<?> getColumnClass(int columnIndex) {
if (columnIndex == 0) {
return Boolean.class;
}
return String.class;
return Column.getById(columnIndex).columnType;
}

@Override
public boolean isCellEditable(int row, int column) {
return column == 0;
return Column.getById(column).editable;
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
RegexEntity regexEntry = regexList.get(rowIndex);

return switch (columnIndex) {
case 0 -> regexEntry.isActive();
case 1 -> regexEntry.getRegex();
case 2 -> regexEntry.getDescription();
case 3 -> regexEntry.getSectionsHumanReadable();
default -> "";
return switch (Column.getById(columnIndex)) {
case ACTIVE -> regexEntry.isActive();
case REGEX -> regexEntry.getRegex();
case DESCRIPTION -> regexEntry.getDescription();
case SECTIONS -> regexEntry.getSectionsHumanReadable();
};
}

Expand All @@ -70,4 +60,39 @@ public void setValueAt(Object value, int rowIndex, int columnIndex) {
regexEntry.setActive((Boolean) value);
fireTableCellUpdated(rowIndex, columnIndex);
}

/**
* Enum representing the columns of the table model for regex lists
*/
public enum Column {
ACTIVE("common-active", true, Boolean.class),
REGEX("common-regex", false, String.class),
DESCRIPTION("common-description", false, String.class),
SECTIONS("common-sections", false, String.class);

private static final List<Column> columns = List.of(ACTIVE, REGEX, DESCRIPTION, SECTIONS);

private final String localeKey;
private final boolean editable;
private final Class<?> columnType;

Column(String localeKey, boolean editable, Class<?> columnType) {
this.localeKey = localeKey;
this.editable = editable;
this.columnType = columnType;
}

public static Column getById(int index) {
return columns.get(index);
}

/**
* Returns the number of elements in this enum
*
* @return the number of elements in this enum
*/
public static int getSize() {
return columns.size();
}
}
}
16 changes: 8 additions & 8 deletions src/main/java/com/cys4/sensitivediscoverer/tab/LoggerTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -322,13 +322,13 @@ private JToggleButton createExportLogsButton() {
java.util.List<String> lines = new ArrayList<>();

lines.add(String.format("\"%s\",\"%s\"",
logsTableModel.getColumnNameFormatted(0),
logsTableModel.getColumnNameFormatted(2)));
LogsTableModel.Column.URL.getNameFormatted(),
LogsTableModel.Column.MATCH.getNameFormatted()));

// values
for (int i = 0; i < logsTableModel.getRowCount(); i++) {
String url = logsTableModel.getValueAt(i, 0).toString();
String matchEscaped = logsTableModel.getValueAt(i, 2).toString().replaceAll("\"", "\"\"");
String url = logsTableModel.getValueAt(i, LogsTableModel.Column.URL.getIndex()).toString();
String matchEscaped = logsTableModel.getValueAt(i, LogsTableModel.Column.MATCH.getIndex()).toString().replaceAll("\"", "\"\"");
lines.add(String.format("\"%s\",\"%s\"", url, matchEscaped));
}

Expand All @@ -340,14 +340,14 @@ private JToggleButton createExportLogsButton() {
itemToJSON.addActionListener(actionEvent -> {
java.util.List<JsonObject> lines = new ArrayList<>();

String prop1 = logsTableModel.getColumnNameFormatted(0);
String prop2 = logsTableModel.getColumnNameFormatted(2);
String prop1 = LogsTableModel.Column.URL.getNameFormatted();
String prop2 = LogsTableModel.Column.MATCH.getNameFormatted();

// values
for (int i = 0; i < logsTableModel.getRowCount(); i++) {
JsonObject obj = new JsonObject();
obj.addProperty(prop1, logsTableModel.getValueAt(i, 0).toString());
obj.addProperty(prop2, logsTableModel.getValueAt(i, 2).toString());
obj.addProperty(prop1, logsTableModel.getValueAt(i, LogsTableModel.Column.URL.getIndex()).toString());
obj.addProperty(prop2, logsTableModel.getValueAt(i, LogsTableModel.Column.MATCH.getIndex()).toString());
lines.add(obj);
}

Expand Down
Loading