Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue (GH6290) in new DialogDisplayerImpl behaviour #6294

Merged
merged 1 commit into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.awt.Frame;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.KeyboardFocusManager;
import java.awt.Window;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;
Expand All @@ -38,6 +37,7 @@
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
import javax.swing.JOptionPane;
import javax.swing.JRootPane;
import javax.swing.SwingUtilities;
import org.netbeans.core.windows.view.ui.DefaultSeparateContainer;
Expand Down Expand Up @@ -120,7 +120,14 @@ public Dialog run () {
}
}
}
NbDialog dlg = new NbDialog(d, w);
NbDialog dlg;
if (w instanceof Frame) {
dlg = new NbDialog(d, (Frame) w);
} else if (w instanceof Dialog) {
dlg = new NbDialog(d, (Dialog) w);
} else {
dlg = new NbDialog(d, WindowManager.getDefault().getMainWindow());
}
customizeDlg(dlg);
dlg.requestFocusInWindow ();
return dlg;
Expand All @@ -130,19 +137,24 @@ public Dialog run () {

private Window findDialogParent() {
Component parentComponent = Utilities.findDialogParent(null);
if (parentComponent instanceof Window) {
return (Window) parentComponent;
}
Window parent = null;
if (parentComponent != null) {
parent = SwingUtilities.windowForComponent(parentComponent);
}
if (parent == null || parent instanceof NbPresenter && ((NbPresenter) parent).isLeaf ()) {
Window parent = findDialogParent(parentComponent);
if (parent == null || parent == JOptionPane.getRootFrame()
|| parent instanceof NbPresenter && ((NbPresenter) parent).isLeaf()) {
return WindowManager.getDefault().getMainWindow();
}
return parent;
}

private Window findDialogParent(Component component) {
if (component == null) {
return null;
}
if (component instanceof Frame || component instanceof Dialog) {
return (Window) component;
}
return findDialogParent(component.getParent());
}

/** Notifies user by a dialog.
* @param descriptor description that contains needed informations
* @return the option that has been choosen in the notification.
Expand Down Expand Up @@ -227,9 +239,21 @@ public void showDialog () {
Window parent = noParent ? null : findDialogParent();

if (descriptor instanceof DialogDescriptor) {
presenter = new NbDialog((DialogDescriptor) descriptor, parent);
if (parent instanceof Dialog) {
presenter = new NbDialog((DialogDescriptor) descriptor, (Dialog) parent);
} else if (parent instanceof Frame) {
presenter = new NbDialog((DialogDescriptor) descriptor, (Frame) parent);
} else {
presenter = new NbDialog((DialogDescriptor) descriptor, (Frame) null);
}
} else {
presenter = new NbPresenter(descriptor, parent, Dialog.DEFAULT_MODALITY_TYPE);
if (parent instanceof Dialog) {
presenter = new NbPresenter(descriptor, (Dialog) parent, true);
} else if (parent instanceof Frame) {
presenter = new NbPresenter(descriptor, (Frame) parent, true);
} else {
presenter = new NbPresenter(descriptor, (Frame) null, true);
}
}

synchronized (this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,6 @@ public NbDialog (DialogDescriptor d, Dialog owner) {
super (d, owner, d.isModal ());
}

/** Creates a new Dialog from specified DialogDescriptor
* @param d The DialogDescriptor to create the dialog from
* @param owner Owner of this dialog.
*/
public NbDialog(DialogDescriptor d, Window owner) {
super(d, owner, d.isModal() ? Dialog.DEFAULT_MODALITY_TYPE : ModalityType.MODELESS);
}

/** Getter for help.
*/
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,21 +184,6 @@ public NbPresenter(NotifyDescriptor d, Dialog owner, boolean modal) {
initialize(d);
}

/**
* Creates a new Dialog from the specified NotifyDescriptor and owner.
*
* @param d the non-null descriptor from which to initialize the dialog
* @param owner the owner of the dialog, must be a {@code Dialog} or
* {@code Frame} instance or {@code null} (not recommended)
* @param modality specifies whether dialog blocks input to other windows
* when shown. {@code null} value and unsupported modality types are
* equivalent to {@code MODELESS}
*/
public NbPresenter(NotifyDescriptor d, Window owner, ModalityType modality) {
super(owner, d.getTitle(), modality);
initialize(d);
}

boolean isLeaf () {
return leaf;
}
Expand Down Expand Up @@ -1599,7 +1584,10 @@ private Window findFocusedWindow() {
}
}
}
while( null != w && !w.isShowing() ) {
while( null != w ) {
if ((w instanceof Frame || w instanceof Dialog) && w.isShowing()) {
break;
}
w = w.getOwner();
}
return w;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
package org.netbeans.core.windows.services;

import java.awt.Dialog;
import java.awt.Frame;
import java.awt.GraphicsEnvironment;
import java.awt.Window;
import javax.swing.JLabel;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.netbeans.junit.NbTestCase;
Expand All @@ -42,12 +41,12 @@ protected boolean runInEQ() {
}

public void testModalityIsDefaultWhenModal() {
NbDialog d = new NbDialog(new DialogDescriptor(null, null, true, null), (Window) null);
NbDialog d = new NbDialog(new DialogDescriptor(null, null, true, null), (Frame) null);
assertEquals(Dialog.DEFAULT_MODALITY_TYPE, d.getModalityType());
}

public void testModalityIsModelessWhenNotModal() {
NbDialog d = new NbDialog(new DialogDescriptor(null, null, false, null), (Window) null);
NbDialog d = new NbDialog(new DialogDescriptor(null, null, false, null), (Frame) null);
assertEquals(Dialog.ModalityType.MODELESS, d.getModalityType());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,16 @@ protected boolean runInEQ () {
}

public void testOwnerIsWindow() {
Window owner = new Frame();
NbPresenter p = new NbPresenter(DESCRIPTOR, owner, Dialog.ModalityType.APPLICATION_MODAL);
Frame owner = new Frame();
NbPresenter p = new NbPresenter(DESCRIPTOR, owner, true);
assertSame(owner, p.getOwner());
}

public void testTitleIsFromDescriptor() {
NbPresenter p = new NbPresenter(DESCRIPTOR, null, Dialog.ModalityType.APPLICATION_MODAL);
NbPresenter p = new NbPresenter(DESCRIPTOR, (Frame) null, true);
assertEquals(TITLE, p.getTitle());
}

public void testModalityIsSet() {
NbPresenter p = new NbPresenter(DESCRIPTOR, null, Dialog.ModalityType.APPLICATION_MODAL);
assertEquals(Dialog.ModalityType.APPLICATION_MODAL, p.getModalityType());
}

public void testDialogsOptionsOnDefaultSystem () {
System.setProperty ("xtest.looks_as_mac", "false");
doTestDialogsOptions ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void run() {
assertNotNull("There is parent window", root);
assertTrue("It is a dialog", root instanceof JDialog);
JDialog d = (JDialog)root;
assertNull("d should have no owner", d.getParent());
assertEquals("The owner of d is the same as owner of dialog without owner", new JDialog().getParent(), d.getParent());

SwingUtilities.invokeAndWait(new Runnable () {
public void run() {
Expand Down