我正在编写一个 Eclipse 插件,作为对某些操作的响应,我对开始一系列操作(在单独的工作中)很感兴趣。其中一项操作是请求用户提供文件名,我正在尝试使用 JFace JDialog 来完成此操作。
但是,我不清楚如何以非模式方式执行此操作;例如,我从哪里获得显示器和外壳?当开发人员可以在对话框中编辑内容时,如何确保 UI 继续工作?
也许您可以看到 Eclipse 本身是如何做到的:
/**
* 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;
}
它确实根据对话框的焦点管理其父外壳。
/**
* 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();
}
}
AShellAdapter
为接口描述的方法提供了默认实现ShellListener
,它提供了处理Shell
.
重要的是样式值应该包括 SWT.MODELESS。
样式是您应该关注的 SWT 中最重要的事情之一,因为您可以控制和初始化很多仅因为样式值。