Skip to content

Commit

Permalink
[cleanup] Update usage of deprecated preferences API
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre-Charles David <pierre-charles.david@obeo.fr>
  • Loading branch information
pcdavid committed Apr 15, 2023
1 parent 01b6488 commit bf20822
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 71 deletions.
@@ -1,5 +1,5 @@
/******************************************************************************
* Copyright (c) 2004, 2009 IBM Corporation, Zeligsoft Inc., and others.
* Copyright (c) 2004, 2009, 2023 IBM Corporation, Zeligsoft Inc., and others.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
Expand Down Expand Up @@ -27,6 +27,8 @@
import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker;
import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;
import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.emf.validation.model.EvaluationMode;
import org.eclipse.emf.validation.model.IConstraintStatus;
import org.eclipse.emf.validation.service.IValidationListener;
Expand Down Expand Up @@ -82,14 +84,10 @@ public LiveValidationListener() {
this.outputUtility = new OutputUtility();
}

/*
* (non-Javadoc) Implements the interface method.
*/
@Override
public void validationOccurred(ValidationEvent event) {
if ((event.getEvaluationMode() == EvaluationMode.LIVE) && (event.getSeverity() >= IStatus.WARNING)
&& isSupportedClientContexts(event.getClientContextIds())) {

showProblemMessages(event);
}
}
Expand All @@ -105,39 +103,30 @@ && isSupportedClientContexts(event.getClientContextIds())) {
* otherwise
*/
private synchronized static boolean isSupportedClientContexts(Collection<String> clientContextIds) {

// take a copy that is safe against concurrent writes
final Set<String> registeredIds = registeredClientContextIds;

for (String next : clientContextIds) {
if (registeredIds.contains(next)) {
return true;
}
}

return false;
}

private static void initializeClientContextIDs() {
registeredClientContextIds = new java.util.HashSet<>();

IExtensionPoint extPoint = Platform.getExtensionRegistry().getExtensionPoint(EP_UI_REGISTERED_CLIENT_CONTEXTS);

for (IExtension extension : extPoint.getExtensions()) {
for (IConfigurationElement next : extension.getConfigurationElements()) {

registeredClientContextIds.add(next.getAttribute(A_ID));
}
}

IExtensionTracker extTracker = ValidationUIPlugin.getExtensionTracker();

if (extTracker != null) {
IExtensionChangeHandler extensionHandler = new IExtensionChangeHandler() {

@Override
public void addExtension(IExtensionTracker tracker, IExtension extension) {

addClientContextIDs(extension.getConfigurationElements());
}

Expand All @@ -146,7 +135,6 @@ public void removeExtension(IExtension extension, Object[] objects) {
// client-context IDs cannot be undefined
}
};

extTracker.registerHandler(extensionHandler, ExtensionTracker.createExtensionPointFilter(extPoint));
}
}
Expand Down Expand Up @@ -177,8 +165,10 @@ private void showProblemMessages(ValidationEvent event) {
}

final ValidationLiveProblemsDestination destination = ValidationLiveProblemsDestination.getPreferenceSetting();
final boolean warningsInDialog = ValidationUIPlugin.getPlugin().getPluginPreferences()
.getBoolean(IPreferenceConstants.VALIDATION_LIVE_WARNINGS_IN_DIALOG);
IEclipsePreferences preferences = InstanceScope.INSTANCE
.getNode(ValidationUIPlugin.getPlugin().getBundle().getSymbolicName());
final boolean warningsInDialog = preferences.getBoolean(IPreferenceConstants.VALIDATION_LIVE_WARNINGS_IN_DIALOG,
false);
final String messages = getProblemMessages(event);

// Get the display if we are in the display thread
Expand All @@ -193,18 +183,15 @@ private void showProblemMessages(ValidationEvent event) {
if (destination == ValidationLiveProblemsDestination.CONSOLE
|| (!getOutputUtility().hasErrors() && !warningsInDialog) || display == null) {
if (messages.length() > 0) {

println(ValidationUIMessages.Validation_problems);
println(messages);
}

final boolean showConsole = ValidationUIPlugin.getPlugin().getPluginPreferences()
.getBoolean(IPreferenceConstants.VALIDATION_LIVE_SHOW_CONSOLE);
final boolean showConsole = preferences.getBoolean(IPreferenceConstants.VALIDATION_LIVE_SHOW_CONSOLE,
false);

if (getOutputUtility().hasProblems()) {
if (showConsole) {
showConsole();
}
if (getOutputUtility().hasProblems() && showConsole) {
showConsole();
}
} else if (destination == ValidationLiveProblemsDestination.DIALOG) {
showLiveValidationDialog(event);
Expand All @@ -222,15 +209,8 @@ private void showLiveValidationDialog(final ValidationEvent event) {
if (workbenchWindow != null) {
IStatus[] details = toStatusArray(event);

String message = event.getSeverity() >= IStatus.ERROR ? ValidationUIMessages.Validation_liveError : null; // show
// the
// constraint
// message
// as
// the
// primary
// message

// show the constraint message as the primary message
String message = event.getSeverity() >= IStatus.ERROR ? ValidationUIMessages.Validation_liveError : null;
// the dialog should show INFO severity for errors because
// the corrective action has already been taken by the
// system; the user is just being informed that everything
Expand Down Expand Up @@ -310,7 +290,6 @@ static IStatus getFirstStatus(IStatus[] statuses, int severity) {
*/
private static IStatus[] toStatusArray(ValidationEvent event) {
List<IConstraintStatus> results = event.getValidationResults();

return results.toArray(new IStatus[results.size()]);
}

Expand Down Expand Up @@ -466,8 +445,10 @@ protected Control createDialogArea(Composite composite) {
public void widgetSelected(SelectionEvent e) {
// toggle the preference setting for display of live
// warnings according to user's selection
ValidationUIPlugin.getPlugin().getPluginPreferences().setValue(
IPreferenceConstants.VALIDATION_LIVE_WARNINGS_IN_DIALOG, !checkbox.getSelection());
IEclipsePreferences preferences = InstanceScope.INSTANCE
.getNode(ValidationUIPlugin.getPlugin().getBundle().getSymbolicName());
preferences.putBoolean(IPreferenceConstants.VALIDATION_LIVE_WARNINGS_IN_DIALOG,
!checkbox.getSelection());
}
});
}
Expand Down
@@ -1,5 +1,5 @@
/******************************************************************************
* Copyright (c) 2004, 2007 IBM Corporation and others.
* Copyright (c) 2004, 2007, 2023 IBM Corporation and others.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
Expand All @@ -11,8 +11,9 @@
****************************************************************************/
package org.eclipse.emf.validation.ui.internal.preferences;

import org.eclipse.core.runtime.Preferences;
import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;
import org.eclipse.core.runtime.preferences.DefaultScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.emf.validation.ui.internal.ValidationUIPlugin;

/**
Expand All @@ -29,18 +30,13 @@ public PreferenceInitializer() {
super();
}

/*
* (non-Javadoc) Implements the inherited method.
*/
@Override
public void initializeDefaultPreferences() {
Preferences prefs = ValidationUIPlugin.getPlugin().getPluginPreferences();

IEclipsePreferences preferenceDefaults = DefaultScope.INSTANCE.getNode(ValidationUIPlugin.getPlugin().getBundle().getSymbolicName());
// validation preference defaults
prefs.setDefault(IPreferenceConstants.VALIDATION_LIVE_PROBLEMS_DISPLAY,
ValidationLiveProblemsDestination.DIALOG.getName());
prefs.setDefault(IPreferenceConstants.VALIDATION_LIVE_WARNINGS_IN_DIALOG, true);
prefs.setDefault(IPreferenceConstants.VALIDATION_LIVE_SHOW_CONSOLE, false);
preferenceDefaults.put(IPreferenceConstants.VALIDATION_LIVE_PROBLEMS_DISPLAY, ValidationLiveProblemsDestination.DIALOG.getName());
preferenceDefaults.putBoolean(IPreferenceConstants.VALIDATION_LIVE_WARNINGS_IN_DIALOG, true);
preferenceDefaults.putBoolean(IPreferenceConstants.VALIDATION_LIVE_SHOW_CONSOLE, false);
}

}
@@ -1,5 +1,5 @@
/******************************************************************************
* Copyright (c) 2004, 2007 IBM Corporation and others.
* Copyright (c) 2004, 2007, 2023 IBM Corporation and others.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
Expand All @@ -14,6 +14,8 @@
import java.util.Arrays;
import java.util.List;

import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.emf.common.util.Enumerator;
import org.eclipse.emf.validation.ui.internal.ValidationUIPlugin;

Expand All @@ -39,13 +41,11 @@ public enum ValidationLiveProblemsDestination implements Enumerator {
* @return the preference setting
*/
public static ValidationLiveProblemsDestination getPreferenceSetting() {
String name = ValidationUIPlugin.getPlugin().getPluginPreferences()
.getString(IPreferenceConstants.VALIDATION_LIVE_PROBLEMS_DISPLAY);

IEclipsePreferences preferences = InstanceScope.INSTANCE.getNode(ValidationUIPlugin.getPlugin().getSymbolicName());
String name = preferences.get(IPreferenceConstants.VALIDATION_LIVE_PROBLEMS_DISPLAY, "");
if (name.equals(DIALOG.getName())) {
return DIALOG;
}

return CONSOLE;
}

Expand Down
@@ -1,5 +1,5 @@
/******************************************************************************
* Copyright (c) 2004, 2007 IBM Corporation and others.
* Copyright (c) 2004, 2007, 2023 IBM Corporation and others.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
Expand All @@ -11,6 +11,7 @@
****************************************************************************/
package org.eclipse.emf.validation.ui.internal.preferences;

import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.emf.validation.ui.internal.ValidationUIPlugin;
import org.eclipse.emf.validation.ui.internal.l10n.ValidationUIMessages;
import org.eclipse.jface.preference.BooleanFieldEditor;
Expand All @@ -24,6 +25,7 @@
import org.eclipse.swt.widgets.Group;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.osgi.service.prefs.BackingStoreException;

/**
* The preference page for Modeler validation controls.
Expand Down Expand Up @@ -181,15 +183,15 @@ protected IPreferenceStore doGetPreferenceStore() {
@Override
public boolean performOk() {
super.performOk();

ValidationUIPlugin.getPlugin().savePluginPreferences();
try {
InstanceScope.INSTANCE.getNode(ValidationUIPlugin.getPlugin().getSymbolicName()).flush();
} catch (BackingStoreException e) {
ValidationUIPlugin.getPlugin().getLog().warn(e.getMessage(), e);
}

return true;
}

/*
* (non-Javadoc) Redefines/Implements/Extends the inherited method.
*/
@Override
public void init(IWorkbench workbench) {
// Nothing to do in this implementation
Expand Down
@@ -1,5 +1,5 @@
/******************************************************************************
* Copyright (c) 2003, 2007 IBM Corporation and others.
* Copyright (c) 2003, 2007, 2023 IBM Corporation and others.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
Expand All @@ -9,13 +9,15 @@
* Contributors:
* IBM Corporation - initial API and implementation
****************************************************************************/

package org.eclipse.emf.validation.preferences;

import org.eclipse.core.runtime.Preferences;
import org.eclipse.core.runtime.preferences.DefaultScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.emf.validation.internal.EMFModelValidationPlugin;
import org.eclipse.emf.validation.service.ConstraintRegistry;
import org.eclipse.emf.validation.service.IConstraintDescriptor;
import org.osgi.service.prefs.BackingStoreException;

/**
* Preferences manager for the EMF model validation plug-in.
Expand All @@ -25,8 +27,6 @@
public class EMFModelValidationPreferences {
static final String CONSTRAINT_DISABLED_PREFIX = "con.disabled/"; //$NON-NLS-1$

private static final Preferences prefs = EMFModelValidationPlugin.getPlugin().getPluginPreferences();

/**
* Not instantiable, as all features are static.
*/
Expand All @@ -38,7 +38,11 @@ private EMFModelValidationPreferences() {
* Saves the preferences.
*/
public static void save() {
EMFModelValidationPlugin.getPlugin().savePluginPreferences();
try {
InstanceScope.INSTANCE.getNode(EMFModelValidationPlugin.getPluginId()).flush();
} catch (BackingStoreException e) {
EMFModelValidationPlugin.getPlugin().getLog().warn(e.getMessage(), e);
}
}

/**
Expand All @@ -48,7 +52,8 @@ public static void save() {
* @return whether it is disabled
*/
public static boolean isConstraintDisabled(String id) {
return prefs.getBoolean(CONSTRAINT_DISABLED_PREFIX + id);
IEclipsePreferences preferences = InstanceScope.INSTANCE.getNode(EMFModelValidationPlugin.getPluginId());
return preferences.getBoolean(CONSTRAINT_DISABLED_PREFIX + id, false);
}

/**
Expand All @@ -59,7 +64,8 @@ public static boolean isConstraintDisabled(String id) {
* @return whether it is disabled
*/
public static boolean isConstraintDisabledByDefault(String id) {
return prefs.getDefaultBoolean(CONSTRAINT_DISABLED_PREFIX + id);
IEclipsePreferences preferenceDefaults = DefaultScope.INSTANCE.getNode(EMFModelValidationPlugin.getPluginId());
return preferenceDefaults.getBoolean(CONSTRAINT_DISABLED_PREFIX + id, false);
}

/**
Expand All @@ -72,23 +78,23 @@ public static void setConstraintDisabled(String id, boolean disabled) {
final String prefName = CONSTRAINT_DISABLED_PREFIX + id;
final IConstraintDescriptor constraint = ConstraintRegistry.getInstance().getDescriptor(id);

prefs.setValue(prefName, disabled);
IEclipsePreferences preferences = InstanceScope.INSTANCE.getNode(EMFModelValidationPlugin.getPluginId());
preferences.putBoolean(prefName, disabled);

if (constraint != null) {
// set its enablement from the new preference value
constraint.setEnabled(!disabled);
} else {
// remove this preference to declutter the prefs.ini file
prefs.setToDefault(prefName);
preferences.remove(prefName);
}
}

/**
* @since 1.4
*/
public static void setConstraintDisabledDefault(String id, boolean disabled) {
final String prefName = CONSTRAINT_DISABLED_PREFIX + id;

prefs.setDefault(prefName, disabled);
IEclipsePreferences preferenceDefaults = DefaultScope.INSTANCE.getNode(EMFModelValidationPlugin.getPluginId());
preferenceDefaults.putBoolean(CONSTRAINT_DISABLED_PREFIX + id, disabled);
}
}

0 comments on commit bf20822

Please sign in to comment.