Menu

quarta-feira, 9 de dezembro de 2015

FoundationFaces CommandButton

While the Button component is used for navigation through HTTP GET requests, the CommandButton component generated an input that submits the parent form by default. A more detailed comparative between both can be found in this Balusc response.

Since I'm sticking to the default JSF renderers as much as possible, CommandButtonUI will be almost identical to ButtonUI. We need just to apply the style classes, which are exactly the same as the button component.

Basically, the only difference will be the tagName and the extended class (HtmlCommandButton instead of HtmlOutcomeTargetButton).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package org.foundation.faces.components;

import java.io.IOException;
import java.util.StringJoiner;
import javax.faces.application.ResourceDependencies;
import javax.faces.application.ResourceDependency;
import javax.faces.component.FacesComponent;
import javax.faces.component.html.HtmlCommandButton;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

/**
 *
 * @author hfluz <hfluz@uel.br>
 */
@ResourceDependencies({
    @ResourceDependency(library = "foundation", name = "css/foundation.min.css")
})
@FacesComponent(createTag = true, namespace = "http://foundation.faces.com/taglib",
        tagName = "commandButton")
public class CommandButtonUI extends HtmlCommandButton {

    enum PropertyKeys {
        sizing, expanded, coloring, hollow, disabled;
    }

    @Override
    public void encodeBegin(FacesContext context) throws IOException {
        ResponseWriter writer = context.getResponseWriter();
        this.setStyleClass(buildStyleClass());
        setDisabled(false);
        super.encodeBegin(context);
    }

    public String buildStyleClass() {
        StringJoiner styleClass = new StringJoiner(" ");
        styleClass.add("button");
        if (getStyleClass() != null) {
            styleClass.add(getStyleClass());
        }
        if (getSizing() != null && getSizing().matches("(.*)(tiny|small|normal|large)(.*)")) {
            styleClass.add(getSizing());
        }
        if (isExpanded()) {
            styleClass.add("expanded");
        }
        if (getColoring() != null && getColoring().matches("(.*)(secondary|success|alert|warning)(.*)")) {
            styleClass.add(getColoring());
        }
        if (isHollow()) {
            styleClass.add("hollow");
        }

        if (isDisabled()) {
            styleClass.add("disabled");
        }
        return styleClass.toString();
    }

    public String getSizing() {
        return (String) getStateHelper().eval(PropertyKeys.sizing, null);
    }

    public void setSizing(String sizing) {
        getStateHelper().put(PropertyKeys.sizing, sizing);
    }

    public Boolean isExpanded() {
        return (Boolean) getStateHelper().eval(PropertyKeys.expanded, Boolean.FALSE);
    }

    public void setExpanded(Boolean expanded) {
        getStateHelper().put(PropertyKeys.expanded, expanded);
    }

    public String getColoring() {
        return (String) getStateHelper().eval(PropertyKeys.coloring, null);
    }

    public void setColoring(String coloring) {
        getStateHelper().put(PropertyKeys.coloring, coloring);
    }

    public Boolean isHollow() {
        return (Boolean) getStateHelper().eval(PropertyKeys.hollow, Boolean.FALSE);
    }

    public void setHollow(Boolean hollow) {
        getStateHelper().put(PropertyKeys.hollow, hollow);
    }
}

Nenhum comentário :