Skip to content

Commit

Permalink
Notification Popup Builder API for shell
Browse files Browse the repository at this point in the history
Allow setting the shell in the builder API for Notification Popups.

Issue: #1226
  • Loading branch information
marcushoepfner authored and vogella committed Oct 17, 2023
1 parent 324afa5 commit e3d651a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Bundle-Name: %pluginName
Bundle-Vendor: %providerName
Bundle-Localization: plugin
Bundle-SymbolicName: org.eclipse.jface.notifications
Bundle-Version: 0.6.100.qualifier
Bundle-Version: 0.7.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-17
Export-Package: org.eclipse.jface.notifications,
org.eclipse.jface.notifications.internal;x-internal:=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
* NotificationPopup is a default implementation of
Expand Down Expand Up @@ -64,11 +65,17 @@ public static class Builder {
private Boolean fadeIn;
private boolean hasCloseButton;
private Image titleImage;
private Shell shell;

private Builder(Display display) {
this.display = display;
}

private Builder shell(Shell shell) {
this.shell = shell;
return this;
}

/**
* Sets the function to create the main content of the notification popup in the
* given composite. Is called when the content is created.
Expand Down Expand Up @@ -183,6 +190,21 @@ public static Builder forDisplay(Display display) {
return new Builder(display);
}

/**
* Creates a new builder instance using a shell.
* <p>
* The shell is set as parent shell in the notification popup.
* </p>
*
* @see AbstractNotificationPopup#setParentShell(Shell)
* @param shell the shell to use
* @return the builder instance
* @since 0.7
*/
public static Builder forShell(Shell shell) {
return new Builder(shell.getDisplay()).shell(shell);
}

private Function<Composite, ? extends Control> contentCreator;
private Function<Composite, Control> titleCreator;
private boolean hasCloseButton;
Expand All @@ -201,6 +223,10 @@ private NotificationPopup(Builder builder) {
if (builder.fadeIn != null) {
setFadingEnabled(builder.fadeIn);
}

if (builder.shell != null) {
setParentShell(builder.shell);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.eclipse.jface.notifications.NotificationPopup;
import org.eclipse.jface.notifications.NotificationPopup.Builder;
import org.eclipse.jface.notifications.internal.CommonImages;
import org.eclipse.jface.widgets.WidgetFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
Expand All @@ -47,6 +48,7 @@ public class NotificationPopupTest {

private Display display;
private Builder builder;
private Shell shell;

@Before
public void setUp() {
Expand All @@ -56,6 +58,10 @@ public void setUp() {

@After
public void tearDown() {
if (shell != null) {
shell.close();
shell.dispose();
}
if (!Platform.isRunning()) {
if (this.display != null) {
this.display.syncExec(() -> this.display.dispose());
Expand Down Expand Up @@ -87,8 +93,7 @@ public void createsWithCloseButton() {
@Test
public void createsWithTextContent() {
Text[] text = new Text[1];
NotificationPopup notication =
this.builder.title("Hello World", false).content(parent -> {
NotificationPopup notication = this.builder.title("Hello World", false).content(parent -> {
text[0] = new Text(parent, SWT.NONE);
text[0].setText("My custom Text");
return text[0];
Expand Down Expand Up @@ -116,6 +121,20 @@ public void createsWithTitleContent() {
assertThat(controls, hasItem(is(text[0])));
}

@Test
public void createsForShell() {
shell = WidgetFactory.shell(SWT.NONE).create(display);
this.builder = NotificationPopup.forShell(shell);

NotificationPopup notication = this.builder.text("This is a test").title("Hello World", false).delay(1).build();
notication.open();
List<Control> controls = getNotificationPopupControls(notication);

assertThat(controls, hasItem(aLabelWith("Hello World")));
assertThat(controls, hasItem(aLabelWith("This is a test")));
notication.close();
}

private List<Control> getNotificationPopupControls(NotificationPopup notication) {
Shell shell = notication.getShell();
return getChildrenStream(shell).toList();
Expand Down

0 comments on commit e3d651a

Please sign in to comment.