1

In OpenXava, I have a class having a "certificateType" property, with a set of fixed values from an enumeration.

In the same class, there is another property that have to be displayed if the certificateType has a specific value, hidden in other cases.

Can you suggest the best approach?

Thanks

2 Answers2

1

You have to add an @OnChange action to your certificateType property. Then in the code of your action you can hide or show any property using getView().setHidden("theProperty", true).

In your entity write your property:

enum CertificateType { A, B, C }
@OnChange(OnChangeCertificateTypeAction.class)
CertificateType certificateType;

Then write an action like this:

public class OnChangeCertificateTypeAction extends OnChangePropertyBaseAction { 
 
    public void execute() throws Exception {
        CerificateType newType = (CerificateType) getNewValue(); 
        if (newType == null) return;
        getView().setHidden("propertyNotForA", newType == CerificateType.A);
    }
 
}

For more information about how to use @OnChange look at the reference documentation, here: https://openxava.org/OpenXavaDoc/docs/view_en.html#View-Property%20customization-Property%20value%20change%20event

javierpaniza
  • 677
  • 4
  • 10
0

I followed the suggestion from Javier, but with a variation.

As the number of properties to hide/reveal is not small, I tried to define a view for each type of configuration (I have only three types). The code is as follows:

package net.mcoletti.glc.guim.actions;

import java.util.*;

import org.openxava.actions.*;

import net.mcoletti.glc.guim.*;

/**
 * Adapts the view used to edit the item according to the item type, in order to
 * hide the properties that ere unnecessary and avoid errors.
 * 
 * @author mcoletti
 *
 */
public class OnChangeTipoCertificato extends OnChangePropertyBaseAction {

    @Override
    public void execute() throws Exception {
         CertificatoDiPagamento.TipoCertificato value = (CertificatoDiPagamento.TipoCertificato) getNewValue();
         
         if (value == null) return;
         
         if (value.equals(CertificatoDiPagamento.TipoCertificato.ANTICIPO)) {
             Map mapIndexValues = getView().getKeyValuesWithValue();   
                getView().setViewName("anticipo");                          
                getView().setValues(mapIndexValues);
                getView().setValue("tipoDiCertificato",value);
         }
         else {
             Map mapIndexValues = getView().getKeyValuesWithValue();   /
                getView().setViewName("default");                          
                getView().setValues(mapIndexValues);
                getView().setValue("tipoDiCertificato",value);
         }
        
    }

}

Please note that after restoring the MapView with the properties value, I had to explicitly restore also the certificateType property with the new value. Otherwise it gets lost.