Extract logic from classes

This commit is contained in:
2021-04-20 09:28:45 +00:00
parent 7d40b2608d
commit 77ddb56d69
14 changed files with 416 additions and 249 deletions

View File

@@ -1,9 +1,8 @@
package app.controllers;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ResourceBundle;
import com.google.common.eventbus.EventBus;
@@ -12,7 +11,6 @@ import com.google.common.eventbus.Subscribe;
import app.events.CopyEvent;
import app.events.CutEvent;
import app.events.ExitApplicationEvent;
import app.events.FileSaveStateChangedEvent;
import app.events.FileSelectedEvent;
import app.events.LanguageChangedEvent;
import app.events.OpenLinkInBrowserEvent;
@@ -25,6 +23,8 @@ import app.events.ToggleCommentEvent;
import app.events.ToggleWrapTextEvent;
import app.events.UndoEvent;
import app.model.Model;
import app.service.DialogBoxes;
import app.service.FileOperations;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
@@ -32,8 +32,6 @@ import javafx.scene.control.CheckMenuItem;
import javafx.scene.control.MenuBar;
import javafx.scene.control.RadioMenuItem;
import javafx.scene.control.ToggleGroup;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
/**
@@ -50,9 +48,7 @@ public class MenubarController implements Initializable, Controller {
private ToggleGroup languageToggleGroup;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
// TODO: implement
}
public void initialize(URL url, ResourceBundle resourceBundle) {}
@Override
public void setEventBus(EventBus eventBus) {
@@ -65,6 +61,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));
}
@@ -86,18 +83,17 @@ public class MenubarController implements Initializable, Controller {
*/
@FXML
public void handleOpenFile() {
FileChooser fc = new FileChooser();
fc.setTitle("Open File");
Stage stage = (Stage) menubar.getScene().getWindow();
File chosenFile = fc.showOpenDialog(stage);
if (chosenFile != null) {
String correctFormat = chosenFile.getAbsolutePath().replace("\\", "\\\\");
try {
File file = FileOperations.openFileWithDialog(stage);
Model.setActiveFilePath(chosenFile.toPath());
this.eventBus.post(new FileSelectedEvent(correctFormat));
// TODO: Move Model modification inside event
Model.setActiveFilePath(file.toPath());
this.eventBus.post(new FileSelectedEvent(file.toPath()));
} catch (FileNotFoundException e) {
DialogBoxes.showErrorMessage("File not found!");
}
}
/**
@@ -105,19 +101,17 @@ public class MenubarController implements Initializable, Controller {
*/
@FXML
private void handleOpenProject() {
DirectoryChooser dc = new DirectoryChooser();
dc.setTitle("Open Project");
Stage stage = (Stage) menubar.getScene().getWindow();
File chosenDir = dc.showDialog(stage);
if (chosenDir != null) {
String correctFormat = chosenDir.getAbsolutePath().replace("\\", "\\\\");
Model.setProjectPath(chosenDir.toPath());
this.eventBus.post(new OpenProjectEvent(correctFormat));
try {
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!");
}
}
/* ------------------------------------------------------------------------ */
@@ -129,12 +123,10 @@ public class MenubarController implements Initializable, Controller {
*/
@FXML
private void handleSaveFile() {
if (Model.getActiveFilePath() != null) {
this.eventBus.post(new SaveFileEvent(Model.getActiveFilePath().toString()));
this.eventBus.post(new FileSaveStateChangedEvent(true));
} else {
if (Model.getActiveFilePath() != null)
this.eventBus.post(new SaveFileEvent(false));
else // New file needs a path
handleSaveAsFile();
}
}
/**
@@ -142,29 +134,7 @@ public class MenubarController implements Initializable, Controller {
*/
@FXML
private void handleSaveAsFile() {
FileChooser fc = new FileChooser();
fc.setTitle("Save as");
if (Model.getProjectPath() != null) {
fc.setInitialDirectory(Model.getProjectPath().toFile());
}
Stage stage = (Stage) menubar.getScene().getWindow();
FileChooser.ExtensionFilter extJava = new FileChooser.ExtensionFilter("Java", "*.java");
FileChooser.ExtensionFilter extMd = new FileChooser.ExtensionFilter("Markdown", "*.md");
fc.getExtensionFilters().addAll(extJava, extMd);
File chosenLocation = fc.showSaveDialog(stage);
if (chosenLocation != null) {
String chosenLocationToString = chosenLocation.toString();
this.eventBus.post(new SaveFileEvent(chosenLocationToString));
this.eventBus.post(new FileSelectedEvent(chosenLocationToString));
Path chosenLocationToPath = Paths.get(chosenLocationToString);
Model.setActiveFilePath(chosenLocationToPath);
this.eventBus.post(new FileSaveStateChangedEvent(true));
}
this.eventBus.post(new SaveFileEvent(true));
}
/**
@@ -274,8 +244,15 @@ public class MenubarController implements Initializable, Controller {
*/
@Subscribe
private void handle(LanguageChangedEvent event) {
this.languageToggleGroup.getToggles().stream().map(RadioMenuItem.class::cast)
.filter(t -> t.getId().equals("toggle" + event.getLanguage())).findFirst().orElseThrow().setSelected(true);
this.languageToggleGroup
.getToggles()
.stream()
.map(RadioMenuItem.class::cast)
.filter(t -> t.getId()
.equals("toggle" + event.getLanguage()))
.findFirst()
.orElseThrow()
.setSelected(true);
}
}