Skip to content

Commit 178c171

Browse files
committed
Implemented Dialogs for JavaFX
1 parent d858376 commit 178c171

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package de.vogel612.helper.ui.jfx;
2+
3+
import javafx.scene.control.ButtonBar;
4+
import javafx.scene.control.ButtonType;
5+
import javafx.scene.control.Dialog;
6+
import javafx.stage.Modality;
7+
import javafx.stage.StageStyle;
8+
9+
import java.util.Optional;
10+
11+
import static javafx.scene.control.ButtonType.CANCEL;
12+
import static javafx.scene.control.ButtonType.OK;
13+
14+
/**
15+
* Created by vogel612 on 02.06.16.
16+
*/
17+
public class JFXDialog implements de.vogel612.helper.ui.Dialog {
18+
19+
private static final ButtonType IGNORE = new ButtonType("Ignore", ButtonBar.ButtonData.RIGHT);
20+
21+
private static Dialog<ButtonType> createBasicDialog(final String title, final String message) {
22+
Dialog<ButtonType> d = new Dialog<>();
23+
d.setTitle(title);
24+
d.setContentText(message);
25+
d.initModality(Modality.APPLICATION_MODAL);
26+
d.initStyle(StageStyle.UNDECORATED);
27+
return d;
28+
}
29+
30+
@Override
31+
public void info(String title, String message) {
32+
Dialog<ButtonType> d = createBasicDialog(title, message);
33+
d.getDialogPane().getButtonTypes().addAll(OK);
34+
d.show();
35+
}
36+
37+
@Override
38+
public void info(String title, String message, Runnable okCallback) {
39+
Dialog<ButtonType> d = createBasicDialog(title, message);
40+
d.getDialogPane().getButtonTypes().addAll(OK);
41+
Optional<ButtonType> result = d.showAndWait();
42+
if (result.isPresent() && result.get() == OK) {
43+
okCallback.run();
44+
}
45+
}
46+
47+
@Override
48+
public void warn(String title, String message, Runnable okCallback, Runnable cancelCallback) {
49+
Dialog<ButtonType> d = createBasicDialog(title, message);
50+
d.getDialogPane().getButtonTypes().addAll(OK, CANCEL);
51+
Optional<ButtonType> result = d.showAndWait();
52+
if (result.isPresent()) {
53+
if (result.get() == OK) {
54+
okCallback.run();
55+
} else if (result.get() == CANCEL) {
56+
cancelCallback.run();
57+
}
58+
}
59+
}
60+
61+
@Override
62+
public void warn(String title, String message, Runnable okCallback, Runnable cancelCallback, Runnable ignoreCallback) {
63+
Dialog<ButtonType> d = createBasicDialog(title, message);
64+
65+
d.getDialogPane().getButtonTypes().addAll(OK, CANCEL, IGNORE);
66+
Optional<ButtonType> result = d.showAndWait();
67+
if (result.isPresent()) {
68+
if (result.get() == OK) {
69+
okCallback.run();
70+
} else if (result.get() == CANCEL) {
71+
cancelCallback.run();
72+
} else if (result.get() == IGNORE) {
73+
ignoreCallback.run();
74+
}
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)