简体   繁体   English

如何在会话之间存储Eclipse插件状态?

[英]How to store Eclipse plug-in state between sessions?

I am working on an Eclipse plugin. 我正在研究Eclipse插件。 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. 但是,一旦用户成功将他的工作保存在中央存储库中10次,他将不再被提醒保存他的工作。

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 ie from the scratch, the next time he opens his workspace. 但是,如果用户在将工作保存到中央存储库9次后退出工作区,他将继续被提醒10次,即从头开始,下次打开工作区时。

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 : 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. 一种方法是使用Eclipse首选项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... 我相信InstanceScope类会跟踪工作区级别的...

http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse.platform.doc.isv/reference/api/org/eclipse/core/runtime/preferences/InstanceScope.html 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. 此API将使您能够以树状结构存储任意内容,然后将其读回。 Make sure that the first node in the tree is your plugin id to avoid collisions with other plugins. 确保树中的第一个节点是您的插件ID,以避免与其他插件发生冲突。

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

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