How to keep the state of the Eclipse plug-in between sessions?

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

However, as soon as the user successfully saves his work in the central repository 10 times, he will no longer be reminded that he will save his work.

This works well in one session. That is, when the user starts working in the workspace and turns on the plugin.

However, if the user leaves the workspace after saving his work to the central repository 9 times, he will be reminded 10 more times from scratch, the next time he will open the workspace.

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

+3
source share
2 answers

You can use the plugin settings to store and retrieve values ​​for your plugin.
Example from Eclipse Frequently Asked Questions :

 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 "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);
}
+7
source

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

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

API , . , node - , .

+2

Source: https://habr.com/ru/post/1787583/


All Articles