如何在会话之间存储Eclipse插件状态? [英] How to store Eclipse plug-in state between sessions?

查看:132
本文介绍了如何在会话之间存储Eclipse插件状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Eclipse插件。该插件提醒用户每次在本地保存工作时将他的工作保存在中央存储库中。



但是,一旦用户成功将其工作保存在中央存储库中10次​​,他将不再需要提醒他的工作。



这在单个会话中运行良好。也就是说,当用户开始工作并启用插件时。



但是,如果用户在将工作保存到中央存储库后退出工作空间
为9次,他将继续提醒10次,即从头开始,下次打开工作区。



我想知道,如果可以增加计数器并将其存储在存储器中,以便插件按照预期工作。

解决方案

您可以使用插件设置来存储和检索插件的值。

Eclipse常见问题解答

  private void savePluginSettings(){
//在工作空间级别保存插件首选项
Preferences prefs =
//Platform.getPreferencesService().getRootNode().node(Plugin.PLUGIN_PREFEERENCES_SCOPE).node(MY_PLUGIN_ID);
new InstanceScope()。getNode(MY_PLUGIN_ID); //所有以上幕后的

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

try {
//在插件的super.stop()期间,prefs会被自动刷新。
prefs.flush();
} catch(BackingStoreException e){
// TODO写一个真正的异常处理程序。
e.printStackTrace();
}
}

private void loadPluginSettings(){
Preferences prefs = new InstanceScope()。getNode(MY_PLUGIN_ID);
//如果您担心其他人更改您的设置,您可能需要调用prefs.sync()
this.someStr = prefs.get(KEY1);
this.someBool = prefs.getBoolean(KEY2);
}


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);
}

这篇关于如何在会话之间存储Eclipse插件状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆