0

Labels in OpenXava are specified in i18n files. But what if I need to change the label in runtime depending on some logic, from an action for example.

Let's say I have an entity with a profit property, but if the property value is negative I want to change the label from "Profit" to "Loss". Like in this action code:

public void execute() throws Exception {
    // ...
    BigDecimal profit = (BigDecimal) getView().getValue("profit");
    if (profit.compareTo(BigDecimal.ZERO) < 0) {
        // Here I want to change the label of profit from "Profit" to "Loss"
    }
}
javierpaniza
  • 677
  • 4
  • 10

1 Answers1

0

The View class has a setLabelId() method to change the label of a property. Although it receives a label id from the i18n labels file, if you send a label that is not a key on the i18n file, the label id is shown literally, also you can send the label between ', and then it is always shown literally. That is you can use this in your action:

getView().setLabelId("myProperty", "This is my property");

And then the label for myProperty will be "This is my property". Although is better practice to use an id from the i18n file instead of the label itself, that is you could write the above code:

getView().setLabelId("myProperty", "thisIsMyProperty");

And in the labels i18n file add:

thisIsMyProperty=This is my property

So, you could write your code in this way:

public void execute() throws Exception {
    // ...
    BigDecimal profit = (BigDecimal) getView().getValue("profit");
    if (profit.compareTo(BigDecimal.ZERO) < 0) {
        getView().setLabelId("profit", "loss");
    }
}
javierpaniza
  • 677
  • 4
  • 10