我愿意解析XML Schema Definition并从解析的文件中获取TreeView,然后在生成XML之前在TreeView中添加和删除项目。
我的问题是当我想从选定的TreeItem中添加一个TreeItem时 - 导致它导致两个TreeItems的错误行为:选择的一个和新的TreeItem。
例如,扩展一个TreeItem会导致扩展另一个TreeItem。
SchemaTreeCell.java
public class SchemaTreeCell extends TreeCell<SchemaElement> {
GridPane grid = new GridPane();
private JFXButton bt ;
private ContextMenu detailsMenu ;
private TreeItem<SchemaElement> newItem;
private MenuItem showItem ;
private MenuItem addMenuItem;
private MenuItem deleteItem;
public SchemaTreeCell() {
showItem = new MenuItem("Show Details");
addMenuItem = new MenuItem("Add Element");
deleteItem = new MenuItem("Remove Element");
detailsMenu = new ContextMenu();
detailsMenu.getItems().add(showItem);
detailsMenu.getItems().add(addMenuItem);
detailsMenu.getItems().add(deleteItem);
bt = new JFXButton("Commit");
showAction();
addAction();
deleteAction();
}
protected void addAction() {
addMenuItem.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
String max = getItem().getMaxOccurs().equals("unbounded")? "100" : getItem().getMaxOccurs();
if (numberOccurence(getTreeItem())>=Integer.parseInt(max)) {
Alert alert = new Alert(AlertType.WARNING);
alert.setHeaderText("Be Careful");
alert.setContentText("Verify the Max Occurence of this item");
alert.show();
}
else {
newItem = new TreeItem<>();
newItem = getTreeView().getSelectionModel().getSelectedItem();
getTreeItem().getParent().getChildren().add(newItem);
addMenuItem.setOnAction(null);
t.consume();
}
}
});
}
@Override
protected void updateItem(SchemaElement item, boolean empty) {
super.updateItem(item, empty);
if (isEmpty()) {
setGraphic(null);
setText(null);
}else {
setText(item == null ? ""
: item.getValue() == null ? "<" + item.getName() + "/>"
: "<" + item.getName() + ">" + item.getValue() + "<" + item.
setContextMenu(detailsMenu);
}
}
这就是我所谓的CellFactory
getSchemaView().setCellFactory(new Callback<TreeView<SchemaElement>, TreeCell<SchemaElement>>() {
@Override
public TreeCell<SchemaElement> call(TreeView<SchemaElement> list) {
return new SchemaTreeCell();
}
});
答案 0 :(得分:1)
您正在将已经选择的完全相同的TreeItem
添加到树中。 TreeItem
具有expanded
状态(以及您几乎肯定不希望复制的其他状态),因此如果展开此树项,则两个表示都将展开。
相反,创建一个具有相同值的新树项,并将其添加到树中:
addMenuItem.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
String max = getItem().getMaxOccurs().equals("unbounded")? "100" : getItem().getMaxOccurs();
if (numberOccurence(getTreeItem())>=Integer.parseInt(max)) {
Alert alert = new Alert(AlertType.WARNING);
alert.setHeaderText("Be Careful");
alert.setContentText("Verify the Max Occurence of this item");
alert.show();
}
else {
newItem = createDeepCopy(getTreeView().getSelectionModel().getSelectedItem());
getTreeItem().getParent().getChildren().add(newItem);
addMenuItem.setOnAction(null);
t.consume();
}
}
});
// ...
private TreeItem<SchemaElement> createDeepCopy(TreeItem<SchemaElement> original) {
TreeItem<SchemaElement> copy = new TreeItem<SchemaElement>(original.getValue());
for (TreeItem<SchemaElement> child : original.getChildren()) {
copy.getChildren().add(createDeepCopy(child));
}
return copy ;
}