-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathToast.java
More file actions
194 lines (163 loc) · 5.35 KB
/
Toast.java
File metadata and controls
194 lines (163 loc) · 5.35 KB
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
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
public class Toast extends JDialog {
private static final long serialVersionUID = -1602907470843951525L;
public enum Style { NORMAL, SUCCESS, ERROR };
public static final int LENGTH_SHORT = 3000;
public static final int LENGTH_LONG = 6000;
public static final Color ERROR_RED = new Color(121, 0, 0);
public static final Color SUCCESS_GREEN = new Color(22, 127, 57);
public static final Color NORMAL_BLACK = new Color(0, 0, 0);
private final float MAX_OPACITY = 0.8f;
private final float OPACITY_INCREMENT = 0.05f;
private final int FADE_REFRESH_RATE = 20;
private final int WINDOW_RADIUS = 15;
private final int CHARACTER_LENGTH_MULTIPLIER = 7;
private final int DISTANCE_FROM_PARENT_TOP = 100;
private JFrame mOwner;
private String mText;
private int mDuration;
private Color mBackgroundColor = Color.BLACK;
private Color mForegroundColor = Color.WHITE;
public Toast(JFrame owner){
super(owner);
mOwner = owner;
}
private void createGUI(){
setLayout(new GridBagLayout());
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
setShape(new RoundRectangle2D.Double(0, 0, getWidth(), getHeight(), WINDOW_RADIUS, WINDOW_RADIUS));
}
});
setAlwaysOnTop(true);
setUndecorated(true);
setFocusableWindowState(false);
setModalityType(ModalityType.MODELESS);
setSize(mText.length() * CHARACTER_LENGTH_MULTIPLIER, 25);
getContentPane().setBackground(mBackgroundColor);
JLabel label = new JLabel(mText);
label.setForeground(mForegroundColor);
add(label);
}
public void fadeIn() {
final Timer timer = new Timer(FADE_REFRESH_RATE, null);
timer.setRepeats(true);
timer.addActionListener(new ActionListener() {
private float opacity = 0;
@Override public void actionPerformed(ActionEvent e) {
opacity += OPACITY_INCREMENT;
setOpacity(Math.min(opacity, MAX_OPACITY));
if (opacity >= MAX_OPACITY){
timer.stop();
}
}
});
setOpacity(0);
timer.start();
setLocation(getToastLocation());
setVisible(true);
}
public void fadeOut() {
final Timer timer = new Timer(FADE_REFRESH_RATE, null);
timer.setRepeats(true);
timer.addActionListener(new ActionListener() {
private float opacity = MAX_OPACITY;
@Override public void actionPerformed(ActionEvent e) {
opacity -= OPACITY_INCREMENT;
setOpacity(Math.max(opacity, 0));
if (opacity <= 0) {
timer.stop();
setVisible(false);
dispose();
}
}
});
setOpacity(MAX_OPACITY);
timer.start();
}
private Point getToastLocation(){
Point ownerLoc = mOwner.getLocation();
int x = (int) (ownerLoc.getX() + ((mOwner.getWidth() - this.getWidth()) / 2));
int y = (int) (ownerLoc.getY() + DISTANCE_FROM_PARENT_TOP);
return new Point(x, y);
}
public void setText(String text){
mText = text;
}
public void setDuration(int duration){
mDuration = duration;
}
@Override
public void setBackground(Color backgroundColor){
mBackgroundColor = backgroundColor;
}
@Override
public void setForeground(Color foregroundColor){
mForegroundColor = foregroundColor;
}
public static Toast makeText(JFrame owner, String text){
return makeText(owner, text, LENGTH_SHORT);
}
public static Toast makeText(JFrame owner, String text, Style style){
return makeText(owner, text, LENGTH_SHORT, style);
}
public static Toast makeText(JFrame owner, String text, int duration){
return makeText(owner, text, duration, Style.NORMAL);
}
public static Toast makeText(JFrame owner, String text, int duration, Style style){
Toast toast = new Toast(owner);
toast.mText = text;
toast.mDuration = duration;
if (style == Style.SUCCESS)
toast.mBackgroundColor = SUCCESS_GREEN;
if (style == Style.ERROR)
toast.mBackgroundColor = ERROR_RED;
if (style == Style.NORMAL)
toast.mBackgroundColor = NORMAL_BLACK;
return toast;
}
public void display(){
new Thread(new Runnable() {
@Override
public void run() {
try{
createGUI();
fadeIn();
Thread.sleep(mDuration);
fadeOut();
}
catch(Exception ex){
ex.printStackTrace();
}
}
}).start();
}
public static void main(String... args){
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(new Dimension(500, 300));
JButton b = new JButton("Toast!");
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Toast.makeText(frame, "Annotations were successfully saved.", Style.SUCCESS).display();
}
});
frame.add(b);
frame.setVisible(true);
}
}