Search in sources :

Example 1 with LABEL_WIDGET_ID

use of org.terasology.nui.widgets.types.TypeWidgetFactory.LABEL_WIDGET_ID 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);
}
Also used : NotifyingBinding(org.terasology.nui.databinding.NotifyingBinding) DefaultBinding(org.terasology.nui.databinding.DefaultBinding) Binding(org.terasology.nui.databinding.Binding) Arrays(java.util.Arrays) Defaults(com.google.common.base.Defaults) LoggerFactory(org.slf4j.LoggerFactory) TypeWidgetLibrary(org.terasology.nui.widgets.types.TypeWidgetLibrary) TypeInfo(org.terasology.reflection.TypeInfo) ExpandableLayoutBuilder(org.terasology.nui.widgets.types.builtin.util.ExpandableLayoutBuilder) FieldsWidgetBuilder(org.terasology.nui.widgets.types.builtin.util.FieldsWidgetBuilder) WidgetUtil(org.terasology.nui.WidgetUtil) Constructor(java.lang.reflect.Constructor) UIBox(org.terasology.nui.widgets.UIBox) NotifyingBinding(org.terasology.nui.databinding.NotifyingBinding) DefaultBinding(org.terasology.nui.databinding.DefaultBinding) ColumnLayout(org.terasology.nui.layouts.ColumnLayout) Parameter(java.lang.reflect.Parameter) StringTextRenderer(org.terasology.nui.itemRendering.StringTextRenderer) Binding(org.terasology.nui.databinding.Binding) UIDropdownScrollable(org.terasology.nui.widgets.UIDropdownScrollable) Logger(org.slf4j.Logger) UILabel(org.terasology.nui.widgets.UILabel) ReflectionUtil(org.terasology.engine.utilities.ReflectionUtil) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) UIWidget(org.terasology.nui.UIWidget) List(java.util.List) UIButton(org.terasology.nui.widgets.UIButton) LABEL_WIDGET_ID(org.terasology.nui.widgets.types.TypeWidgetFactory.LABEL_WIDGET_ID) Optional(java.util.Optional) UILabel(org.terasology.nui.widgets.UILabel) DefaultBinding(org.terasology.nui.databinding.DefaultBinding) TypeInfo(org.terasology.reflection.TypeInfo) InvocationTargetException(java.lang.reflect.InvocationTargetException) UIWidget(org.terasology.nui.UIWidget) ColumnLayout(org.terasology.nui.layouts.ColumnLayout) Parameter(java.lang.reflect.Parameter)

Aggregations

Defaults (com.google.common.base.Defaults)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Parameter (java.lang.reflect.Parameter)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 ReflectionUtil (org.terasology.engine.utilities.ReflectionUtil)1 UIWidget (org.terasology.nui.UIWidget)1 WidgetUtil (org.terasology.nui.WidgetUtil)1 Binding (org.terasology.nui.databinding.Binding)1 DefaultBinding (org.terasology.nui.databinding.DefaultBinding)1 NotifyingBinding (org.terasology.nui.databinding.NotifyingBinding)1 StringTextRenderer (org.terasology.nui.itemRendering.StringTextRenderer)1 ColumnLayout (org.terasology.nui.layouts.ColumnLayout)1 UIBox (org.terasology.nui.widgets.UIBox)1 UIButton (org.terasology.nui.widgets.UIButton)1