(MappingFormat format)
| 213 | } |
| 214 | |
| 215 | private void saveMappings(MappingFormat format) { |
| 216 | Window window = gui.getScene().getWindow(); |
| 217 | Path path; |
| 218 | |
| 219 | if (format == null || format.hasSingleFile()) { |
| 220 | FileChooser fileChooser = new FileChooser(); |
| 221 | fileChooser.setTitle("Save mapping file"); |
| 222 | |
| 223 | for (MappingFormat f : MappingFormat.values()) { |
| 224 | if (f.hasSingleFile()) { |
| 225 | FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter(f.name, "*."+f.fileExt); |
| 226 | fileChooser.getExtensionFilters().add(filter); |
| 227 | |
| 228 | if (f == format) fileChooser.setSelectedExtensionFilter(filter); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | File file = fileChooser.showSaveDialog(window); |
| 233 | if (file == null) return; |
| 234 | |
| 235 | path = file.toPath(); |
| 236 | format = getFormat(fileChooser.getSelectedExtensionFilter().getDescription()); |
| 237 | } else { |
| 238 | path = Gui.requestDir("Save mapping dir", window); |
| 239 | if (path == null) return; |
| 240 | |
| 241 | if (Files.exists(path) && !isDirEmpty(path)) { // reusing existing dir, clear out after confirmation |
| 242 | if (!gui.requestConfirmation("Save Confirmation", "Replace existing data", "The selected save location is not empty.\nDo you want to clear and reuse it?")) return; |
| 243 | |
| 244 | try { |
| 245 | if (!Util.clearDir(path, file -> !Files.isDirectory(file) && !file.getFileName().toString().endsWith(".mapping"))) { |
| 246 | gui.showAlert(AlertType.ERROR, "Save error", "Error while preparing save location", "The target directory contains non-mapping files."); |
| 247 | return; |
| 248 | } |
| 249 | } catch (IOException e) { |
| 250 | e.printStackTrace(); |
| 251 | gui.showAlert(AlertType.ERROR, "Save error", "Error while preparing save location", e.getMessage()); |
| 252 | return; |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | if (format == null) { |
| 258 | format = getFormat(path); |
| 259 | if (format == null) throw new IllegalStateException("mapping format detection failed"); |
| 260 | |
| 261 | if (format.hasSingleFile()) { |
| 262 | path = path.resolveSibling(path.getFileName().toString()+"."+format.fileExt); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | if (Files.exists(path)) { |
| 267 | if (Files.isDirectory(path) != !format.hasSingleFile()) { |
| 268 | gui.showAlert(AlertType.ERROR, "Save error", "Invalid file selection", "The selected file is of the wrong type."); |
| 269 | return; |
| 270 | } |
| 271 | } |
| 272 |
no test coverage detected