use of org.terasology.nui.databinding.NotifyingBinding in project Terasology by MovingBlocks.
the class ObjectLayoutBuilder method populateNullLayout.
private void populateNullLayout(Binding<T> binding, ColumnLayout layout, UILabel nameWidget) {
// TODO: Translate
if (MODIFY_LABEL.equals(nameWidget.getText())) {
nameWidget.setText(NULL_LABEL);
}
List<Constructor<T>> constructors = Arrays.stream(type.getRawType().getConstructors()).map(constructor -> (Constructor<T>) constructor).collect(Collectors.toList());
if (constructors.isEmpty()) {
// TODO: Translate
UIBox box = buildErrorWidget("No accessible constructors found");
layout.addWidget(box);
return;
}
ColumnLayout parameterLayout = createDefaultLayout();
UIButton createInstanceButton = new UIButton();
// TODO: Translate
createInstanceButton.setText("Create Instance");
UIDropdownScrollable<Constructor<T>> constructorSelection = new UIDropdownScrollable<>();
Binding<Constructor<T>> selectedConstructor = new NotifyingBinding<Constructor<T>>(constructors.get(0)) {
@Override
protected void onSet() {
populateConstructorParameters(binding, parameterLayout, createInstanceButton, this);
}
};
constructorSelection.setOptions(constructors);
constructorSelection.bindSelection(selectedConstructor);
constructorSelection.setOptionRenderer(new StringTextRenderer<Constructor<T>>() {
@Override
public String getString(Constructor<T> value) {
return ReflectionUtil.resolvedMethodToString(type.getType(), value, true);
}
});
// TODO: Translate
constructorSelection.setTooltip("Select the constructor to use to create the new object");
layout.addWidget(constructorSelection);
layout.addWidget(parameterLayout);
layout.addWidget(createInstanceButton);
}
use of org.terasology.nui.databinding.NotifyingBinding in project Terasology by MovingBlocks.
the class SubtypeLayoutBuilder method populate.
@Override
protected void populate(Binding<T> binding, ColumnLayout layout, ColumnLayout mainLayout) {
ColumnLayout widgetContainer = new ColumnLayout();
Binding<TypeInfo<T>> editingType = new NotifyingBinding<TypeInfo<T>>(baseType) {
@Override
protected void onSet() {
widgetContainer.removeAllWidgets();
TypeWidgetBuilder<T> builder = SubtypeLayoutBuilder.this.library.getBuilder(get()).orElse(baseTypeWidgetBuilder);
if (builder == null) {
LOGGER.error("Could not find widget for type {}, editing as base type {}", get(), baseType);
return;
}
widgetContainer.addWidget(builder.build(binding));
}
};
if (binding.get() != null && !editingType.get().getRawType().equals(binding.get().getClass())) {
Type actual = ReflectionUtil.parameterizeandResolveRawType(baseType.getType(), binding.get().getClass());
TypeInfo<T> actualType = (TypeInfo<T>) TypeInfo.of(actual);
if (!allowedSubtypes.contains(actualType)) {
Optional<TypeInfo<T>> closestMatch = allowedSubtypes.stream().filter(subtype -> subtype.getRawType().isAssignableFrom(actualType.getRawType())).findFirst();
// closestMatch is always present since editingType is guaranteed to be a subtype of T
assert closestMatch.isPresent();
editingType.set(closestMatch.get());
} else {
editingType.set(actualType);
}
}
UIDropdownScrollable<TypeInfo<T>> typeSelection = new UIDropdownScrollable<>();
typeSelection.setOptions(allowedSubtypes);
typeSelection.bindSelection(editingType);
typeSelection.setOptionRenderer(new StringTextRenderer<TypeInfo<T>>() {
@Override
public String getString(TypeInfo<T> value) {
return getTypeName(value);
}
});
// TODO: Translate
typeSelection.setTooltip("Select the type for the new object");
layout.addWidget(typeSelection);
layout.addWidget(widgetContainer);
}
Aggregations