All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.evasion.common.component.FormItem Maven / Gradle / Ivy

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.evasion.common.component;

import com.evasion.common.Component;
import com.evasion.common.Constante;
import java.io.IOException;
import javax.el.ValueExpression;
import javax.faces.application.ResourceDependencies;
import javax.faces.application.ResourceDependency;
import javax.faces.component.FacesComponent;
import javax.faces.component.UIInput;
import javax.faces.component.html.HtmlMessage;
import javax.faces.component.html.HtmlOutputLabel;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

/**
 *
 * @author glon-56610
 */
@FacesComponent(value = "com.evasion.common.component.FormItem")
@ResourceDependencies({
    @ResourceDependency(name = "component/core.css", target = "head"),
    @ResourceDependency(name = "component/theme.css", target = "head")
})
public abstract class FormItem extends Component {

    private HtmlOutputLabel labelUser = null;

    private UIInput inputUser = null;

    private HtmlMessage message = null;

    private enum PropertyKeys {

        format,
        label,
        value,
        minLength,
        maxLength,
        required,
        showMessage,
        showLabel,
        styleClass;

        String toString;

        PropertyKeys(String toString) {
            this.toString = toString;
        }

        PropertyKeys() {
        }

        @Override
        public String toString() {
            return ((toString != null) ? toString : super.toString());
        }
    }

    @Override
    public void encodeBegin(FacesContext context) throws IOException {
        if (!isRendered()) {
            return;
        }
        ResponseWriter responseWriter = context.getResponseWriter();
        responseWriter.startElement(Constante.HTML_ELEMENT_DIV, null);
        responseWriter.writeAttribute(Constante.HTML_ATTREBUT_CLASS, "ui-form-item ui-widget ui-helper-reset", null);
        // Champ nom d'utilisateur


        StringBuilder idGenerate = new StringBuilder();
        idGenerate.append(getId()).append('-').append("input");

        createLabel();
        inputUser = createInput();
        inputUser.setId(idGenerate.toString());
        inputUser.setRequired(isRequired());
        inputUser.setValue(getValue());
        this.getChildren().add(inputUser);
        createMessage();
    }

    public abstract UIInput createInput();

    private void createLabel() {
        if (isShowLabel()) {

            labelUser = new HtmlOutputLabel();
            StringBuilder idGenerate = new StringBuilder();
            idGenerate.append(getId()).append('-').append("label");
            labelUser.setId(idGenerate.toString());
            labelUser.setFor(inputUser.getId());
            StringBuilder value = new StringBuilder();
            value.append(getValue()).append(" :");
            if (isRequired()) {
                value.append("*");
            }
            labelUser.setValue(value.toString());
            this.getChildren().add(labelUser);
        }
    }

    private void createMessage() {
        if (isShowMessage()) {

            message = new HtmlMessage();
            message.setId(getId() + "-message");
            message.setFor(inputUser.getId());
            this.getChildren().add(message);
        }
    }

    @Override
    public void encodeChildren(FacesContext context) throws IOException {
        super.encodeChildren(context);
    }

    @Override
    public void encodeEnd(FacesContext context) throws IOException {
        if (!isRendered()) {
            return;
        }
        ResponseWriter responseWriter = context.getResponseWriter();


        //  createMessage();


        responseWriter.endElement(Constante.HTML_ELEMENT_DIV);

    }



    public String getFormat() {
        return (String) getStateHelper().eval(PropertyKeys.format, "text");
    }

    public void setLabel(ValueExpression format) {
        getStateHelper().put(PropertyKeys.label, format);
        handleAttribute(PropertyKeys.label.toString(), format);
    }

    public ValueExpression getLabel() {
        return (ValueExpression) getStateHelper().eval(PropertyKeys.label, null);
    }

    public void setValue(ValueExpression format) {
        getStateHelper().put(PropertyKeys.value, format);
        handleAttribute(PropertyKeys.value.toString(), format);
    }

    public ValueExpression getValue() {
        return (ValueExpression) getStateHelper().eval(PropertyKeys.value, null);
    }

    public void setStyleClass(String format) {
        getStateHelper().put(PropertyKeys.styleClass, format);
        handleAttribute(PropertyKeys.styleClass.toString(), format);
    }

    public ValueExpression getStyleClass() {
        return (ValueExpression) getStateHelper().eval(PropertyKeys.styleClass, null);
    }

    public void setShowLabel(Boolean showLabel) {
        getStateHelper().put(PropertyKeys.showLabel, showLabel);
        handleAttribute(PropertyKeys.showLabel.toString(), showLabel);
    }

    public Boolean isShowLabel() {
        return (Boolean) getStateHelper().eval(PropertyKeys.showLabel, true);
    }

    public void setRequired(Boolean required) {
        getStateHelper().put(PropertyKeys.required, required);
        handleAttribute(PropertyKeys.required.toString(), required);
    }

    public Boolean isRequired() {
        return (Boolean) getStateHelper().eval(PropertyKeys.required, true);
    }

    public void setShowMessage(Boolean showMessage) {
        getStateHelper().put(PropertyKeys.showMessage, showMessage);
        handleAttribute(PropertyKeys.showMessage.toString(), showMessage);
    }

    public Boolean isShowMessage() {
        return (Boolean) getStateHelper().eval(PropertyKeys.showMessage, true);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy