-
Notifications
You must be signed in to change notification settings - Fork 3
/
DialogBuilder.java
238 lines (209 loc) · 7.35 KB
/
DialogBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package wan.dormsystem.utils;
import com.jfoenix.controls.JFXAlert;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXDialogLayout;
import com.sun.istack.internal.Nullable;
import java.util.Optional;
import javafx.scene.control.Control;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.Border;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Paint;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.Window;
/**
* @author StarsOne
* @date Create in 2019/6/2 0002 20:51
* @description
*/
public class DialogBuilder {
private String title, message;
private JFXButton negativeBtn = null;
private JFXButton positiveBtn = null;
private Window window;
private Paint negativeBtnPaint = Paint.valueOf("#747474");//否定按钮文字颜色,默认灰色
private Paint positiveBtnPaint = Paint.valueOf("#0099ff");
private Hyperlink hyperlink = null;
private TextField textField = null;
private JFXAlert<String> alert;
private OnInputListener onInputListener = null;
/**
* 构造方法
*
* @param control 任意一个控件
*/
public DialogBuilder(Control control) {
window = control.getScene().getWindow();
}
public DialogBuilder setTitle(String title) {
this.title = title;
return this;
}
public DialogBuilder setMessage(String message) {
this.message = message;
return this;
}
public DialogBuilder setPositiveBtn(String positiveBtnText) {
return setPositiveBtn(positiveBtnText, null, null);
}
public DialogBuilder setNegativeBtn(String negativeBtnText) {
return setNegativeBtn(negativeBtnText, null, null);
}
/**
* 设置否定按钮文字和文字颜色
*
* @param negativeBtnText 文字
* @param color 文字颜色 十六进制 #fafafa
* @return
*/
public DialogBuilder setNegativeBtn(String negativeBtnText, String color) {
return setNegativeBtn(negativeBtnText, null, color);
}
/**
* 设置按钮文字和按钮文字颜色,按钮监听器和
*
* @param negativeBtnText
* @param negativeBtnOnclickListener
* @param color 文字颜色 十六进制 #fafafa
* @return
*/
public DialogBuilder setNegativeBtn(String negativeBtnText, @Nullable OnClickListener negativeBtnOnclickListener, String color) {
if (color != null) {
this.negativeBtnPaint = Paint.valueOf(color);
}
return setNegativeBtn(negativeBtnText, negativeBtnOnclickListener);
}
/**
* 设置按钮文字和点击监听器
*
* @param negativeBtnText 按钮文字
* @param negativeBtnOnclickListener 点击监听器
* @return
*/
public DialogBuilder setNegativeBtn(String negativeBtnText, @Nullable OnClickListener negativeBtnOnclickListener) {
negativeBtn = new JFXButton(negativeBtnText);
negativeBtn.setCancelButton(true);
negativeBtn.setTextFill(negativeBtnPaint);
negativeBtn.setButtonType(JFXButton.ButtonType.FLAT);
negativeBtn.setOnAction(addEvent -> {
alert.hideWithAnimation();
if (negativeBtnOnclickListener != null) {
negativeBtnOnclickListener.onClick();
}
});
return this;
}
/**
* 设置按钮文字和颜色
*
* @param positiveBtnText 文字
* @param color 颜色 十六进制 #fafafa
* @return
*/
public DialogBuilder setPositiveBtn(String positiveBtnText, String color) {
return setPositiveBtn(positiveBtnText, null, color);
}
/**
* 设置按钮文字,颜色和点击监听器
*
* @param positiveBtnText 文字
* @param positiveBtnOnclickListener 点击监听器
* @param color 颜色 十六进制 #fafafa
* @return
*/
public DialogBuilder setPositiveBtn(String positiveBtnText, @Nullable OnClickListener positiveBtnOnclickListener, String color) {
if (color != null) {
this.positiveBtnPaint = Paint.valueOf(color);
}
return setPositiveBtn(positiveBtnText, positiveBtnOnclickListener);
}
/**
* 设置按钮文字和监听器
*
* @param positiveBtnText 文字
* @param positiveBtnOnclickListener 点击监听器
* @return
*/
public DialogBuilder setPositiveBtn(String positiveBtnText, @Nullable OnClickListener positiveBtnOnclickListener) {
positiveBtn = new JFXButton(positiveBtnText);
positiveBtn.setDefaultButton(true);
positiveBtn.setTextFill(positiveBtnPaint);
positiveBtn.setOnAction(closeEvent -> {
alert.hideWithAnimation();
if (positiveBtnOnclickListener != null) {
positiveBtnOnclickListener.onClick();//回调onClick方法
}
});
return this;
}
/**
* 设置超链接(文件输出路径,网址跳转),会自动打开指定浏览器或者是资源管理器执行操作
*
* @param text 文件的路径,或者是网址,
* @return
*/
public DialogBuilder setHyperLink(String text) {
hyperlink = new Hyperlink(text);
hyperlink.setBorder(Border.EMPTY);
hyperlink.setOnMouseClicked(event -> {
MyUtils.setLinkAutoAction(hyperlink);
});
return this;
}
public DialogBuilder setTextFieldText(OnInputListener onInputListener) {
this.textField = new TextField();
this.onInputListener = onInputListener;
return this;
}
/**
* 创建对话框并显示
*
* @return JFXAlert<String>
*/
public JFXAlert<String> create() {
alert = new JFXAlert<>((Stage) (window));
alert.initModality(Modality.APPLICATION_MODAL);
alert.setOverlayClose(false);
JFXDialogLayout layout = new JFXDialogLayout();
layout.setHeading(new Label(title));
//添加hyperlink超链接文本或者是输入框
if (hyperlink != null) {
layout.setBody(new HBox(new Label(this.message), hyperlink));
} else if (textField != null) {
layout.setBody(new VBox(new Label(this.message), textField));
positiveBtn.setOnAction(event -> {
alert.setResult(textField.getText());
alert.hideWithAnimation();
});
} else {
layout.setBody(new VBox(new Label(this.message)));
}
//添加确定和取消按钮
if (negativeBtn != null && positiveBtn != null) {
layout.setActions(negativeBtn, positiveBtn);
} else {
if (negativeBtn != null) {
layout.setActions(negativeBtn);
} else if (positiveBtn != null) {
layout.setActions(positiveBtn);
}
}
alert.setContent(layout);
Optional<String> input = alert.showAndWait();
//不为空,则回调接口
if (input.isPresent()) {
onInputListener.onGetText(input.get());
}
return alert;
}
public interface OnClickListener {
void onClick();
}
public interface OnInputListener {
void onGetText(String result);
}
}