Search in sources :

Example 6 with ColumnLayout

use of org.terasology.nui.layouts.ColumnLayout 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

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