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

3
  • I assume this is "displayed" and "hidden" in some way that's related to openxava? Please provide much more information about exactly what you mean.
    – Jon Skeet
    Commented Aug 24, 2023 at 9:44
  • Duplicated in Sourceforge Forum Commented Aug 24, 2023 at 12:43
  • I have no idea how that comment is meant to help clarify the question - but really, you should include more information.
    – Jon Skeet
    Commented Aug 24, 2023 at 12:54

2 Answers 2

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

3
  • Many thanks, as usual. I have inserted the adopted solution as an additional solution for readability. Commented Aug 30, 2023 at 9:52
  • Perhaps I missed something, but I think this solution works well when actually changing certificateType, however, when the persisted form is loaded back later, it would not initialize the hidden state according to the loaded value of certificateType. Can it be done as well? Commented Jan 23 at 22:34
  • To execute onchange events when you load a persisted form you have to define the XAVA_SEARCH_ACTION as CRUD.searchExecutingOnChange. Look at: openxava.org/OpenXavaDoc/docs/… Commented Jan 25 at 14:23
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.

1
  • The values are lost because of getView().setViewName("..."). When you change the view name the values are cleared. Commented Aug 31, 2023 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.