Make nullable paths explicitly nullable
This commit is contained in:
parent
c865a38349
commit
d63a5d90bf
@ -146,8 +146,7 @@ public class EditorController implements Initializable, Controller {
|
|||||||
*
|
*
|
||||||
* @param filePath The path of the file
|
* @param filePath The path of the file
|
||||||
*/
|
*/
|
||||||
public void setEditorContent(Path filePath) {
|
public void setEditorContent(String newContent) {
|
||||||
String newContent = FileOperations.readFile(filePath);
|
|
||||||
editor.clear();
|
editor.clear();
|
||||||
editor.appendText(newContent);
|
editor.appendText(newContent);
|
||||||
}
|
}
|
||||||
@ -182,7 +181,12 @@ public class EditorController implements Initializable, Controller {
|
|||||||
*/
|
*/
|
||||||
@Subscribe
|
@Subscribe
|
||||||
public void handle(FileSelectedEvent event) {
|
public void handle(FileSelectedEvent event) {
|
||||||
this.setEditorContent(event.getPath());
|
String newContent =
|
||||||
|
event
|
||||||
|
.getPath()
|
||||||
|
.map(path -> FileOperations.readFile(path))
|
||||||
|
.orElse("");
|
||||||
|
this.setEditorContent(newContent);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -10,6 +10,7 @@ import java.io.File;
|
|||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.ResourceBundle;
|
import java.util.ResourceBundle;
|
||||||
|
|
||||||
import com.google.common.eventbus.EventBus;
|
import com.google.common.eventbus.EventBus;
|
||||||
@ -75,9 +76,7 @@ public class FiletreeController implements Initializable, Controller {
|
|||||||
Path path = FiletreeOperations.getPathOfTreeItem(item);
|
Path path = FiletreeOperations.getPathOfTreeItem(item);
|
||||||
|
|
||||||
if (!Files.isDirectory(path)) {
|
if (!Files.isDirectory(path)) {
|
||||||
// TODO: Add setActiveFilePath Model modification into FileSelectedEvent
|
this.eventBus.post(new FileSelectedEvent(Optional.ofNullable(path)));
|
||||||
Model.setActiveFilePath(path);
|
|
||||||
this.eventBus.post(new FileSelectedEvent(path));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package app.controllers;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.ResourceBundle;
|
import java.util.ResourceBundle;
|
||||||
|
|
||||||
import com.google.common.eventbus.EventBus;
|
import com.google.common.eventbus.EventBus;
|
||||||
@ -33,6 +34,7 @@ import javafx.scene.control.MenuBar;
|
|||||||
import javafx.scene.control.RadioMenuItem;
|
import javafx.scene.control.RadioMenuItem;
|
||||||
import javafx.scene.control.ToggleGroup;
|
import javafx.scene.control.ToggleGroup;
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A FXML controller that controls the menubar component of the UI
|
* A FXML controller that controls the menubar component of the UI
|
||||||
@ -65,9 +67,7 @@ public class MenubarController implements Initializable, Controller {
|
|||||||
/* ------------------------------------------------------------------------ */
|
/* ------------------------------------------------------------------------ */
|
||||||
@FXML
|
@FXML
|
||||||
private void handleNewFile() {
|
private void handleNewFile() {
|
||||||
// TODO: Move Model modification inside event
|
this.eventBus.post(new FileSelectedEvent(Optional.empty()));
|
||||||
Model.setActiveFilePath(null);
|
|
||||||
this.eventBus.post(new FileSelectedEvent(null));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
@ -89,9 +89,7 @@ public class MenubarController implements Initializable, Controller {
|
|||||||
try {
|
try {
|
||||||
File file = FileOperations.openFileWithDialog(stage);
|
File file = FileOperations.openFileWithDialog(stage);
|
||||||
|
|
||||||
// TODO: Move Model modification inside event
|
this.eventBus.post(new FileSelectedEvent(Optional.ofNullable(file.toPath())));
|
||||||
Model.setActiveFilePath(file.toPath());
|
|
||||||
this.eventBus.post(new FileSelectedEvent(file.toPath()));
|
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
DialogBoxes.showErrorMessage("File not found!");
|
DialogBoxes.showErrorMessage("File not found!");
|
||||||
}
|
}
|
||||||
@ -108,7 +106,6 @@ public class MenubarController implements Initializable, Controller {
|
|||||||
File dir = FileOperations.openFolderWithDialog(stage);
|
File dir = FileOperations.openFolderWithDialog(stage);
|
||||||
|
|
||||||
// TODO: Move Model modification inside event
|
// TODO: Move Model modification inside event
|
||||||
Model.setProjectPath(dir.toPath());
|
|
||||||
this.eventBus.post(new OpenProjectEvent(dir.toPath()));
|
this.eventBus.post(new OpenProjectEvent(dir.toPath()));
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
DialogBoxes.showErrorMessage("Folder not found!");
|
DialogBoxes.showErrorMessage("Folder not found!");
|
||||||
|
@ -2,6 +2,10 @@ package app.events;
|
|||||||
|
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import app.model.Model;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Event signalizing that a file was selected in the filetree.
|
* Event signalizing that a file was selected in the filetree.
|
||||||
*
|
*
|
||||||
@ -9,7 +13,7 @@ import java.nio.file.Path;
|
|||||||
*/
|
*/
|
||||||
public class FileSelectedEvent extends Event {
|
public class FileSelectedEvent extends Event {
|
||||||
|
|
||||||
private Path path;
|
private Optional<Path> path;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Event signalizing that a file was selected in the filetree.
|
* 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}
|
* Not to be confused with {@link OpenFileEvent}
|
||||||
* @param path The path of the selected file
|
* @param path The path of the selected file
|
||||||
*/
|
*/
|
||||||
public FileSelectedEvent(Path path) {
|
public FileSelectedEvent(Optional<Path> path) {
|
||||||
this.path = path;
|
this.path = path;
|
||||||
|
Model.setActiveFilePath(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return The path of the selected file
|
* @return The path of the selected file
|
||||||
*/
|
*/
|
||||||
public Path getPath() {
|
public Optional<Path> getPath() {
|
||||||
return this.path;
|
return this.path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package app.events;
|
package app.events;
|
||||||
|
|
||||||
import java.nio.file.Path;
|
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.
|
* Event signalizing that a file outside the current project is supposed to be opened in the editor.
|
||||||
@ -9,20 +12,21 @@ import java.nio.file.Path;
|
|||||||
*/
|
*/
|
||||||
public class OpenFileEvent extends Event {
|
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.
|
* 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
|
* @param path The path of the file to be opened
|
||||||
*/
|
*/
|
||||||
public OpenFileEvent(Path path) {
|
public OpenFileEvent(Optional<Path> path) {
|
||||||
this.path = path;
|
this.path = path;
|
||||||
|
Model.setActiveFilePath(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return The path of the file to be opened
|
* @return The path of the file to be opened
|
||||||
*/
|
*/
|
||||||
public Path getPath() {
|
public Optional<Path> getPath() {
|
||||||
return this.path;
|
return this.path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package app.events;
|
package app.events;
|
||||||
|
|
||||||
import java.nio.file.Path;
|
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.
|
* 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) {
|
public OpenProjectEvent(Path path) {
|
||||||
this.path = path;
|
this.path = path;
|
||||||
|
Model.setProjectPath(Optional.of(path));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package app.model;
|
package app.model;
|
||||||
|
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import app.settings.SettingsProvider;
|
import app.settings.SettingsProvider;
|
||||||
import javafx.scene.Scene;
|
import javafx.scene.Scene;
|
||||||
@ -13,26 +14,26 @@ import javafx.scene.Scene;
|
|||||||
*/
|
*/
|
||||||
public class Model {
|
public class Model {
|
||||||
private static boolean fileIsSaved;
|
private static boolean fileIsSaved;
|
||||||
private static Path activeFilePath;
|
private static Optional<Path> activeFilePath;
|
||||||
private static Path currentProjectPath;
|
private static Optional<Path> currentProjectPath;
|
||||||
private static ProgrammingLanguage currentProgrammingLanguage;
|
private static ProgrammingLanguage currentProgrammingLanguage;
|
||||||
private static String theme;
|
private static String theme;
|
||||||
private static Scene scene;
|
private static Scene scene;
|
||||||
private static SettingsProvider settings;
|
private static SettingsProvider settings;
|
||||||
|
|
||||||
public static Path getActiveFilePath() {
|
public static Optional<Path> getActiveFilePath() {
|
||||||
return activeFilePath;
|
return activeFilePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setActiveFilePath(Path path) {
|
public static void setActiveFilePath(Optional<Path> path) {
|
||||||
Model.activeFilePath = path;
|
Model.activeFilePath = path;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Path getProjectPath() {
|
public static Optional<Path> getProjectPath() {
|
||||||
return currentProjectPath;
|
return currentProjectPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setProjectPath(Path path) {
|
public static void setProjectPath(Optional<Path> path) {
|
||||||
Model.currentProjectPath = path;
|
Model.currentProjectPath = path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ import java.io.File;
|
|||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
import app.model.Model;
|
import app.model.Model;
|
||||||
@ -64,8 +65,7 @@ public class FileOperations {
|
|||||||
FileChooser fc = new FileChooser();
|
FileChooser fc = new FileChooser();
|
||||||
fc.setTitle("Save as");
|
fc.setTitle("Save as");
|
||||||
|
|
||||||
if (Model.getProjectPath() != null)
|
Model.getProjectPath().ifPresent(path -> fc.setInitialDirectory(path.toFile()));
|
||||||
fc.setInitialDirectory(Model.getProjectPath().toFile());
|
|
||||||
|
|
||||||
FileChooser.ExtensionFilter extJava = new FileChooser.ExtensionFilter("Java", "*.java");
|
FileChooser.ExtensionFilter extJava = new FileChooser.ExtensionFilter("Java", "*.java");
|
||||||
FileChooser.ExtensionFilter extMd = new FileChooser.ExtensionFilter("Markdown", "*.md");
|
FileChooser.ExtensionFilter extMd = new FileChooser.ExtensionFilter("Markdown", "*.md");
|
||||||
@ -77,7 +77,7 @@ public class FileOperations {
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (saveFile(chosenLocation.getAbsolutePath(), content)) {
|
if (saveFile(chosenLocation.getAbsolutePath(), content)) {
|
||||||
Model.setActiveFilePath(chosenLocation.toPath());
|
Model.setActiveFilePath(Optional.of(chosenLocation.toPath()));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,7 +130,7 @@ public class FiletreeOperations {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Path getPathOfTreeItem(TreeItem<String> item) {
|
public static Path getPathOfTreeItem(TreeItem<String> item) {
|
||||||
String root = Model.getProjectPath().getFileName().toString();
|
String root = Model.getProjectPath().orElseThrow().getFileName().toString();
|
||||||
String path = "";
|
String path = "";
|
||||||
while (!root.equals(item.getValue())) {
|
while (!root.equals(item.getValue())) {
|
||||||
path = File.separator + item.getValue() + path;
|
path = File.separator + item.getValue() + path;
|
||||||
|
@ -2,9 +2,7 @@ package app.controllers;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.util.Optional;
|
||||||
import java.util.List;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
import org.fxmisc.richtext.CodeArea;
|
import org.fxmisc.richtext.CodeArea;
|
||||||
import org.fxmisc.richtext.model.StyleSpans;
|
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.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
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.any;
|
||||||
import static org.mockito.ArgumentMatchers.anyInt;
|
import static org.mockito.ArgumentMatchers.anyInt;
|
||||||
import static org.mockito.ArgumentMatchers.anyString;
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
@ -94,7 +91,7 @@ public class EditorControllerTest extends FxTestTemplate {
|
|||||||
mocked.when(() -> FileOperations.readFile(any()))
|
mocked.when(() -> FileOperations.readFile(any()))
|
||||||
.thenReturn("");
|
.thenReturn("");
|
||||||
|
|
||||||
eventBus.post(new FileSelectedEvent(file.toPath()));
|
eventBus.post(new FileSelectedEvent(Optional.of(file.toPath())));
|
||||||
|
|
||||||
mocked.verify(() -> FileOperations.readFile(any()));
|
mocked.verify(() -> FileOperations.readFile(any()));
|
||||||
}
|
}
|
||||||
@ -105,7 +102,7 @@ public class EditorControllerTest extends FxTestTemplate {
|
|||||||
public void testFileSelectedEventWithUnrealFile() throws IOException {
|
public void testFileSelectedEventWithUnrealFile() throws IOException {
|
||||||
|
|
||||||
String brokenFilePath = "/doesNotExist.txt";
|
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();
|
verify(editor, never()).clear();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user