Skip to content

Commit

Permalink
Include leading dots in file extensions for simplicity
Browse files Browse the repository at this point in the history
  • Loading branch information
Juuxel committed Oct 5, 2023
1 parent f5cf8dc commit b7673ae
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -456,10 +456,11 @@ private static void prepareSaveMappingsAsMenu(JMenu saveMappingsAsMenu, JMenuIte
// Check that the file name ends with the extension.
String fileName = savePath.getFileName().toString();
String extension = format.getFileType().extension();
assert extension != null;

if (!fileName.endsWith("." + extension)) {
if (!fileName.endsWith(extension)) {
// If not, add the extension.
savePath = savePath.resolveSibling(fileName + "." + extension);
savePath = savePath.resolveSibling(fileName + extension);
// Store the adjusted file, so that it shows up properly
// the next time this dialog is used.
fileChooser.setSelectedFile(savePath.toFile());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ public final class ExtensionFileFilter extends FileFilter {
private final String formatName;
private final List<String> extensions;

/**
* Constructs an {@code ExtensionFileFilter}.
*
* @param formatName the human-readable name of the file format
* @param extensions the file extensions with their leading dots (e.g. {@code .txt})
*/
public ExtensionFileFilter(String formatName, String... extensions) {
this.formatName = formatName;
this.extensions = List.of(extensions);
Expand All @@ -25,7 +31,7 @@ public boolean accept(File f) {
}

for (String extension : extensions) {
if (f.getName().endsWith("." + extension)) {
if (f.getName().endsWith(extension)) {
return true;
}
}
Expand All @@ -38,7 +44,7 @@ public String getDescription() {
var joiner = new StringJoiner(", ");

for (String extension : extensions) {
joiner.add("*." + extension);
joiner.add("*" + extension);
}

return I18n.translateFormatted("menu.file.mappings.file_filter", formatName, joiner.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ public FileType getFileType() {
* A file type. It can be either a single file with an extension, or a directory
* with a {@code null} extension.
*
* @param extension the file extension without the trailing dot {@code .}, or {@code null} for a directory
* @param extension the file extension with the leading dot {@code .}, or {@code null} for a directory
*/
public record FileType(@Nullable String extension) {
public static final FileType DIRECTORY = new FileType(null);
public static final FileType MAPPING = new FileType("mapping");
public static final FileType SRG = new FileType("srg");
public static final FileType TINY = new FileType("tiny");
public static final FileType TXT = new FileType("txt");
public static final FileType ZIP = new FileType("zip");
public static final FileType MAPPING = new FileType(".mapping");
public static final FileType SRG = new FileType(".srg");
public static final FileType TINY = new FileType(".tiny");
public static final FileType TXT = new FileType(".txt");
public static final FileType ZIP = new FileType(".zip");

public boolean isDirectory() {
return extension == null;
Expand Down

0 comments on commit b7673ae

Please sign in to comment.