Skip to content

Commit

Permalink
Fix: code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
davidebresaola committed Mar 20, 2024
1 parent b8a9750 commit 80879f9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.awt.*;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.Supplier;

import static com.cys4.sensitivediscoverer.Messages.getLocaleString;
Expand Down Expand Up @@ -258,8 +257,6 @@ private JMenuItem createListSaveMenuItem(List<RegexEntity> regexEntities) {

String fileName = Utils.selectFile(options, false);

if (Objects.isNull(fileName)) return;

if (fileName.toUpperCase().endsWith("JSON")) {
Utils.saveListToJSON(fileName, regexEntities);
} else if (fileName.toUpperCase().endsWith("CSV")) {
Expand All @@ -279,8 +276,6 @@ private JMenuItem createListOpenMenuItem(RegexListContext ctx,

String fileName = Utils.selectFile(options, true);

if (Objects.isNull(fileName)) return;

if (fileName.toUpperCase().endsWith("JSON")) {
Utils.openListFromJSON(fileName, ctx);
} else if (fileName.toUpperCase().endsWith("CSV")) {
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/com/cys4/sensitivediscoverer/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public static void saveToFile(String extensionName, List<String> lines) {
* Open JFileChooser to get a file name
*
* @param extensionNames the extensions to filter files
* @param openFile Set to true if the file should be opened, false if it should be saved
* @param openFile Set to true if the file should be opened, false if it should be saved
* @return The filename, or null if there was an error
*/
public static String selectFile(List<String> extensionNames, boolean openFile) {
Expand All @@ -103,22 +103,22 @@ public static String selectFile(List<String> extensionNames, boolean openFile) {
.forEachOrdered(fileChooser::addChoosableFileFilter);

//set window title to Open or Save
fileChooser.setDialogTitle(getLocaleString(openFile?
getLocaleString("utils-linesFromFile-importFile")
fileChooser.setDialogTitle(getLocaleString(openFile ?
"utils-linesFromFile-importFile"
: "utils-saveToFile-exportFile"));

//show the Open or Save window
int userSelection = openFile ?
fileChooser.showOpenDialog(parentFrame) :
fileChooser.showSaveDialog(parentFrame);

if (userSelection != JFileChooser.APPROVE_OPTION) return null;
if (userSelection != JFileChooser.APPROVE_OPTION) return "";

String exportFilePath = fileChooser.getSelectedFile().getAbsolutePath();

String selectedExt = fileChooser.getFileFilter().getDescription().toLowerCase();

return exportFilePath.toLowerCase().endsWith(selectedExt) ? exportFilePath : exportFilePath+selectedExt;
return exportFilePath.toLowerCase().endsWith(selectedExt) ? exportFilePath : exportFilePath + selectedExt;
}

/**
Expand Down Expand Up @@ -235,7 +235,6 @@ public static void saveListToJSON(String jsonFile, List<RegexEntity> regexEntiti
}).getType();

writeLinesToFile(jsonFile, List.of(gson.toJson(lines, tListEntries)));

}

public static void openListFromCSV(String csvFile, RegexListContext ctx) {
Expand Down

0 comments on commit 80879f9

Please sign in to comment.