Replace FileSelectedEvent with OpenFileEvent

This commit is contained in:
Oystein Kristoffer Tveit 2021-04-22 21:49:35 +02:00
parent 02c738e761
commit 581bb0ad3b
7 changed files with 24 additions and 60 deletions

View File

@ -26,7 +26,6 @@ import app.events.ToggleCommentEvent;
import app.events.ToggleWrapTextEvent; import app.events.ToggleWrapTextEvent;
import app.events.UndoEvent; import app.events.UndoEvent;
import app.events.FileSaveStateChangedEvent; import app.events.FileSaveStateChangedEvent;
import app.events.FileSelectedEvent;
import app.model.Model; import app.model.Model;
import app.service.FileOperations; import app.service.FileOperations;
import app.service.LanguageOperations; import app.service.LanguageOperations;
@ -181,7 +180,7 @@ public class EditorController implements Initializable, Controller {
* Updates Code Area (read from file) whenever the FileSelected is changed * Updates Code Area (read from file) whenever the FileSelected is changed
*/ */
@Subscribe @Subscribe
public void handle(FileSelectedEvent event) { public void handle(OpenFileEvent event) {
String newContent = String newContent =
event event
.getPath() .getPath()

View File

@ -16,7 +16,7 @@ import java.util.ResourceBundle;
import com.google.common.eventbus.EventBus; import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe; import com.google.common.eventbus.Subscribe;
import app.events.FileSelectedEvent; import app.events.OpenFileEvent;
import app.events.OpenProjectEvent; import app.events.OpenProjectEvent;
import app.events.SaveFileEvent; import app.events.SaveFileEvent;
import app.model.Model; import app.model.Model;
@ -77,7 +77,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)) {
this.eventBus.post(new FileSelectedEvent(Optional.ofNullable(path))); this.eventBus.post(new OpenFileEvent(Optional.ofNullable(path)));
} }
} }
} }

View File

@ -12,8 +12,8 @@ import com.google.common.eventbus.Subscribe;
import app.events.CopyEvent; import app.events.CopyEvent;
import app.events.CutEvent; import app.events.CutEvent;
import app.events.ExitApplicationEvent; import app.events.ExitApplicationEvent;
import app.events.FileSelectedEvent;
import app.events.LanguageChangedEvent; import app.events.LanguageChangedEvent;
import app.events.OpenFileEvent;
import app.events.OpenLinkInBrowserEvent; import app.events.OpenLinkInBrowserEvent;
import app.events.OpenProjectEvent; import app.events.OpenProjectEvent;
import app.events.PasteEvent; import app.events.PasteEvent;
@ -65,7 +65,7 @@ public class MenubarController implements Initializable, Controller {
/* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */
@FXML @FXML
private void handleNewFile() { private void handleNewFile() {
this.eventBus.post(new FileSelectedEvent(Optional.empty())); this.eventBus.post(new OpenFileEvent(Optional.empty()));
} }
@FXML @FXML
@ -87,7 +87,7 @@ public class MenubarController implements Initializable, Controller {
try { try {
File file = FileOperations.openFileWithDialog(stage); File file = FileOperations.openFileWithDialog(stage);
this.eventBus.post(new FileSelectedEvent(Optional.ofNullable(file.toPath()))); this.eventBus.post(new OpenFileEvent(Optional.ofNullable(file.toPath())));
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
DialogBoxes.showErrorMessage("File not found!"); DialogBoxes.showErrorMessage("File not found!");
} }
@ -105,9 +105,7 @@ public class MenubarController implements Initializable, Controller {
// TODO: Move Model modification inside event // TODO: Move Model modification inside event
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!");
}
} }
/* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */
@ -127,7 +125,7 @@ public class MenubarController implements Initializable, Controller {
*/ */
@FXML @FXML
private void handleSaveAsFile() { private void handleSaveAsFile() {
handleSaveFile(); this.eventBus.post(new SaveFileEvent(false));
} }
/** /**

View File

@ -1,36 +0,0 @@
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.
*
* Not to be confused with {@link OpenFileEvent}
*/
public class FileSelectedEvent extends Event {
private Optional<Path> path;
/**
* Event signalizing that a file was selected in the filetree.
*
* Not to be confused with {@link OpenFileEvent}
* @param path The path of the selected file
*/
public FileSelectedEvent(Optional<Path> path) {
this.path = path;
Model.setActiveFilePath(path);
}
/**
* @return The path of the selected file
*/
public Optional<Path> getPath() {
return this.path;
}
}

View File

@ -7,8 +7,6 @@ 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.
*
* Not to be confused with {@link FileSelectedEvent}
*/ */
public class OpenFileEvent extends Event { public class OpenFileEvent extends Event {

View File

@ -8,15 +8,20 @@ public class SaveFileEvent extends Event {
/** /**
* Event signalizing that a file is to be saved. * Event signalizing that a file is to be saved.
*
* Not to be confused with {@link FileSelectedEvent}
*
* @param isNewFile The path of the selected file
*/ */
public SaveFileEvent() { public SaveFileEvent() {
this.isNewFile = Model.getActiveFilePath().isEmpty(); this.isNewFile = Model.getActiveFilePath().isEmpty();
} }
/**
* Event signalizing that a file is to be saved.
*
* @param isNewFile The path of the selected file
*/
public SaveFileEvent(boolean isNewFile) {
this.isNewFile = isNewFile;
}
/** /**
* @return Whether or not * @return Whether or not
*/ */

View File

@ -38,7 +38,7 @@ import app.service.FileOperations;
import app.service.LanguageOperations; import app.service.LanguageOperations;
import app.events.CopyEvent; import app.events.CopyEvent;
import app.events.CutEvent; import app.events.CutEvent;
import app.events.FileSelectedEvent; import app.events.OpenFileEvent;
import app.events.LanguageChangedEvent; import app.events.LanguageChangedEvent;
import app.events.PasteEvent; import app.events.PasteEvent;
import app.events.RedoEvent; import app.events.RedoEvent;
@ -80,8 +80,8 @@ public class EditorControllerTest extends FxTestTemplate {
} }
@Test @Test
@DisplayName("Test handling of FileSelectedEvent with a real file") @DisplayName("Test handling of OpenFileEvent with a real file")
public void testFileSelectedEventWithRealFile() throws IOException { public void testOpenFileEventWithRealFile() throws IOException {
String resourcePath = "/testfile.txt"; String resourcePath = "/testfile.txt";
String filePath = getClass().getResource(resourcePath).getPath(); String filePath = getClass().getResource(resourcePath).getPath();
@ -91,18 +91,18 @@ public class EditorControllerTest extends FxTestTemplate {
mocked.when(() -> FileOperations.readFile(any())) mocked.when(() -> FileOperations.readFile(any()))
.thenReturn(""); .thenReturn("");
eventBus.post(new FileSelectedEvent(Optional.of(file.toPath()))); eventBus.post(new OpenFileEvent(Optional.of(file.toPath())));
mocked.verify(() -> FileOperations.readFile(any())); mocked.verify(() -> FileOperations.readFile(any()));
} }
} }
@Test @Test
@DisplayName("Test handling of FileSelectedEvent with a file that doesn't exist") @DisplayName("Test handling of OpenFileEvent with a file that doesn't exist")
public void testFileSelectedEventWithUnrealFile() throws IOException { public void testOpenFileEventWithUnrealFile() throws IOException {
String brokenFilePath = "/doesNotExist.txt"; String brokenFilePath = "/doesNotExist.txt";
eventBus.post(new FileSelectedEvent(Optional.ofNullable(new File(brokenFilePath).toPath()))); eventBus.post(new OpenFileEvent(Optional.ofNullable(new File(brokenFilePath).toPath())));
verify(editor, never()).clear(); verify(editor, never()).clear();
} }