Example of using the ADF Faces ChooseColor component with a SelectInputColor for picking a color

2

One of the components in the ADF Faces library you may not make use of on a daily base is the . In this article, I will give you an example of how to apply this component – as it is slightly more complex than you may expect at first glance (or at least than I expected at first).

 

 

This extremely simple sample demonstrates what the ColorChooser allows you to do: the end user can select a color from a color palette (and you can specify what color palette to present, which colors are available for selection) and the selected color can be applied to a backing bean and used in any way you see fit. In this example, we use the color to update the Background Color for the heading as well as the Panelbox. Let’s take a look at the code behind it:....

The source for our SampleColorChooser.jspx:

&lt;?xml version='1.0' encoding='UTF-8'?&gt;<br />&lt;jsp:root xmlns:jsp=&quot;http://java.sun.com/JSP/Page&quot; version=&quot;2.0&quot;<br />          xmlns:h=&quot;http://java.sun.com/jsf/html&quot;<br />          xmlns:f=&quot;http://java.sun.com/jsf/core&quot;<br />          xmlns:afh=&quot;http://xmlns.oracle.com/adf/faces/html&quot;<br />          xmlns:af=&quot;http://xmlns.oracle.com/adf/faces&quot;&gt;<br />  &lt;jsp:output omit-xml-declaration=&quot;true&quot; doctype-root-element=&quot;HTML&quot;<br />              doctype-system=&quot;http://www.w3.org/TR/html4/loose.dtd&quot;<br />              doctype-public=&quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;/&gt;<br />  &lt;jsp:directive.page contentType=&quot;text/html;charset=UTF-8&quot;/&gt;<br />  &lt;f:view&gt;<br />    &lt;html&gt;<br />      &lt;head&gt;<br />        &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;/&gt;<br />        &lt;title&gt;Sample ADF Faces ColorChooser&lt;/title&gt;<br />      &lt;/head&gt;<br />      &lt;body&gt;&lt;h:form&gt;<br />          &lt;af:outputLabel value=&quot;Here is a demonstration of the ADF Faces Color Chooser&quot;<br />                          inlineStyle=&quot;font-size:larger;#{ColorCache.style};font-family:Arial;&quot;/&gt;<br />          &lt;af:objectSpacer height=&quot;25&quot;/&gt;<br />          &lt;af:panelBox inlineStyle=&quot;#{ColorCache.style}&quot;&gt;<br />            &lt;af:outputLabel value=&quot;Choose the color of your dreams:&quot;/&gt;<br />            &lt;af:selectInputColor id=&quot;sic1&quot; value=&quot;#{ColorCache.awtColor}&quot;<br />                                 compact=&quot;true&quot; chooseId=&quot;cc2&quot;/&gt;<br />            &lt;af:chooseColor id=&quot;cc2&quot;/&gt;<br />            &lt;af:commandButton text=&quot;Apply New Color&quot;/&gt;<br />          &lt;/af:panelBox&gt;<br />        &lt;/h:form&gt;&lt;/body&gt;<br />    &lt;/html&gt;<br />  &lt;/f:view&gt;<br />&lt;/jsp:root&gt;<br />&nbsp;

It is quite straightfoward. The ChooseColor component (id=cc2) is included somewhere in the page. It displays a palette that the user can choose from. However, it does not have a value attribute: if you do nothing else but embed the ChooseColor in your page, the selected color goes nowhere.

The companion component for the ColorChooser is a SelectInputColor component. This component is used to receive a selected color from a ColorChooser. The link between the two is established through the chooseId attribute on the selectInputColor component: the value of this attribute should refer to (be equal to) the id of a ChooseColor component.

The value attribute of the SelectInputColor component is bound in an EL Expression (#{ColorCache.awtColor}) to a managed bean property. The name of the property reveals a little about the mechanics; the value held in the selectInputColor is a java.awt.Color and hence the property it gets bound to should also be of that type. Note: using a convertColor converter, we should be able to bind the value attribute to for example a String that contains a #RRGGBB pattern. However, I could not get this converter to work.

The managed bean ColorCache has two external properties: awtColor, set by the SelectInputColor, and style, derived from the awtColor inside the ColorCache bean:

package nl.amis.jsf.beans;<br /><br />import java.awt.Color;<br /><br />import oracle.adfinternal.view.faces.share.text.RGBColorFormat;<br /><br />public class ColorCache {<br /><br />    Color awtColor;<br />    String rgbColor;<br />    String style;<br />    <br />    public ColorCache() {<br />    }<br /><br />    public void setAwtColor(Color awtColor) {<br />        this.awtColor = awtColor;<br />        rgbColor= new RGBColorFormat(&quot;r,g,b&quot;).format(awtColor);<br />    }<br /><br />    public Color getAwtColor() {<br />        return awtColor;<br />    }<br /><br /><br />    public void setStyle(String style) {<br />        this.style = style;<br />    }<br /><br />    public String getStyle() {<br />        return &quot;background-color:rgb(&quot;+rgbColor+&quot;);&quot;;<br />    }<br />}<br />&nbsp;

Note that when the awtColor property is set, the internal rgbColor is set with the r,g,b pattern of the selected awtColor.

In the JSF page, we have to components referring to the style property on the ColorCache bean: the inlineStyle attribute on the panelBox is defined as #{ColorCache.style}. That means that the background color of the panelbox is set to whatever color we select in the SelectInputColor component. The inlineStyle of the outputLabel on top is slightly more interesting: "font-size:larger;#{ColorCache.style};font-family:Arial;". It picks the background color from the ColorCache bean and adds its own font-size and font-family.

Choosing another color and pressing the Apply Color button will redisplay the page with fresh colors:

Resources

OTN Documentation on ChooseColor component and the ConvertColor component

Frank Nimphius: ADF Faces: How-to set the field background color with css

 

Share.

About Author

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director for Fusion Middleware. Consultant, trainer and instructor on diverse areas including Oracle Database (SQL & PLSQL), Service Oriented Architecture, BPM, ADF, Java in various shapes and forms and many other things. Author of the Oracle Press book: Oracle SOA Suite 11g Handbook. Frequent presenter on conferences such as JavaOne, Oracle OpenWorld, ODTUG Kaleidoscope, Devoxx and OBUG. Presenter for Oracle University Celebrity specials.

2 Comments