Skip to content

Commit

Permalink
TheCoder4eu/BootsFaces-OSP#919 demo for the converters
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanrauh committed Mar 10, 2018
1 parent 6a1b6de commit 0122c5b
Show file tree
Hide file tree
Showing 6 changed files with 430 additions and 5 deletions.
32 changes: 32 additions & 0 deletions src/main/java/net/bootsfaces/demo/selectOneMenu/Beer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package net.bootsfaces.demo.selectOneMenu;

/**
* Example taken from : https://memorynotfound.com/using-custom-converter-for-hselectonemenu/
*/

public class Beer {

private Integer id;
private String brand;

public Beer(Integer id, String brand) {
this.id = id;
this.brand = brand;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getBrand() {
return brand;
}

public void setBrand(String brand) {
this.brand = brand;
}
}
32 changes: 32 additions & 0 deletions src/main/java/net/bootsfaces/demo/selectOneMenu/BeerConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package net.bootsfaces.demo.selectOneMenu;

import javax.el.ValueExpression;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

/**
* Example taken from : https://memorynotfound.com/using-custom-converter-for-hselectonemenu/
*/

@FacesConverter(value = "beerConverter")
public class BeerConverter implements Converter {

@Override
public Object getAsObject(FacesContext ctx, UIComponent uiComponent, String beerId) {
System.out.println("getAsObject:" + beerId);

ValueExpression vex = ctx.getApplication().getExpressionFactory().createValueExpression(ctx.getELContext(),
"#{beersBean}", BeersBean.class);

BeersBean beers = (BeersBean) vex.getValue(ctx.getELContext());
return beers.getBeer(Integer.valueOf(beerId));
}

@Override
public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object beer) {
System.out.println("getAsString:" + beer);
return ((Beer) beer).getId().toString();
}
}
72 changes: 72 additions & 0 deletions src/main/java/net/bootsfaces/demo/selectOneMenu/BeersBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package net.bootsfaces.demo.selectOneMenu;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;

/**
* Example taken from : https://memorynotfound.com/using-custom-converter-for-hselectonemenu/
*/
@ManagedBean
@javax.faces.bean.ViewScoped
public class BeersBean implements Serializable {

/**
*
*/
private static final long serialVersionUID = -3192521384162408966L;
private Beer selectedBeer;
private Beer selectedBeer2;

public Beer getSelectedBeer2() {
return selectedBeer2;
}

public void setSelectedBeer2(Beer selectedBeer2) {
this.selectedBeer2 = selectedBeer2;
}

private List<Beer> beers;

public BeersBean() {
_init();
}

public void _init() {
beers = new ArrayList<Beer>();
beers.add(new Beer(10, "La Chouffe"));
beers.add(new Beer(20, "Stella Artois"));
beers.add(new Beer(30, "Westmalle Trippel"));
}

public Beer getSelectedBeer() {
return selectedBeer;
}

public void setSelectedBeer(Beer selectedBeer) {
System.out.println("set:" + selectedBeer);
this.selectedBeer = selectedBeer;
}

public List<Beer> getBeers() {
return beers;
}

public void setBeers(List<Beer> beers) {
this.beers = beers;
}

public Beer getBeer(Integer id) {
if (id == null) {
throw new IllegalArgumentException("no id provided");
}
for (Beer beer : beers) {
if (id.equals(beer.getId())) {
return beer;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package net.bootsfaces.demo;
package net.bootsfaces.demo.selectOneMenu;

import java.io.Serializable;

Expand Down
136 changes: 136 additions & 0 deletions src/main/webapp/forms/converters.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<b:listLinks>
<b:navLink href="#jsf_converters" value="JSF converters" />
<b:navLink href="#converter_demo" value="Converter demo" />
<b:navLink href="#select_one" value="SelectOneMenu with converters" />
</b:listLinks>
</ui:define>

Expand Down Expand Up @@ -161,6 +162,141 @@
</b:tabView>
</f:facet>
</b:panel>
<h1><a id="select_one">Using converters with &lt;b:selectOneMenu /&gt;</a><b:badge value="Since 1.2.1" /></h1>
<p>This example has been inspired by <a href="#" target="https://memorynotfound.com/using-custom-converter-for-hselectonemenu/">https://memorynotfound.com/using-custom-converter-for-hselectonemenu/</a>.
</p>
<b:panel title="Using a converter with b:selectOneMenu">
<b:form>
<b:panelGrid columns="3">
<b:selectOneMenu value="#{beersBean.selectedBeer}" ajax="true" process="@this" update="@next @next:@next" label="Your favorite beer brand?">
<f:converter converterId="beerConverter" />
<f:selectItems value="#{beersBean.beers}" var="beer"
itemValue="#{beer}" itemLabel="#{beer.brand}" />
</b:selectOneMenu>
<b:inputText value="#{beersBean.selectedBeer.id}" label="id" disabled="true"/>
<b:inputText value="#{beersBean.selectedBeer.brand}" label="name" disabled="true"/>

<b:selectOneMenu value="#{beersBean.selectedBeer2}" ajax="true" process="@this" update="@after"
converter="beerConverter" label="Any other beer you like?">
<f:selectItems value="#{beersBean.beers}" var="beer"
itemValue="#{beer}" itemLabel="#{beer.brand}" />
</b:selectOneMenu>
<b:inputText value="#{beersBean.selectedBeer2.id}" label="id" disabled="true"/>
<b:inputText value="#{beersBean.selectedBeer2.brand}" label="name" disabled="true"/>
</b:panelGrid>
</b:form>
<f:facet name="footer">
<b:tabView activeIndex="0" contentStyle="border-right: 1px solid #ddd;border-left: 1px solid #ddd;border-bottom: 1px solid #ddd;padding:10px">
<b:tab title="JSF markup">
<b:panel>
<script type="syntaxhighlighter"
class="brush: xml; toolbar: false;gutter: false; first-line: 1">
<![CDATA[
<b:form>
<b:panelGrid columns="3">
<b:selectOneMenu value="&num;{beersBean.selectedBeer}" ajax="true" process="@this" update="@next @next:@next" label="Your favorite beer brand?">
<f:converter converterId="beerConverter" />
<f:selectItems value="&num;{beersBean.beers}" var="beer"
itemValue="&num;{beer}" itemLabel="&num;{beer.brand}" />
</b:selectOneMenu>
<b:inputText value="&num;{beersBean.selectedBeer.id}" label="id" disabled="true"/>
<b:inputText value="&num;{beersBean.selectedBeer.brand}" label="name" disabled="true"/>

<b:selectOneMenu value="&num;{beersBean.selectedBeer2}" ajax="true" process="@this" update="@after"
converter="beerConverter" label="Any other beer you like?">
<f:selectItems value="&num;{beersBean.beers}" var="beer"
itemValue="&num;{beer}" itemLabel="&num;{beer.brand}" />
</b:selectOneMenu>
<b:inputText value="&num;{beersBean.selectedBeer2.id}" label="id" disabled="true"/>
<b:inputText value="&num;{beersBean.selectedBeer2.brand}" label="name" disabled="true"/>
</b:panelGrid>
</b:form>
]]>
</script>
</b:panel>
</b:tab>
<b:tab title="Custom converter">
<b:panel>
<script type="syntaxhighlighter"
class="brush: java; toolbar: false;gutter: false; first-line: 1">
<![CDATA[
@FacesConverter(value = "beerConverter")
public class BeerConverter implements Converter {

@Override
public Object getAsObject(FacesContext ctx, UIComponent uiComponent, String beerId) {
System.out.println("getAsObject:" + beerId);

ValueExpression vex = ctx.getApplication().getExpressionFactory().createValueExpression(ctx.getELContext(),
"#{beersBean}", BeersBean.class);

BeersBean beers = (BeersBean) vex.getValue(ctx.getELContext());
return beers.getBeer(Integer.valueOf(beerId));
}

@Override
public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object beer) {
System.out.println("getAsString:" + beer);
return ((Beer) beer).getId().toString();
}
}

]]>
</script>
</b:panel>
</b:tab>
<b:tab title="Java Bean">
<b:panel>
<script type="syntaxhighlighter"
class="brush: java; toolbar: false;gutter: false; first-line: 1">
<![CDATA[
@ManagedBean
@javax.faces.bean.ViewScoped
public class BeersBean implements Serializable {
private static final long serialVersionUID = -3192521384162408966L;
private Beer selectedBeer;
private Beer selectedBeer2;

public Beer getSelectedBeer2() {
return selectedBeer2;
}

public void setSelectedBeer2(Beer selectedBeer2) {
this.selectedBeer2 = selectedBeer2;
}

private List<Beer> beers;

public BeersBean() {
beers = new ArrayList<Beer>();
beers.add(new Beer(10, "La Chouffe"));
beers.add(new Beer(20, "Stella Artois"));
beers.add(new Beer(30, "Westmalle Trippel"));
}

// getters and setters omitted for the sage of brevity
public Beer getBeer(Integer id) {
if (id == null) {
throw new IllegalArgumentException("no id provided");
}
for (Beer beer : beers) {
if (id.equals(beer.getId())) {
return beer;
}
}
return null;
}
}
]]>
</script>
</b:panel>
</b:tab>
</b:tabView>
</f:facet>
</b:panel>



<script type="text/javascript">
SyntaxHighlighter.all();
</script>
Expand Down
Loading

0 comments on commit 0122c5b

Please sign in to comment.