Skip to content

Commit

Permalink
Revert "[cleanup] Update usage of deprecated preferences API"
Browse files Browse the repository at this point in the history
This reverts commit bf20822. It seems
to have caused at least one regression detected by
XmlConstraintProviderTest.test_getConstraintDisabledByDefault. Will
try to move to the new APIs again in a later version.

Signed-off-by: Pierre-Charles David <pierre-charles.david@obeo.fr>
  • Loading branch information
pcdavid committed Apr 22, 2023
1 parent 08e0bab commit 4fb584f
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 56 deletions.
@@ -1,5 +1,5 @@
/******************************************************************************
* Copyright (c) 2004, 2009, 2023 IBM Corporation, Zeligsoft Inc., and others.
* Copyright (c) 2004, 2009 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,8 +27,6 @@
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 @@ -84,10 +82,14 @@ 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 @@ -103,30 +105,39 @@ && 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 @@ -135,6 +146,7 @@ public void removeExtension(IExtension extension, Object[] objects) {
// client-context IDs cannot be undefined
}
};

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

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

// Get the display if we are in the display thread
Expand All @@ -183,15 +193,18 @@ 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 = preferences.getBoolean(IPreferenceConstants.VALIDATION_LIVE_SHOW_CONSOLE,
false);
final boolean showConsole = ValidationUIPlugin.getPlugin().getPluginPreferences()
.getBoolean(IPreferenceConstants.VALIDATION_LIVE_SHOW_CONSOLE);

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

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

// 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 @@ -290,6 +310,7 @@ 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 @@ -445,10 +466,8 @@ protected Control createDialogArea(Composite composite) {
public void widgetSelected(SelectionEvent e) {
// toggle the preference setting for display of live
// warnings according to user's selection
IEclipsePreferences preferences = InstanceScope.INSTANCE
.getNode(ValidationUIPlugin.getPlugin().getBundle().getSymbolicName());
preferences.putBoolean(IPreferenceConstants.VALIDATION_LIVE_WARNINGS_IN_DIALOG,
!checkbox.getSelection());
ValidationUIPlugin.getPlugin().getPluginPreferences().setValue(
IPreferenceConstants.VALIDATION_LIVE_WARNINGS_IN_DIALOG, !checkbox.getSelection());
}
});
}
Expand Down
@@ -1,5 +1,5 @@
/******************************************************************************
* Copyright (c) 2004, 2007, 2023 IBM Corporation and others.
* Copyright (c) 2004, 2007 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,9 +11,8 @@
****************************************************************************/
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 @@ -30,13 +29,18 @@ public PreferenceInitializer() {
super();
}

/*
* (non-Javadoc) Implements the inherited method.
*/
@Override
public void initializeDefaultPreferences() {
IEclipsePreferences preferenceDefaults = DefaultScope.INSTANCE.getNode(ValidationUIPlugin.getPlugin().getBundle().getSymbolicName());
Preferences prefs = ValidationUIPlugin.getPlugin().getPluginPreferences();

// validation preference defaults
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);
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);
}

}
@@ -1,5 +1,5 @@
/******************************************************************************
* Copyright (c) 2004, 2007, 2023 IBM Corporation and others.
* Copyright (c) 2004, 2007 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,8 +14,6 @@
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 @@ -41,11 +39,13 @@ public enum ValidationLiveProblemsDestination implements Enumerator {
* @return the preference setting
*/
public static ValidationLiveProblemsDestination getPreferenceSetting() {
IEclipsePreferences preferences = InstanceScope.INSTANCE.getNode(ValidationUIPlugin.getPlugin().getSymbolicName());
String name = preferences.get(IPreferenceConstants.VALIDATION_LIVE_PROBLEMS_DISPLAY, "");
String name = ValidationUIPlugin.getPlugin().getPluginPreferences()
.getString(IPreferenceConstants.VALIDATION_LIVE_PROBLEMS_DISPLAY);

if (name.equals(DIALOG.getName())) {
return DIALOG;
}

return CONSOLE;
}

Expand Down
@@ -1,5 +1,5 @@
/******************************************************************************
* Copyright (c) 2004, 2007, 2023 IBM Corporation and others.
* Copyright (c) 2004, 2007 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,7 +11,6 @@
****************************************************************************/
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 @@ -25,7 +24,6 @@
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 @@ -183,15 +181,15 @@ protected IPreferenceStore doGetPreferenceStore() {
@Override
public boolean performOk() {
super.performOk();
try {
InstanceScope.INSTANCE.getNode(ValidationUIPlugin.getPlugin().getSymbolicName()).flush();
} catch (BackingStoreException e) {
ValidationUIPlugin.getPlugin().getLog().warn(e.getMessage(), e);
}

ValidationUIPlugin.getPlugin().savePluginPreferences();

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, 2023 IBM Corporation and others.
* Copyright (c) 2003, 2007 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,15 +9,13 @@
* Contributors:
* IBM Corporation - initial API and implementation
****************************************************************************/

package org.eclipse.emf.validation.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.core.runtime.Preferences;
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 @@ -27,6 +25,8 @@
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,11 +38,7 @@ private EMFModelValidationPreferences() {
* Saves the preferences.
*/
public static void save() {
try {
InstanceScope.INSTANCE.getNode(EMFModelValidationPlugin.getPluginId()).flush();
} catch (BackingStoreException e) {
EMFModelValidationPlugin.getPlugin().getLog().warn(e.getMessage(), e);
}
EMFModelValidationPlugin.getPlugin().savePluginPreferences();
}

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

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

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

IEclipsePreferences preferences = InstanceScope.INSTANCE.getNode(EMFModelValidationPlugin.getPluginId());
preferences.putBoolean(prefName, disabled);
prefs.setValue(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
preferences.remove(prefName);
prefs.setToDefault(prefName);
}
}

/**
* @since 1.4
*/
public static void setConstraintDisabledDefault(String id, boolean disabled) {
IEclipsePreferences preferenceDefaults = DefaultScope.INSTANCE.getNode(EMFModelValidationPlugin.getPluginId());
preferenceDefaults.putBoolean(CONSTRAINT_DISABLED_PREFIX + id, disabled);
final String prefName = CONSTRAINT_DISABLED_PREFIX + id;

prefs.setDefault(prefName, disabled);
}
}

0 comments on commit 4fb584f

Please sign in to comment.