【问题标题】:SWT FileDialog Browse LocationSWT FileDialog 浏览位置
【发布时间】:2020-11-05 21:21:54
【问题描述】:

我的 RCP 应用程序中有一个简单的文件对话框,允许用户根据下面的代码 sn-p 选择文件

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.open 的结果,它返回路径并设置filePathText 文本控件的setText(Text text) 方法,每当我的WizardPage 重新打开以显示上一个选择的浏览位置但我关闭后我失去了对浏览位置的访问权限RCP 应用程序,所以下次我重新打开我的应用程序时,我无法将 filePathText 文本设置为以前浏览过的位置 即使在我单击浏览按钮后 Eclipse 确实指向以前浏览过的位置但是在我点击浏览按钮之前我需要知道之前浏览过的位置,以便它可以显示在Text控件中。

我在这个网站上找到了一些建议 - https://dzone.com/articles/remember-state,但我认为这不会帮助我记住相对于 FileDialog 的浏览位置状态

如果我在这里遗漏了什么,请纠正我。

【问题讨论】:

    标签: eclipse eclipse-plugin swt eclipse-rcp jface


    【解决方案1】:

    您使用链接中提到的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);
    }
    

    【讨论】:

    • 谢谢!我会尝试这种方法,但你能告诉我这个 Activator 类属于哪个包吗?我在三个不同的包中找到了它——org.eclipse.core.internal.runtime、org.eclipse.core.internal.content 和 org.eclispe.core.internal.preferences。
    • 它是您插件的激活器 - 您可能还没有创建它。
    • 明白你的意思,我将创建一个并尝试这种方法。谢谢
    • 这行得通,再次感谢,只是一个小的编辑,而不是 settings.set(key,value) 它应该是 settings.put(key,value)。
    猜你喜欢
    • 1970-01-01
    • 2014-01-05
    • 2013-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多