Skip to content

Commit b107fe8

Browse files
committed
- More simplifications of error handling, some refactors
1 parent 6b09c02 commit b107fe8

File tree

9 files changed

+62
-69
lines changed

9 files changed

+62
-69
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,11 @@
172172
<list>java.net.URL</list>
173173
<list>stl.threebodysimulation.CanvasPanelFXMLController</list>
174174
<list>stl.threebodysimulation.CanvasWrapper</list>
175-
<list>stl.threebodysimulation.WarningMessage</list>
175+
<list>stl.threebodysimulation.FilenameSpecificMessage</list>
176176
<list>stl.threebodysimulation.DefaultSavePreviewFXMLController</list>
177177
<list>stl.threebodysimulation.DefaultTemplates</list>
178178
<list>stl.threebodysimulation.DesktopAPI</list>
179-
<list>stl.threebodysimulation.ErrorMessage</list>
179+
<list>stl.threebodysimulation.FilenameUnspecificMessage</list>
180180
<list>stl.threebodysimulation.InfoFXMLController</list>
181181
<list>stl.threebodysimulation.InfoPanelFXMLController</list>
182182
<list>stl.threebodysimulation.Launcher</list>

src/main/java/stl/threebodysimulation/CanvasPanelFXMLController.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public CanvasPanelFXMLController() {
135135
* @param filename The name of the CSV file to be setup
136136
* @return The path of the new CSV file.
137137
*/
138-
private static String setupCSV(String filename) {
138+
private String setupCSV(String filename) {
139139
String filepath = String.format(SceneFXMLController.CSVFilePathTemplate, filename);
140140
File CSVDirectory = new File(SceneFXMLController.CSV_DIRECTORY_NAME);
141141
File CSVFile = new File(filepath);
@@ -148,6 +148,10 @@ private static String setupCSV(String filename) {
148148
//noinspection ResultOfMethodCallIgnored : this should always return true, since it only runs if the file was successfully deleted.
149149
CSVFile.createNewFile(); // And try creating the new file again.
150150
} else {
151+
breakSimulation(new FilenameSpecificMessage(
152+
FilenameSpecificMessage.Type.OVERWRITE_ERROR,
153+
CSVFile.getAbsolutePath()
154+
));
151155
return "";
152156
}
153157
}
@@ -232,7 +236,6 @@ void runSimulation(SimulationSettings settings) {
232236
} else {
233237
CSVFilePath = setupCSV(settings.getCSVFileName());
234238
if (CSVFilePath.equals("")) {
235-
breakSimulation(ErrorMessage.OVERWRITE_ERROR);
236239
return;
237240
}
238241
}
@@ -257,22 +260,22 @@ void runSimulation(SimulationSettings settings) {
257260
} catch (NumberIsTooSmallException e) {
258261
// Asymptote error (the integrator can't converge and gives up)
259262
System.out.println(e.getMessage());
260-
breakSimulation(ErrorMessage.ASYMPTOTE_ERROR);
263+
breakSimulationAfterUpdate(FilenameUnspecificMessage.ASYMPTOTE_ERROR);
261264
return;
262265
} catch (NumberIsTooLargeException e) {
263266
// Double overflow error (inputs too large for double datatype to handle)
264267
System.out.println(e.getMessage());
265-
breakSimulation(ErrorMessage.OVERFLOW_ERROR);
268+
breakSimulationAfterUpdate(FilenameUnspecificMessage.OVERFLOW_ERROR);
266269
return;
267270
} catch (Exception e) {
268271
// Other errors
269272
System.out.println(e.getMessage());
270-
breakSimulation(ErrorMessage.UNKNOWN_ERROR);
273+
breakSimulationAfterUpdate(FilenameUnspecificMessage.UNKNOWN_ERROR);
271274
return;
272275
}
273276
}
274277

275-
double[][] scales = generateScale(particleDifferentialEquations, integrator, flattenedParticles.clone(), settings);
278+
double[][] scales = generateScale(integrator, flattenedParticles.clone(), settings);
276279

277280
updateParticles();
278281

@@ -305,13 +308,12 @@ void runSimulation(SimulationSettings settings) {
305308
/**
306309
* Calculates the maximum and minimum x/y coordinates during the first ten seconds of simulation.
307310
*
308-
* @param equations The differential equations.
309311
* @param integrator The integrator used.
310312
* @param particles The particles' initial states in flattened form.
311313
* @param settings The settings of the simulation.
312314
* @return The four corners of the smallest possible rectangle that all three particles do not escape in the first 10 seconds of simulation.
313315
*/
314-
private double[][] generateScale(ParticleDifferentialEquations equations, DormandPrince853Integrator integrator, double[] particles, SimulationSettings settings) {
316+
private double[][] generateScale(DormandPrince853Integrator integrator, double[] particles, SimulationSettings settings) {
315317
final int SIMULATION_LENGTH = 10;
316318
double simulationTime = currentTime;
317319

@@ -465,17 +467,17 @@ protected Void call() throws Exception {
465467
} catch (NumberIsTooSmallException e) {
466468
// Asymptote error catching
467469
System.out.println(e.getMessage());
468-
Platform.runLater(() -> breakSimulation(ErrorMessage.ASYMPTOTE_ERROR));
470+
Platform.runLater(() -> breakSimulationAfterUpdate(FilenameUnspecificMessage.ASYMPTOTE_ERROR));
469471
break;
470472
} catch (NumberIsTooLargeException e) {
471473
// Double overflow error catching.
472474
System.out.println(e.getMessage());
473-
Platform.runLater(() -> breakSimulation(ErrorMessage.OVERFLOW_ERROR));
475+
Platform.runLater(() -> breakSimulationAfterUpdate(FilenameUnspecificMessage.OVERFLOW_ERROR));
474476
break;
475477
} catch (Exception e) {
476478
// Other errors
477479
System.out.println(e.getMessage());
478-
Platform.runLater(() -> breakSimulation(ErrorMessage.UNKNOWN_ERROR));
480+
Platform.runLater(() -> breakSimulationAfterUpdate(FilenameUnspecificMessage.UNKNOWN_ERROR));
479481
break;
480482
}
481483

@@ -547,8 +549,12 @@ public void pausePressed() {
547549
*
548550
* @param errorMessage What error message to send to the user.
549551
*/
550-
private void breakSimulation(ErrorMessage errorMessage) {
552+
private void breakSimulationAfterUpdate(PopupMessage errorMessage) {
551553
updateAll();
554+
breakSimulation(errorMessage);
555+
}
556+
557+
private void breakSimulation(PopupMessage errorMessage) {
552558
stopPressed();
553559
SceneFXMLController.openErrorWindow(errorMessage, canvas.getScene().getWindow());
554560
}

src/main/java/stl/threebodysimulation/WarningMessage.java renamed to src/main/java/stl/threebodysimulation/FilenameSpecificMessage.java

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package stl.threebodysimulation;
22

33
/**
4-
* A class that provides a template for user confirmation messages.
4+
* A class that provides a template for popup messages that involve custom file names.
55
*/
6-
class WarningMessage implements PopupMessage {
6+
class FilenameSpecificMessage implements PopupMessage {
77

88
/**
99
* The title of the message.
@@ -15,28 +15,16 @@ class WarningMessage implements PopupMessage {
1515
private final String message;
1616

1717
/**
18-
* Constructs a new confirmation message with a filename. Should only be used for overwriting files.
18+
* Constructs a new popup message with a filename.
1919
*
2020
* @param type The type of message it is.
2121
* @param filepath The filepath that it points to.
2222
*/
23-
WarningMessage(Type type, String filepath) {
23+
FilenameSpecificMessage(Type type, String filepath) {
2424
title = type.getTitle();
2525
message = String.format(type.getMessageTemplate(), filepath);
2626
}
2727

28-
/**
29-
* Constructs a new confirmation message without a filename. Should only be used for non-filename confirmations.
30-
*
31-
* @param type The type of message it is.
32-
*/
33-
@SuppressWarnings("SameParameterValue")
34-
// In case other errors come up, this constructor will stay parameterized.
35-
WarningMessage(Type type) {
36-
title = type.getTitle();
37-
message = type.getMessageTemplate();
38-
}
39-
4028
/**
4129
* Gets the title of the message.
4230
*
@@ -98,17 +86,33 @@ String getMessageTemplate() {
9886
}
9987
},
10088
/**
101-
* For confirmations related to loading settings files.
89+
* An error that occurs if a file is unable to be deleted.
90+
*/
91+
DELETE_ERROR {
92+
@Override
93+
String getTitle() {
94+
return "Delete File Error";
95+
}
96+
97+
@Override
98+
String getMessageTemplate() {
99+
return "The following file could not be deleted:\n'%s'\nIt might be open in another window.";
100+
}
101+
},
102+
/**
103+
* An error that occurs when the user attempts to overwrite a CSV file that refuses to be edited.
102104
*/
103-
LOAD_CONFIRMATION {
105+
OVERWRITE_ERROR {
106+
@Override
104107
String getTitle() {
105-
return "Load Template Confirmation";
108+
return "Overwrite CSV Error";
106109
}
107110

111+
@Override
108112
String getMessageTemplate() {
109-
return "You are about to overwrite existing settings with a saved template. Are you sure you want to proceed?";
113+
return "The following file could not be edited:\n'%s'\nIt might be open in another window.";
110114
}
111-
};
115+
},;
112116

113117
/**
114118
* Returns the title of the message.

src/main/java/stl/threebodysimulation/ErrorMessage.java renamed to src/main/java/stl/threebodysimulation/FilenameUnspecificMessage.java

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
/**
55
* A class that represents error messages that can be shown in a popup.
66
*/
7-
enum ErrorMessage implements PopupMessage {
7+
enum FilenameUnspecificMessage implements PopupMessage {
88
/**
99
* An error that occurs when inputs for a simulation are incorrect.
1010
*/
@@ -62,31 +62,15 @@ public String getMessage() {
6262
}
6363
},
6464
/**
65-
* An error that occurs if a file is unable to be deleted.
65+
* For confirmations related to loading settings files.
6666
*/
67-
DELETE_ERROR {
68-
@Override
67+
LOAD_CONFIRMATION {
6968
public String getTitle() {
70-
return "Delete File Error";
69+
return "Load Template Confirmation";
7170
}
7271

73-
@Override
7472
public String getMessage() {
75-
return "The file cannot be deleted. It might be open in another window. Close all other instances of this app before trying again.";
76-
}
77-
},
78-
/**
79-
* An error that occurs when the user attempts to overwrite a CSV file that refuses to be edited.
80-
*/
81-
OVERWRITE_ERROR {
82-
@Override
83-
public String getTitle() {
84-
return "Overwrite CSV Error";
85-
}
86-
87-
@Override
88-
public String getMessage() {
89-
return "The CSV file you specified cannot be edited. It might be open in another window. Please close all other instances of this app and try again.";
73+
return "You are about to overwrite existing settings with a saved template. Are you sure you want to proceed?";
9074
}
9175
},
9276
/**
@@ -103,5 +87,4 @@ public String getMessage() {
10387
return "An unknown error occurred during the simulation. Please let the developers know, and try your simulation again.";
10488
}
10589
}
106-
10790
}

src/main/java/stl/threebodysimulation/SavePreviewFXMLController.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import javafx.fxml.FXML;
44
import javafx.scene.control.Button;
5-
import javafx.scene.control.Label;
6-
import javafx.scene.control.TitledPane;
75
import javafx.scene.paint.Color;
86
import org.kordamp.ikonli.material.Material;
97

src/main/java/stl/threebodysimulation/SavesPanelFXMLController.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,13 @@ private void showNoSavesMessage() {
228228
*/
229229
private void showDeleteConfirmation(File saveFile) {
230230
SceneFXMLController.openWarningWindow(
231-
new WarningMessage(WarningMessage.Type.DELETE_CONFIRMATION, saveFile.getAbsolutePath()),
231+
new FilenameSpecificMessage(FilenameSpecificMessage.Type.DELETE_CONFIRMATION, saveFile.getAbsolutePath()),
232232
savesBox.getScene().getWindow(),
233233
() -> {
234234
if (!saveFile.delete()) {
235-
SceneFXMLController.openErrorWindow(ErrorMessage.DELETE_ERROR, savesBox.getScene().getWindow());
235+
SceneFXMLController.openErrorWindow(
236+
new FilenameSpecificMessage(FilenameSpecificMessage.Type.DELETE_ERROR, saveFile.getAbsolutePath()),
237+
savesBox.getScene().getWindow());
236238
}
237239
refreshSaves();
238240
}
@@ -247,7 +249,7 @@ private void showDeleteConfirmation(File saveFile) {
247249
private void showLoadConfirmation(SimulationSettings settings) {
248250
this.settings = settings;
249251
SceneFXMLController.openWarningWindow(
250-
new WarningMessage(WarningMessage.Type.LOAD_CONFIRMATION),
252+
FilenameUnspecificMessage.LOAD_CONFIRMATION,
251253
savesBox.getScene().getWindow(),
252254
onLoadListener);
253255
}

src/main/java/stl/threebodysimulation/SceneFXMLController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public SceneFXMLController() {
117117
* @param message The error message to display.
118118
* @param parent The window that the error popup must block.
119119
*/
120-
static void openErrorWindow(ErrorMessage message, Window parent) {
120+
static void openErrorWindow(PopupMessage message, Window parent) {
121121
openPopupWindow(
122122
"/stl/threebodysimulation/layouts/errorWindowLayout.fxml",
123123
message,
@@ -134,7 +134,7 @@ static void openErrorWindow(ErrorMessage message, Window parent) {
134134
* @param parent The window that the warning popup must block.
135135
* @param confirmListener The listener that will be called if the user confirms.
136136
*/
137-
static void openWarningWindow(WarningMessage message, Window parent, Listener confirmListener) {
137+
static void openWarningWindow(PopupMessage message, Window parent, Listener confirmListener) {
138138
openPopupWindow(
139139
"/stl/threebodysimulation/layouts/warningWindowLayout.fxml",
140140
message,
@@ -228,7 +228,7 @@ private SettingsPanelFXMLController setupSettingsPanel() throws IOException {
228228

229229
panelController.setup(); // Set up settings panel.
230230
panelController.setOnRunSimulationListener(() -> runSimulation(settingsPanelController.getSimulationSettings())); // Sets up what happens when simulation is run.
231-
panelController.setOnRunErrorListener(() -> openErrorWindow(ErrorMessage.INPUT_ERROR, sceneLayout.getScene().getWindow())); // Sets up what happens when an error occurs.
231+
panelController.setOnRunErrorListener(() -> openErrorWindow(FilenameUnspecificMessage.INPUT_ERROR, sceneLayout.getScene().getWindow())); // Sets up what happens when an error occurs.
232232
panelController.setOnSaveTemplateListener(() -> {
233233
savesPanelController.refreshSaves();
234234
tabPane.getSelectionModel().select(1);

src/main/java/stl/threebodysimulation/SettingsPanelFXMLController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ private boolean CSVFileConflict() {
368368

369369
// If the user presses yes, we will restart the runSimulation method from the start (with forceCSV true this time).
370370
SceneFXMLController.openWarningWindow(
371-
new WarningMessage(WarningMessage.Type.CSV_CONFIRMATION, CSVFile.getAbsolutePath()),
371+
new FilenameSpecificMessage(FilenameSpecificMessage.Type.CSV_CONFIRMATION, CSVFile.getAbsolutePath()),
372372
settingsBox.getScene().getWindow(),
373373
() -> {
374374
forceCSV = true;
@@ -487,7 +487,7 @@ public void saveTemplate() {
487487
}
488488

489489
if (!readiness) {
490-
SceneFXMLController.openErrorWindow(ErrorMessage.SAVE_ERROR, settingsBox.getScene().getWindow());
490+
SceneFXMLController.openErrorWindow(FilenameUnspecificMessage.SAVE_ERROR, settingsBox.getScene().getWindow());
491491
return;
492492
}
493493

@@ -506,7 +506,7 @@ public void saveTemplate() {
506506
storeTemplate(settings, filepath);
507507
} else {
508508
SceneFXMLController.openWarningWindow(
509-
new WarningMessage(WarningMessage.Type.TEMPLATE_CONFIRMATION, saveFile.getAbsolutePath()),
509+
new FilenameSpecificMessage(FilenameSpecificMessage.Type.TEMPLATE_CONFIRMATION, saveFile.getAbsolutePath()),
510510
settingsBox.getScene().getWindow(),
511511
() -> {
512512
forceTemplateSave = true;

src/main/resources/stl/threebodysimulation/layouts/errorWindowLayout.fxml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@
3030
</font>
3131
</Label>
3232
</HBox>
33-
<Button fx:id="closeWindowButton" alignment="CENTER" mnemonicParsing="false" onAction="#closeWindow"
33+
<Button alignment="CENTER" mnemonicParsing="false" onAction="#closeWindow"
3434
prefHeight="40.0" styleClass="primary" stylesheets="@../styles/bootstrap3.css" text="OK"/>
3535
</VBox>

0 commit comments

Comments
 (0)