From 4fb584f65822f39e9ce9721429a29987bd80dbd4 Mon Sep 17 00:00:00 2001 From: Pierre-Charles David Date: Sat, 22 Apr 2023 11:41:10 +0200 Subject: [PATCH] Revert "[cleanup] Update usage of deprecated preferences API" This reverts commit bf20822070b73e9c45d038260afb994f4370eadd. 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 --- .../ui/internal/LiveValidationListener.java | 53 +++++++++++++------ .../preferences/PreferenceInitializer.java | 18 ++++--- .../ValidationLiveProblemsDestination.java | 10 ++-- .../preferences/ValidationPreferencePage.java | 14 +++-- .../EMFModelValidationPreferences.java | 32 +++++------ 5 files changed, 71 insertions(+), 56 deletions(-) diff --git a/bundles/org.eclipse.emf.validation.ui/src/org/eclipse/emf/validation/ui/internal/LiveValidationListener.java b/bundles/org.eclipse.emf.validation.ui/src/org/eclipse/emf/validation/ui/internal/LiveValidationListener.java index 758186e..b313ce3 100644 --- a/bundles/org.eclipse.emf.validation.ui/src/org/eclipse/emf/validation/ui/internal/LiveValidationListener.java +++ b/bundles/org.eclipse.emf.validation.ui/src/org/eclipse/emf/validation/ui/internal/LiveValidationListener.java @@ -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/ @@ -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; @@ -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); } } @@ -103,30 +105,39 @@ && isSupportedClientContexts(event.getClientContextIds())) { * otherwise */ private synchronized static boolean isSupportedClientContexts(Collection clientContextIds) { + // take a copy that is safe against concurrent writes final Set 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()); } @@ -135,6 +146,7 @@ public void removeExtension(IExtension extension, Object[] objects) { // client-context IDs cannot be undefined } }; + extTracker.registerHandler(extensionHandler, ExtensionTracker.createExtensionPointFilter(extPoint)); } } @@ -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 @@ -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); @@ -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 @@ -290,6 +310,7 @@ static IStatus getFirstStatus(IStatus[] statuses, int severity) { */ private static IStatus[] toStatusArray(ValidationEvent event) { List results = event.getValidationResults(); + return results.toArray(new IStatus[results.size()]); } @@ -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()); } }); } diff --git a/bundles/org.eclipse.emf.validation.ui/src/org/eclipse/emf/validation/ui/internal/preferences/PreferenceInitializer.java b/bundles/org.eclipse.emf.validation.ui/src/org/eclipse/emf/validation/ui/internal/preferences/PreferenceInitializer.java index 19bf663..9ebb9dc 100644 --- a/bundles/org.eclipse.emf.validation.ui/src/org/eclipse/emf/validation/ui/internal/preferences/PreferenceInitializer.java +++ b/bundles/org.eclipse.emf.validation.ui/src/org/eclipse/emf/validation/ui/internal/preferences/PreferenceInitializer.java @@ -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/ @@ -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; /** @@ -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); } } diff --git a/bundles/org.eclipse.emf.validation.ui/src/org/eclipse/emf/validation/ui/internal/preferences/ValidationLiveProblemsDestination.java b/bundles/org.eclipse.emf.validation.ui/src/org/eclipse/emf/validation/ui/internal/preferences/ValidationLiveProblemsDestination.java index 42c77f9..83b1fa6 100644 --- a/bundles/org.eclipse.emf.validation.ui/src/org/eclipse/emf/validation/ui/internal/preferences/ValidationLiveProblemsDestination.java +++ b/bundles/org.eclipse.emf.validation.ui/src/org/eclipse/emf/validation/ui/internal/preferences/ValidationLiveProblemsDestination.java @@ -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/ @@ -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; @@ -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; } diff --git a/bundles/org.eclipse.emf.validation.ui/src/org/eclipse/emf/validation/ui/internal/preferences/ValidationPreferencePage.java b/bundles/org.eclipse.emf.validation.ui/src/org/eclipse/emf/validation/ui/internal/preferences/ValidationPreferencePage.java index d01297a..e6e5bbb 100644 --- a/bundles/org.eclipse.emf.validation.ui/src/org/eclipse/emf/validation/ui/internal/preferences/ValidationPreferencePage.java +++ b/bundles/org.eclipse.emf.validation.ui/src/org/eclipse/emf/validation/ui/internal/preferences/ValidationPreferencePage.java @@ -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/ @@ -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; @@ -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. @@ -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 diff --git a/bundles/org.eclipse.emf.validation/src/org/eclipse/emf/validation/preferences/EMFModelValidationPreferences.java b/bundles/org.eclipse.emf.validation/src/org/eclipse/emf/validation/preferences/EMFModelValidationPreferences.java index f52d33b..e3fb862 100644 --- a/bundles/org.eclipse.emf.validation/src/org/eclipse/emf/validation/preferences/EMFModelValidationPreferences.java +++ b/bundles/org.eclipse.emf.validation/src/org/eclipse/emf/validation/preferences/EMFModelValidationPreferences.java @@ -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/ @@ -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. @@ -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. */ @@ -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(); } /** @@ -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); } /** @@ -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); } /** @@ -78,15 +72,14 @@ 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); } } @@ -94,7 +87,8 @@ public static void setConstraintDisabled(String id, boolean disabled) { * @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); } }