How to use renderer feature easily?

Hi, i have read a lot about renderers and i think it is much more complicated than converters and validator for example.

For example i would like that the inputText that come by default with jsf don't be gray but red. I know that i can fix its css attributes and every thing ok, but what i want is that when i pick on the component toolbar the inputText and drag it into the page the component shows itself red.

So, to do that i have to write a Renderer subclass, define a new jsp tag and its handler class, i think this is a lot of work just to change a default color.

There is another way to do that?

I think for instace, extend the default renderer class of the inputText component of jsf and in some way define the new color the component will be rendered.

[793 byte] By [maykella] at [2007-10-3 0:02:44]
# 1

Hi,

i had a similar problem. I wanted to render the rows and columns of my dataTable dependend on the underlying data. So i just did what you mentioned:

Extend the renderer for this component and decide what to do to its attributes.

Example:

public void encodeChildren(FacesContext context, UIComponent component)

throws IOException

{

if(context == null || component == null)

throw new NullPointerException(Util.getExceptionMessageString("com.sun.faces.NULL_PARAMETERS_ERROR"));

if(log.isDebugEnabled())

log.debug("Begin encoding children " + component.getId());

if(!component.isRendered())

{

String columnClasses[] = getColumnClasses(data);

int columnStyle = 0;

int columnStyles = columnClasses.length;

String AllrowClasses[] = getRowClasses(data);

// Clean AllrowClasses for iteratable rowStyles (without 'Feiertage')

String rowClasses[] = shiftStylesforActWeekday(AllrowClasses,data);

do

{

if(rows > 0 && ++processed > rows)

break;

...

// If act. row is 'Feiertag' include style for all columns for this row

columnCount++;

if ((tagN != null && !tagN.equalsIgnoreCase(""))|| (tagS != null && !tagS.equalsIgnoreCase("")&& isSaarWorker))

//**

if(columnStyles > 0)

{

writer.writeAttribute("class", columnClasses[columnStyle++], "columnClasses");

if(columnStyle >= columnStyles)

columnStyle = 0;

}

...

// Insert extra row after "So"

if (wtag.equalsIgnoreCase("So")){

InsertExtraRow(writer, columnCount);

}

} while(true);

...

I had to change the column and rowClasses array depending which row is first in dataTable. So i shift this array and the renderer uses the shifted array to style each row and column.

I hope that may help!? Regards,

ak

barntia at 2007-7-14 16:50:27 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Yes your example is a great help, but i just don't know the class name i have to extend in order to modify the default components, for instance, if i want to modify the way the inputText component is rendered what renderer class i have to extend?
maykella at 2007-7-14 16:50:27 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Yes your example is a great help, but i just don't know the class name i have to extend in order to modify the default components, for instance, if i want to modify the way the inputText component is rendered what renderer class i have to extend? please, help me...
maykella at 2007-7-14 16:50:27 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Yes your example is a great help, but i just don't know the class name i have to extend in order to modify the default components, for instance, if i want to modify the way the inputText component is rendered what renderer class i have to extend? please, help me...
maykella at 2007-7-14 16:50:27 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Hi,

first you have to determine which component to render with which renderer:

<renderer>

<component-family>javax.faces.Data</component-family>

<renderer-type>javax.faces.Table</renderer-type>

<renderer-class>de.wfm.tag.MyUIDataTableRenderer

</renderer-class>

</renderer>

After that you implement the mentioned renderer "MyUIDataTableRenderer" in the specified location "de.wfm.tag.MyUIDataTableRenderer". I chose to extend from "HtmlBasicRenderer". The hardest thing will be to implement the behaviour of your renderer. But perhaps you find the basic implementation somewhere to have a look on (myFaces?).

Good luck, regards,

ak

barntia at 2007-7-14 16:50:27 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...