1.tableviewer初始化
TableViewer viewer = new TableViewer(chooseModuleGroup,SWT.FULL_SELECTION|SWT.BORDER);
Table table=viewer.getTable();
GridData griddata = new GridData(GridData.FILL_BOTH);
griddata.horizontalSpan = 2;
griddata.verticalSpan = 2;
griddata.heightHint=100;
table.setLayoutData(griddata);
TableLayout layout = new TableLayout();
layout.addColumnData(new ColumnWeightData(120, true));
layout.addColumnData(new ColumnWeightData(120, true));
TableColumn column1 = new TableColumn(table,SWT.CENTER);
column1.setText("choose");
TableColumn column2 = new TableColumn(table,SWT.CENTER);
column2.setText("module");
viewer.setColumnProperties(new String[]{"choose","module"});
table.setLayout(layout);
table.setLinesVisible(true);
table.setHeaderVisible(true);
viewer.setCellEditors(new CellEditor[] {new CheckboxCellEditor(table) ,new TextCellEditor(table)});
viewer.setLabelProvider(new CheckBoxLabelProvider());
viewer.setContentProvider(new CheckBoxContentProvider());
viewer.setCellModifier(new ModuleSelectionModifier());
viewer.setInput(selectionList);
selectionList.collectSelectedModules();
2.创建ICellModifier
class ModuleSelectionModifier implements ICellModifier{
public boolean canModify(Object element, String property) {
if(property.equals("choose")){
return true;
}
return false;
}
public Object getValue(Object element, String property) {
Object result=null;
MichelleSelectedModule module=(MichelleSelectedModule)element;
if(property.equals("choose")){
return new Boolean(module.getSelected());
}else if(property.equals("module")){
return module.getPlugin();
}else{
return result;
}
}
public void modify(Object element, String property, Object value) {
MichelleSelectedModule module=(MichelleSelectedModule)((TableItem) element).getData();
if(property.equals("choose")){
module.setSelected(((Boolean)value).booleanValue());
}
selectionList.moduleChanged(module);
}
}
}
3.创建modellist 为tableviewer提供添加 删除 更新等model的方法
private ModuleSelectionList selectionList=new ModuleSelectionList();
class CheckBoxContentProvider implements IStructuredContentProvider,IModuleListViewer{
public Object[] getElements(Object inputElement) {
return selectionList.getElements();
}
public void dispose() {
selectionList.removeChangeListener(this);
}
public void addModule(MichelleSelectedModule module) {
viewer.add(module);
}
public void removeModule(MichelleSelectedModule module) {
viewer.remove(module);
}
public void updateModule(MichelleSelectedModule module) {
viewer.update(module, null);
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
if (newInput != null)
((ModuleSelectionList) newInput).addChangeListener(this);
if (oldInput != null)
((ModuleSelectionList) oldInput).removeChangeListener(this);
}
}
4.创建LabelProvider (这个LabelProvider 在第一列提供checkbox选项)
class CheckBoxLabelProvider extends LabelProvider implements ITableLabelProvider{
private Image getImageByBoolean(boolean selected) {
return selected?checkImage.createImage():unCheckImage.createImage();
}
public Image getColumnImage(Object element, int columnIndex) {
return (columnIndex == 0) ? // COMPLETED_COLUMN?
getImageByBoolean(((MichelleSelectedModule) element).getSelected()) :
null;
}
public String getColumnText(Object element, int columnIndex) {
String result = "";
if(element==null)return result;
MichelleSelectedModule module=(MichelleSelectedModule)element;
switch (columnIndex) {
case 0: // COMPLETED_COLUMN
break;
case 1 :
result =module.getPlugin();
break;
default :
break;
}
return result;
}
}