向已有的TreeViewer和TableViewer上添加编辑功能,可以使用CellEditor和CellModifier。
CellEditor定义了某个列被编辑时显示的外观,它可以是文本框、下拉列表框或单选框,也可以自己定义。
通常使用的CellEditor的子类就是:CheckboxCellEditor、ComboBoxCellEditor和TextCellEditor。
CellEditor一般用数组来保存,如果某个列不需要编辑,则可将该列的CellEditor设为null。
当CellEditor的数组定义完后,即可利用setCellEditors(CellEditor[] editors)方法将该数组设置到对应的TreeViewer或TableViewer中去。例如:
CellEditor[]cellEditors = new CellEditor[ 5 ];
cellEditors[ 0 ] = new TextCellEditor(tableViewer.getTable());
cellEditors[ 1 ] = null ;
cellEditors[ 2 ] = new ComboBoxCellEditor(tableViewer.getTable(), new String[]{ " first " , " second " , " third " , " forth " });
cellEditors[ 3 ] = new CheckboxCellEditor(tableViewer.getTable());
cellEditors[ 4 ] = new CustomizedTextCellEditor(tableViewer.getTable());
tableViewer.setCellEditors(cellEditors);
其中CustomizedTextCellEditor是自定义的CellEditor,避免了设置value时造成的空指针异常。
protectedclassCustomizedTextCellEditorextendsTextCellEditor{
publicCustomizedTextCellEditor(Compositeparent){
super(parent);
}
protectedvoiddoSetValue(Objectvalue){
if(value==null)
return;
super.doSetValue(value);
}
}
CellEditor负责外观,它对要编辑的模型信息一无所知。所以jface中引入了ICellModifier接口,将model与CellEditor联系在一起。为了确定在CellModifier中的列,需要定义columnProperties的String[]数组,用以区分不同列对应的不同属性。使用setColumnProperties(String[] columnProperties)设置该属性集。
ICellModifier定义了三个接口方法:
public boolean canModify(Object element, String property);
该方法判断何时该列可以被编辑。其中element是对应的model。返回true表示此时该列可以被编辑。
public Object getValue(Object element, String property);
该方法一般在activateCellEditor()时调用,用于设定CellEditor的初始值。其中element是对应的model。
此处虽然可以返回Object类型的引用,但是使用时需小心,特定的CellEditor仅接受特定类型的Value。比如:
TextCellEditor对应String类型的Value;
ComboBoxCellEditor对应Integer类型的Value;
CheckBoxCellEditor对应Boolean类型的Value;
若返回了不适合的Value对象,则会抛出AssertionFailedException。
public void modify(Object element, String property, Object value);
该方法执行保存修改。一般在saveEditorValue之类的方法中调用。此处的element不再是model,而是Item类型的引用。取用对应的模型,需要使用((Item) element).getData()方法。一般此处的value值,也就是当前CellEditor的Value值,使用CellEditor.getValue()得到。另外,在执行完更改后,需要刷新对应的TableViewer或TreeViewer,使做出的更新可见。
org.eclipse.debug.internal.ui.elements.adapters.DefaultVariableCellModifier是ICellModifier的一个完整实现:
importorg.eclipse.debug.core.DebugException;
importorg.eclipse.debug.core.model.IVariable;
importorg.eclipse.debug.internal.ui.DebugUIPlugin;
importorg.eclipse.debug.internal.ui.DefaultLabelProvider;
importorg.eclipse.debug.internal.ui.VariableValueEditorManager;
importorg.eclipse.debug.ui.actions.IVariableValueEditor;
importorg.eclipse.jface.viewers.ICellModifier;
/***//**
*@since3.2
*
*/
publicclassDefaultVariableCellModifierimplementsICellModifier{
/**//*(non-Javadoc)
*@seeorg.eclipse.jface.viewers.ICellModifier#canModify(java.lang.Object,java.lang.String)
*/
publicbooleancanModify(Objectelement,Stringproperty){
if(VariableColumnPresentation.COLUMN_VARIABLE_VALUE.equals(property)){
if(elementinstanceofIVariable){
return((IVariable)element).supportsValueModification();
}
}
returnfalse;
}
/**//*(non-Javadoc)
*@seeorg.eclipse.jface.viewers.ICellModifier#getValue(java.lang.Object,java.lang.String)
*/
publicObjectgetValue(Objectelement,Stringproperty){
if(VariableColumnPresentation.COLUMN_VARIABLE_VALUE.equals(property)){
if(elementinstanceofIVariable){
IVariablevariable=(IVariable)element;
try{
returnDefaultLabelProvider.escapeSpecialChars(variable.getValue().getValueString());
}catch(DebugExceptione){
DebugUIPlugin.log(e);
}
}
}
returnnull;
}
/**//*(non-Javadoc)
*@seeorg.eclipse.jface.viewers.ICellModifier#modify(java.lang.Object,java.lang.String,java.lang.Object)
*/
publicvoidmodify(Objectelement,Stringproperty,Objectvalue){
ObjectoldValue=getValue(element,property);
if(!value.equals(oldValue)){
if(VariableColumnPresentation.COLUMN_VARIABLE_VALUE.equals(property)){
if(elementinstanceofIVariable){
IVariablevariable=(IVariable)element;
IVariableValueEditoreditor=VariableValueEditorManager.getDefault().getVariableValueEditor(variable.getModelIdentifier());
if(valueinstanceofString){
value=DefaultLabelProvider.encodeEsacpedChars((String)value);
}
if(editor!=null){
if(editor.saveVariable(variable,(String)value,DebugUIPlugin.getShell())){
return;
}
}
try{
variable.setValue((String)value);
}catch(DebugExceptione){
DebugUIPlugin.errorDialog(DebugUIPlugin.getShell(),Messages.VariableColumnPresentation_4,Messages.VariableColumnPresentation_5,e.getStatus());
}
}
}
}
}
}
分享到:
相关推荐
点击Enter键,对TableViewer的单元格进行逐个遍历的辅助类,推荐用于使用ViewerColumn.setEditingSupport的可编辑TableViewer。 Since 3.3, an alternative API is available, see ViewerColumn.setEditingSupport...
Plugins-JFace-TableViewer.rar Plugins-JFace-TableViewer.rar Plugins-JFace-TableViewer.rar Plugins-JFace-TableViewer.rar Plugins-JFace-TableViewer.rar
NULL 博文链接:https://1299020954.iteye.com/blog/2231923
NULL 博文链接:https://arizona.iteye.com/blog/526298
NULL 博文链接:https://marsvaadin.iteye.com/blog/1320182
TableViewer
Jface TableViewer控件实现的对标签文件的标签属性的修改功能。 用TextCellEditor和ComboBoxCellEditor给表格添加编辑功能。 通过表格的addListener事件为表格的单元格添加编辑功能,事例根据鼠标点击时鼠标的X和Y...
swt/Jface用到的最基本的jar包
记事本 用SWT和Jface编写应用起来还可以 可直接运行
RCP jface 数据绑定 例子 测试 范例 RCP jface 数据绑定 例子 测试 范例 RCP jface 数据绑定 例子 测试 范例
9.4 可拖动的工具栏(CoolBar和CoolItem) 161 9.4.1 带有下拉选项的工具栏 163 9.4.2 常用的方法 163 9.5 系统托盘(Tray和TrayItem) 164 9.6 滑动组件 167 9.6.1 滑块(Slider) 167 9.6.2 刻度条...
SWT和Jface开发使用必使用jar包之:equinox包,配合其他jar包一起使用
《Eclipse SWT/Jface核心应用》全面介绍了SWT、JFace和RCP的相关知识。全书共分5篇,第1篇介绍了SWT产生的背景以及SWT的一些基本概念和基础知识。第2篇介绍了SWT基本控件的使用,以及事件处理、布局等SWT基本知识的...
SWT和Jface开发使用必使用jar包之:jface核心jar包,配置其他使用
Eclipse中使用SWT和JFace的简单介绍、入门教程
jface programing
swt和jface开发包,需要的话随意下载吧
该工程实现了swt/jface tableviwer 的单元格编辑功能,有Text,combo,fileDialog三种单元格编辑样式
JFace是建立在SWT之上的UI部件,是Eclipse组织为了开发Eclipse IDE...JFace在其API和实现方面都是独立于窗口系统的,它在窗口小部件系统上运行,即JFace旨在使用SWT而不隐藏它。本资源包含了JFace 和 SWT所需的所有类库