简体   繁体   中英

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 . 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.

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

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.

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 = 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. 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
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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