由于eclipse自己的TextPropertyDescrptor没有disabled属性,所以,在程序中要求
实现某个属性只读比较困难(可能是我没有找到:)),所以,修改了一下
TextPropertyDescrptor,以实现该效果,具体如下:
package com.companyname.projectname.modulename.model.properties;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.views.properties.TextPropertyDescriptor;
public class MyTextPropertyDescriptor extends TextPropertyDescriptor {
private boolean readonly;
public boolean isReadonly() {
return readonly;
}
public void setReadonly(boolean readonly) {
this.readonly = readonly;
}
public MyTextPropertyDescriptor(Object id, String displayName) {
super(id, displayName);
}
public CellEditor createPropertyEditor(Composite parent) {
CellEditor editor = new TextCellEditor(parent);
//-->这是关键,设置控件的Enabled属性来实现...
if (this.readonly){
editor.getControl().setEnabled(false);
}
//<--
if (getValidator() != null)
editor.setValidator(getValidator());
return editor;
}
其它的PropertyDescriptor类似
在IPropertySource的继承类中,就用这个MyTextPropertyDescriptor替换原来的
TextPropertyDescriptor即可:
PropertyDescriptor descriptor = null;
descriptor = new MyTextPropertyDescriptor(ID_X, ID_X);
//设置分类
descriptor.setCategory("Basic Info");
descriptors.add(descriptor);
}