Skip to content

Commit ed35345

Browse files
committed
Add fallback to workspace when adding a new item in the tree view and improve error logging
1 parent 5648eb1 commit ed35345

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

src/main/java/com/kodedu/controller/ApplicationController.java

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,15 +1164,13 @@ private Map<String, List<Path>> getLibraryDependencies(Path lib) {
11641164
}
11651165

11661166
private void deleteSelectedItems(Event event) {
1167-
List<TreeItem<Item>> selectedItems = fileSystemView
1167+
List<TreeItem<Item>> selectedItems = new ArrayList<>(fileSystemView
11681168
.getSelectionModel()
1169-
.getSelectedItems()
1170-
.stream()
1171-
.collect(Collectors.toList());
1169+
.getSelectedItems());
11721170

11731171
List<Path> pathList = selectedItems.stream()
1174-
.map(e -> e.getValue())
1175-
.map(e -> e.getPath())
1172+
.map(TreeItem::getValue)
1173+
.map(Item::getPath)
11761174
.collect(Collectors.toList());
11771175

11781176
AlertHelper.deleteAlert(pathList).ifPresent(btn -> {
@@ -2818,6 +2816,17 @@ public void error(String message) {
28182816
logger.error(message.replace("\\\"","").replace("\"",""));
28192817
}
28202818

2819+
/**
2820+
* Get tñe path to a selected item in tñe view or to tñe workspace if no item is selected
2821+
* @return The selected path or workspace (or an empty optional if neither is set)
2822+
*/
2823+
private Optional<Path> getSelectedItemOrWorkspacePath() {
2824+
TreeItem<Item> selection = fileSystemView.getSelectionModel().getSelectedItem();
2825+
return Optional.ofNullable(selection)
2826+
.map(s -> s.getValue().getPath())
2827+
.or(() -> directoryService.getWorkingDirectory());
2828+
}
2829+
28212830
@FXML
28222831
public void createFolder(ActionEvent actionEvent) {
28232832

@@ -2830,16 +2839,14 @@ public void createFolder(ActionEvent actionEvent) {
28302839

28312840
if (result.matches(DialogBuilder.FOLDER_NAME_REGEX)) {
28322841

2833-
Path path = fileSystemView.getSelectionModel().getSelectedItem()
2834-
.getValue().getPath();
2842+
Path path = getSelectedItemOrWorkspacePath().orElseThrow(() -> {
2843+
throw new IllegalStateException("Can't add a folder without a workspace or a selected item in the view");
2844+
});
28352845

28362846
Path folderPath = path.resolve(result);
28372847

28382848
threadService.runTaskLater(() -> {
28392849
IOHelper.createDirectories(folderPath);
2840-
// threadService.runActionLater(() -> {
2841-
// directoryService.changeWorkigDir(folderPath);
2842-
// });
28432850
});
28442851
}
28452852
};
@@ -2863,14 +2870,17 @@ public void createFile(ActionEvent actionEvent) {
28632870

28642871
if (result.matches(DialogBuilder.FILE_NAME_REGEX)) {
28652872

2866-
Path path = fileSystemView.getSelectionModel().getSelectedItem()
2867-
.getValue().getPath();
2873+
Path path = getSelectedItemOrWorkspacePath().orElseThrow(() -> {
2874+
throw new IllegalStateException("Can't add a file without a workspace or a selected item in the view");
2875+
});
28682876

28692877
IOHelper.createDirectories(path);
28702878
Optional<Exception> exception = IOHelper.writeToFile(path.resolve(result), "", CREATE_NEW, WRITE);
28712879

28722880
if (!exception.isPresent()) {
28732881
tabService.addTab(path.resolve(result));
2882+
} else {
2883+
logger.debug(exception.get().getMessage(), exception);
28742884
}
28752885
}
28762886
};
@@ -2921,13 +2931,6 @@ public void renameFile(Event actionEvent) {
29212931
dialog.showAndWait().ifPresent(consumer);
29222932
}
29232933

2924-
/*
2925-
@WebkitCall
2926-
public void fillModeList(String mode) {
2927-
threadService.runActionLater(() -> {
2928-
modeList.add(mode);
2929-
});
2930-
}*/
29312934
public void clearImageCache(Path imagePath) {
29322935
rightShowerHider.getShowing()
29332936
.ifPresent(w -> w.clearImageCache(imagePath));
@@ -2974,8 +2977,9 @@ public void newSlide(ActionEvent actionEvent) {
29742977

29752978
if (folderName.matches(DialogBuilder.FOLDER_NAME_REGEX)) {
29762979

2977-
Path path = fileSystemView.getSelectionModel().getSelectedItem()
2978-
.getValue().getPath();
2980+
Path path = getSelectedItemOrWorkspacePath().orElseThrow(() -> {
2981+
throw new IllegalStateException("Can't add a slide without a workspace or a selected item in the view");
2982+
});
29792983

29802984
Path folderPath = path.resolve(folderName);
29812985

0 commit comments

Comments
 (0)