简体   繁体   English

如何在Eclipse中保存编辑器状态

[英]How to save an editor state in eclipse

I have created an Editor which opens from a view. 我创建了一个从视图中打开的编辑器。

More specifically, I have created an editor class which extends EditorPart and an editor input which extends IEditorInput . 更具体地说,我创建了一个扩展了EditorPart的编辑器类和一个扩展了IEditorInput的编辑器输入。 I also have created a view, where if you double click an element in the view, the editor will open. 我还创建了一个视图,如果您双击视图中的元素,将打开编辑器。 The editor simply shows a tree. 编辑器仅显示一棵树。

Everything works well. 一切正常。 What I need to do is, to save the state of the editor when I change it, for example when I add a treeItem to the tree. 我需要做的是在更改编辑器时保存其状态,例如,当我将treeItem添加到树中时。

I've read some tutorials, but most of them explain how to save a view state by using mementos. 我已经阅读了一些教程,但是其中大多数教程都说明了如何通过使用备忘录来保存视图状态。 I'm newbie in Eclipse development, so please bear with me :P 我是Eclipse开发的新手,所以请多多包涵:P

If you don't have a file to save in you could put the data in the 'state location' for your plugin - this is a folder in the workspace .metadata/.plugins directory which your plugin can use as you like. 如果没有要保存的文件,则可以将数据放在插件的“状态位置”中-这是工作区.metadata / .plugins目录中的文件夹,您的插件可以随意使用。

You get the state location using: 您可以使用以下方式获取状态位置:

Bundle bundle = Platform.getBundle("your plugin id");

IPath stateLoc = Platform.getStateLocation(bundle);

Note: There are several ways to get the Bundle , for example you can also use: 注意:有几种获取Bundle ,例如,您也可以使用:

Bundle bundle = FrameworkUtil.getBundle(getClass());

which returns the bundle for the current class. 返回当前类的包。

You can save / restore your file in any format you like. 您可以使用任何喜欢的格式保存/恢复文件。 You mention the Memento format. 您提到了Memento格式。 Write a memento using: 使用以下方法编写纪念品:

XMLMemento memento = XMLMemento.createWriteRoot("root");

 ... add your entries

try (Writer writer = new OutputStreamWriter(new FileOutputStream("file name"), StandardCharsets.UTF_8)) 
 {
    memento.save(writer);
 }

Read the memento with something like: 阅读类似以下内容的纪念品:

try (Reader reader = new InputStreamReader(new FileInputStream("file name"), StandardCharsets.UTF_8)) 
 {
   IMemento memento = XMLMemento.createReadRoot(reader);

   ... read the memento contents
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM