use of org.terasology.nui.databinding.Binding in project Terasology by MovingBlocks.
the class LocaleConstraintWidgetFactory method buildWidget.
@Override
protected Optional<UIWidget> buildWidget() {
Setting<Locale> setting = getSetting();
Binding<Locale> binding = new Binding<Locale>() {
@Override
public Locale get() {
return setting.get();
}
@Override
public void set(Locale value) {
setting.set(value);
}
};
UIDropdownScrollable<Locale> dropdownScrollable = new UIDropdownScrollable<>();
ResourceUrn menuUrn = new ResourceUrn("engine:menu");
TranslationProject menuProject = translationSystem.getProject(menuUrn);
List<Locale> locales = new ArrayList<>(menuProject.getAvailableLocales());
for (Locale languageExcluded : languagesExcluded) {
locales.remove(languageExcluded);
}
Collections.sort(locales, (Comparator.comparing((Function<Object, String>) Object::toString)));
dropdownScrollable.setOptions(Lists.newArrayList(locales));
// Set maximum number of options visible for scrolling
dropdownScrollable.setVisibleOptions(5);
dropdownScrollable.bindSelection(binding);
dropdownScrollable.setOptionRenderer(new LocaleRenderer(translationSystem));
return Optional.of(dropdownScrollable);
}
use of org.terasology.nui.databinding.Binding in project Terasology by MovingBlocks.
the class NUIEditorMenuTreeBuilder method getFieldValue.
private Object getFieldValue(Field field, Class clazz) throws IllegalAccessException, InstantiationException {
if (Enum.class.isAssignableFrom(field.getType())) {
return field.getType().getEnumConstants()[0].toString();
} else {
Object value;
if (Binding.class.isAssignableFrom(field.getType())) {
if (AbstractWidget.class.isAssignableFrom(clazz) && Objects.equals(field.getName(), "family")) {
// The default - and acceptable - value for the AbstractWidget.family binding is null, but a user
// most likely wants to use a custom family. Therefore, the default is set to an empty string instead.
value = "";
} else {
Binding binding = (Binding) field.get(newInstance(clazz));
value = binding.get();
}
} else if (Optional.class.isAssignableFrom(field.getType())) {
value = newInstance((Class) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0]);
} else {
value = field.get(newInstance(clazz));
}
if (value != null && value instanceof Boolean) {
value = !(Boolean) value;
}
return (value != null || Binding.class.isAssignableFrom(field.getType())) ? value : newInstance(field.getType());
}
}
use of org.terasology.nui.databinding.Binding in project Terasology by MovingBlocks.
the class ObjectLayoutBuilder method populateConstructorParameters.
private void populateConstructorParameters(Binding<T> binding, ColumnLayout parameterLayout, UIButton createInstanceButton, Binding<Constructor<T>> selectedConstructor) {
parameterLayout.removeAllWidgets();
Parameter[] parameters = selectedConstructor.get().getParameters();
List<TypeInfo<?>> parameterTypes = Arrays.stream(parameters).map(Parameter::getParameterizedType).map(parameterType -> ReflectionUtil.resolveType(type.getType(), parameterType)).map(TypeInfo::of).collect(Collectors.toList());
List<Binding<?>> argumentBindings = parameterTypes.stream().map(parameterType -> new DefaultBinding<>(Defaults.defaultValue(parameterType.getRawType()))).collect(Collectors.toList());
createInstanceButton.subscribe(widget -> {
Object[] arguments = argumentBindings.stream().map(Binding::get).toArray();
try {
binding.set(selectedConstructor.get().newInstance(arguments));
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
});
if (argumentBindings.isEmpty()) {
// TODO: Translate
parameterLayout.addWidget(new UILabel("Constructor has no parameters"));
return;
}
ColumnLayout parametersExpandableLayout = WidgetUtil.createExpandableLayout(// TODO: Translate
"Constructor Parameters", this::createDefaultLayout, layout -> {
for (int i = 0; i < parameterTypes.size(); i++) {
TypeInfo<?> parameterType = parameterTypes.get(i);
Binding<?> argumentBinding = argumentBindings.get(i);
Parameter parameter = parameters[i];
Optional<UIWidget> optionalWidget = library.getBaseTypeWidget((Binding) argumentBinding, parameterType);
if (!optionalWidget.isPresent()) {
LOGGER.warn("Could not create widget for parameter of type {} of constructor {}", parameter, selectedConstructor.get());
continue;
}
UIWidget widget = optionalWidget.get();
String parameterLabelText = ReflectionUtil.typeToString(parameterType.getType(), true);
layout.addWidget(WidgetUtil.labelize(widget, parameterLabelText, LABEL_WIDGET_ID));
}
}, this::createDefaultLayout);
parameterLayout.addWidget(parametersExpandableLayout);
}
use of org.terasology.nui.databinding.Binding 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.Binding 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