/
Notifier.java
126 lines (108 loc) · 3.16 KB
/
Notifier.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
package jp.toastkid.gui.jfx.noodle_timer;
import java.io.File;
import org.apache.commons.lang3.StringUtils;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.media.Media;
import javafx.scene.media.MediaException;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
/**
* Notifier.
* @author Toast kid
*
*/
public class Notifier {
/** this use when passed value is empty. */
private static final String DEFAULT_TEXT = "Time up";
/** media player. */
private MediaPlayer media;
/** Notification dialog. */
private final Alert alert;
/**
* {@link Notifier}'s builder.
* @author Toast kid
*
*/
public static class Builder {
/** path/to/nav. */
private String pathToWav;
/** dialog's title. */
private String title;
/** dialog's message. */
private String message;
/**
* set param and return oneself.
* @param pathToWav
* @return
*/
public Builder setPathToWav(final String pathToWav) {
this.pathToWav = pathToWav;
return this;
}
/**
* set param and return oneself.
* @param title
* @return
*/
public Builder setTitle(final String title) {
this.title = title;
return this;
}
/**
* set param and return oneself.
* @param message
* @return
*/
public Builder setMessage(final String message) {
this.message = message;
return this;
}
/**
* build {@link Notifier}'s object.
* @return
*/
public Notifier build() {
return new Notifier(this);
}
}
/**
* initialize with media file.
* @param pathToWav
*/
private Notifier(final Builder b) {
try {
final File wav = new File(b.pathToWav);
// ファイルが見つからない時は初期化をスキップした方が高速.
if (wav.exists()) {
media = new MediaPlayer(new Media(wav.toURI().toString()));
}
} catch (final MediaException e) {
e.printStackTrace();
}
alert = new Alert(AlertType.INFORMATION);
final Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
final Image image = new Image(getClass().getClassLoader().getResourceAsStream("ramen.png"));
stage.getIcons().add(image);
alert.setGraphic(new ImageView(image));
alert.setTitle(StringUtils.isEmpty(b.title) ? DEFAULT_TEXT : b.title);
alert.setHeaderText(StringUtils.isEmpty(b.title) ? DEFAULT_TEXT : b.message);
}
/**
* attempt to notification.
*/
public void play() {
if (media != null) {
media.play();
try {
Thread.sleep(2000L);
} catch (final InterruptedException e) {
e.printStackTrace();
}
media.stop();
}
alert.show();
}
}