我正在尝试将初始输入设置为树视图,但我必须忘记一些因为它不显示任何内容。
public class TreeEditor extends OperationDetailsAspectEditor {
private TreeViewer fConditionField;
private BooleanOperator root;
@Override
public void createControl(Composite parent, FormToolkit toolkit) {
createConditionControls(parent, toolkit);
}
private void createConditionControls(final Composite parent, FormToolkit toolkit) {
fConditionField = new TreeViewer(parent);
fConditionField.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 5));
fConditionField.setLabelProvider(new ConditionsLabelProvider());
fConditionField.setContentProvider(new ConditionsContentProvider());
fConditionField.setUseHashlookup(true);
fConditionField.setInput(getInitialTreeConditions());
fConditionField.expandAll();
}
private BooleanOperator getInitialTreeConditions(){
root = new BooleanOperator(Operator.AND);
return root;
}
}
public class ConditionsLabelProvider implements ILabelProvider {
@Override
public void addListener(ILabelProviderListener paramILabelProviderListener) {}
@Override
public void dispose() { }
@Override
public boolean isLabelProperty(Object paramObject, String paramString) {return false;}
@Override
public void removeListener(
ILabelProviderListener paramILabelProviderListener) {}
@Override
public Image getImage(Object paramObject) {return null;}
@Override
public String getText(Object element) {
if(element instanceof Model){
return ((Model) element).getDisplayName();
} else {
throw unknownElement(element);
}
}
protected RuntimeException unknownElement(Object element) {
return new RuntimeException("Unknown type of element in tree of type " + element.getClass().getName());
}
}
public class ConditionsContentProvider implements ITreeContentProvider {
private static Object[] EMPTY_ARRAY = new Object[0];
protected TreeViewer viewer;
@Override
public void dispose() {
}
@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
this.viewer = (TreeViewer) viewer;
}
@Override
public Object[] getElements(Object inputElement) {
return getChildren(inputElement);
}
@Override
public Object[] getChildren(Object parentElement) {
if(parentElement instanceof BooleanOperator){
BooleanOperator operator = ((BooleanOperator) parentElement);
return concat(operator.getOperators().toArray(),
operator.getExpresions().toArray());
}
return EMPTY_ARRAY;
}
@Override
public Object getParent(Object element) {
if(element instanceof Model) {
return ((Model)element).getParent();
}
return null;
}
@Override
public boolean hasChildren(Object element) {
return getChildren(element).length > 0;
}
protected Object[] concat(Object[] a, Object[] b) {
int aLen = a.length;
int bLen = b.length;
Object[] c = new Object[aLen + bLen];
System.arraycopy(a, 0, c, 0, aLen);
System.arraycopy(b, 0, c, aLen, bLen);
return c;
}
protected Object[] concat(Object[]... arrays) {
Object[] array = arrays[0] != null? arrays[0] : null;
for(int i = 1; i < arrays.length; i++) {
array = concat(arrays[i-1], arrays[i]);
}
return array;
}
}
模型
public abstract class Model {
protected BooleanOperator parent;
protected String displayName;
public String getDisplayName(){ return displayName; }
public void setDisplayName(String displayName){
this.displayName = displayName;
}
public BooleanOperator getParent() { return parent; }
public void setParent(BooleanOperator parent) { this.parent = parent; }
public Model(String displayName) { this.displayName = displayName; }
public Model(){}
}
public class BooleanOperator extends Model {
private Operator operator;
private List<BooleanOperator> operators;
private List<Expression> expressions;
public Operator getOperator() {
return operator;
}
public List<BooleanOperator> getOperators() {
return operators;
}
public List<Expresion> getExpresions() {
return expresions;
}
public BooleanOperator() {
operators = new ArrayList<BooleanOperator>();
expresions = new ArrayList<Expresion>();
}
public BooleanOperator(Operator operator) {
this();
this.operator = operator;
this.displayName = operator.toString();
}
}
这个想法是Tree包含如下内容:
AND
|- Attribute_1 == value_1
|- Attribute_2 == value_2
|- OR
|- Attribute_3 == value_3
节点是布尔运算符,叶子是表达式。