我正在尝试将我的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 我google了很多,发现了很多关于这个的线程,但我找不到我的错误= /。 任何人都有一个建议,为什么我的默认首选项初始化程序不会被调用?
提前致谢
答案 0 :(得分:0)
您仍必须使用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);
}
答案 1 :(得分:0)
最后,我找到了解决此问题的方法。 意外地再次解决了这个问题,错误发生在Activator中。我错误地将ID设置为错误的名称。我把它重置为我的项目名称,现在它正在工作!
public ScopedPreferenceStore getPreferenceStore() {
if (null == preferenceStore) {
synchronized (this) {
if (null == preferenceStore)
preferenceStore = new ScopedPreferenceStore(ConfigurationScope.INSTANCE, ID);
}
}
return preferenceStore;
}
ID =项目名称