我的RCP应用程序中有一个简单的文件对话框,它允许用户按照下面的代码片段选择一个文件。
Label filePathLabel = new Label(composite, SWT.NULL);
filePathLabel.setText("File Path");
Text filePathText = new Text(composite, SWT.BORDER);
filePathText.setText("");
Button browseButton = new Button(composite, SWT.PUSH);
FileDialog fileDialog = new FileDialog(getShell(), SWT.SAVE);
fileDialog.setFilterExtensions(new String[] {"*.txt"});
fileDialog.setFilterNames(new String[] {"Textfiles(*.txt)"});
browseButton.addSelectionListener(new SelectionAdapter()
{
@override
public void widgetSelected(final SelectionEvent e)
{
String path = fileDialog.open();
if(path != null && !path.isEmpty())
{
filePathText.setText(path);
}
}
});我面临的问题是,在关闭RCP应用程序并再次启动它之后,我无法获得文件的前一个浏览位置,因为所有控件(文本、FileDialog)都将被重新创建。我保存fileDialog.open的结果,它返回路径并设置filePathText文本控件的setText(Text text)方法,每当我的WizardPage被重新打开以显示所选的前一个浏览位置时,但是在关闭RCP应用程序之后,我会失去对浏览位置的访问权限,所以下次重新打开应用程序时,我无法将filePathText文本设置为以前浏览过的位置,即使在单击“浏览”按钮之后,它也指向以前浏览过的位置,但是我需要在单击“浏览”按钮之前就知道以前浏览过的位置,以便可以在Text控件中显示它。
我在这个网站上找到了一些建议-- https://dzone.com/articles/remember-state,但我认为它不会帮助我记住相对于FileDialog的浏览位置的状态。
如果我漏掉了什么,请纠正我。
发布于 2020-07-16 09:17:55
使用链接中提到的IDialogSettings保存和还原向导的信息。向导提供了一些帮助方法。
在主Wizard类的构造函数中,设置向导应该使用的对话框设置。这可能是:
public MyWizard()
{
setDialogSettings(Activator.getDefault().getDialogSettings());
}其中,Activator是插件的激活器(只有在激活器扩展AbstractUIPlugin时才有效)。
完成此操作后,WizardPage可以访问以下设置:
IDialogSettings settings = getDialogSettings()当“文件”对话框返回该位置时,可以将其保存在设置中:
settings.put("path", path);在创建文件路径Text时,可以检查是否保存了一个值:
String savedPath = settings.get("path");
if (savedPath != null) {
filePathText.setText(savedPath);
}https://stackoverflow.com/questions/62930766
复制相似问题