Skip to content

Commit 5a4c904

Browse files
committed
Adding new Notification API snippet to demonstrate the usage of user
interaction This new notification example will show a button which the user can press and another dialog is opened.
1 parent edc926d commit 5a4c904

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

docs/JFaceSnippets.md

+11
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Contents
2323
* [2.1 Snippet081 - Notification API](#Snippet081---Notification-API)
2424
* [2.2 Snippet083 - Notification Popup with Functions](#Snippet083---Notification-Popup-with-Functions)
2525
* [2.3 Snippet084 - Notification Popup with Custom Delay and Fade](#Snippet084---Notification-Popup-with-Custom-Delay-and-Fade)
26+
* [2.4 Snippet085 - Notification popup with user interaction](#Snippet085---Notification-popup-with-user-interaction)
2627
* [3 Layout](#Layout)
2728
* [3.1 Snippet013 - Grid Layout Factory](#Snippet013---Grid-Layout-Factory)
2829
* [3.2 Snippet016 - Table Layout](#Snippet016---Table-Layout)
@@ -141,6 +142,16 @@ Demonstrates the creation of notification popups that include function callbacks
141142

142143
Shows how to create notification popups with custom delay and fade effects for enhanced visual feedback.
143144

145+
### [Snippet085 - Notification popup with user interaction](https://github.com/vogella/eclipse-jface-snippets/blob/main/src/org/eclipse/jface/snippets/notifications/Snippet085NotificationPopupWithUserInteraction.java)
146+
147+
This snippet demonstrates how to create a `NotificationPopup` that includes user interaction with a button. When the button is clicked, a confirmation dialog is shown.
148+
149+
For the full code, please visit the [GitHub repository](https://github.com/vogella/eclipse-jface-snippets/blob/main/src/org/eclipse/jface/snippets/notifications/Snippet085NotificationPopupWithUserInteraction.java).
150+
151+
152+
![Snippet085.png](https://raw.githubusercontent.com/eclipse-platform/eclipse.platform.ui/master/docs/images/Snippet085NotificationPopupWithUserInteraction.png)
153+
154+
144155
Layout
145156
------
146157

Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.eclipse.jface.snippets.notifications;
2+
3+
import org.eclipse.jface.dialogs.MessageDialog;
4+
import org.eclipse.jface.notifications.NotificationPopup;
5+
import org.eclipse.jface.widgets.WidgetFactory;
6+
import org.eclipse.swt.SWT;
7+
import org.eclipse.swt.graphics.Font;
8+
import org.eclipse.swt.layout.GridData;
9+
import org.eclipse.swt.layout.GridLayout;
10+
import org.eclipse.swt.widgets.Button;
11+
import org.eclipse.swt.widgets.Composite;
12+
import org.eclipse.swt.widgets.Display;
13+
import org.eclipse.swt.widgets.Label;
14+
import org.eclipse.swt.widgets.Shell;
15+
16+
public class Snippet085NotificationPopupWithUserInteraction {
17+
18+
public static void main(String[] args) {
19+
Display display = new Display();
20+
Shell shell = new Shell(display); // Create the shell
21+
22+
// Set the size of the shell
23+
shell.setSize(400, 200);
24+
25+
// Create content for the notification popup
26+
Composite contentComposite = new Composite(shell, SWT.NONE);
27+
GridLayout layout = new GridLayout();
28+
layout.marginWidth = 10; // Margin width
29+
layout.marginHeight = 10; // Margin height
30+
contentComposite.setLayout(layout);
31+
32+
// Create a bold label with wrapped text
33+
Label firstLine = new Label(contentComposite, SWT.WRAP);
34+
firstLine.setText("It is recommended that you");
35+
Font boldFont = new Font(display, "Arial", 10, SWT.BOLD);
36+
firstLine.setFont(boldFont);
37+
// Create a bold label with wrapped text
38+
Label secondLine = new Label(contentComposite, SWT.WRAP);
39+
secondLine.setText("update your configuration");
40+
secondLine.setFont(boldFont);
41+
42+
43+
// Create a button that will show the confirmation dialog when clicked
44+
Button button = new Button(contentComposite, SWT.PUSH);
45+
button.setText("Confirm");
46+
47+
button.addListener(SWT.Selection, event -> {
48+
MessageDialog.openConfirm(shell, "Confirmation", "Button was pressed!");
49+
});
50+
51+
// Set GridData for button to align properly in the layout
52+
GridData gridData = new GridData(SWT.LEFT, SWT.CENTER, false, false);
53+
button.setLayoutData(gridData);
54+
55+
// Create the notification popup
56+
NotificationPopup.forDisplay(display)
57+
.content(composite -> {
58+
contentComposite.setParent(composite);
59+
return composite;
60+
})
61+
.title(composite -> WidgetFactory.label(SWT.NONE).text("System update!!!").create(composite), true)
62+
.open();
63+
64+
shell.open(); // Open the shell
65+
66+
while (!shell.isDisposed()) {
67+
if (!display.readAndDispatch())
68+
display.sleep();
69+
}
70+
71+
boldFont.dispose(); // Dispose of the font when done
72+
display.dispose();
73+
}
74+
}

0 commit comments

Comments
 (0)