-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add file extensions to open and save dialogs (#532)
* Add file extensions to Save As dialog * Include leading dots in file extensions for simplicity * Add file extensions to open mappings dialogs * Remove unused tinyMappingsFileChooser * Use the same file chooser for all mapping IO * Fix NPE by using Enigma directories as the default mapping format Fixes #533. * Fix code style * Allow .mappings extension for single Enigma files * gradlew.bat --------- Co-authored-by: NebelNidas <[email protected]>
- Loading branch information
1 parent
31b6e9e
commit 3030b64
Showing
6 changed files
with
197 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
enigma-swing/src/main/java/cuchaz/enigma/gui/util/ExtensionFileFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package cuchaz.enigma.gui.util; | ||
|
||
import java.io.File; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.StringJoiner; | ||
|
||
import javax.swing.JFileChooser; | ||
import javax.swing.filechooser.FileFilter; | ||
|
||
import cuchaz.enigma.translation.mapping.serde.MappingFormat; | ||
import cuchaz.enigma.utils.I18n; | ||
|
||
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, List<String> extensions) { | ||
this.formatName = formatName; | ||
this.extensions = extensions; | ||
} | ||
|
||
public List<String> getExtensions() { | ||
return extensions; | ||
} | ||
|
||
@Override | ||
public boolean accept(File f) { | ||
// Always accept directories so the user can see them. | ||
if (f.isDirectory()) { | ||
return true; | ||
} | ||
|
||
for (String extension : extensions) { | ||
if (f.getName().endsWith(extension)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
var joiner = new StringJoiner(", "); | ||
|
||
for (String extension : extensions) { | ||
joiner.add("*" + extension); | ||
} | ||
|
||
return I18n.translateFormatted("menu.file.mappings.file_filter", formatName, joiner.toString()); | ||
} | ||
|
||
/** | ||
* Sets up a file chooser with a mapping format. This method resets the choosable filters, | ||
* and adds and selects a new filter based on the provided mapping format. | ||
* | ||
* @param fileChooser the mapping format | ||
*/ | ||
public static void setupFileChooser(JFileChooser fileChooser, MappingFormat format) { | ||
// Remove previous custom filters. | ||
fileChooser.resetChoosableFileFilters(); | ||
|
||
if (format.getFileType().isDirectory()) { | ||
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); | ||
} else { | ||
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); | ||
String formatName = I18n.translate("mapping_format." + format.name().toLowerCase(Locale.ROOT)); | ||
var filter = new ExtensionFileFilter(formatName, format.getFileType().extensions()); | ||
// Add our new filter to the list... | ||
fileChooser.addChoosableFileFilter(filter); | ||
// ...and choose it as the default. | ||
fileChooser.setFileFilter(filter); | ||
} | ||
} | ||
|
||
/** | ||
* Fixes a missing file extension in a save file path when the selected filter | ||
* is an {@code ExtensionFileFilter}. | ||
* | ||
* @param fileChooser the file chooser to check | ||
* @return the fixed path | ||
*/ | ||
public static Path getSavePath(JFileChooser fileChooser) { | ||
Path savePath = fileChooser.getSelectedFile().toPath(); | ||
|
||
if (fileChooser.getFileFilter() instanceof ExtensionFileFilter extensionFilter) { | ||
// Check that the file name ends with the extension. | ||
String fileName = savePath.getFileName().toString(); | ||
boolean hasExtension = false; | ||
|
||
for (String extension : extensionFilter.getExtensions()) { | ||
if (fileName.endsWith(extension)) { | ||
hasExtension = true; | ||
break; | ||
} | ||
} | ||
|
||
if (!hasExtension) { | ||
String defaultExtension = extensionFilter.getExtensions().get(0); | ||
// If not, add the extension. | ||
savePath = savePath.resolveSibling(fileName + defaultExtension); | ||
// Store the adjusted file, so that it shows up properly | ||
// the next time this dialog is used. | ||
fileChooser.setSelectedFile(savePath.toFile()); | ||
} | ||
} | ||
|
||
return savePath; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters