问题
Background
I am working on a plugin for uDig, a project build on Eclipse RCP. My plugin contains a table that gets populated with data from the Workbench selection. The table data can be grouped, sorted and filtered by the user. When the workbench selection changes the group, sort and filter information is reset and the new selection data loaded into the table.
Question
How do I save values to he workbench so I can access them later?
When the workbench selection gets changed I wish to save the group, sort and filter state out to the Workbench so that if the workbench selection ever changes back to the saved selection I can restore the state.
Research
I have looked at IViewPart.saveState(IMemento memento) but get the feeling this is only useful when you are adding and removing a view.
I have also looked at using preferences but feel this is an incorrect usage of this resource.
IEclipsePreferences node = InstanceScope.INSTANCE.getNode(PluginUI.PLUGIN_ID);
node.put(key, value);
Finally
I am some what new to Eclipse RCP and may be attacking this problem the wrong way. If there is a better way to save and load this information please feel free to share.
回答1:
From your description, it sounds like you have a view with the table. The text book way to save state for a view, is via IMemento
. You can see the code to use this interface in this presentation. As an alternative, you can also use the IPreferenceStore
.
You usually use preferences when the state has a global scope and memento when they are specific to a view.
回答2:
You can use dialog settings for this.
回答3:
I am assuming you are trying to communicate two views using the selection service as described here http://www.eclipse.org/articles/Article-WorkbenchSelections/article.html
If that is the case you really dont have to save a selection as the results upon selecting are computed on the fly (as that is the whole purpose of the selection service)
But in case you still want to persist the intermediate view data you can always define your custom XML format for persisting the changes to the disk. And then reload them whenever required.
(Another option is Java inbuilt object serialization which can perist the view bound data objects to disk.)
来源:https://stackoverflow.com/questions/9371886/eclipse-rcp-save-values-to-workbench