Skip to content

Commit 2068481

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

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
@@ -1177,15 +1177,13 @@ private Map<String, List<Path>> getLibraryDependencies(Path lib) {
11771177
}
11781178

11791179
private void deleteSelectedItems(Event event) {
1180-
List<TreeItem<Item>> selectedItems = fileSystemView
1180+
List<TreeItem<Item>> selectedItems = new ArrayList<>(fileSystemView
11811181
.getSelectionModel()
1182-
.getSelectedItems()
1183-
.stream()
1184-
.collect(Collectors.toList());
1182+
.getSelectedItems());
11851183

11861184
List<Path> pathList = selectedItems.stream()
1187-
.map(e -> e.getValue())
1188-
.map(e -> e.getPath())
1185+
.map(TreeItem::getValue)
1186+
.map(Item::getPath)
11891187
.collect(Collectors.toList());
11901188

11911189
AlertHelper.deleteAlert(pathList).ifPresent(btn -> {
@@ -2831,6 +2829,17 @@ public void error(String message) {
28312829
logger.error(message.replace("\\\"","").replace("\"",""));
28322830
}
28332831

2832+
/**
2833+
* Get tñe path to a selected item in tñe view or to tñe workspace if no item is selected
2834+
* @return The selected path or workspace (or an empty optional if neither is set)
2835+
*/
2836+
private Optional<Path> getSelectedItemOrWorkspacePath() {
2837+
TreeItem<Item> selection = fileSystemView.getSelectionModel().getSelectedItem();
2838+
return Optional.ofNullable(selection)
2839+
.map(s -> s.getValue().getPath())
2840+
.or(() -> directoryService.getWorkingDirectory());
2841+
}
2842+
28342843
@FXML
28352844
public void createFolder(ActionEvent actionEvent) {
28362845

@@ -2843,16 +2852,14 @@ public void createFolder(ActionEvent actionEvent) {
28432852

28442853
if (result.matches(DialogBuilder.FOLDER_NAME_REGEX)) {
28452854

2846-
Path path = fileSystemView.getSelectionModel().getSelectedItem()
2847-
.getValue().getPath();
2855+
Path path = getSelectedItemOrWorkspacePath().orElseThrow(() -> {
2856+
throw new IllegalStateException("Can't add a folder without a workspace or a selected item in the view");
2857+
});
28482858

28492859
Path folderPath = path.resolve(result);
28502860

28512861
threadService.runTaskLater(() -> {
28522862
IOHelper.createDirectories(folderPath);
2853-
// threadService.runActionLater(() -> {
2854-
// directoryService.changeWorkigDir(folderPath);
2855-
// });
28562863
});
28572864
}
28582865
};
@@ -2876,14 +2883,17 @@ public void createFile(ActionEvent actionEvent) {
28762883

28772884
if (result.matches(DialogBuilder.FILE_NAME_REGEX)) {
28782885

2879-
Path path = fileSystemView.getSelectionModel().getSelectedItem()
2880-
.getValue().getPath();
2886+
Path path = getSelectedItemOrWorkspacePath().orElseThrow(() -> {
2887+
throw new IllegalStateException("Can't add a file without a workspace or a selected item in the view");
2888+
});
28812889

28822890
IOHelper.createDirectories(path);
28832891
Optional<Exception> exception = IOHelper.writeToFile(path.resolve(result), "", CREATE_NEW, WRITE);
28842892

28852893
if (!exception.isPresent()) {
28862894
tabService.addTab(path.resolve(result));
2895+
} else {
2896+
logger.debug(exception.get().getMessage(), exception);
28872897
}
28882898
}
28892899
};
@@ -2934,13 +2944,6 @@ public void renameFile(Event actionEvent) {
29342944
dialog.showAndWait().ifPresent(consumer);
29352945
}
29362946

2937-
/*
2938-
@WebkitCall
2939-
public void fillModeList(String mode) {
2940-
threadService.runActionLater(() -> {
2941-
modeList.add(mode);
2942-
});
2943-
}*/
29442947
public void clearImageCache(Path imagePath) {
29452948
rightShowerHider.getShowing()
29462949
.ifPresent(w -> w.clearImageCache(imagePath));
@@ -2987,8 +2990,9 @@ public void newSlide(ActionEvent actionEvent) {
29872990

29882991
if (folderName.matches(DialogBuilder.FOLDER_NAME_REGEX)) {
29892992

2990-
Path path = fileSystemView.getSelectionModel().getSelectedItem()
2991-
.getValue().getPath();
2993+
Path path = getSelectedItemOrWorkspacePath().orElseThrow(() -> {
2994+
throw new IllegalStateException("Can't add a slide without a workspace or a selected item in the view");
2995+
});
29922996

29932997
Path folderPath = path.resolve(folderName);
29942998

0 commit comments

Comments
 (0)