Search in sources :

Example 1 with Deprecation

use of org.jboss.hal.dmr.Deprecation in project console by hal.

the class AbstractFormItemTest method setDeprecated.

@Test
public void setDeprecated() {
    ModelNode modelNode = new ModelNode();
    modelNode.get(SINCE).set("1.2.3");
    modelNode.get(REASON).set("why not");
    Deprecation deprecation = new Deprecation(modelNode);
    AbstractFormItem<String> formItem = formItem(false);
    assertFalse(formItem.isDeprecated());
    formItem.setDeprecated(deprecation);
    assertTrue(formItem.isDeprecated());
    verify(readOnlyAppearance).apply(DEPRECATED, deprecation);
    verify(editingAppearance).apply(DEPRECATED, deprecation);
    formItem.setDeprecated(null);
    assertFalse(formItem.isDeprecated());
    verify(readOnlyAppearance).unapply(DEPRECATED);
    verify(editingAppearance).unapply(DEPRECATED);
}
Also used : Deprecation(org.jboss.hal.dmr.Deprecation) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ModelNode(org.jboss.hal.dmr.ModelNode) Test(org.junit.Test)

Example 2 with Deprecation

use of org.jboss.hal.dmr.Deprecation in project console by hal.

the class DefaultFormItemProvider method createFrom.

@Override
public FormItem<?> createFrom(Property property) {
    FormItem<?> formItem = null;
    String name = property.getName();
    String label = labelBuilder.label(property);
    ModelNode attributeDescription = property.getValue();
    // don't use 'required' here!
    boolean required = attributeDescription.hasDefined(NILLABLE) && !attributeDescription.get(NILLABLE).asBoolean();
    boolean expressionAllowed = attributeDescription.hasDefined(EXPRESSIONS_ALLOWED) && attributeDescription.get(EXPRESSIONS_ALLOWED).asBoolean();
    boolean readOnly = attributeDescription.hasDefined(ACCESS_TYPE) && (READ_ONLY.equals(attributeDescription.get(ACCESS_TYPE).asString()) || METRIC.equals(attributeDescription.get(ACCESS_TYPE).asString()));
    String unit = attributeDescription.hasDefined(UNIT) ? attributeDescription.get(UNIT).asString() : null;
    Deprecation deprecation = attributeDescription.hasDefined(DEPRECATED) ? new Deprecation(attributeDescription.get(DEPRECATED)) : null;
    if (attributeDescription.hasDefined(TYPE)) {
        ModelType type = attributeDescription.get(TYPE).asType();
        ModelType valueType = (attributeDescription.has(VALUE_TYPE) && attributeDescription.get(VALUE_TYPE).getType() != ModelType.OBJECT) ? ModelType.valueOf(attributeDescription.get(VALUE_TYPE).asString()) : null;
        switch(type) {
            case BOOLEAN:
                {
                    SwitchItem switchItem = new SwitchItem(name, label);
                    if (attributeDescription.hasDefined(DEFAULT)) {
                        switchItem.assignDefaultValue(attributeDescription.get(DEFAULT).asBoolean());
                    }
                    formItem = switchItem;
                    break;
                }
            case BIG_INTEGER:
            case INT:
            case LONG:
                {
                    long min, max;
                    if (type == ModelType.INT) {
                        min = attributeDescription.get(MIN).asLong(Integer.MIN_VALUE);
                        max = attributeDescription.get(MAX).asLong(Integer.MAX_VALUE);
                    } else {
                        min = attributeDescription.get(MIN).asLong(MIN_SAFE_LONG);
                        max = attributeDescription.get(MAX).asLong(MAX_SAFE_LONG);
                    }
                    NumberItem numberItem = new NumberItem(name, label, unit, min, max);
                    if (attributeDescription.hasDefined(DEFAULT)) {
                        long defaultValue = attributeDescription.get(DEFAULT).asLong();
                        numberItem.assignDefaultValue(defaultValue);
                    }
                    formItem = numberItem;
                    break;
                }
            case DOUBLE:
                {
                    long min = attributeDescription.get(MIN).asLong(MIN_SAFE_LONG);
                    long max = attributeDescription.get(MAX).asLong(MAX_SAFE_LONG);
                    NumberDoubleItem numberItem = new NumberDoubleItem(name, label, unit, min, max);
                    if (attributeDescription.hasDefined(DEFAULT)) {
                        double defaultValue = attributeDescription.get(DEFAULT).asDouble();
                        numberItem.assignDefaultValue(defaultValue);
                    }
                    formItem = numberItem;
                    break;
                }
            case LIST:
                {
                    if (valueType != null && ModelType.STRING == valueType) {
                        List<String> allowedValues = stringValues(attributeDescription, ALLOWED);
                        if (!allowedValues.isEmpty()) {
                            MultiSelectBoxItem multiSelectBoxItem = new MultiSelectBoxItem(name, label, allowedValues);
                            if (attributeDescription.hasDefined(DEFAULT)) {
                                List<String> defaultValues = stringValues(attributeDescription, DEFAULT);
                                if (!defaultValues.isEmpty()) {
                                    multiSelectBoxItem.assignDefaultValue(defaultValues);
                                }
                            }
                            formItem = multiSelectBoxItem;
                        } else {
                            ListItem listItem = new ListItem(name, label);
                            if (attributeDescription.hasDefined(DEFAULT)) {
                                List<String> defaultValues = stringValues(attributeDescription, DEFAULT);
                                if (!defaultValues.isEmpty()) {
                                    listItem.assignDefaultValue(defaultValues);
                                }
                            }
                            formItem = listItem;
                            checkCapabilityReference(attributeDescription, formItem);
                        }
                    } else if (isSimpleTuple(attributeDescription)) {
                        // process OBJECT type attribute if all of its subattributes are simple types
                        formItem = new TuplesListItem(name, label, metadata.forComplexAttribute(property.getName()));
                    } else {
                        logger.warn("Unsupported model type {} for attribute {} in metadata {}. Unable to create a form item. Attribute will be skipped.", type.name(), property.getName(), metadata.getTemplate());
                        break;
                    }
                    break;
                }
            case OBJECT:
                {
                    if (valueType != null && ModelType.STRING == valueType) {
                        PropertiesItem propertiesItem = new PropertiesItem(name, label);
                        List<Property> properties = ModelNodeHelper.getOrDefault(attributeDescription, DEFAULT, () -> attributeDescription.get(DEFAULT).asPropertyList(), emptyList());
                        if (!properties.isEmpty()) {
                            Map<String, String> defaultValues = new HashMap<>();
                            for (Property p : properties) {
                                defaultValues.put(p.getName(), p.getValue().asString());
                            }
                            propertiesItem.assignDefaultValue(defaultValues);
                        }
                        formItem = propertiesItem;
                    }
                    break;
                }
            case STRING:
                {
                    List<String> allowedValues = stringValues(attributeDescription, ALLOWED);
                    if (allowedValues.isEmpty()) {
                        FormItem<String> textBoxItem = new TextBoxItem(name, label, null);
                        boolean sensitive = failSafeGet(attributeDescription, ACCESS_CONSTRAINTS + "/" + SENSITIVE).isDefined();
                        if (PASSWORD.equals(name) || sensitive) {
                            textBoxItem.mask();
                        }
                        if (attributeDescription.hasDefined(DEFAULT)) {
                            textBoxItem.assignDefaultValue(attributeDescription.get(DEFAULT).asString());
                        }
                        formItem = textBoxItem;
                        checkCapabilityReference(attributeDescription, formItem);
                    } else {
                        SingleSelectBoxItem singleSelectBoxItem = new SingleSelectBoxItem(name, label, allowedValues, !required);
                        if (attributeDescription.hasDefined(DEFAULT)) {
                            singleSelectBoxItem.assignDefaultValue(attributeDescription.get(DEFAULT).asString());
                        }
                        formItem = singleSelectBoxItem;
                    }
                    break;
                }
            // unsupported types
            case BIG_DECIMAL:
            case BYTES:
            case EXPRESSION:
            case PROPERTY:
            case TYPE:
            case UNDEFINED:
                logger.warn("Unsupported model type {} for attribute {} in metadata {}. Unable to create a form item. Attribute will be skipped.", type.name(), property.getName(), metadata.getTemplate());
                break;
            default:
                break;
        }
        if (formItem != null) {
            formItem.setRequired(required);
            formItem.setDeprecated(deprecation);
            if (formItem.supportsExpressions()) {
                formItem.setExpressionAllowed(expressionAllowed);
                formItem.addResolveExpressionHandler(event -> {
                    // resend as application event
                    Core.INSTANCE.eventBus().fireEvent(event);
                });
            }
            if (readOnly) {
                formItem.setEnabled(false);
                // if the attribute is read-only and required, the form validation prevents to save the form
                // remove the required constraint to allow the save operation.
                formItem.setRequired(false);
            }
        }
    }
    return formItem;
}
Also used : TuplesListItem(org.jboss.hal.core.ui.TuplesListItem) SingleSelectBoxItem(org.jboss.hal.ballroom.form.SingleSelectBoxItem) FormItem(org.jboss.hal.ballroom.form.FormItem) NumberItem(org.jboss.hal.ballroom.form.NumberItem) MultiSelectBoxItem(org.jboss.hal.ballroom.form.MultiSelectBoxItem) PropertiesItem(org.jboss.hal.ballroom.form.PropertiesItem) TextBoxItem(org.jboss.hal.ballroom.form.TextBoxItem) Deprecation(org.jboss.hal.dmr.Deprecation) ModelType(org.jboss.hal.dmr.ModelType) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) NumberDoubleItem(org.jboss.hal.ballroom.form.NumberDoubleItem) ListItem(org.jboss.hal.ballroom.form.ListItem) TuplesListItem(org.jboss.hal.core.ui.TuplesListItem) ModelNode(org.jboss.hal.dmr.ModelNode) HashMap(java.util.HashMap) Map(java.util.Map) Property(org.jboss.hal.dmr.Property) SwitchItem(org.jboss.hal.ballroom.form.SwitchItem)

Example 3 with Deprecation

use of org.jboss.hal.dmr.Deprecation in project console by hal.

the class ReadOnlyAppearance method safeApply.

// ------------------------------------------------------ decorations
@Override
protected <C> void safeApply(Decoration decoration, C context) {
    switch(decoration) {
        case DEFAULT:
            defaultValue.textContent = String.valueOf(context);
            if (isApplied(HINT)) {
                valueContainer.insertBefore(defaultValue, hintElement);
            } else {
                valueContainer.appendChild(defaultValue);
            }
            break;
        case DEPRECATED:
            markAsDeprecated((Deprecation) context);
            break;
        case EXPRESSION:
            ExpressionContext ec = (ExpressionContext) context;
            expressionHandler = bind(expressionLink, click, event -> ec.callback.resolveExpression(masked ? backupValue : valueElement.textContent));
            if (isApplied(HINT)) {
                valueContainer.insertBefore(expressionLink, hintElement);
            } else {
                valueContainer.appendChild(expressionLink);
            }
            break;
        case HINT:
            hintElement.textContent = String.valueOf(context);
            valueContainer.appendChild(hintElement);
            break;
        case RESTRICTED:
            valueElement.textContent = "";
            Elements.removeChildrenFrom(valueContainer);
            valueContainer.appendChild(restrictedMarker);
            break;
        case SENSITIVE:
            if (isApplied(EXPRESSION)) {
                valueContainer.insertBefore(peekLink, expressionLink);
            } else {
                valueContainer.appendChild(peekLink);
            }
            mask();
            break;
        // not supported
        case ENABLED:
        case INVALID:
        case REQUIRED:
        case SUGGESTIONS:
            break;
        default:
            break;
    }
}
Also used : Elements.div(org.jboss.gwt.elemento.core.Elements.div) CSS.controlLabel(org.jboss.hal.resources.CSS.controlLabel) CSS.clickable(org.jboss.hal.resources.CSS.clickable) EXPRESSION(org.jboss.hal.ballroom.form.Decoration.EXPRESSION) CSS.formGroup(org.jboss.hal.resources.CSS.formGroup) CSS.halFormLabel(org.jboss.hal.resources.CSS.halFormLabel) READONLY(org.jboss.hal.ballroom.form.Form.State.READONLY) Strings(com.google.common.base.Strings) HandlerRegistration(com.google.web.bindery.event.shared.HandlerRegistration) SENSITIVE(org.jboss.hal.ballroom.form.Decoration.SENSITIVE) CSS.formControlStatic(org.jboss.hal.resources.CSS.formControlStatic) ExpressionContext(org.jboss.hal.ballroom.form.AbstractFormItem.ExpressionContext) Elements.p(org.jboss.gwt.elemento.core.Elements.p) HTMLElement(elemental2.dom.HTMLElement) Deprecation(org.jboss.hal.dmr.Deprecation) HINT(org.jboss.hal.ballroom.form.Decoration.HINT) CSS.marginRight5(org.jboss.hal.resources.CSS.marginRight5) HIDDEN(org.jboss.hal.resources.UIConstants.HIDDEN) CSS.hint(org.jboss.hal.resources.CSS.hint) Ids(org.jboss.hal.resources.Ids) MASK_CHARACTER(org.jboss.hal.resources.UIConstants.MASK_CHARACTER) Set(java.util.Set) CSS(org.jboss.hal.resources.CSS) EventType.click(org.jboss.gwt.elemento.core.EventType.click) Elements.label(org.jboss.gwt.elemento.core.Elements.label) TRUE(org.jboss.hal.resources.UIConstants.TRUE) EventType.bind(org.jboss.gwt.elemento.core.EventType.bind) CSS.empty(org.jboss.hal.resources.CSS.empty) Elements.span(org.jboss.gwt.elemento.core.Elements.span) CSS.halFormInput(org.jboss.hal.resources.CSS.halFormInput) Elements(org.jboss.gwt.elemento.core.Elements) CSS.fontAwesome(org.jboss.hal.resources.CSS.fontAwesome) ExpressionContext(org.jboss.hal.ballroom.form.AbstractFormItem.ExpressionContext)

Aggregations

Deprecation (org.jboss.hal.dmr.Deprecation)3 ModelNode (org.jboss.hal.dmr.ModelNode)2 Strings (com.google.common.base.Strings)1 HandlerRegistration (com.google.web.bindery.event.shared.HandlerRegistration)1 HTMLElement (elemental2.dom.HTMLElement)1 Collections.emptyList (java.util.Collections.emptyList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors.toList (java.util.stream.Collectors.toList)1 Elements (org.jboss.gwt.elemento.core.Elements)1 Elements.div (org.jboss.gwt.elemento.core.Elements.div)1 Elements.label (org.jboss.gwt.elemento.core.Elements.label)1 Elements.p (org.jboss.gwt.elemento.core.Elements.p)1 Elements.span (org.jboss.gwt.elemento.core.Elements.span)1 EventType.bind (org.jboss.gwt.elemento.core.EventType.bind)1 EventType.click (org.jboss.gwt.elemento.core.EventType.click)1 ExpressionContext (org.jboss.hal.ballroom.form.AbstractFormItem.ExpressionContext)1 EXPRESSION (org.jboss.hal.ballroom.form.Decoration.EXPRESSION)1