tv.setColumnProperties(new String[] { "name", "value" });//设定列的别名,新名字
final CellEditor[] cellEditors = new CellEditor[2];
cellEditors[0] = null;//第1列不可编辑
cmbVariable = new VariableComboBoxCellEditor(tv.getTable(), variableNames);//重写的一个下拉框
cellEditors[1] = cmbVariable;
tv.setCellEditors(cellEditors);
tv.setCellModifier(new ICellModifier() {
@Override
public boolean canModify(final Object element, final String property) {
return true;
}
private int getIndex(final String strItem) {
for (int i = 0; i < variableNames.length; i++) {
if (variableNames[i].equals(strItem)) {
return i;
}
}
return -1;
}
@Override
public Object getValue(final Object element, final String property) {
if (element instanceof ParameterValue) {
final ParameterValue p_v = (ParameterValue) element;
if (property.equals("value")) {
if (getIndex(p_v.getParameterValue()) == -1) {//如果下拉框市被编辑了,没有选择index,返回-1
return p_v.getParameterValue();
}
return getIndex(p_v.getParameterValue());
}
}
return null;
}
@Override
public void modify(final Object element, final String property, final Object value) {
final TableItem item = (TableItem) element;
final ParameterValue pvNew = (ParameterValue) item.getData();
String newValue = "";
if (property.equals("value")) {
if (value instanceof String) {
newValue = (String) value;
} else {
newValue = variableNames[(Integer) value];
}
pvNew.setParameterValue(newValue);
try {
setTestableProperty(testable, CoreConstants.VARIABLE_ID, ApplicationUtil.serialize(input));
} catch (final IOException e) {
// TODO
UIPlugin.log(e.getMessage(), IStatus.ERROR);
}
}
}
});
//重写下拉框,使之能获得被编辑的新值
// 重写下拉框,获取编辑的值
private class VariableComboBoxCellEditor extends ComboBoxCellEditor {
public VariableComboBoxCellEditor(final Composite parent, final String[] items) {
super(parent, items);
}
@Override
protected void doSetValue(final Object value) {
if (value instanceof String) {
((CCombo) getControl()).setText((String) value);
} else {
super.doSetValue(value);
}
}
@Override
protected Object doGetValue() {
final Object value = super.doGetValue();
if (value instanceof Integer && (Integer) value == -1) {
return ((CCombo) getControl()).getText();
}
return value;
}
}