Skip to content

Commit 39ea298

Browse files
committed
show fallback dialogs for notifications in dialog factory
1 parent 0792176 commit 39ea298

1 file changed

Lines changed: 28 additions & 3 deletions

File tree

src/main/java/gr/sqlbrowserfx/factories/DialogFactory.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import org.slf4j.LoggerFactory;
1111

1212
import gr.sqlbrowserfx.LoggerConf;
13-
import gr.sqlbrowserfx.SqlBrowserFXApp;
1413
import gr.sqlbrowserfx.nodes.CustomHBox;
1514
import gr.sqlbrowserfx.nodes.CustomVBox;
1615
import gr.sqlbrowserfx.nodes.tableviews.MapTableViewRow;
@@ -42,6 +41,11 @@ public class DialogFactory {
4241

4342
private static final Pos NOTIFICATION_POS = Pos.TOP_RIGHT;
4443
private static String DEFAULT_STYLESHEET;
44+
private static Stage STAGE;
45+
46+
public static void setStage(Stage stage) {
47+
STAGE = stage;
48+
}
4549

4650
public static void createErrorDialog(Throwable e) {
4751
createErrorDialog(e, null);
@@ -197,7 +201,20 @@ public static void createNotification(String title, String message) {
197201
createNotification(title, message, 3);
198202
}
199203

204+
private static void checkStage() {
205+
if (STAGE == null) {
206+
throw new RuntimeException("Stage not set for DialogFactory. Please set it before using notifications. You must call DialogFactory.setStage(stage) in the start method of your JavaFX application.");
207+
}
208+
}
209+
200210
public static void createNotification(String title, String message, int durationInSecs) {
211+
checkStage();
212+
213+
if (STAGE == null) {
214+
createInfoDialog(title, message);
215+
return;
216+
}
217+
201218
Platform.runLater(() -> {
202219
Notifications.create()
203220
.title(title)
@@ -206,7 +223,7 @@ public static void createNotification(String title, String message, int duration
206223
.hideAfter(Duration.seconds(durationInSecs))
207224
.position(NOTIFICATION_POS)
208225
.onAction(actionEvent -> createInfoDialog(title, message))
209-
.owner(SqlBrowserFXApp.STAGE)
226+
.owner(STAGE)
210227
.showInformation();
211228

212229
});
@@ -231,6 +248,14 @@ public static void createErrorNotification(String title, String message, Throwab
231248
formattedMessage.append(message);
232249
}
233250
final String finalMessage = formattedMessage.toString();
251+
252+
checkStage();
253+
254+
if (STAGE == null && throwable != null) {
255+
createErrorDialog(throwable, null);
256+
return;
257+
}
258+
234259
Platform.runLater(() -> {
235260
Notifications.create()
236261
.title(title)
@@ -243,7 +268,7 @@ public static void createErrorNotification(String title, String message, Throwab
243268
createErrorDialog(throwable, null);
244269
}
245270
})
246-
.owner(SqlBrowserFXApp.STAGE)
271+
.owner(STAGE)
247272
.showError();
248273

249274
});

0 commit comments

Comments
 (0)