mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-09-07 18:43:32 +02:00
- to make workspace/project override of properties work, change to store indiviual checkers id in properties
This commit is contained in:
parent
3ccd43f5ed
commit
59cf9d5fad
7 changed files with 173 additions and 117 deletions
|
@ -16,4 +16,5 @@ package org.eclipse.cdt.codan.core;
|
|||
public class PreferenceConstants {
|
||||
public static final String P_RUN_ON_BUILD = "booleanPreference";
|
||||
public static final String P_PROBLEMS = "problems";
|
||||
public static final String P_USE_PARENT = "useParentScope";
|
||||
}
|
||||
|
|
|
@ -10,21 +10,17 @@
|
|||
*******************************************************************************/
|
||||
package org.eclipse.cdt.codan.core.builder;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import org.eclipse.cdt.codan.core.model.CodanProblem;
|
||||
import org.eclipse.cdt.codan.core.model.CodanSeverity;
|
||||
import org.eclipse.cdt.codan.core.model.IProblem;
|
||||
import org.eclipse.cdt.codan.core.model.IProblemProfile;
|
||||
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
|
||||
|
||||
/**
|
||||
* @author Alena
|
||||
*
|
||||
*/
|
||||
public class CodanPreferencesLoader {
|
||||
private static String LIST_SEP = ",";
|
||||
private IProblemProfile baseModel;
|
||||
|
||||
/**
|
||||
|
@ -45,101 +41,79 @@ public class CodanPreferencesLoader {
|
|||
}
|
||||
|
||||
/**
|
||||
* Stored as element=true|false,...
|
||||
*
|
||||
* @param stringList
|
||||
* @return
|
||||
*/
|
||||
public Object modelFromString(String stringList) {
|
||||
String[] arr = stringList.split(LIST_SEP);
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
String elem = arr[i];
|
||||
String[] pair = elem.split("=", 2);
|
||||
if (pair.length == 0)
|
||||
continue;
|
||||
String id = pair[0];
|
||||
IProblem p = problemFromString(id);
|
||||
if (p == null) {
|
||||
System.err.println("cannot find '" + id + "'");
|
||||
continue;
|
||||
}
|
||||
if (pair.length == 1) {
|
||||
((CodanProblem) p).setEnabled(true);
|
||||
} else {
|
||||
String check = pair[1];
|
||||
Boolean c = Boolean.valueOf(check);
|
||||
((CodanProblem) p).setEnabled(c);
|
||||
}
|
||||
}
|
||||
return baseModel;
|
||||
public IProblem[] getProblems() {
|
||||
IProblem[] problems = baseModel.getProblems();
|
||||
return problems;
|
||||
}
|
||||
|
||||
protected String problemToString(Object element) {
|
||||
IProblem p = ((IProblem) element);
|
||||
return p.getId() + ":" + p.getSeverity();
|
||||
}
|
||||
|
||||
protected IProblem problemFromString(String string) {
|
||||
String[] pair = string.split(":");
|
||||
if (pair.length == 0)
|
||||
return null;
|
||||
String id = pair[0];
|
||||
String arg = "";
|
||||
if (pair.length > 1) {
|
||||
arg = pair[1];
|
||||
/**
|
||||
* @param id
|
||||
* @param s
|
||||
*/
|
||||
public void setProperty(String id, String s) {
|
||||
IProblem prob = baseModel.findProblem(id);
|
||||
if (!(prob instanceof CodanProblem))
|
||||
return;
|
||||
String sevs = s;
|
||||
boolean enabled = true;
|
||||
if (sevs.startsWith("-")) {
|
||||
sevs = sevs.substring(1);
|
||||
enabled = false;
|
||||
}
|
||||
((CodanProblem) prob).setEnabled(enabled);
|
||||
CodanSeverity sev;
|
||||
try {
|
||||
sev = CodanSeverity.valueOf(arg);
|
||||
sev = CodanSeverity.valueOf(sevs);
|
||||
} catch (RuntimeException e) {
|
||||
sev = CodanSeverity.Warning;
|
||||
}
|
||||
IProblem prob = baseModel.findProblem(id);
|
||||
if (prob instanceof CodanProblem) {
|
||||
((CodanProblem) prob).setSeverity(sev);
|
||||
}
|
||||
return prob;
|
||||
((CodanProblem) prob).setSeverity(sev);
|
||||
}
|
||||
|
||||
/**
|
||||
* Combines the given list of items into a single string. This method is the
|
||||
* converse of <code>parseString</code>.
|
||||
* <p>
|
||||
* Subclasses may implement this method.
|
||||
* </p>
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @return the combined string
|
||||
* @see #parseString
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
public String modelToString(Object model) {
|
||||
StringBuffer buf = new StringBuffer();
|
||||
Map<Object, Boolean> map = fillChecked(model,
|
||||
new HashMap<Object, Boolean>());
|
||||
for (Iterator iterator = map.keySet().iterator(); iterator.hasNext();) {
|
||||
Object element = iterator.next();
|
||||
buf.append(problemToString(element));
|
||||
buf.append('=');
|
||||
buf.append(map.get(element));
|
||||
if (iterator.hasNext())
|
||||
buf.append(LIST_SEP);
|
||||
}
|
||||
return buf.toString();
|
||||
@Override
|
||||
public String toString() {
|
||||
return getInput().toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param input
|
||||
* @param hashMap
|
||||
* @return
|
||||
*/
|
||||
private Map<Object, Boolean> fillChecked(Object element,
|
||||
HashMap<Object, Boolean> hashMap) {
|
||||
if (element instanceof IProblemProfile) {
|
||||
IProblemProfile profile = (IProblemProfile) element;
|
||||
IProblem[] problems = profile.getProblems();
|
||||
for (IProblem iProblem : problems) {
|
||||
hashMap.put(iProblem, iProblem.isEnabled());
|
||||
public IProblemProfile getInput() {
|
||||
return baseModel;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
public String getProperty(String id) {
|
||||
IProblem prob = baseModel.findProblem(id);
|
||||
if (!(prob instanceof CodanProblem))
|
||||
return null;
|
||||
String enabled = prob.isEnabled() ? "" : "-";
|
||||
String severity = prob.getSeverity().toString();
|
||||
String res = enabled + severity;
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param storePreferences
|
||||
*/
|
||||
public void load(IEclipsePreferences storePreferences) {
|
||||
IProblem[] probs = getProblems();
|
||||
for (int i = 0; i < probs.length; i++) {
|
||||
String id = probs[i].getId();
|
||||
String s = storePreferences.get(id, null);
|
||||
if (s != null) {
|
||||
setProperty(id, s);
|
||||
}
|
||||
}
|
||||
return hashMap;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -192,9 +192,7 @@ public class CheckersRegisry implements Iterable<IChecker> {
|
|||
wp = (IProblemProfile) getDefaultProfile().clone();
|
||||
// load default values
|
||||
CodanPreferencesLoader loader = new CodanPreferencesLoader(wp);
|
||||
String s = CodanCorePlugin.getDefault().getStorePreferences()
|
||||
.get(PreferenceConstants.P_PROBLEMS, "");
|
||||
loader.modelFromString(s);
|
||||
loader.load(CodanCorePlugin.getDefault().getStorePreferences());
|
||||
} catch (CloneNotSupportedException e) {
|
||||
wp = getDefaultProfile();
|
||||
}
|
||||
|
@ -202,6 +200,13 @@ public class CheckersRegisry implements Iterable<IChecker> {
|
|||
return wp;
|
||||
}
|
||||
|
||||
public void updateProfile(IResource element, IProblemProfile profile) {
|
||||
if (profile == null)
|
||||
profiles.remove(element);
|
||||
else
|
||||
profiles.put(element, profile);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param element
|
||||
* @return
|
||||
|
@ -218,8 +223,12 @@ public class CheckersRegisry implements Iterable<IChecker> {
|
|||
IEclipsePreferences node = new ProjectScope(
|
||||
(IProject) element)
|
||||
.getNode(CodanCorePlugin.PLUGIN_ID);
|
||||
String s = node.get(PreferenceConstants.P_PROBLEMS, "");
|
||||
loader.modelFromString(s);
|
||||
boolean useWorkspace = node.getBoolean(
|
||||
PreferenceConstants.P_USE_PARENT, false);
|
||||
if (!useWorkspace) {
|
||||
loader.load(node);
|
||||
}
|
||||
updateProfile(element, prof);
|
||||
} catch (CloneNotSupportedException e) {
|
||||
// cant
|
||||
}
|
||||
|
@ -228,7 +237,30 @@ public class CheckersRegisry implements Iterable<IChecker> {
|
|||
} else {
|
||||
prof = getResourceProfile(element.getProject());
|
||||
}
|
||||
} else {
|
||||
}
|
||||
return prof;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param element
|
||||
* @return
|
||||
*/
|
||||
public IProblemProfile getResourceProfileWorkingCopy(IResource element) {
|
||||
if (element instanceof IProject) {
|
||||
try {
|
||||
IProblemProfile prof = (IProblemProfile) getWorkspaceProfile()
|
||||
.clone();
|
||||
// load default values
|
||||
CodanPreferencesLoader loader = new CodanPreferencesLoader(prof);
|
||||
IEclipsePreferences node = new ProjectScope((IProject) element)
|
||||
.getNode(CodanCorePlugin.PLUGIN_ID);
|
||||
loader.load(node);
|
||||
return prof;
|
||||
} catch (CloneNotSupportedException e) {
|
||||
// cant
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -187,7 +187,6 @@ public abstract class CheckedTreeEditor extends FieldEditor implements
|
|||
*/
|
||||
protected void doLoadDefault() {
|
||||
if (getTreeControl() != null) {
|
||||
treeViewer.refresh();
|
||||
String s = getPreferenceStore().getDefaultString(
|
||||
getPreferenceName());
|
||||
getViewer().setInput(modelFromString(s));
|
||||
|
|
|
@ -12,6 +12,7 @@ package org.eclipse.cdt.codan.internal.ui.preferences;
|
|||
|
||||
import org.eclipse.cdt.codan.core.CodanCorePlugin;
|
||||
import org.eclipse.cdt.codan.core.model.CheckersRegisry;
|
||||
import org.eclipse.cdt.codan.core.model.IProblemProfile;
|
||||
import org.eclipse.core.resources.IResource;
|
||||
import org.eclipse.core.runtime.preferences.InstanceScope;
|
||||
import org.eclipse.ui.IWorkbench;
|
||||
|
@ -30,6 +31,8 @@ import org.eclipse.ui.preferences.ScopedPreferenceStore;
|
|||
*/
|
||||
public class CodanPreferencePage extends FieldEditorOverlayPage implements
|
||||
IWorkbenchPreferencePage {
|
||||
private IProblemProfile profile;
|
||||
|
||||
public CodanPreferencePage() {
|
||||
super(GRID);
|
||||
setPreferenceStore(new ScopedPreferenceStore(new InstanceScope(),
|
||||
|
@ -47,14 +50,27 @@ public class CodanPreferencePage extends FieldEditorOverlayPage implements
|
|||
* editor knows how to save and restore itself.
|
||||
*/
|
||||
public void createFieldEditors() {
|
||||
profile = isPropertyPage() ? CheckersRegisry.getInstance()
|
||||
.getResourceProfileWorkingCopy((IResource) getElement())
|
||||
: CheckersRegisry.getInstance().getWorkspaceProfile();
|
||||
CheckedTreeEditor checkedTreeEditor = new ProblemsTreeEditor(
|
||||
getFieldEditorParent(), isPropertyPage() ? CheckersRegisry
|
||||
.getInstance().getResourceProfile(
|
||||
(IResource) getElement()) : CheckersRegisry
|
||||
.getInstance().getWorkspaceProfile());
|
||||
getFieldEditorParent(), profile);
|
||||
addField(checkedTreeEditor);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.eclipse.jface.preference.PreferencePage#performApply()
|
||||
*/
|
||||
@Override
|
||||
public boolean performOk() {
|
||||
if (isPropertyPage())
|
||||
CheckersRegisry.getInstance().updateProfile(
|
||||
(IResource) getElement(), null);
|
||||
return super.performOk();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
|
|
|
@ -15,12 +15,10 @@ import java.util.Iterator;
|
|||
import java.util.List;
|
||||
|
||||
import org.eclipse.cdt.codan.core.CodanCorePlugin;
|
||||
import org.eclipse.cdt.codan.core.PreferenceConstants;
|
||||
import org.eclipse.core.resources.IProject;
|
||||
import org.eclipse.core.resources.IResource;
|
||||
import org.eclipse.core.resources.ProjectScope;
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
import org.eclipse.core.runtime.IAdaptable;
|
||||
import org.eclipse.core.runtime.QualifiedName;
|
||||
import org.eclipse.core.runtime.preferences.IScopeContext;
|
||||
import org.eclipse.core.runtime.preferences.InstanceScope;
|
||||
import org.eclipse.jface.preference.FieldEditor;
|
||||
|
@ -49,13 +47,6 @@ import org.eclipse.ui.preferences.ScopedPreferenceStore;
|
|||
*/
|
||||
public abstract class FieldEditorOverlayPage extends FieldEditorPreferencePage
|
||||
implements IWorkbenchPropertyPage {
|
||||
/***
|
||||
* Name of resource property for the selection of workbench or project
|
||||
* settings
|
||||
***/
|
||||
public static final String USEPROJECTSETTINGS = "useProjectSettings"; //$NON-NLS-1$
|
||||
private static final String FALSE = "false"; //$NON-NLS-1$
|
||||
private static final String TRUE = "true"; //$NON-NLS-1$
|
||||
// Stores all created field editors
|
||||
private List editors = new ArrayList();
|
||||
// Stores owning element of properties
|
||||
|
@ -223,15 +214,15 @@ public abstract class FieldEditorOverlayPage extends FieldEditorPreferencePage
|
|||
});
|
||||
// Set workspace/project radio buttons
|
||||
try {
|
||||
String use = ((IResource) getElement())
|
||||
.getPersistentProperty(new QualifiedName(pageId,
|
||||
USEPROJECTSETTINGS));
|
||||
if (TRUE.equals(use)) {
|
||||
Boolean useWorkspace = getPreferenceStore().getBoolean(
|
||||
PreferenceConstants.P_USE_PARENT);
|
||||
if (useWorkspace) {
|
||||
useWorkspaceSettingsButton.setSelection(true);
|
||||
} else {
|
||||
useProjectSettingsButton.setSelection(true);
|
||||
configureButton.setEnabled(false);
|
||||
} else
|
||||
useWorkspaceSettingsButton.setSelection(true);
|
||||
} catch (CoreException e) {
|
||||
}
|
||||
} catch (Exception e) {
|
||||
useWorkspaceSettingsButton.setSelection(true);
|
||||
}
|
||||
}
|
||||
|
@ -306,14 +297,8 @@ public abstract class FieldEditorOverlayPage extends FieldEditorPreferencePage
|
|||
boolean result = super.performOk();
|
||||
if (result && isPropertyPage()) {
|
||||
// Save state of radiobuttons in project properties
|
||||
IResource resource = (IResource) getElement();
|
||||
try {
|
||||
String value = (useProjectSettingsButton.getSelection()) ? TRUE
|
||||
: FALSE;
|
||||
resource.setPersistentProperty(new QualifiedName(pageId,
|
||||
USEPROJECTSETTINGS), value);
|
||||
} catch (CoreException e) {
|
||||
}
|
||||
getPreferenceStore().setValue(PreferenceConstants.P_USE_PARENT,
|
||||
!useProjectSettingsButton.getSelection());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -183,6 +183,55 @@ public class ProblemsTreeEditor extends CheckedTreeEditor {
|
|||
getViewer().setInput(profile);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.eclipse.cdt.codan.internal.ui.preferences.CheckedTreeEditor#doLoad()
|
||||
*/
|
||||
@Override
|
||||
protected void doLoad() {
|
||||
if (getTreeControl() != null) {
|
||||
IProblem[] probs = codanPreferencesLoader.getProblems();
|
||||
for (int i = 0; i < probs.length; i++) {
|
||||
String id = probs[i].getId();
|
||||
String s = getPreferenceStore().getString(id);
|
||||
codanPreferencesLoader.setProperty(id, s);
|
||||
}
|
||||
getViewer().setInput(codanPreferencesLoader.getInput());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doLoadDefault() {
|
||||
if (getTreeControl() != null) {
|
||||
IProblem[] probs = codanPreferencesLoader.getProblems();
|
||||
for (int i = 0; i < probs.length; i++) {
|
||||
String id = probs[i].getId();
|
||||
String s = getPreferenceStore().getDefaultString(id);
|
||||
codanPreferencesLoader.setProperty(id, s);
|
||||
}
|
||||
getViewer().setInput(codanPreferencesLoader.getInput());
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.eclipse.cdt.codan.internal.ui.preferences.CheckedTreeEditor#doStore()
|
||||
*/
|
||||
@Override
|
||||
protected void doStore() {
|
||||
codanPreferencesLoader.setInput(getViewer().getInput());
|
||||
IProblem[] probs = codanPreferencesLoader.getProblems();
|
||||
for (int i = 0; i < probs.length; i++) {
|
||||
String id = probs[i].getId();
|
||||
String s = codanPreferencesLoader.getProperty(id);
|
||||
getPreferenceStore().setValue(id, s);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
|
@ -191,7 +240,7 @@ public class ProblemsTreeEditor extends CheckedTreeEditor {
|
|||
*/
|
||||
@Override
|
||||
protected Object modelFromString(String s) {
|
||||
return codanPreferencesLoader.modelFromString(s);
|
||||
return codanPreferencesLoader.getInput();
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -203,6 +252,6 @@ public class ProblemsTreeEditor extends CheckedTreeEditor {
|
|||
*/
|
||||
@Override
|
||||
protected String modelToString(Object model) {
|
||||
return codanPreferencesLoader.modelToString(model);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue