我正在使用附加的Properties视图的自定义编辑器(eclipse提供的默认属性视图)。在编辑器中,我有TreeViewer
,我在属性视图中显示所选节点属性。
TreeViewer正在处理XML文件。在createpartControl
方法中,我正在设置选择提供程序。
public void createPartControl(Composite parent) {
.........
treeViewer.getTree().setHeaderVisible(true);
treeViewer.getTree().setLinesVisible(true);
treeViewer.setContentProvider(new CopybookXMLEditorContentProvider());
//creating columns
createColumns(treeColLayout);
//setting tree input
treeViewer.setInput(inputFileDoc.getDocumentElement());
//adding tree to workbench selection service for providing properties
getSite().setSelectionProvider(treeViewer);
.....
}
由于选择将是一个DOM节点,我添加了一个提供IPropertySourceProvider
对象的适配器。
public <T> T getAdapter(Object adaptableObject, Class<T> adapterType) {
if(adaptableObject instanceof Node && adapterType == IPropertySourceProvider.class){
return (T) new CopybookXMLEditorPropertyProvider(((Node)adaptableObject));
}
return null;
}
@Override
public Class<?>[] getAdapterList() {
return new Class[]{IPropertySourceProvider.class};
}
在实现IPropertySourceProvider
接口的类中,我从传递的节点获取属性并将其设置在POJO类中。我在IPropertySource
方法中将此对象设为getPropertySource()
类。
public class CopybookXMLEditorPropertyProvider implements IPropertySourceProvider {
private Node selectedNode;
public CopybookXMLEditorPropertyProvider(Node selectedNode) {
this.selectedNode = selectedNode;
}
@Override
public IPropertySource getPropertySource(Object object) {
CopybookItem item = prepareItemValues();
return new CopybookXMLEditorPropertySource(item);
}
private CopybookItem prepareItemValues() {
CopybookItem item = new CopybookItem();
Node nameNode = selectedNode.getAttributes().getNamedItem("name");
if(nameNode!=null) {
item.setCopybookFieldName(nameNode.getNodeValue());
}
....
return item;
}
CopybookItem类
public class CopybookItem {
private String copybookFieldName = CommonConstants.EMPTY_STRING;
.......
public String getCopybookFieldName() {
return copybookFieldName;
}
public void setCopybookFieldName(String copybookFieldName) {
this.copybookFieldName = copybookFieldName;
}
//....remaining getter setters
}
在实现IPropertySource
接口
public class CopybookXMLEditorPropertySource implements IPropertySource{
private CopybookItem copybookItem;
public CopybookXMLEditorPropertySource(CopybookItem copybookItem) {
this.copybookItem = copybookItem;
}
@Override
public Object getEditableValue() {
return this;
}
@Override
public IPropertyDescriptor[] getPropertyDescriptors() {
IPropertyDescriptor[] propertyDescArr = new IPropertyDescriptor[7];
TextPropertyDescriptor javaBeanDescriptor = new TextPropertyDescriptor("javaBean", "Java Property Name");
.....
return propertyDescArr;
}
@Override
public Object getPropertyValue(Object id) {
String property = (String)id;
switch (property) {
case "javaBean":
return copybookItem.getJavaPropertyName();
....
}
}
@Override
public void setPropertyValue(Object id, Object value) {
String property = (String)id;
String propertyvalue = String.valueOf(value);
switch (property) {
case "javaBean":
copybookItem.setJavaPropertyName(propertyvalue);
...more cases
}
}
属性是可编辑的,因此一旦用户更改它们,我想将树查看器与更改同步并将编辑器标记为脏。如何在树查看器中监听这些更改并更新原始XML节点。
任何人都可以就如何实现这一点给出一些指示。 谢谢!