Colouring a Label in OpenXava based on a Field Value

 

OpenXava takes the pain of screen layout and consistency away from you. Yes, there are various ways of grouping and using tabs, dropdowns etc. but they are all coded from the entity definition. This is a huge benefit. However, occasionally you might want to add a little colour to your page. Again, this can be done from within the entity class definition.

I wanted to colour a screen label (as opposed to a screen message, warning or error) based on the summation of a detail collection. It could be that the sum of invoice lines is above a limit and you require a screen message to reflect this in the colour red.

It is surprisingly easy to add new screen elements using an OpenXava stereotype LABEL as follows:

@Depends(“wgpercenttotal”) . // On change of total change colour of label

@Stereotype(“LABEL”) // or consider @Stereotype(“HTML_TEXT”)

@Transient    // Not saved to database

@ReadOnly

public String getWarningMessage() {

if (getWgpercenttotal() > 100) {

return “<font color=” + “red” + “>Total Wealthgroup Percent is greater than 100</font>”;

} else if (getWgpercenttotal() < 100) {

return “<font color=” + “orange” + “>Total Wealthgroup Percent is less than 100</font>”;

} else if (getWgpercenttotal() == 100) {

return “<font color=” + “green” + “>Total Wealthgroup Percent is 100</font>”;

}

return null;

}

If you are unfamiliar with the OpenXava framework more details are at http://openxava.org/

OpenXava is a Java based framework that we are using for a project. Technologies include Java, Hibernate, Tomcat and MySQL i.e. OpenSource.