From 63c5b6c950c4338b2fb90a331b033eb910b20a09 Mon Sep 17 00:00:00 2001 From: Sergey Prigogin Date: Thu, 19 May 2011 04:06:02 +0000 Subject: [PATCH] Bug 346365 - Codan ignores default preferences. --- .../codan/internal/core/CheckersRegistry.java | 12 +++- .../internal/core/CodanPreferencesLoader.java | 64 +++++++++++++++---- 2 files changed, 62 insertions(+), 14 deletions(-) diff --git a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/CheckersRegistry.java b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/CheckersRegistry.java index da25cbcfb78..7e3c86400b6 100644 --- a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/CheckersRegistry.java +++ b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/CheckersRegistry.java @@ -7,6 +7,7 @@ * * Contributors: * Alena Laskavaia - initial API and implementation + * Sergey Prigogin (Google) *******************************************************************************/ package org.eclipse.cdt.codan.internal.core; @@ -39,6 +40,9 @@ import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IConfigurationElement; import org.eclipse.core.runtime.IExtensionPoint; import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.preferences.ConfigurationScope; +import org.eclipse.core.runtime.preferences.DefaultScope; +import org.eclipse.core.runtime.preferences.InstanceScope; import org.osgi.service.prefs.Preferences; /** @@ -350,7 +354,12 @@ public class CheckersRegistry implements Iterable, ICheckersRegistry { ((ProblemProfile) wp).setResource(ResourcesPlugin.getWorkspace()); // load default values CodanPreferencesLoader loader = new CodanPreferencesLoader(wp); - loader.load(CodanPreferencesLoader.getWorkspaceNode()); + Preferences[] preferences = { + InstanceScope.INSTANCE.getNode(CodanCorePlugin.PLUGIN_ID), + ConfigurationScope.INSTANCE.getNode(CodanCorePlugin.PLUGIN_ID), + DefaultScope.INSTANCE.getNode(CodanCorePlugin.PLUGIN_ID), + }; + loader.load(preferences); profiles.put(ResourcesPlugin.getWorkspace(), wp); } return wp; @@ -391,7 +400,6 @@ public class CheckersRegistry implements Iterable, ICheckersRegistry { } else { prof = getResourceProfile(element.getProject()); } - } else { } return prof; } diff --git a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/CodanPreferencesLoader.java b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/CodanPreferencesLoader.java index 190a13743cc..89dddd7aa61 100644 --- a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/CodanPreferencesLoader.java +++ b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/CodanPreferencesLoader.java @@ -1,12 +1,13 @@ /******************************************************************************* - * Copyright (c) 2009, 2010 Alena Laskavaia + * Copyright (c) 2009, 2011 Alena Laskavaia * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * Alena Laskavaia - initial API and implementation + * Alena Laskavaia - initial API and implementation + * Sergey Prigogin (Google) *******************************************************************************/ package org.eclipse.cdt.codan.internal.core; @@ -18,7 +19,10 @@ import org.eclipse.cdt.codan.core.model.IProblemWorkingCopy; import org.eclipse.cdt.codan.core.param.IProblemPreference; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.ProjectScope; +import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.preferences.IEclipsePreferences; +import org.eclipse.core.runtime.preferences.IPreferencesService; +import org.eclipse.core.runtime.preferences.InstanceScope; import org.osgi.service.prefs.Preferences; /** @@ -112,7 +116,7 @@ public class CodanPreferencesLoader { /** * Takes string values from storePreferences and applies them to the problem - * profile + * profile. * * @param storePreferences */ @@ -128,9 +132,28 @@ public class CodanPreferencesLoader { } } + /** + * Takes string values from storePreferences and applies them to the problem + * profile + * + * @param storePreferences + */ + public void load(Preferences[] storePreferences) { + IPreferencesService prefService = Platform.getPreferencesService(); + IProblem[] probs = getProblems(); + for (int i = 0; i < probs.length; i++) { + String id = probs[i].getId(); + String s = prefService.get(id, null, storePreferences); + if (s != null) { + setProperty(id, s); + setProblemPreferenceValues(id, storePreferences); + } + } + } + /** * Takes string values of the problem preferences from storePreferences - * and applies them to the problem profile + * and applies them to the problem profile. * * @param problemId * @param storePreferences @@ -151,6 +174,29 @@ public class CodanPreferencesLoader { } } + /** + * Takes string values of the problem preferences from storePreferences + * and applies them to the problem profile. + * + * @param problemId + * @param storePreferences + */ + private void setProblemPreferenceValues(String problemId, Preferences[] storePreferences) { + IPreferencesService prefService = Platform.getPreferencesService(); + IProblem prob = baseModel.findProblem(problemId); + String prefKey = getPreferencesKey(problemId); + if (prefKey == null) + return; + String exported = prefService.get(prefKey, null, storePreferences); + if (exported != null) { + try { + prob.getPreference().importValue(exported); + } catch (IllegalArgumentException e) { + CodanCorePlugin.log(e); + } + } + } + /** * Return preference node (osgi preferences) for the project * @@ -160,10 +206,7 @@ public class CodanPreferencesLoader { public static IEclipsePreferences getProjectNode(IProject project) { if (!project.exists()) return null; - IEclipsePreferences prefNode = new ProjectScope(project).getNode(CodanCorePlugin.PLUGIN_ID); - if (prefNode == null) - return null; - return prefNode; + return new ProjectScope(project).getNode(CodanCorePlugin.PLUGIN_ID); } /** @@ -172,10 +215,7 @@ public class CodanPreferencesLoader { * @return project preferences node */ public static IEclipsePreferences getWorkspaceNode() { - IEclipsePreferences prefNode = CodanCorePlugin.getDefault().getStorePreferences(); - if (prefNode == null) - return null; - return prefNode; + return InstanceScope.INSTANCE.getNode(CodanCorePlugin.PLUGIN_ID); } /**