May be you could see how Eclipse itself does it:
FindAndReplaceDialog.java
/**
* Creates a new dialog with the given shell as parent.
* @param parentShell the parent shell
*/
public FindReplaceDialog(Shell parentShell) {
super(parentShell);
fParentShell= null;
[...]
readConfiguration();
setShellStyle(SWT.CLOSE | SWT.MODELESS | SWT.BORDER | SWT.TITLE | SWT.RESIZE);
setBlockOnOpen(false);
}
/**
* Returns this dialog's parent shell.
* @return the dialog's parent shell
*/
public Shell getParentShell() {
return super.getParentShell();
}
/**
* Sets the parent shell of this dialog to be the given shell.
*
* @param shell the new parent shell
*/
public void setParentShell(Shell shell) {
if (shell != fParentShell) {
if (fParentShell != null)
fParentShell.removeShellListener(fActivationListener);
fParentShell= shell;
fParentShell.addShellListener(fActivationListener);
}
fActiveShell= shell;
}
It does manage its parent shell depending on the focus of the Dialog.
/**
* Updates the find replace dialog on activation changes.
*/
class ActivationListener extends ShellAdapter {
/*
* @see ShellListener#shellActivated(ShellEvent)
*/
public void shellActivated(ShellEvent e) {
fActiveShell= (Shell)e.widget;
updateButtonState();
if (fGiveFocusToFindField && getShell() == fActiveShell &&
okToUse(fFindField))
fFindField.setFocus();
}
/*
* @see ShellListener#shellDeactivated(ShellEvent)
*/
public void shellDeactivated(ShellEvent e) {
fGiveFocusToFindField= false;
storeSettings();
[...]
fActiveShell= null;
updateButtonState();
}
}
A ShellAdapter
is provides default implementations for the methods described by the ShellListener
interface, which provides methods that deal with changes in state of Shell
.