Fix some small bugs

This commit is contained in:
SiboVG 2023-03-23 11:14:33 +01:00
parent 24de589f69
commit f4ea3275eb

View File

@ -6,6 +6,8 @@ import net.sf.openrocket.startup.Application;
import javax.swing.JFileChooser; import javax.swing.JFileChooser;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import java.io.File; import java.io.File;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
public class SaveFileChooser extends JFileChooser { public class SaveFileChooser extends JFileChooser {
private static final Translator trans = Application.getTranslator(); private static final Translator trans = Application.getTranslator();
@ -20,55 +22,79 @@ public class SaveFileChooser extends JFileChooser {
@Override @Override
public void setSelectedFile(File file) { public void setSelectedFile(File file) {
currentFile = file; currentFile = file;
if (file != null) { if (file == null) {
if (file.getParentFile() != getCurrentDirectory()) { super.setSelectedFile(null);
cwd = getCurrentDirectory(); return;
fileName = getFilenameInput(file); }
if (file.getParentFile() != getCurrentDirectory()) {
cwd = getCurrentDirectory();
fileName = getFilenameInput(currentFile, cwd);
if (getIllegalChar(fileName) != null) {
return; return;
} else {
fileName = file.getName();
} }
} }
super.setSelectedFile(file); super.setSelectedFile(file);
fileName = file.getName();
cwd = getCurrentDirectory();
} }
@Override @Override
public void approveSelection() { public void approveSelection() {
if (!containsIllegalChars(fileName)) { Character c = getIllegalChar(fileName);
if (c != null) {
// Illegal character found
JOptionPane.showMessageDialog(getParent(),
String.format(trans.get("SaveAsFileChooser.illegalFilename.message"), fileName, c),
trans.get("SaveAsFileChooser.illegalFilename.title"),
JOptionPane.WARNING_MESSAGE);
} else {
// Successful filename
super.setSelectedFile(currentFile); super.setSelectedFile(currentFile);
setCurrentDirectory(cwd); setCurrentDirectory(cwd);
super.approveSelection(); super.approveSelection();
} }
} }
private boolean containsIllegalChars(String filename) { /**
* Returns an illegal character if one is found in the filename, otherwise returns null.
* @param filename The filename to check
* @return The illegal character, or null if none is found
*/
private Character getIllegalChar(String filename) {
if (filename == null || filename.isEmpty()) { if (filename == null || filename.isEmpty()) {
return false; return null;
} }
for (char c : ILLEGAL_CHARS) { for (char c : ILLEGAL_CHARS) {
if (filename.indexOf(c) >= 0) { if (filename.indexOf(c) >= 0) {
// Illegal character found return c;
JOptionPane.showMessageDialog(getParent(),
String.format(trans.get("SaveAsFileChooser.illegalFilename.message"), filename, c),
trans.get("SaveAsFileChooser.illegalFilename.title"),
JOptionPane.WARNING_MESSAGE);
return true;
} }
} }
return false; return null;
} }
private String getFilenameInput(File file) { /**
if (file == null) { * Returns the filename input by the user, or null if the filename is invalid.
* You can't simply use getSelectedFile().getName() because it won't work for malformed filenames.
* @param file The file to get the filename from
* @param cwd The current working directory
* @return The filename input by the user, or null if the filename is invalid
*/
private String getFilenameInput(File file, File cwd) {
if (file == null || cwd == null) {
return null; return null;
} }
String fullPath = file.getAbsolutePath(); String fullPath = file.getAbsolutePath();
String cwdPath = cwd.getAbsolutePath(); String cwdPath = cwd.getAbsolutePath();
String relativePath = fullPath.replaceFirst("^" + cwdPath, "").trim(); try {
relativePath = relativePath.replaceFirst("^" + File.separator, ""); String relativePath = fullPath.replaceFirst(Pattern.quote(cwdPath), "").trim();
relativePath = relativePath.replaceFirst(Pattern.quote(File.separator), "");
return relativePath; return relativePath;
} catch (PatternSyntaxException e) {
return null;
}
} }
} }