Skip to content

Commit

Permalink
Update dialog parent API, displayer impl and test.
Browse files Browse the repository at this point in the history
Update Utilities findDialogParent API to not use fallback Supplier and update docs.
Update DialogDisplayerImpl to validate preferred parent.
Update DialogDisplayerImplTest to stop stray modal dialog breaking parent test.
  • Loading branch information
neilcsmith-net committed Jul 18, 2023
1 parent e9d3df1 commit d7474cf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,14 @@ public Dialog createDialog (final DialogDescriptor d, final Frame preferredParen
return Mutex.EVENT.readAccess (new Mutex.Action<Dialog> () {
public Dialog run () {
Window w = preferredParent;
if( null == w ) {
if (w != null) {
// Verify the preferred parent
Component p = Utilities.findDialogParent(w);
if (p != w) {
w = null;
}
}
if (w == null) {
w = findDialogParent();
if (!(w instanceof NbPresenter) || !w.isVisible()) {
// undocked window is not instanceof NbPresenter although it's NetBeans's native window
Expand All @@ -122,7 +129,7 @@ public Dialog run () {
}

private Window findDialogParent() {
Component parentComponent = Utilities.findDialogParent(null, WindowManager.getDefault()::getMainWindow);
Component parentComponent = Utilities.findDialogParent(null);
if (parentComponent instanceof Window) {
return (Window) parentComponent;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ public void testNestedDialogParent() throws Exception {
assertShowing("Child is invisible", true, child);
Window w = SwingUtilities.windowForComponent(child);
assertSame("Window parent is not owner", owner, w.getParent());
postInAwtAndWaitOutsideAwt(() -> owner.setVisible(false));
}

static void postInAwtAndWaitOutsideAwt (final Runnable run) throws Exception {
Expand Down
39 changes: 21 additions & 18 deletions platform/openide.util.ui/src/org/openide/util/Utilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -1345,36 +1345,38 @@ private static Frame findMainWindow()
}

/**
* This is for use in situations where a standard swing API,
* such as {@linkplain javax.swing.JOptionPane}.show* or {@linkplain javax.swing.JFileChooser}.show*,
* is used to display a dialog. {@code null} should never be used
* as a dialog's parent because it
* Finds an appropriate component to use for a dialog's parent. This is for
* use in situations where a standard swing API, such as
* {@linkplain javax.swing.JOptionPane}.show* or
* {@linkplain javax.swing.JFileChooser}.show*, is used to display a dialog.
* {@code null} should never be used as a dialog's parent because it
* frequently does the wrong thing in a multi-screen setup.
* <p>
* The use of the NetBeans API
* <a href="@org-openide-dialogs@/org/openide/DialogDisplayer.html#getDefault--">DialogDisplayer.getDefault*</a>
* is encouraged to display a dialog, but stuff happens.
* @return A suitable parent component for swing dialog displayers.
* is encouraged to display a dialog.
*
* @return A suitable parent component for swing dialogs
* @since 9.26
*/
// PR4739
public static Component findDialogParent() {
return findDialogParent(null, Utilities::findMainWindow);
return findDialogParent(null);
}

/**
* Finds an appropriate component to use for a modal dialog's parent. Similar to {@link #findDialogParent()}
* with the ability to specify a suggested parent component and fallback.
* Finds an appropriate component to use for a dialog's parent. Similar to
* {@link #findDialogParent()} with the ability to specify a suggested
* parent component. The suggested parent will be returned if it is
* non-null, and either there is no active modal dialog or it is contained
* within that dialog.
*
* @param suggestedParent
* the component to return if non-null and valid
* @param fallback
* a supplier for the parent if no other suitable candidate exists
* @return the suggested parent if there is either no active modal dialog or it is contained
* within that dialog, otherwise the active modal dialog or the fallback.
* @param suggestedParent the component to return if non-null and valid
* @return the suggested parent if suitable, otherwise another suitable
* parent component for swing dialogs
* @since 9.30
*/
public static Component findDialogParent(Component suggestedParent, Supplier<Component> fallback) {
public static Component findDialogParent(Component suggestedParent) {
Component parent = suggestedParent;
if (parent == null) {
parent = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
Expand All @@ -1389,13 +1391,14 @@ public static Component findDialogParent(Component suggestedParent, Supplier<Com
}
}
if (parent == null) {
return fallback.get();
parent = findMainWindow();
}
return parent;
}

/**
* Gets whether a modal dialog is open.
* Check whether a modal dialog is open.
*
* @return true if a modal dialog is open, false otherwise
* @since 9.30
*/
Expand Down

0 comments on commit d7474cf

Please sign in to comment.