I’m in process of creating a Custom Action to create Child Row which will automatically set the selected record in the TreeDataGrid as parent of new record.
I got stuck in accessing the hierachy property of TreeDataGrid. I can’t find a getter method of it. Only I could see is getter to HierarchyColumn which is different altogether.
Below is my current code which producing NPE because i have used hierarchyColumn getter.
Why would you want to add your own logic to the CreateAction#setInitializer() method? Your code will not run unless the user himself specifies an initializer in his code.
Also, the initializer is set during the screen loading process, and not when you click on the button that triggers the action. You can move your code to CreateAction#execute() method and then you will be able to access the hierarchyProperty.
I’m creating a Child Action, which will create a Child record for the selected record in the TreeDataGrid. So I need to set the parent property attribute of the new entity record. Actually the initializer is getting called but the hierarchyColumn is always null.
But I will try with execute method and see if hierarcyColumn would work there
Does hierarchyProperty and hierarchyColumn are same?
If so why 2 different attributes provided for them in XML?
I guess hierarchyColumn is something only for display, that where the hierarchy dropdown icon will display however hierarchyProperty is something basic to define the hierarchy of the data. So get property path of hierarchyColumn might not help me in this case.
@ActionType(CreateChildAction.ID)
public class CreateChildAction<E extends Entity> extends CreateAction<E> {
public static final String ID = "createChild";
public CreateChildAction() {
this(ID);
}
public CreateChildAction(String id) {
super(id);
this.primary = true;
}
@Inject
protected void setMessages(Messages messages) {
this.caption = messages.getMainMessage("actions.CreateChild");
}
@SuppressWarnings({"ConstantConditions", "rawtypes", "unchecked"})
@Override
public void execute() {
if (target == null) {
throw new IllegalStateException("CreateChildAction target is not set");
}
if (!(target.getItems() instanceof EntityDataUnit)) {
throw new IllegalStateException("CreateChildAction target items is null or does not implement EntityDataUnit");
}
MetaClass metaClass = ((EntityDataUnit) target.getItems()).getEntityMetaClass();
if (metaClass == null) {
throw new IllegalStateException("Target is not bound to entity");
}
EditorBuilder builder = screenBuilders.editor(target);
if (newEntitySupplier != null) {
E entity = newEntitySupplier.get();
builder = builder.newEntity(entity);
} else {
builder = builder.newEntity();
}
if (initializer != null) {
builder = builder.withInitializer(initializer);
} else {
builder = builder.withInitializer(getDefaultInitializer());
}
builder = screenInitializer.initBuilder(builder);
if (transformation != null) {
builder.withTransformation(transformation);
}
Screen editor = builder.build();
if (afterCommitHandler != null) {
editor.addAfterCloseListener(afterCloseEvent -> {
CloseAction closeAction = afterCloseEvent.getCloseAction();
if (closeAction.equals(WINDOW_COMMIT_AND_CLOSE_ACTION)) {
Entity committedEntity = ((EditorScreen) editor).getEditedEntity();
afterCommitHandler.accept((E) committedEntity);
}
});
}
screenInitializer.initScreen(editor);
editor.show();
}
@SuppressWarnings({"unchecked", "ConstantConditions"})
protected Consumer<E> getDefaultInitializer() {
return (entity) -> {
if (target instanceof TreeTable
&& target.getItems() instanceof TreeTableItems
&& ((TreeTableItems<E>) target.getItems()).getHierarchyPropertyName() != null) {
String hierarchyPropertyName = ((TreeTableItems<E>) target.getItems()).getHierarchyPropertyName();
entity.setValue(hierarchyPropertyName, target.getSingleSelected());
}
};
}
}
TreeDataGrid
The hierarchyColumn is responsible for which column will be the main one when displaying hierarchical data.
Unfortunately, ContainerTreeDataGridItems class does not have getter for hierarchyProperty field. I have created an issue in Github to add a getter for a hierarchyProperty field.