Ensure proper file name is parsed to character checker

This commit is contained in:
SiboVG 2023-03-23 03:35:32 +01:00
parent 605c92b325
commit 24de589f69

View File

@ -1,54 +1,74 @@
package net.sf.openrocket.gui.widgets; package net.sf.openrocket.gui.widgets;
import net.sf.openrocket.gui.main.BasicFrame;
import net.sf.openrocket.l10n.Translator; import net.sf.openrocket.l10n.Translator;
import net.sf.openrocket.startup.Application; import net.sf.openrocket.startup.Application;
import javax.swing.JFileChooser; import javax.swing.JFileChooser;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import java.awt.Component;
import java.awt.HeadlessException;
import java.io.File; import java.io.File;
public class SaveFileChooser extends JFileChooser { public class SaveFileChooser extends JFileChooser {
private static final Translator trans = Application.getTranslator(); private static final Translator trans = Application.getTranslator();
private static final char[] ILLEGAL_CHARS = new char[] { '/', '\\', ':', '*', '?', '"', '<', '>', '|' }; private static final char[] ILLEGAL_CHARS = new char[] { '/', '\\', ':', '*', '?', '"', '<', '>', '|' };
public static final int ILLEGAL_FILENAME_ERROR = 23111998;
private File cwd = null;
private File currentFile = null;
private String fileName = null;
@Override @Override
public int showSaveDialog(Component parent) throws HeadlessException { public void setSelectedFile(File file) {
int option = super.showSaveDialog(parent); currentFile = file;
if (option != JFileChooser.APPROVE_OPTION) { if (file != null) {
return option; if (file.getParentFile() != getCurrentDirectory()) {
cwd = getCurrentDirectory();
fileName = getFilenameInput(file);
return;
} else {
fileName = file.getName();
}
}
super.setSelectedFile(file);
} }
// Check for invalid characters @Override
File file = getSelectedFile(); public void approveSelection() {
if (file == null) { if (!containsIllegalChars(fileName)) {
return ERROR_OPTION; super.setSelectedFile(currentFile);
setCurrentDirectory(cwd);
super.approveSelection();
}
}
private boolean containsIllegalChars(String filename) {
if (filename == null || filename.isEmpty()) {
return false;
} }
String filename = file.getName();
for (char c : ILLEGAL_CHARS) { for (char c : ILLEGAL_CHARS) {
if (filename.indexOf(c) >= 0) { if (filename.indexOf(c) >= 0) {
// Illegal character found // Illegal character found
JOptionPane.showMessageDialog(parent, JOptionPane.showMessageDialog(getParent(),
String.format(trans.get("SaveAsFileChooser.illegalFilename.message"), filename, c), String.format(trans.get("SaveAsFileChooser.illegalFilename.message"), filename, c),
trans.get("SaveAsFileChooser.illegalFilename.title"), trans.get("SaveAsFileChooser.illegalFilename.title"),
JOptionPane.WARNING_MESSAGE); JOptionPane.WARNING_MESSAGE);
return true;
option = ILLEGAL_FILENAME_ERROR; }
}
// If the user entered an illegal filename, show the dialog again return false;
while (option == SaveFileChooser.ILLEGAL_FILENAME_ERROR) {
option = showSaveDialog(parent);
} }
return option; private String getFilenameInput(File file) {
} if (file == null) {
return null;
} }
return option; String fullPath = file.getAbsolutePath();
String cwdPath = cwd.getAbsolutePath();
String relativePath = fullPath.replaceFirst("^" + cwdPath, "").trim();
relativePath = relativePath.replaceFirst("^" + File.separator, "");
return relativePath;
} }
} }