我创建了一个从 View 中打开的编辑器。
更具体地说,我创建了一个扩展 EditorPart
的编辑器类和一个扩展 IEditorInput
的编辑器输入。我还创建了一个 View ,如果双击 View 中的元素,编辑器将打开。编辑器仅显示一棵树。
一切正常。我需要做的是,在更改编辑器时保存编辑器的状态,例如当我向树中添加树项时。
我读过一些教程,但大多数都解释了如何使用备忘录来保存 View 状态。我是 Eclipse 开发的新手,所以请耐心等待:P
最佳答案
如果您没有要保存的文件,您可以将数据放在插件的“状态位置”中 - 这是工作区 .metadata/.plugins 目录中的一个文件夹,您的插件可以根据需要使用它.
您可以使用以下方式获取状态位置:
Bundle bundle = Platform.getBundle("your plugin id");
IPath stateLoc = Platform.getStateLocation(bundle);
注意:有多种方式获取Bundle
,例如您还可以使用:
Bundle bundle = FrameworkUtil.getBundle(getClass());
返回当前类的包。
您可以以任何您喜欢的格式保存/恢复文件。你提到了 Memento 格式。使用以下方式编写纪念品:
XMLMemento memento = XMLMemento.createWriteRoot("root");
... add your entries
try (Writer writer = new OutputStreamWriter(new FileOutputStream("file name"), StandardCharsets.UTF_8))
{
memento.save(writer);
}
阅读以下内容的备忘录:
try (Reader reader = new InputStreamReader(new FileInputStream("file name"), StandardCharsets.UTF_8))
{
IMemento memento = XMLMemento.createReadRoot(reader);
... read the memento contents
}
关于java - 如何在 Eclipse 中保存编辑器状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31919542/