开发者

How to store Eclipse plug-in state between sessions?

I am working on an Eclipse plugin. The plugin reminds the user to save his work in a central repository every time he saves his work locally.

However, once the user successfully saves his work on a central repository for 10 times he will no longer be reminded to save his work.

This works well in a single session. That is, when the user starts working in the workspace and enables the plugin.

However, if the user exits from the workspace after saving his work to the central repository for 9 times, he will continue to be reminded for 10 more times i.开发者_高级运维e from the scratch, the next time he opens his workspace.

I want to know, if it possible to increment a counter and store it in the memory, so that the plugin works as intended.


You may use the plugin Settings to store and retrieve values for your plugin.
The example from Eclipse FAQ:

 private void savePluginSettings() {
  // saves plugin preferences at the workspace level
  Preferences prefs =
    //Platform.getPreferencesService().getRootNode().node(Plugin.PLUGIN_PREFEERENCES_SCOPE).node(MY_PLUGIN_ID);
    new InstanceScope().getNode(MY_PLUGIN_ID); // does all the above behind the scenes

  prefs.put(KEY1, this.someStr);
  prefs.put(KEY2, this.someBool);

  try {
    // prefs are automatically flushed during a plugin's "super.stop()".
    prefs.flush();
  } catch(BackingStoreException e) {
    //TODO write a real exception handler.
    e.printStackTrace();
  }
}

private void loadPluginSettings() {
  Preferences prefs = new InstanceScope().getNode(MY_PLUGIN_ID);
  // you might want to call prefs.sync() if you're worried about others changing your settings
  this.someStr = prefs.get(KEY1);
  this.someBool= prefs.getBoolean(KEY2);
}


You need to save your state to disk. The details of how to do this are completely up to you. There are numerous ways to do this. One way is to use Eclipse preferences API. It is based on the concept of different scopes... install vs. workspace vs. project. I believe that the workspace-level ones are tracked by InstanceScope class...

http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse.platform.doc.isv/reference/api/org/eclipse/core/runtime/preferences/InstanceScope.html

This API will give you ability to store arbitrary content in a tree-like structure and then read it back out. Make sure that the first node in the tree is your plugin id to avoid collisions with other plugins.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜