【问题标题】:E4 Preference Initializer won´t get calledE4 Preference Initializer 不会被调用
【发布时间】:2016-01-07 23:34:35
【问题描述】:

我正在尝试将我的 e3-rcp-app 迁移到 e4-rcp-app。

因此我需要定义我的默认首选项。 (不是 Pref.Pages)

通过这样做和尝试,我无法调用我的初始化程序。这是我的初始化程序类:

public class MyPreferenceInitializer extends AbstractPreferenceInitializer {

public MyPreferenceInitializer (){}

@Override
public void initializeDefaultPreferences() {

Preferences defaults = DefaultScope.INSTANCE.getNode(InspectIT.ID);
          // Set defaults using things like:
          defaults.put("DUMMY", "DUMMYCONTENT");
          try {
            defaults.flush();
        } catch (BackingStoreException e) {
            e.printStackTrace();
        }

          //And this other approach to make sure that one of them works
          IPreferenceStore store = InspectIT.getDefault().getPreferenceStore();
          store.setDefault("DUMMY", "DUMMYCONTENT");         
          try {
            ((Preferences) store).flush();
        } catch (BackingStoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //Dummy impl        
       default Preferences....,
    }

}

我还得到了一个具有以下结构的 Activator 类:(只需发布相关方法(?))

 public class Activator implements BundleActivator {

        private static BundleContext context;

        static BundleContext getContext() {
            return context;
        }


        private static Activator plugin;

        private volatile ScopedPreferenceStore  preferenceStore;

        public void start(BundleContext context) throws Exception {
            plugin = this;
            Activator.context = context;
            locateRuntimeDir();     
            logListener = new LogListener();
            Platform.addLogListener(logListener);

//access to my initializor
        String text = getPreferenceStore().getDefaultString("DUMMY");
        String text2 = getPreferenceStore().getString("DUMMY");

        }

        public void stop(BundleContext context) throws Exception {

        Activator.context = null;
        plugin = null; 
    }

    public static <E> E getService(Class<E> clazz) {
        ServiceReference<E> reference = context.getServiceReference(clazz);
        if (null != reference) {  

            return context.getService(reference);
        }
        throw new RuntimeException("Requested service of the class " + clazz.getName() + " is not registered in the bundle.");
    }

    public ScopedPreferenceStore getPreferenceStore() {

        if (null == preferenceStore) {
            synchronized (this) {
                if (null == preferenceStore) { 


                    preferenceStore = new  ScopedPreferenceStore(ConfigurationScope.INSTANCE, ID); 
                } 
            } 
        } 
        return preferenceStore;
    }
}

我正在使用的 ScopedPreferenceStore 位于:https://github.com/opcoach/e4Preferences/tree/master/com.opcoach.e4.preferences

同样,我这样声明了 plugin.xml 扩展(我确实需要这个,对吗?)

...
 <extension
         point="org.eclipse.core.runtime.preferences">
      <initializer            class="MyApplication.rcp.preferences.MyPreferenceInitializer ">
      </initializer>
   </extension>
...

我在 win7 x64 上使用 Eclipse 4.5.1 我用谷歌搜索了很多,发现了很多关于这个的线程,但我找不到我的错误 =/。 有人建议为什么我的默认首选项初始化程序不会被调用?

提前致谢

【问题讨论】:

  • 您不需要在 start 方法中使用 @PostConstruct。不注入激活剂。
  • 谢谢,我解决了这个问题,但我的首选项初始化程序仍然不会被调用。

标签: eclipse preferences rcp e4 initializer


【解决方案1】:

您仍必须使用 org.eclipse.core.runtime.preferences 扩展点来定义首选项初始化程序。

<extension
     point="org.eclipse.core.runtime.preferences">
  <initializer
        class="package.MyPreferenceInitializer">
  </initializer>
</extension>

在初始化器中使用:

@Override
public void initializeDefaultPreferences()
{
  Preferences defaults = DefaultScope.INSTANCE.getNode(Activator.ID);

  // Set defaults using things like:
  defaults.putInt("pref id", 0);
}

【讨论】:

  • 感谢您的评论,我正在使用扩展点(代码刚刚被剪掉,我重新编辑了我的帖子;)
  • 你是如何引用首选项的?
  • 我在初始化程序中通过 IPreferenceStore store = InspectIT.getDefault().getPreferenceStore(); 引用它们Map 初始化条目 = PreferenceSupplierDUMMY.getInitializationEntries(); for(Map.Entry entry : 初始化Entries.entrySet()) { store.setDefault(entry.getKey(), entry.getValue()); } store.setDefault... 等等,就像我读到的:openchrom.wordpress.com/2014/01/11/… 但我什至没有调用这个代码
  • 不确定这是否可行。我已经添加了我用于初始化程序的代码,它确实有效。
  • 我试过了,但初始化程序仍然没有接到电话。这就像 Eclipse 只是跳过了整个课程。不会调用构造函数或 init 方法。
【解决方案2】:

我终于找到了解决这个问题的方法。 不小心又解决了这个问题,错误出在激活器上。我错误地将 ID 设置为错误的名称。我将它重置为我的项目名称,现在它正在工作!

public ScopedPreferenceStore getPreferenceStore() {

    if (null == preferenceStore) {      
        synchronized (this) {
            if (null == preferenceStore) 
            preferenceStore = new ScopedPreferenceStore(ConfigurationScope.INSTANCE, ID); 
        }                   
    } 
    return preferenceStore;
}

ID = 项目名称

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-10
    • 1970-01-01
    • 2020-01-26
    • 2020-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多