mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 22:52:11 +02:00
- added limited support for string parameters
This commit is contained in:
parent
92923566ce
commit
53e15f64c2
5 changed files with 214 additions and 39 deletions
|
@ -11,12 +11,14 @@
|
|||
package org.eclipse.cdt.codan.core.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* @author Alena
|
||||
*
|
||||
*/
|
||||
public class AbstractListParameterInfo extends AbstractProblemParameterInfo {
|
||||
public abstract class AbstractListParameterInfo extends
|
||||
AbstractProblemParameterInfo {
|
||||
protected ArrayList<IProblemParameterInfo> list = new ArrayList<IProblemParameterInfo>(
|
||||
1);
|
||||
|
||||
|
@ -27,8 +29,8 @@ public class AbstractListParameterInfo extends AbstractProblemParameterInfo {
|
|||
* org.eclipse.cdt.codan.core.model.AbstractProblemParameterInfo#getType()
|
||||
*/
|
||||
@Override
|
||||
public String getType() {
|
||||
return TYPE_LIST;
|
||||
public ParameterTypes getType() {
|
||||
return ParameterTypes.TYPE_LIST;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -71,4 +73,9 @@ public class AbstractListParameterInfo extends AbstractProblemParameterInfo {
|
|||
protected IProblemParameterInfo getElement(int i) {
|
||||
return list.get(i);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<IProblemParameterInfo> getIterator() {
|
||||
return list.iterator();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,6 +10,8 @@
|
|||
*******************************************************************************/
|
||||
package org.eclipse.cdt.codan.core.model;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* Default implementation for single parameter checker of type string.
|
||||
*
|
||||
|
@ -32,8 +34,8 @@ public abstract class AbstractProblemParameterInfo implements
|
|||
*
|
||||
* @see org.eclipse.cdt.codan.core.model.IProblemParameterInfo#getType()
|
||||
*/
|
||||
public String getType() {
|
||||
return TYPE_STRING;
|
||||
public ParameterTypes getType() {
|
||||
return ParameterTypes.TYPE_STRING;
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -73,4 +75,13 @@ public abstract class AbstractProblemParameterInfo implements
|
|||
public String getToolTip() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.eclipse.cdt.codan.core.model.IProblemParameterInfo#getIterator()
|
||||
*/
|
||||
public Iterator<IProblemParameterInfo> getIterator() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,6 +10,8 @@
|
|||
*******************************************************************************/
|
||||
package org.eclipse.cdt.codan.core.model;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* Problem parameter usually key=value settings that allows to alter checker
|
||||
* behaviour for given problem. For example if checker finds violation of naming
|
||||
|
@ -23,12 +25,34 @@ package org.eclipse.cdt.codan.core.model;
|
|||
* @noimplement This interface is not intended to be implemented by clients.
|
||||
*/
|
||||
public interface IProblemParameterInfo {
|
||||
final static String TYPE_STRING = "string"; //$NON-NLS-1$
|
||||
final static String TYPE_INTEGER = "integer"; //$NON-NLS-1$
|
||||
final static String TYPE_BOOLEAN = "boolean"; //$NON-NLS-1$
|
||||
final static String TYPE_FILE = "file"; //$NON-NLS-1$
|
||||
final static String TYPE_LIST = "list"; //$NON-NLS-1$
|
||||
final static String TYPE_HASH = "hash"; //$NON-NLS-1$
|
||||
enum ParameterTypes {
|
||||
TYPE_STRING("string"), //$NON-NLS-1$
|
||||
TYPE_INTEGER("integer"), //$NON-NLS-1$
|
||||
TYPE_BOOLEAN("boolean"), //$NON-NLS-1$
|
||||
TYPE_FILE("file"), //$NON-NLS-1$
|
||||
TYPE_LIST("list"), //$NON-NLS-1$
|
||||
TYPE_HASH("hash"); //$NON-NLS-1$
|
||||
private String literal;
|
||||
|
||||
private ParameterTypes(String literal) {
|
||||
this.literal = literal;
|
||||
}
|
||||
|
||||
public static ParameterTypes valueOfLiteral(String name) {
|
||||
ParameterTypes[] values = values();
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
ParameterTypes e = values[i];
|
||||
if (e.literal.equals(name))
|
||||
return e;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return literal;
|
||||
}
|
||||
}
|
||||
|
||||
String getKey();
|
||||
|
||||
|
@ -40,7 +64,7 @@ public interface IProblemParameterInfo {
|
|||
*
|
||||
* @return string value of the type
|
||||
*/
|
||||
String getType();
|
||||
ParameterTypes getType();
|
||||
|
||||
/**
|
||||
* Additional info on how it is represented in the ui, for example boolean
|
||||
|
@ -73,4 +97,12 @@ public interface IProblemParameterInfo {
|
|||
* @return
|
||||
*/
|
||||
IProblemParameterInfo getElement(String key);
|
||||
|
||||
/**
|
||||
* Available if type is list or hash. Returns iterator over parameter values
|
||||
* for list and hash.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
Iterator<IProblemParameterInfo> getIterator();
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ import org.eclipse.cdt.codan.core.CodanRuntime;
|
|||
import org.eclipse.cdt.codan.core.model.ICheckersRegistry;
|
||||
import org.eclipse.cdt.codan.core.model.IProblem;
|
||||
import org.eclipse.cdt.codan.core.model.IProblemProfile;
|
||||
import org.eclipse.cdt.codan.core.model.IProblemWorkingCopy;
|
||||
import org.eclipse.core.resources.IResource;
|
||||
import org.eclipse.core.runtime.preferences.InstanceScope;
|
||||
import org.eclipse.jface.viewers.ISelectionChangedListener;
|
||||
|
@ -45,31 +46,29 @@ import org.eclipse.ui.preferences.ScopedPreferenceStore;
|
|||
public class CodanPreferencePage extends FieldEditorOverlayPage implements
|
||||
IWorkbenchPreferencePage {
|
||||
private IProblemProfile profile;
|
||||
private Composite parametersComposite;
|
||||
private Composite parametersTab;
|
||||
private ISelectionChangedListener problemSelectionListener;
|
||||
private IProblem selectedProblem;
|
||||
|
||||
public CodanPreferencePage() {
|
||||
super(GRID);
|
||||
setPreferenceStore(new ScopedPreferenceStore(new InstanceScope(),
|
||||
CodanCorePlugin.PLUGIN_ID));
|
||||
setDescription("Code Analyzers Preference Page");
|
||||
problemSelectionListener = new ISelectionChangedListener(){
|
||||
|
||||
problemSelectionListener = new ISelectionChangedListener() {
|
||||
public void selectionChanged(SelectionChangedEvent event) {
|
||||
if (parametersComposite!=null ) {
|
||||
if (parametersTab != null) {
|
||||
if (event.getSelection() instanceof ITreeSelection) {
|
||||
ITreeSelection s = (ITreeSelection) event.getSelection();
|
||||
if (s.getFirstElement() instanceof IProblem)
|
||||
ITreeSelection s = (ITreeSelection) event
|
||||
.getSelection();
|
||||
if (s.getFirstElement() instanceof IProblem)
|
||||
setSelectedProblem((IProblem) s.getFirstElement());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected String getPageId() {
|
||||
return "org.eclipse.cdt.codan.internal.ui.preferences.CodanPreferencePage"; //$NON-NLS-1$
|
||||
}
|
||||
|
@ -92,39 +91,41 @@ public class CodanPreferencePage extends FieldEditorOverlayPage implements
|
|||
// createMainTab(tabFolder);
|
||||
createParamtersTab(tabFolder);
|
||||
createScopeTab(tabFolder);
|
||||
checkedTreeEditor.getTreeViewer().addSelectionChangedListener(problemSelectionListener);
|
||||
|
||||
checkedTreeEditor.getTreeViewer().addSelectionChangedListener(
|
||||
problemSelectionListener);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param selection
|
||||
*/
|
||||
protected void setSelectedProblem(IProblem problem) {
|
||||
Control[] children = parametersComposite.getChildren();
|
||||
parametersComposite.setLayout(new GridLayout());
|
||||
if (this.selectedProblem != problem) {
|
||||
saveProblemEdits();
|
||||
}
|
||||
this.selectedProblem = problem;
|
||||
Control[] children = parametersTab.getChildren();
|
||||
for (int i = 0; i < children.length; i++) {
|
||||
Control control = children[i];
|
||||
control.dispose();
|
||||
}
|
||||
if (problem.getParameterInfo()==null) {
|
||||
Label label = new Label(parametersComposite, SWT.NULL);
|
||||
label.setText("No Parameters");
|
||||
} else {
|
||||
Label label = new Label(parametersComposite, SWT.NULL);
|
||||
label.setText("Parameters: TODO");
|
||||
}
|
||||
parametersComposite.pack(true);
|
||||
parametersComposite.layout(true);
|
||||
ParametersComposite comp = new ParametersComposite(parametersTab,
|
||||
problem);
|
||||
comp.setLayoutData(new GridData(GridData.FILL_BOTH));
|
||||
parametersTab.pack(true);
|
||||
parametersTab.layout(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param tabFolder
|
||||
*/
|
||||
private void createParamtersTab(TabFolder tabFolder) {
|
||||
TabItem tabItem1 = new TabItem(tabFolder, SWT.NULL);
|
||||
tabItem1.setText("Parameters");
|
||||
parametersComposite = new Composite(tabFolder, SWT.NONE);
|
||||
tabItem1.setControl(parametersComposite);
|
||||
parametersTab = new Composite(tabFolder, SWT.NONE);
|
||||
tabItem1.setControl(parametersTab);
|
||||
parametersTab.setLayout(new GridLayout());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param tabFolder
|
||||
*/
|
||||
|
@ -137,8 +138,8 @@ public class CodanPreferencePage extends FieldEditorOverlayPage implements
|
|||
Label label = new Label(comp, SWT.NONE);
|
||||
label.setText("Scope: TODO");
|
||||
label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
*/
|
||||
|
@ -153,11 +154,26 @@ public class CodanPreferencePage extends FieldEditorOverlayPage implements
|
|||
*/
|
||||
@Override
|
||||
public boolean performOk() {
|
||||
//if (isPropertyPage())
|
||||
getRegistry().updateProfile((IResource) getElement(), null);
|
||||
// if (isPropertyPage())
|
||||
getRegistry().updateProfile((IResource) getElement(), null);
|
||||
saveProblemEdits();
|
||||
return super.performOk();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private void saveProblemEdits() {
|
||||
if (selectedProblem==null) return;
|
||||
Control[] children = parametersTab.getChildren();
|
||||
for (int i = 0; i < children.length; i++) {
|
||||
Control control = children[i];
|
||||
if (control instanceof ParametersComposite) {
|
||||
((ParametersComposite) control).save((IProblemWorkingCopy) selectedProblem); // XXX
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
|
|
|
@ -0,0 +1,109 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2009 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
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.codan.internal.ui.preferences;
|
||||
|
||||
import org.eclipse.cdt.codan.core.model.IProblem;
|
||||
import org.eclipse.cdt.codan.core.model.IProblemParameterInfo;
|
||||
import org.eclipse.cdt.codan.core.model.IProblemWorkingCopy;
|
||||
import org.eclipse.jface.preference.FieldEditorPreferencePage;
|
||||
import org.eclipse.jface.preference.PreferenceStore;
|
||||
import org.eclipse.jface.preference.StringFieldEditor;
|
||||
import org.eclipse.swt.SWT;
|
||||
import org.eclipse.swt.layout.GridData;
|
||||
import org.eclipse.swt.layout.GridLayout;
|
||||
import org.eclipse.swt.widgets.Composite;
|
||||
|
||||
/**
|
||||
* @author Alena
|
||||
*
|
||||
*/
|
||||
public class ParametersComposite extends Composite {
|
||||
private FieldEditorPreferencePage page;
|
||||
private IProblem problem;
|
||||
private PreferenceStore pref;
|
||||
|
||||
/**
|
||||
* @param parent
|
||||
* @param problem
|
||||
* @param style
|
||||
*/
|
||||
public ParametersComposite(Composite parent, final IProblem problem) {
|
||||
super(parent, SWT.NONE);
|
||||
this.setLayout(new GridLayout(2, false));
|
||||
this.problem = problem;
|
||||
this.pref = new PreferenceStore();
|
||||
page = new FieldEditorPreferencePage() {
|
||||
@Override
|
||||
protected void createFieldEditors() {
|
||||
noDefaultAndApplyButton();
|
||||
IProblemParameterInfo parameterInfo = problem
|
||||
.getParameterInfo();
|
||||
createFieldEditorsForParameters(parameterInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param info
|
||||
*/
|
||||
private void createFieldEditorsForParameters(
|
||||
IProblemParameterInfo info) {
|
||||
switch (info.getType()) {
|
||||
case TYPE_STRING:
|
||||
StringFieldEditor fe = new StringFieldEditor(info.getKey(),
|
||||
info.getLabel(), getFieldEditorParent());
|
||||
addField(fe);
|
||||
break;
|
||||
default:
|
||||
throw new UnsupportedOperationException(info.getType()
|
||||
.toString());
|
||||
}
|
||||
}
|
||||
};
|
||||
IProblemParameterInfo info = problem.getParameterInfo();
|
||||
initPrefStore(info);
|
||||
page.setPreferenceStore(pref);
|
||||
page.createControl(parent);
|
||||
page.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));
|
||||
}
|
||||
|
||||
public void save(IProblemWorkingCopy problemwc) {
|
||||
page.performOk();
|
||||
IProblemParameterInfo info = problemwc.getParameterInfo();
|
||||
savePrefStore(info, problemwc);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param info
|
||||
* @param problemwc
|
||||
*/
|
||||
private void savePrefStore(IProblemParameterInfo info,
|
||||
IProblemWorkingCopy problemwc) {
|
||||
String key = info.getKey();
|
||||
Object parameter = problem.getParameter(key);
|
||||
if (parameter instanceof String) {
|
||||
String newValue = pref.getString(key);
|
||||
problemwc.setParameter(key, newValue);
|
||||
} else
|
||||
throw new UnsupportedOperationException(info.getType().toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param info
|
||||
*/
|
||||
private void initPrefStore(IProblemParameterInfo info) {
|
||||
String key = info.getKey();
|
||||
Object parameter = problem.getParameter(key);
|
||||
if (parameter instanceof String) {
|
||||
pref.setDefault(key, (String) parameter);
|
||||
pref.setValue(key, (String) parameter);
|
||||
} else
|
||||
throw new UnsupportedOperationException(info.getType().toString());
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue