Skip to content

Commit

Permalink
Adding new Notification API snippet to demonstrate the usage of user
Browse files Browse the repository at this point in the history
interaction

This new notification example will show a button which the user can
press and another dialog is opened.
  • Loading branch information
vogella committed Nov 8, 2024
1 parent edc926d commit 5a4c904
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
11 changes: 11 additions & 0 deletions docs/JFaceSnippets.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Contents
* [2.1 Snippet081 - Notification API](#Snippet081---Notification-API)
* [2.2 Snippet083 - Notification Popup with Functions](#Snippet083---Notification-Popup-with-Functions)
* [2.3 Snippet084 - Notification Popup with Custom Delay and Fade](#Snippet084---Notification-Popup-with-Custom-Delay-and-Fade)
* [2.4 Snippet085 - Notification popup with user interaction](#Snippet085---Notification-popup-with-user-interaction)
* [3 Layout](#Layout)
* [3.1 Snippet013 - Grid Layout Factory](#Snippet013---Grid-Layout-Factory)
* [3.2 Snippet016 - Table Layout](#Snippet016---Table-Layout)
Expand Down Expand Up @@ -141,6 +142,16 @@ Demonstrates the creation of notification popups that include function callbacks

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

### [Snippet085 - Notification popup with user interaction](https://github.com/vogella/eclipse-jface-snippets/blob/main/src/org/eclipse/jface/snippets/notifications/Snippet085NotificationPopupWithUserInteraction.java)

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.

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


![Snippet085.png](https://raw.githubusercontent.com/eclipse-platform/eclipse.platform.ui/master/docs/images/Snippet085NotificationPopupWithUserInteraction.png)


Layout
------

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.eclipse.jface.snippets.notifications;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.notifications.NotificationPopup;
import org.eclipse.jface.widgets.WidgetFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class Snippet085NotificationPopupWithUserInteraction {

public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display); // Create the shell

// Set the size of the shell
shell.setSize(400, 200);

// Create content for the notification popup
Composite contentComposite = new Composite(shell, SWT.NONE);
GridLayout layout = new GridLayout();
layout.marginWidth = 10; // Margin width
layout.marginHeight = 10; // Margin height
contentComposite.setLayout(layout);

// Create a bold label with wrapped text
Label firstLine = new Label(contentComposite, SWT.WRAP);
firstLine.setText("It is recommended that you");
Font boldFont = new Font(display, "Arial", 10, SWT.BOLD);
firstLine.setFont(boldFont);
// Create a bold label with wrapped text
Label secondLine = new Label(contentComposite, SWT.WRAP);
secondLine.setText("update your configuration");
secondLine.setFont(boldFont);


// Create a button that will show the confirmation dialog when clicked
Button button = new Button(contentComposite, SWT.PUSH);
button.setText("Confirm");

button.addListener(SWT.Selection, event -> {
MessageDialog.openConfirm(shell, "Confirmation", "Button was pressed!");
});

// Set GridData for button to align properly in the layout
GridData gridData = new GridData(SWT.LEFT, SWT.CENTER, false, false);
button.setLayoutData(gridData);

// Create the notification popup
NotificationPopup.forDisplay(display)
.content(composite -> {
contentComposite.setParent(composite);
return composite;
})
.title(composite -> WidgetFactory.label(SWT.NONE).text("System update!!!").create(composite), true)
.open();

shell.open(); // Open the shell

while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}

boldFont.dispose(); // Dispose of the font when done
display.dispose();
}
}

0 comments on commit 5a4c904

Please sign in to comment.