Como usar o JFace FileDialog de dentro de um plugin do Eclipse de uma forma não modal?
Pergunta
Estou escrevendo um plugin do Eclipse, e em resposta a alguma ação estou interessante em iniciar uma série de operações (dentro de um trabalho em separado). Uma dessas operações é solicitar que o usuário forneça um nome de arquivo, o que eu estou tentando fazer com o JFace JDialog.
No entanto, eu não sou claro como fazer isso de uma forma não modal; por exemplo, onde posso obter um monitor e shell? Como posso garantir a interface do usuário continua a trabalhar enquanto o desenvolvedor pode editar coisas no diálogo?
Solução
Pode ser que você pode ver como o próprio Eclipse faz isso:
/**
* 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;
}
Ele faz gerir a sua shell pai dependendo do foco do diálogo.
/**
* 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
é fornece implementações padrão para os métodos descritos pelo ShellListener
interface, que fornece métodos que lidam com mudanças no estado de Shell
.
Outras dicas
A coisa importent é que o valor de estilo deve incluir SWT.MODELESS.
O estilo é uma das coisas mais importantes em SWT que você deve olhar, porque você pode controlar e inicializar um monte só por causa do valor styel.