Make nullable paths explicitly nullable

master
Oystein Kristoffer Tveit 2021-04-20 23:42:33 +02:00
parent c865a38349
commit d63a5d90bf
10 changed files with 57 additions and 46 deletions

View File

@ -146,8 +146,7 @@ public class EditorController implements Initializable, Controller {
*
* @param filePath The path of the file
*/
public void setEditorContent(Path filePath) {
String newContent = FileOperations.readFile(filePath);
public void setEditorContent(String newContent) {
editor.clear();
editor.appendText(newContent);
}
@ -182,7 +181,12 @@ public class EditorController implements Initializable, Controller {
*/
@Subscribe
public void handle(FileSelectedEvent event) {
this.setEditorContent(event.getPath());
String newContent =
event
.getPath()
.map(path -> FileOperations.readFile(path))
.orElse("");
this.setEditorContent(newContent);
}
/**

View File

@ -10,6 +10,7 @@ import java.io.File;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
import java.util.ResourceBundle;
import com.google.common.eventbus.EventBus;
@ -75,9 +76,7 @@ public class FiletreeController implements Initializable, Controller {
Path path = FiletreeOperations.getPathOfTreeItem(item);
if (!Files.isDirectory(path)) {
// TODO: Add setActiveFilePath Model modification into FileSelectedEvent
Model.setActiveFilePath(path);
this.eventBus.post(new FileSelectedEvent(path));
this.eventBus.post(new FileSelectedEvent(Optional.ofNullable(path)));
}
}
}

View File

@ -3,6 +3,7 @@ package app.controllers;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.Optional;
import java.util.ResourceBundle;
import com.google.common.eventbus.EventBus;
@ -33,6 +34,7 @@ import javafx.scene.control.MenuBar;
import javafx.scene.control.RadioMenuItem;
import javafx.scene.control.ToggleGroup;
import javafx.stage.Stage;
import java.util.Optional;
/**
* A FXML controller that controls the menubar component of the UI
@ -65,9 +67,7 @@ public class MenubarController implements Initializable, Controller {
/* ------------------------------------------------------------------------ */
@FXML
private void handleNewFile() {
// TODO: Move Model modification inside event
Model.setActiveFilePath(null);
this.eventBus.post(new FileSelectedEvent(null));
this.eventBus.post(new FileSelectedEvent(Optional.empty()));
}
@FXML
@ -89,9 +89,7 @@ public class MenubarController implements Initializable, Controller {
try {
File file = FileOperations.openFileWithDialog(stage);
// TODO: Move Model modification inside event
Model.setActiveFilePath(file.toPath());
this.eventBus.post(new FileSelectedEvent(file.toPath()));
this.eventBus.post(new FileSelectedEvent(Optional.ofNullable(file.toPath())));
} catch (FileNotFoundException e) {
DialogBoxes.showErrorMessage("File not found!");
}
@ -108,7 +106,6 @@ public class MenubarController implements Initializable, Controller {
File dir = FileOperations.openFolderWithDialog(stage);
// TODO: Move Model modification inside event
Model.setProjectPath(dir.toPath());
this.eventBus.post(new OpenProjectEvent(dir.toPath()));
} catch (FileNotFoundException e) {
DialogBoxes.showErrorMessage("Folder not found!");

View File

@ -2,6 +2,10 @@ package app.events;
import java.nio.file.Path;
import java.util.Optional;
import app.model.Model;
/**
* Event signalizing that a file was selected in the filetree.
*
@ -9,7 +13,7 @@ import java.nio.file.Path;
*/
public class FileSelectedEvent extends Event {
private Path path;
private Optional<Path> path;
/**
* Event signalizing that a file was selected in the filetree.
@ -17,14 +21,15 @@ public class FileSelectedEvent extends Event {
* Not to be confused with {@link OpenFileEvent}
* @param path The path of the selected file
*/
public FileSelectedEvent(Path path) {
public FileSelectedEvent(Optional<Path> path) {
this.path = path;
Model.setActiveFilePath(path);
}
/**
* @return The path of the selected file
*/
public Path getPath() {
public Optional<Path> getPath() {
return this.path;
}

View File

@ -1,6 +1,9 @@
package app.events;
import java.nio.file.Path;
import java.util.Optional;
import app.model.Model;
/**
* Event signalizing that a file outside the current project is supposed to be opened in the editor.
@ -9,21 +12,22 @@ import java.nio.file.Path;
*/
public class OpenFileEvent extends Event {
private Path path;
private Optional<Path> path;
/**
* Event signalizing that a file outside the current project is supposed to be opened in the editor.
* @param path The path of the file to be opened
*/
public OpenFileEvent(Path path) {
this.path = path;
}
/**
* Event signalizing that a file outside the current project is supposed to be opened in the editor.
* @param path The path of the file to be opened
*/
public OpenFileEvent(Optional<Path> path) {
this.path = path;
Model.setActiveFilePath(path);
}
/**
* @return The path of the file to be opened
*/
public Path getPath() {
return this.path;
}
/**
* @return The path of the file to be opened
*/
public Optional<Path> getPath() {
return this.path;
}
}

View File

@ -1,6 +1,9 @@
package app.events;
import java.nio.file.Path;
import java.util.Optional;
import app.model.Model;
/**
* Event signalizing that a folder is supposed to be opened in the filetree.
@ -15,6 +18,7 @@ public class OpenProjectEvent extends Event {
*/
public OpenProjectEvent(Path path) {
this.path = path;
Model.setProjectPath(Optional.of(path));
}
/**

View File

@ -1,6 +1,7 @@
package app.model;
import java.nio.file.Path;
import java.util.Optional;
import app.settings.SettingsProvider;
import javafx.scene.Scene;
@ -13,26 +14,26 @@ import javafx.scene.Scene;
*/
public class Model {
private static boolean fileIsSaved;
private static Path activeFilePath;
private static Path currentProjectPath;
private static Optional<Path> activeFilePath;
private static Optional<Path> currentProjectPath;
private static ProgrammingLanguage currentProgrammingLanguage;
private static String theme;
private static Scene scene;
private static SettingsProvider settings;
public static Path getActiveFilePath() {
public static Optional<Path> getActiveFilePath() {
return activeFilePath;
}
public static void setActiveFilePath(Path path) {
public static void setActiveFilePath(Optional<Path> path) {
Model.activeFilePath = path;
}
public static Path getProjectPath() {
public static Optional<Path> getProjectPath() {
return currentProjectPath;
}
public static void setProjectPath(Path path) {
public static void setProjectPath(Optional<Path> path) {
Model.currentProjectPath = path;
}

View File

@ -4,6 +4,7 @@ import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.nio.file.Path;
import java.util.Optional;
import java.util.Scanner;
import app.model.Model;
@ -64,8 +65,7 @@ public class FileOperations {
FileChooser fc = new FileChooser();
fc.setTitle("Save as");
if (Model.getProjectPath() != null)
fc.setInitialDirectory(Model.getProjectPath().toFile());
Model.getProjectPath().ifPresent(path -> fc.setInitialDirectory(path.toFile()));
FileChooser.ExtensionFilter extJava = new FileChooser.ExtensionFilter("Java", "*.java");
FileChooser.ExtensionFilter extMd = new FileChooser.ExtensionFilter("Markdown", "*.md");
@ -77,7 +77,7 @@ public class FileOperations {
return false;
if (saveFile(chosenLocation.getAbsolutePath(), content)) {
Model.setActiveFilePath(chosenLocation.toPath());
Model.setActiveFilePath(Optional.of(chosenLocation.toPath()));
return true;
}

View File

@ -130,7 +130,7 @@ public class FiletreeOperations {
}
public static Path getPathOfTreeItem(TreeItem<String> item) {
String root = Model.getProjectPath().getFileName().toString();
String root = Model.getProjectPath().orElseThrow().getFileName().toString();
String path = "";
while (!root.equals(item.getValue())) {
path = File.separator + item.getValue() + path;

View File

@ -2,9 +2,7 @@ package app.controllers;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Optional;
import org.fxmisc.richtext.CodeArea;
import org.fxmisc.richtext.model.StyleSpans;
@ -16,7 +14,6 @@ import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
@ -94,7 +91,7 @@ public class EditorControllerTest extends FxTestTemplate {
mocked.when(() -> FileOperations.readFile(any()))
.thenReturn("");
eventBus.post(new FileSelectedEvent(file.toPath()));
eventBus.post(new FileSelectedEvent(Optional.of(file.toPath())));
mocked.verify(() -> FileOperations.readFile(any()));
}
@ -105,7 +102,7 @@ public class EditorControllerTest extends FxTestTemplate {
public void testFileSelectedEventWithUnrealFile() throws IOException {
String brokenFilePath = "/doesNotExist.txt";
eventBus.post(new FileSelectedEvent(new File(brokenFilePath).toPath()));
eventBus.post(new FileSelectedEvent(Optional.ofNullable(new File(brokenFilePath).toPath())));
verify(editor, never()).clear();
}