Search in sources :

Example 1 with NotifyingBinding

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);
}
Also used : 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) Constructor(java.lang.reflect.Constructor) UIBox(org.terasology.nui.widgets.UIBox) UIDropdownScrollable(org.terasology.nui.widgets.UIDropdownScrollable) ColumnLayout(org.terasology.nui.layouts.ColumnLayout) UIButton(org.terasology.nui.widgets.UIButton) NotifyingBinding(org.terasology.nui.databinding.NotifyingBinding)

Example 2 with NotifyingBinding

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);
}
Also used : ModuleManager(org.terasology.engine.core.module.ModuleManager) LoggerFactory(org.slf4j.LoggerFactory) TypeWidgetLibrary(org.terasology.nui.widgets.types.TypeWidgetLibrary) TypeInfo(org.terasology.reflection.TypeInfo) TypeRegistry(org.terasology.reflection.TypeRegistry) ExpandableLayoutBuilder(org.terasology.nui.widgets.types.builtin.util.ExpandableLayoutBuilder) NotifyingBinding(org.terasology.nui.databinding.NotifyingBinding) ColumnLayout(org.terasology.nui.layouts.ColumnLayout) StringTextRenderer(org.terasology.nui.itemRendering.StringTextRenderer) Binding(org.terasology.nui.databinding.Binding) UIDropdownScrollable(org.terasology.nui.widgets.UIDropdownScrollable) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) PermissionProvider(org.terasology.gestalt.module.sandbox.PermissionProvider) Module(org.terasology.gestalt.module.Module) ModuleEnvironment(org.terasology.gestalt.module.ModuleEnvironment) ModuleContext(org.terasology.engine.core.module.ModuleContext) Set(java.util.Set) ReflectionUtil(org.terasology.engine.utilities.ReflectionUtil) Collectors(java.util.stream.Collectors) List(java.util.List) TypeWidgetBuilder(org.terasology.nui.widgets.types.TypeWidgetBuilder) Type(java.lang.reflect.Type) Modifier(java.lang.reflect.Modifier) Optional(java.util.Optional) Name(org.terasology.gestalt.naming.Name) UIDropdownScrollable(org.terasology.nui.widgets.UIDropdownScrollable) TypeInfo(org.terasology.reflection.TypeInfo) Type(java.lang.reflect.Type) ColumnLayout(org.terasology.nui.layouts.ColumnLayout) NotifyingBinding(org.terasology.nui.databinding.NotifyingBinding)

Aggregations

List (java.util.List)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 ReflectionUtil (org.terasology.engine.utilities.ReflectionUtil)2 Binding (org.terasology.nui.databinding.Binding)2 NotifyingBinding (org.terasology.nui.databinding.NotifyingBinding)2 StringTextRenderer (org.terasology.nui.itemRendering.StringTextRenderer)2 ColumnLayout (org.terasology.nui.layouts.ColumnLayout)2 UIDropdownScrollable (org.terasology.nui.widgets.UIDropdownScrollable)2 TypeWidgetLibrary (org.terasology.nui.widgets.types.TypeWidgetLibrary)2 ExpandableLayoutBuilder (org.terasology.nui.widgets.types.builtin.util.ExpandableLayoutBuilder)2 TypeInfo (org.terasology.reflection.TypeInfo)2 Defaults (com.google.common.base.Defaults)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Modifier (java.lang.reflect.Modifier)1 Parameter (java.lang.reflect.Parameter)1