Menu
Mostrando postagens com marcador FoundationFaces. Mostrar todas as postagens
Mostrando postagens com marcador FoundationFaces. Mostrar todas as postagens

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);
    }
}

domingo, 6 de dezembro de 2015

FoundationFaces Button

According to JSF 2 h:button documentation:

Render an HTML "input" element of type "button". The value of the component is rendered as the button text and the outcome of the component is used to determine the target URL which is activated by onclick. If "image" attribute is specified, render it as the value of the "src" attribute after passing it to the getResourceURL() method of the ViewHandler for this application, and passing the result through the encodeResourceURL() method of the ExternalContext.

We can extend HtmlOutcomeTargetButton, so we don't have to start from scratch.

This is our component class:

1
2
3
4
5
6
7
8
@ResourceDependencies({
    @ResourceDependency(library = "foundation", name = "css/foundation.min.css")
})
@FacesComponent(createTag = true, namespace = "http://foundation.faces.com/taglib",
        tagName = "button")
public class ButtonUI extends HtmlOutcomeTargetButton {
   //code
}
  • The @ResourceDependencies annotation defines which resources this component requires to be rendered.
  • The @FacesComponent annotation defines the name of our component tag and also which namespace it uses.
NOTE: Since JSF 2 we can define the component metadata through annotations instead of defining them through the taglib.xml file, however most IDEs won't be able to identify its attributes for auto-completion.

To make it look like Foundation Buttons we have to add class="button" to the generated HTML button and a few more attributes to our ButtonUI:

  • sizing: change the button size
  • expanded: expand the button to fill the whole row
  • coloring: change the button color related to a meaning
  • hollow: invert the colors of the background/border and the font
  • disabled: add opacity and prevent cursor events

Since all the attributes are related to the styleClass, I don't need to reimplement the HtmlOutcomeTargetButton enconding methods, all I have to do is to change the styleClass value:

 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
    @Override
    public void encodeBegin(FacesContext context) throws IOException {
        ResponseWriter writer = context.getResponseWriter();
        this.setStyleClass(buildStyleClass());
        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();
    }

Let's try our component

page.xhtml:
 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
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:fo="http://foundation.faces.com/taglib">
    <h:head>
        <title>Buttons</title>
    </h:head>
    <h:body >
        <h:form prependId="false">
            <fo:button outcome="page2.xhtml" value="Tiny" sizing="tiny"/>
            <fo:button outcome="page2.xhtml" value="Small" sizing="small"/>
            <fo:button outcome="page2.xhtml" value="Normal"/>
            <fo:button outcome="page2.xhtml" value="Large" sizing="large"/>
            <fo:button outcome="page2.xhtml" value="Expanded small" sizing="small" expanded="true"/>
            <fo:button outcome="page2.xhtml" value="Expanded" expanded="true"/>
            <fo:button outcome="page2.xhtml" value="Secondary" coloring="secondary"/>
            <fo:button outcome="page2.xhtml" value="Success" coloring="success"/>
            <fo:button outcome="page2.xhtml" value="Alert" coloring="alert"/>
            <fo:button outcome="page2.xhtml" value="Warning" coloring="warning"/>
            <fo:button outcome="page2.xhtml" value="Hollow Secondary" coloring="secondary" hollow="true"/>
            <fo:button outcome="page2.xhtml" value="Hollow Success" coloring="success" hollow="true"/>
            <fo:button outcome="page2.xhtml" value="Hollow" hollow="true"/>
            <fo:button outcome="page2.xhtml" value="Hollow Alert" coloring="alert" hollow="true"/>
            <fo:button outcome="page2.xhtml" value="Hollow Warning" coloring="warning" hollow="true"/>
            <fo:button outcome="page2.xhtml" value="Disabled" disabled="true" />
        </h:form>
    </h:body>
</html>

This is the output:

The only button that wasn't displayed as expected is the disabled one because it's almost invisible. Let's see what happened.

According to foundation documentation, a disabled button should be rendered like this:

1
<a class="disabled button" href="#">Disabled Button</a>

However, our disabled button received one unexpected attribute:

1
<input value="Disabled" class="button disabled" disabled="disabled" type="button">

The disabled attribute is used by JSF to disable the button, but that way we are applying 2 differente disabled styles simultaneosly:

1
2
3
4
5
6
7
8
9
button.disabled {
    opacity: 0.25;
    cursor: not-allowed;
    pointer-events: none;
}
input:disabled, input[readonly], textarea:disabled, textarea[readonly] {
    background-color: #E6E6E6;
    cursor: default;
}

The simplest solution is to set disabled to false after we have already built our styleClass attribute and before it's used by HtmlOutcomeTargetButton encodeBegin method:

1
2
3
4
5
6
7
    @Override
    public void encodeBegin(FacesContext context) throws IOException {
        ResponseWriter writer = context.getResponseWriter();
        this.setStyleClass(buildStyleClass());
        setDisabled(false);
        super.encodeBegin(context);
    }

Now our disable button is rendered how it should:

ButtonUI final source code

 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
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.HtmlOutcomeTargetButton;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
@ResourceDependencies({
    @ResourceDependency(library = "foundation", name = "css/foundation.min.css")
})
@FacesComponent(createTag = true, namespace = "http://foundation.faces.com/taglib",
        tagName = "button")
public class ButtonUI extends HtmlOutcomeTargetButton {
    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);
    }
    public boolean isDisabled() {
        return (Boolean) getStateHelper().eval(PropertyKeys.disabled, false);
    }
    public void setDisabled(boolean disabled) {
        getStateHelper().put(PropertyKeys.disabled, disabled);
    }
}

domingo, 29 de novembro de 2015

New Maven project for FoundationFaces

The first step to start a new JavaServer Faces component library is to create the project itself.

I choose the Java Application wizard of Netbeans new project Maven category, I filled the project name FoundationFaces, the group id org.foundation.faces and I clicked to finish the wizard.

Then I added the org.glassfish.javax.faces dependency to my pom.xml:

 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
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.foundation.faces</groupId>
    <artifactId>FoundationFaces</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <developers>
        <developer>
            <id>hfluz</id>
            <name>Humberto Ferreira da Luz Jr</name>
            <email>hfluz@uel.br</email>
        </developer>
    </developers>
    <dependencies>
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.faces</artifactId>
            <version>2.2.12</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
</project>

In the next step I downloaded the Foundation for Sites resources (CSS, JS and fonts) and put them in this path:


FoundationFaces/src/main/resources/
└── META-INF
    └── resources
        └── foundation
            ├── css
            │   ├── foundation-icons.css
            │   └── foundation.min.css
            ├── fonts
            │   ├── foundation-icons.eot
            │   ├── foundation-icons.svg
            │   ├── foundation-icons.ttf
            │   └── foundation-icons.woff
            └── js
                ├── foundation.min.js
                └── jquery.min.js

And that's going to be enough to create FoundationFaces first components in the following posts.

JSF Custom Components Development

Here at the State University of Londrina IT department we adopted JavaServer Faces as the presentation layer of our Java EE apps. Since I deal with JSF every day, I'm always interested to deepen my knowledge, be it by reading books or the Java subreddit.

For convenience of our developers I already customized some Primefaces components (I keep our own fork with those customizations) and in a few cases I developed simple custom components to solve issues because it seemed to be the cleaner approach and I found very interesting to learn how JSF components work under the hood.

Earlier this year I created a github repo to develop a component library based on Zurb Foundation (just like Bootsfaces did with Boostrap).

I began creating a few Foundation for Sites components. Then I found out there was also the Foundation for Apps (which uses AngularJS). At first it seemed the latter one had more to do with JSF goals, but after each created component I felt there was some overlap between Angular and JSF custom components, which made me choose the Foundation for Sites as the base for my component library.

There isn't a lot of detailed resources about component development available. The Java EE 7 Tutorial was a good start to understand the main concepts and also to learn a few good practices. The Primefaces and Bootsfaces source code also helped a lot to understand more advanced concepts.

I intend to keep this blog updated in order to document my progress and also to help those who want another resource to understand how to develop custom components or how to change existing components to their needs. Furthermore my work might provide another responsive component library to the JSF community.