fix preferences storage

This commit is contained in:
Eyck Jentzsch 2020-03-21 15:17:34 +01:00
parent 348ffe20d6
commit 767b083a22
6 changed files with 56 additions and 20 deletions

View File

@ -11,8 +11,6 @@ import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Widget; import org.eclipse.swt.widgets.Widget;
import com.minres.scviewer.database.swt.Constants; import com.minres.scviewer.database.swt.Constants;

View File

@ -31,6 +31,7 @@ import javax.annotation.PostConstruct;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import org.eclipse.core.internal.preferences.InstancePreferences;
import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus; import org.eclipse.core.runtime.MultiStatus;
@ -41,9 +42,11 @@ import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.Job; import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.jobs.JobChangeAdapter; import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.core.runtime.jobs.JobGroup; import org.eclipse.core.runtime.jobs.JobGroup;
import org.eclipse.core.runtime.preferences.ConfigurationScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences; import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener; import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener;
import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent; import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.di.annotations.Optional; import org.eclipse.e4.core.di.annotations.Optional;
import org.eclipse.e4.core.di.extensions.Preference; import org.eclipse.e4.core.di.extensions.Preference;
import org.eclipse.e4.core.services.events.IEventBroker; import org.eclipse.e4.core.services.events.IEventBroker;
@ -81,6 +84,7 @@ import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn; import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem; import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Widget; import org.eclipse.swt.widgets.Widget;
import org.osgi.service.prefs.BackingStoreException;
import com.minres.scviewer.database.DataType; import com.minres.scviewer.database.DataType;
import com.minres.scviewer.database.ITx; import com.minres.scviewer.database.ITx;
@ -192,8 +196,8 @@ public class WaveformViewer implements IFileChangeListener, IPreferenceChangeLis
/** The prefs. */ /** The prefs. */
@Inject @Inject
@Preference(nodePath = PreferenceConstants.PREFERENCES_SCOPE) @Preference(value = ConfigurationScope.SCOPE, nodePath = PreferenceConstants.PREFERENCES_SCOPE)
IEclipsePreferences prefs; protected IEclipsePreferences prefs;
@Inject @Optional DesignBrowser designBrowser; @Inject @Optional DesignBrowser designBrowser;
@ -465,17 +469,21 @@ public class WaveformViewer implements IFileChangeListener, IPreferenceChangeLis
*/ */
@Override @Override
public void preferenceChange(PreferenceChangeEvent event) { public void preferenceChange(PreferenceChangeEvent event) {
InstancePreferences pref = (InstancePreferences)event.getSource();
if (PreferenceConstants.DATABASE_RELOAD.equals(event.getKey())) { if (PreferenceConstants.DATABASE_RELOAD.equals(event.getKey())) {
checkForUpdates = (Boolean) event.getNewValue(); checkForUpdates = pref.getBoolean(PreferenceConstants.DATABASE_RELOAD, true);
fileChecker = null; fileChecker = null;
if (checkForUpdates) if (checkForUpdates)
fileChecker = fileMonitor.addFileChangeListener(WaveformViewer.this, filesToLoad, fileChecker = fileMonitor.addFileChangeListener(WaveformViewer.this, filesToLoad,
FILE_CHECK_INTERVAL); FILE_CHECK_INTERVAL);
else else
fileMonitor.removeFileChangeListener(this); fileMonitor.removeFileChangeListener(this);
} else { } else if (!PreferenceConstants.SHOW_HOVER.equals(event.getKey())){
setupColors(); setupColors();
} }
try {
pref.flush();
} catch (BackingStoreException e) { }
} }
/** /**

View File

@ -11,15 +11,14 @@
package com.minres.scviewer.e4.application.preferences; package com.minres.scviewer.e4.application.preferences;
import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer; import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;
import org.eclipse.core.runtime.preferences.InstanceScope; import org.eclipse.core.runtime.preferences.DefaultScope;
import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.jface.resource.StringConverter; import org.eclipse.jface.resource.StringConverter;
import org.eclipse.swt.SWT; import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Color;
import org.eclipse.wb.swt.SWTResourceManager; import org.eclipse.wb.swt.SWTResourceManager;
import com.minres.scviewer.database.ui.WaveformColors; import com.minres.scviewer.database.ui.WaveformColors;
import com.opcoach.e4.preferences.ScopedPreferenceStore;
/** /**
* The Class DefaultValuesInitializer. * The Class DefaultValuesInitializer.
@ -64,13 +63,22 @@ public class DefaultValuesInitializer extends AbstractPreferenceInitializer {
*/ */
@Override @Override
public void initializeDefaultPreferences() { public void initializeDefaultPreferences() {
IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, PreferenceConstants.PREFERENCES_SCOPE); IEclipsePreferences node = DefaultScope.INSTANCE.getNode(PreferenceConstants.PREFERENCES_SCOPE);
if (node != null)
store.setDefault(PreferenceConstants.DATABASE_RELOAD, true); {
store.setDefault(PreferenceConstants.SHOW_HOVER, true); node.putBoolean(PreferenceConstants.DATABASE_RELOAD, true);
for (WaveformColors c : WaveformColors.values()) { node.putBoolean(PreferenceConstants.SHOW_HOVER, true);
store.setDefault(c.name()+"_COLOR", StringConverter.asString(colors[c.ordinal()].getRGB())); //$NON-NLS-1$ for (WaveformColors c : WaveformColors.values()) {
} node.put(c.name()+"_COLOR", StringConverter.asString(colors[c.ordinal()].getRGB())); //$NON-NLS-1$
}
}
// IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, PreferenceConstants.PREFERENCES_SCOPE);
//
// store.setDefault(PreferenceConstants.DATABASE_RELOAD, true);
// store.setDefault(PreferenceConstants.SHOW_HOVER, true);
// for (WaveformColors c : WaveformColors.values()) {
// store.setDefault(c.name()+"_COLOR", StringConverter.asString(colors[c.ordinal()].getRGB())); //$NON-NLS-1$
// }
} }
} }

View File

@ -0,0 +1,18 @@
package com.minres.scviewer.e4.application.preferences;
import org.eclipse.core.runtime.preferences.ConfigurationScope;
import org.eclipse.jface.preference.IPreferenceStore;
import com.opcoach.e4.preferences.IPreferenceStoreProvider;
import com.opcoach.e4.preferences.ScopedPreferenceStore;
public class PreferencesStoreProvider implements IPreferenceStoreProvider{
public PreferencesStoreProvider(){
}
@Override
public IPreferenceStore getPreferenceStore() {
return new ScopedPreferenceStore(ConfigurationScope.INSTANCE, PreferenceConstants.PREFERENCES_SCOPE);
}
}

View File

@ -7,7 +7,7 @@
</configIni> </configIni>
<launcherArgs> <launcherArgs>
<programArgs>-clearPersistedState -data @none <programArgs>-clearPersistedState
</programArgs> </programArgs>
<vmArgs>-Xmx2G <vmArgs>-Xmx2G
</vmArgs> </vmArgs>

View File

@ -46,6 +46,7 @@ public class E4PreferenceRegistry
public static final String PREFS_PAGE_XP = "com.opcoach.e4.preferences.e4PreferencePages"; // $NON-NLS-1$ public static final String PREFS_PAGE_XP = "com.opcoach.e4.preferences.e4PreferencePages"; // $NON-NLS-1$
public static final String PREF_STORE_PROVIDER = "com.opcoach.e4.preferences.e4PreferenceStoreProvider"; // $NON-NLS-1$ public static final String PREF_STORE_PROVIDER = "com.opcoach.e4.preferences.e4PreferenceStoreProvider"; // $NON-NLS-1$
public static final String KEY_PREF_STORE_PROVIDERS = "com.opcoach.e4.preferences.e4PreferenceStoreProviders"; // $NON-NLS-1$
protected static final String ELMT_PAGE = "page"; // $NON-NLS-1$ protected static final String ELMT_PAGE = "page"; // $NON-NLS-1$
protected static final String ATTR_ID = "id"; // $NON-NLS-1$ protected static final String ATTR_ID = "id"; // $NON-NLS-1$
protected static final String ATTR_CATEGORY = "category"; // $NON-NLS-1$ protected static final String ATTR_CATEGORY = "category"; // $NON-NLS-1$
@ -235,6 +236,7 @@ public class E4PreferenceRegistry
IContributionFactory factory = context.get(IContributionFactory.class); IContributionFactory factory = context.get(IContributionFactory.class);
psProviders = new HashMap<String, Object>(); psProviders = new HashMap<String, Object>();
IExtensionRegistry registry = context.get(IExtensionRegistry.class);
// Read extensions and fill the map... // Read extensions and fill the map...
for (IConfigurationElement elmt : registry.getConfigurationElementsFor(PREF_STORE_PROVIDER)) for (IConfigurationElement elmt : registry.getConfigurationElementsFor(PREF_STORE_PROVIDER))
@ -261,7 +263,7 @@ public class E4PreferenceRegistry
Object data = objectId; Object data = objectId;
if (classname != null) if (classname != null)
{ {
data = factory.create(classname, context); data = factory.create("bundleclass://"+declaringBundle+"/"+classname, context);
if (!(data instanceof IPreferenceStoreProvider)) if (!(data instanceof IPreferenceStoreProvider))
{ {
logger.warn("In extension " + PREF_STORE_PROVIDER + " the class must implements IPreferenceStoreProvider. Check the plugin " + declaringBundle); logger.warn("In extension " + PREF_STORE_PROVIDER + " the class must implements IPreferenceStoreProvider. Check the plugin " + declaringBundle);
@ -272,6 +274,8 @@ public class E4PreferenceRegistry
psProviders.put(pluginId, data); psProviders.put(pluginId, data);
} }
context.set(KEY_PREF_STORE_PROVIDERS, psProviders);
} }
} }