|
| 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