Skip to content

Commit 336a2f8

Browse files
committed
#178: show error dialog if something goes wrong when opening stages
1 parent 276632d commit 336a2f8

File tree

4 files changed

+56
-41
lines changed

4 files changed

+56
-41
lines changed

src/main/java/de/doubleslash/keeptime/App.java

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Optional;
2525
import java.util.stream.Collectors;
2626

27+
import javafx.stage.Window;
2728
import org.slf4j.Logger;
2829
import org.slf4j.LoggerFactory;
2930
import org.springframework.boot.SpringApplication;
@@ -110,36 +111,43 @@ public void start(final Stage primaryStage) {
110111
} catch (final Exception e) {
111112
LOG.error("There was an error while initialising the UI", e);
112113

113-
final Alert alert = new Alert(AlertType.ERROR);
114-
alert.setTitle("Error");
115-
alert.setHeaderText("Could not start application");
116-
alert.setContentText("Please send the error with your logs folder to a developer");
114+
showErrorDialogAndWait("Error", "Could not start application",
115+
"Please send the error with your logs folder to a developer", e, null);
116+
System.exit(1);
117+
}
118+
}
117119

118-
final StringWriter sw = new StringWriter();
119-
final PrintWriter pw = new PrintWriter(sw);
120-
e.printStackTrace(pw);
121-
final String exceptionText = sw.toString();
120+
public static void showErrorDialogAndWait(String title, String header, String content, final Exception e, Window window) {
121+
final Alert alert = new Alert(AlertType.ERROR);
122+
alert.setTitle(title);
123+
alert.setHeaderText(header);
124+
alert.setContentText(content);
125+
if(window != null) {
126+
alert.initOwner(window);
127+
}
128+
final StringWriter sw = new StringWriter();
129+
final PrintWriter pw = new PrintWriter(sw);
130+
e.printStackTrace(pw);
131+
final String exceptionText = sw.toString();
122132

123-
final Label label = new Label("The exception stacktrace was:");
133+
final Label label = new Label("The exception stacktrace was:");
124134

125-
final TextArea textArea = new TextArea(exceptionText);
126-
textArea.setEditable(false);
127-
textArea.setWrapText(true);
135+
final TextArea textArea = new TextArea(exceptionText);
136+
textArea.setEditable(false);
137+
textArea.setWrapText(true);
128138

129-
textArea.setMaxWidth(Double.MAX_VALUE);
130-
textArea.setMaxHeight(Double.MAX_VALUE);
131-
GridPane.setVgrow(textArea, Priority.ALWAYS);
132-
GridPane.setHgrow(textArea, Priority.ALWAYS);
139+
textArea.setMaxWidth(Double.MAX_VALUE);
140+
textArea.setMaxHeight(Double.MAX_VALUE);
141+
GridPane.setVgrow(textArea, Priority.ALWAYS);
142+
GridPane.setHgrow(textArea, Priority.ALWAYS);
133143

134-
final GridPane expContent = new GridPane();
135-
expContent.setMaxWidth(Double.MAX_VALUE);
136-
expContent.add(label, 0, 0);
137-
expContent.add(textArea, 0, 1);
144+
final GridPane expContent = new GridPane();
145+
expContent.setMaxWidth(Double.MAX_VALUE);
146+
expContent.add(label, 0, 0);
147+
expContent.add(textArea, 0, 1);
138148

139-
alert.getDialogPane().setExpandableContent(expContent);
140-
alert.showAndWait();
141-
System.exit(1);
142-
}
149+
alert.getDialogPane().setExpandableContent(expContent);
150+
alert.showAndWait();
143151
}
144152

145153
private void initialiseApplication(final Stage primaryStage) throws Exception {

src/main/java/de/doubleslash/keeptime/view/ReportController.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import javafx.stage.Stage;
6464
import javafx.util.Callback;
6565

66+
import static de.doubleslash.keeptime.App.showErrorDialogAndWait;
6667
import static de.doubleslash.keeptime.view.ViewController.createFXMLLoader;
6768

6869
@Component
@@ -138,17 +139,18 @@ private void initialize() {
138139
}
139140

140141
private void initHeimatIntegration() {
141-
// TODO if heimat active show or hide button
142+
heimatSyncButton.setVisible(model.getHeimatSettings().isHeimatActive());
142143
heimatSyncButton.setOnAction(ae-> {
143144
try {
144145
showSyncStage();
145-
} catch (IOException e) {
146-
throw new RuntimeException(e);
146+
} catch (FXMLLoaderException e) {
147+
LOG.error("Error while loading sync stage", e);
148+
showErrorDialogAndWait("Error", "Could not load sync stage", "Please make sure your AccessToken is valid and you have internet.", e, this.stage);
147149
}
148150
});
149151
}
150152

151-
private void showSyncStage() throws IOException {
153+
private void showSyncStage(){
152154
try{
153155
// Settings stage
154156
final FXMLLoader fxmlLoader2 = createFXMLLoader(RESOURCE.FXML_EXT_PROJECT_SYNC);
@@ -159,7 +161,6 @@ private void showSyncStage() throws IOException {
159161
Stage settingsStage = new Stage();
160162
settingsController.setStage(settingsStage);
161163
settingsStage.initOwner(this.stage);
162-
//settingsStage.initModality(Modality.WINDOW_MODAL);
163164
settingsStage.setTitle("External Project Sync");
164165
settingsStage.setResizable(true);
165166
settingsStage.getIcons().add(new Image(Resources.getResource(RESOURCE.ICON_MAIN).toString()));
@@ -174,9 +175,7 @@ private void showSyncStage() throws IOException {
174175

175176
settingsStage.setScene(settingsScene);
176177
settingsStage.showAndWait();
177-
//settingsStage.setOnHiding(e -> this.mainStage.setAlwaysOnTop(true));
178-
} catch (final IOException e) {
179-
LOG.error("Error while loading sub stage");
178+
} catch (final Exception e) {
180179
throw new FXMLLoaderException(e);
181180
}
182181
}

src/main/java/de/doubleslash/keeptime/view/SettingsController.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.slf4j.Logger;
4444
import org.slf4j.LoggerFactory;
4545
import org.springframework.beans.factory.annotation.Autowired;
46+
import org.springframework.context.annotation.Scope;
4647
import org.springframework.stereotype.Component;
4748

4849
import de.doubleslash.keeptime.ApplicationProperties;
@@ -68,6 +69,7 @@
6869
import javafx.stage.FileChooser.ExtensionFilter;
6970
import javafx.stage.Stage;
7071

72+
import static de.doubleslash.keeptime.App.showErrorDialogAndWait;
7173
import static de.doubleslash.keeptime.view.ViewController.createFXMLLoader;
7274

7375
@Component
@@ -218,6 +220,9 @@ public class SettingsController {
218220
@FXML
219221
private Button heimatMapProjectsButton;
220222

223+
@FXML
224+
private Label mapProjectsButtonLabel;
225+
221226
private final String propertiesFilePath = "application.properties";
222227

223228
private static final String GITHUB_PAGE = "https://www.github.com/doubleSlashde/KeepTime";
@@ -412,10 +417,11 @@ private void initializeHeimat() {
412417
final HeimatAPI heimatAPI = new HeimatAPI(heimatUrlTextField.getText() , heimatPatTextField.getText());
413418
try {
414419
heimatAPI.isLoginValid();
415-
heimatValidateConnectionLabel.setText("Connection is valid");
420+
heimatValidateConnectionLabel.setText("Connection is valid.");
416421
}catch(Exception e){
417422
LOG.error("Error while validating Heimat connection", e);
418423
heimatValidateConnectionLabel.setText("Invalid connection: " + e.getMessage());
424+
heimatValidateConnectionLabel.setTooltip(new Tooltip(e.getMessage()));
419425
}
420426
});
421427

@@ -429,22 +435,25 @@ private void initializeHeimat() {
429435
heimatUrlTextField.setText(heimatSettings.getHeimatUrl());
430436
heimatPatTextField.setText(heimatSettings.getHeimatPat());
431437

438+
mapProjectsButtonLabel.setText("Please save settings before mapping projects.");
439+
heimatMapProjectsButton.disableProperty().bind(heimatActivationCheckbox.selectedProperty().not());
440+
432441
heimatMapProjectsButton.setOnAction((ae) -> {
433442
try {
434443
showMapProjectsStage();
435-
} catch (IOException e) {
436-
throw new RuntimeException(e);
444+
} catch (FXMLLoaderException e) {
445+
LOG.error("Error while loading map stage", e);
446+
showErrorDialogAndWait("Error", "Could not load mapping stage", "Try to save settings first. Make sure you have internet.", e, thisStage);
437447
}
438448
});
439449
}
440450

441-
private void showMapProjectsStage() throws IOException {
451+
private void showMapProjectsStage() {
442452
try{
443453
// Settings stage
444454
final FXMLLoader fxmlLoader2 = createFXMLLoader(RESOURCE.FXML_EXT_PROJECT_MAPPING);
445455
fxmlLoader2.setControllerFactory(model.getSpringContext()::getBean);
446456
final Parent settingsRoot = fxmlLoader2.load();
447-
// TODO somehow the url is not available after saving - only when restarted
448457
MapExternalProjectsController settingsController = fxmlLoader2.getController();
449458
Stage settingsStage = new Stage();
450459
settingsController.setStage(settingsStage);
@@ -462,10 +471,8 @@ private void showMapProjectsStage() throws IOException {
462471
});
463472

464473
settingsStage.setScene(settingsScene);
465-
settingsStage.showAndWait();
466-
//settingsStage.setOnHiding(e -> this.mainStage.setAlwaysOnTop(true));
467-
} catch (final IOException e) {
468-
LOG.error("Error while loading sub stage");
474+
settingsStage.showAndWait();
475+
} catch (final Exception e) {
469476
throw new FXMLLoaderException(e);
470477
}
471478
}

src/main/resources/layouts/settings.fxml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,7 @@
722722
<HBox alignment="CENTER_LEFT">
723723
<children>
724724
<Button fx:id="heimatMapProjectsButton" mnemonicParsing="false" text="Map projects" />
725+
<Label fx:id="mapProjectsButtonLabel" />
725726
</children>
726727
</HBox>
727728
</children>

0 commit comments

Comments
 (0)