-
Notifications
You must be signed in to change notification settings - Fork 98
/
Notifier.java
334 lines (284 loc) · 10.3 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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*******************************************************************************
* Copyright (c) 2011 Laurent CARON
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors: Laurent CARON (laurent.caron at gmail dot com) - initial API
* and implementation
*******************************************************************************/
package org.eclipse.nebula.widgets.opal.notifier;
import org.eclipse.nebula.widgets.opal.commons.SWTGraphicUtil;
import org.eclipse.nebula.widgets.opal.notifier.NotifierColorsFactory.NotifierTheme;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
/**
* This class provides a notifier window, which is a window that appears in the
* bottom of the screen and slides.
*/
public class Notifier {
private static final int FONT_SIZE = 10;
private static final int MAX_DURATION_FOR_OPENING = 500;
private static final int DISPLAY_TIME = 4500;
private static final int FADE_TIMER = 50;
private static final int FADE_OUT_STEP = 8;
private static final int STEP = 5;
/**
* Starts a notification. A window will appear in the bottom of the screen, then
* will disappear after 4.5 s
*
* @param title the title of the popup window
* @param text the text of the notification
*
*/
public static void notify(final String title, final String text) {
notify(null, title, text, NotifierTheme.YELLOW_THEME);
}
/**
* Starts a notification. A window will appear in the bottom of the screen, then
* will disappear after 4.5 s
*
* @param image the image to display (if <code>null</code>, a default image is
* displayed)
* @param title the title of the popup window
* @param text the text of the notification
*
*/
public static void notify(final Image image, final String title, final String text) {
notify(image, title, text, NotifierTheme.YELLOW_THEME);
}
/**
* Starts a notification. A window will appear in the bottom of the screen, then
* will disappear after 4.5 s
*
* @param title the title of the popup window
* @param text the text of the notification
* @param theme the graphical theme. If <code>null</code>, the yellow theme is
* used
*
* @see NotifierTheme
*/
public static void notify(final String title, final String text, final NotifierTheme theme) {
notify(null, title, text, theme);
}
/**
* Starts a notification. A window will appear in the bottom of the screen, then
* will disappear after 4.5 s
*
* @param image the image to display (if <code>null</code>, a default image is
* displayed)
* @param title the title of the popup window
* @param text the text of the notification
* @param theme the graphical theme. If <code>null</code>, the yellow theme is
* used
*
* @see NotifierTheme
*/
public static void notify(final Image image, final String title, final String text, final NotifierTheme theme) {
final Shell shell = createNotificationWindow(image, title, text,
NotifierColorsFactory.getColorsForTheme(theme));
makeShellAppears(shell);
}
/**
* Creates a notification window
*
* @param image image. If <code>null</code>, a default image is used
* @param title title, the title of the window
* @param text text of the window
* @param colors color set
* @return the notification window as a shell object
*/
protected static Shell createNotificationWindow(final Image image, final String title, final String text,
final NotifierColors colors) {
final Shell shell = new Shell(Display.getDefault().getActiveShell(), SWT.NO_TRIM | SWT.NO_FOCUS | SWT.ON_TOP);
shell.setLayout(new GridLayout(2, false));
shell.setBackgroundMode(SWT.INHERIT_FORCE);
createTitle(shell, title, colors);
createImage(shell, image);
createText(shell, text, colors);
createBackground(shell, colors);
createCloseAction(shell);
shell.addListener(SWT.Dispose, event -> {
colors.dispose();
});
shell.pack();
shell.setMinimumSize(320, 100);
return shell;
}
/**
* Creates the title part of the window
*
* @param shell the window
* @param title the title
* @param colors the color set
*/
private static void createTitle(final Shell shell, final String title, final NotifierColors colors) {
final Label titleLabel = new Label(shell, SWT.NONE);
final GridData gdLabel = new GridData(GridData.BEGINNING, GridData.BEGINNING, true, false, 2, 1);
gdLabel.horizontalIndent = 40;
titleLabel.setLayoutData(gdLabel);
final Color titleColor = colors.titleColor;
titleLabel.setForeground(titleColor);
final Font titleFont = SWTGraphicUtil.buildFontFrom(titleLabel, SWT.BOLD, FONT_SIZE);
titleLabel.setFont(titleFont);
titleLabel.setText(title);
SWTGraphicUtil.addDisposer(shell, titleFont);
}
/**
* Creates the image part of the window
*
* @param shell the window
* @param image the image
*/
private static void createImage(final Shell shell, final Image image) {
final Label labelImage = new Label(shell, SWT.NONE);
final GridData gdImage = new GridData(GridData.CENTER, GridData.BEGINNING, false, true);
gdImage.horizontalIndent = 10;
labelImage.setLayoutData(gdImage);
if (image == null) {
final Image temp = SWTGraphicUtil.createImageFromFile("images/information.png");
labelImage.setImage(temp);
SWTGraphicUtil.addDisposer(shell, temp);
} else {
labelImage.setImage(image);
}
}
/**
* Creates the text part of the window
*
* @param shell the window
* @param text the text
* @param colors the color set
*/
private static void createText(final Shell shell, final String text, final NotifierColors colors) {
final StyledText textLabel = new StyledText(shell, SWT.WRAP | SWT.READ_ONLY);
final GridData gdText = new GridData(GridData.FILL, GridData.FILL, true, true);
gdText.horizontalIndent = 15;
textLabel.setLayoutData(gdText);
textLabel.setEnabled(false);
final Font textFont = SWTGraphicUtil.buildFontFrom(textLabel, SWT.NONE, 10);
textLabel.setFont(textFont);
final Color textColor = colors.textColor;
textLabel.setForeground(textColor);
final Color fillColor = colors.rightColor;
textLabel.setBackground(fillColor);
textLabel.setText(text);
SWTGraphicUtil.applyHTMLFormating(textLabel);
SWTGraphicUtil.addDisposer(shell, textFont);
}
/**
* Creates the background of the window
*
* @param shell the window
* @param colors the color set of the window
*/
private static void createBackground(final Shell shell, final NotifierColors colors) {
shell.addListener(SWT.Resize, event -> {
final Rectangle rect = shell.getClientArea();
final Image newImage = new Image(Display.getDefault(), Math.max(1, rect.width), rect.height);
final GC gc = new GC(newImage);
gc.setAntialias(SWT.ON);
final Color borderColor = colors.borderColor;
final Color fillColor1 = colors.leftColor;
final Color fillColor2 = colors.rightColor;
gc.setBackground(borderColor);
gc.fillRoundRectangle(0, 0, rect.width, rect.height, 8, 8);
gc.setBackground(fillColor1);
gc.fillRoundRectangle(1, 1, rect.width - 2, rect.height - 2, 8, 8);
gc.setBackground(fillColor2);
gc.fillRoundRectangle(30, 1, rect.width - 32, rect.height - 2, 8, 8);
gc.fillRectangle(30, 1, 10, rect.height - 2);
final Image closeImage = SWTGraphicUtil.createImageFromFile("images/close.png");
gc.drawImage(closeImage, rect.width - 21, 13);
gc.dispose();
closeImage.dispose();
SWTGraphicUtil.addDisposer(shell, newImage);
shell.setBackgroundImage(newImage);
});
}
/**
* @param shell shell that will appear
*/
protected static void makeShellAppears(final Shell shell) {
if (shell == null || shell.isDisposed()) {
return;
}
final Rectangle clientArea = Display.getDefault().getPrimaryMonitor().getClientArea();
final int startX = clientArea.x + clientArea.width - shell.getSize().x;
final int stepForPosition = MAX_DURATION_FOR_OPENING / shell.getSize().y * STEP;
final int stepForAlpha = STEP * 255 / shell.getSize().y;
final int lastPosition = clientArea.y + clientArea.height - shell.getSize().y;
shell.setAlpha(0);
shell.setLocation(startX, clientArea.y + clientArea.height);
shell.open();
shell.getDisplay().timerExec(stepForPosition, new Runnable() {
@Override
public void run() {
if (shell == null || shell.isDisposed()) {
return;
}
shell.setLocation(startX, shell.getLocation().y - STEP);
shell.setAlpha(shell.getAlpha() + stepForAlpha);
if (shell.getLocation().y >= lastPosition) {
shell.getDisplay().timerExec(stepForPosition, this);
} else {
shell.setAlpha(255);
Display.getDefault().timerExec(DISPLAY_TIME, fadeOut(shell, false));
}
}
});
}
/**
* @param shell shell that will disappear
* @param fast if true, the fading is much faster
* @return a runnable
*/
private static Runnable fadeOut(final Shell shell, final boolean fast) {
return new Runnable() {
@Override
public void run() {
if (shell == null || shell.isDisposed()) {
return;
}
int currentAlpha = shell.getAlpha();
currentAlpha -= FADE_OUT_STEP * (fast ? 8 : 1);
if (currentAlpha <= 0) {
shell.setAlpha(0);
shell.dispose();
return;
}
shell.setAlpha(currentAlpha);
Display.getDefault().timerExec(FADE_TIMER, this);
}
};
}
/**
* Add a listener to the shell in order to handle the clicks on the close button
*
* @param shell associated shell
*/
private static void createCloseAction(final Shell shell) {
shell.addListener(SWT.MouseUp, event -> {
final Rectangle rect = shell.getClientArea();
final int xUpperLeftCorner = rect.width - 21;
final int yUpperLeftCorner = 13;
if (event.x >= xUpperLeftCorner && event.x <= xUpperLeftCorner + 8 && event.y >= yUpperLeftCorner
&& event.y <= yUpperLeftCorner + 8) {
Display.getDefault().timerExec(0, fadeOut(shell, true));
}
});
}
}