exit dialog

This commit is contained in:
Oystein 2021-04-12 10:51:53 +02:00
parent 1156b827ac
commit fcab3df9fc
4 changed files with 26 additions and 57 deletions

View File

@ -4,6 +4,8 @@ import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import javax.swing.JOptionPane;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
@ -66,6 +68,7 @@ public class MainController implements Initializable {
/**
* Set a reference to the global Host Services API
*
* @param hostServices The JavaFX HostServices object
* @see #getHostServices()
*/
@ -75,6 +78,7 @@ public class MainController implements Initializable {
/**
* Replace a CSS file in a specific location in the application CSS array
*
* @param position The position of the CSS file to replace
* @param cssPath The path in resources to the new CSS file
*/
@ -87,6 +91,7 @@ public class MainController implements Initializable {
/**
* Change the CSS according to which language is being used
*
* @param event
*/
@Subscribe
@ -96,6 +101,7 @@ public class MainController implements Initializable {
/**
* Change the CSS according to which theme the user chooses
*
* @param event
*/
@Subscribe
@ -105,6 +111,7 @@ public class MainController implements Initializable {
/**
* Open a link in the browser.
*
* @param event
*/
@Subscribe
@ -113,14 +120,24 @@ public class MainController implements Initializable {
}
/**
* Handle an exit request for the whole program
* Handle an exit request for the whole program Checking if all is saved before
* closing the app. The user can either choose to exit or go back to the
* application and save.
*
* @param event
*/
@Subscribe
private void handle(ExitApplicationEvent event) {
// TODO: send save file event, exit application safely.
if (!Model.getFileIsSaved()) {
int g = JOptionPane.showConfirmDialog(null, "Your files are not saved.\nSave before you exit?", "Exit",
JOptionPane.YES_NO_OPTION);
if (!(g == JOptionPane.YES_OPTION)) {
Platform.exit();
}
} else {
Platform.exit();
}
}
}

View File

@ -8,9 +8,6 @@ import java.util.Collection;
import java.util.ResourceBundle;
import java.util.Scanner;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
@ -24,7 +21,7 @@ import org.fxmisc.richtext.model.TwoDimensional.Position;
import app.events.CopyEvent;
import app.events.CutEvent;
import app.events.EditorChangedEvent;
import app.events.ExitApplicationEvent;
import app.events.LanguageChangedEvent;
import app.events.PasteEvent;
import app.events.RedoEvent;
@ -196,22 +193,9 @@ public class EditorController implements Initializable, Controller {
/**
* Checking if all is saved before closing the app. The user can either choose
* to exit or go back and save.
* to exit or go back to the application and save.
*/
private void exitApp(boolean exit) {
if (!exit) {
int g = JOptionPane.showConfirmDialog(null, "Your files are not saved.\nSave before you exit?", "Exit",
JOptionPane.YES_NO_OPTION);
if (g == JOptionPane.YES_OPTION) {
// Got stuk here, to get back to "default" and keep condition of file that is
// not saved
}
}
}
/* ------------------------------------------------------------------------ */
/* SUBSCRIPTIONS */
/* ------------------------------------------------------------------------ */
@ -232,11 +216,6 @@ public class EditorController implements Initializable, Controller {
this.saveCodeArea(event.getPath());
}
@Subscribe
private void handle(ExitApplicationEvent event) {
this.exitApp(event.getIsSaved());
}
@Subscribe
private void handle(LanguageChangedEvent event) {
this.refreshHighlighting();

View File

@ -1,15 +1,11 @@
package app.controllers;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ResourceBundle;
import javax.swing.JOptionPane;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
@ -32,12 +28,10 @@ import app.model.Model;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.CheckMenuItem;
import javafx.scene.control.MenuBar;
import javafx.scene.control.RadioMenuItem;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.Alert.AlertType;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

View File

@ -1,29 +1,8 @@
package app.events;
import app.model.Model;
/**
* Event signalizing a shutdown request for the whole applicaton
*/
public class ExitApplicationEvent extends Event {
private boolean isSaved;
/**
* Event signalizing if a file is saved or modified.
*
*
*/
public ExitApplicationEvent() {
this.isSaved = Model.getFileIsSaved();
}
/**
* @return Whether or not the file has been modified or saved
*/
public boolean getIsSaved() {
return this.isSaved;
}
}