在PropertySheetPage显示的属性中, 如果需要自定义属性显示的上下顺序, 就需要给PropertySheetPage添加一个PropertySheetSorter, 从而决定属性显示的上下顺序.
protected void setSorter(PropertySheetSorter sorter) {
但是, 这个方法是protected的, 所以, 只有在给Editor添加PropertySheetPage的时候, 需要使用比较脏的方法给PropertySheetPage 设置一个sorter了.
我的做法如下:
propertySheetPage = new PropertySheetPage() {
/**
* @see org.eclipse.ui.views.properties.PropertySheetPage#createControl(org.eclipse.swt.widgets.Composite)
*/
@Override
public void createControl(Composite parent) {
// 设置一个使用描述来排序的Sorter
PropertySheetSorter sorter = new PropertySheetSorter() {
public int compare(IPropertySheetEntry entryA, IPropertySheetEntry entryB) {
// 使用IPropertySheetEntry的description排序.
return getCollator().compare(entryA.getDescription(), entryB.getDescription());
}
};
this.setSorter(sorter);
super.createControl(parent);
}
};
注: 排序的规制是使用entryA.getDescription()来作为排序依据.
于是: Model中的代码如下:
public IPropertyDescriptor[] getPropertyDescriptors() {
TextPropertyDescriptor locationXPD = new TextPropertyDescriptor(P_LOCATION_X, "X坐标");
locationXPD.setDescription("01");
TextPropertyDescriptor locationYPD = new TextPropertyDescriptor(P_LOCATION_Y, "Y坐标");
locationYPD.setDescription("02");
return new IPropertyDescriptor[] { locationXPD, locationYPD};
}
效果: 'X坐标' 排在了 'Y坐标' 的前面了.