Friday, November 27, 2015

ADF 12.x: Changing the List Of Values "No rows to display" text

In this post I describe how I implemented an interesting Use Case where the customer wanted to have an alternate message in the LOV when there is no data found. We all know how this can be changed in an af:table component by simply changing the emptyText property. In a List of Values, this works 'slightly' different.

Lets start with the basics

By default you will see the following List Of Values, telling you that no data was found:

However in the case of my customer, they were not happy with the text. So I had to come up with something else. That is interesting, because the ListOfValues is an ADF component in itself and the individual components inside the LOV cannot be manipulated by means of properties. I had to find a way to get access to the table component inside the List Of Values.
The way to do this has been described by Frank Nimphius, a long while ago, in his Forum Harvest posts. To be more specific, it is the harvest of November 2010, section "How-to define access keys to buttons displayed in an LOV dialog"

This really helped me to solve the issue and exactly describes what should happen at runtime.

  1. A user clicks the LOV button next to an Input List of Values component. In response to this, the LOV component launch event is fired, which we listen to in a managed bean.
  2. The launch event however is handled during the JSF Invoke Application phase, whereas the LOV displays during Render Response. Because of this, it is not possible to use a dialog launch listener to set the access keys for the buttons. Instead, the launch listener is used to invoke client side JavaScript.
  3. The client side JavaScript addresses an af:serverListener component to queue a custom event in ADF Faces. The custom event invokes a managed bean method that accesses the list of value component to eventually find the table component.

I started with the code sample as was provided in Franks post and was able to extend it for my needs. The trick is in lines 32-39:

1:    //method called from the server listener  
2:    public void changeLovInternals(ClientEvent ce) {  
3:      //get the LOV component binding reference  
4:      RichInputListOfValues lov = getListOfValues();  
5:      String id = lov.getClientId(fctx);  
6:      RichPopup popup = null;  
7:      String facetName = null;  
8:      UIComponent componentFacet = null;  
9:      Iterator facetNames = lov.getFacetNames();  
10:      while (facetNames.hasNext()) {  
11:        facetName = (String);  
12:        if (facetName != null) {  
13:          componentFacet = lov.getFacet(facetName);  
14:          if (componentFacet != null && componentFacet instanceof RichPopup) {  
15:            popup = (RichPopup) componentFacet;  
16:            break;  
17:          }  
18:        }  
19:      }  
20:      RichDialog lovDialog = (RichDialog) popup.getChildren().get(0);  
21:      lovDialog.setCancelTextAndAccessKey("&Take me back");  
22:      lovDialog.setAffirmativeTextAndAccessKey("&Make my choice");  
23:      //refresh the LOV to show the buttons  
24:      AdfFacesContext.getCurrentInstance().addPartialTarget(lovDialog);  
25:      // I found that the dialog consists of a panelGridLayout with 2 gridrows  
26:      if (lovDialog.getChildCount() == 1 && lovDialog.getChildren().get(0) != null &&  
27:        lovDialog.getChildren().get(0) instanceof RichPanelGridLayout) {  
28:        RichPanelGridLayout panelGridLayout =  
29:          (RichPanelGridLayout) lovDialog.getChildren().get(0);  
30:        List uiCompList = panelGridLayout.getChildren();  
31:        // always gridrows and always two.  
32:        if (uiCompList.size() > 0) {  
33:          // the first one is the search panel  
34:          // the second contains the table  
35:          RichGridRow richGridRow = (RichGridRow) uiCompList.get(1);  
36:          RichGridCell richCell = (RichGridCell) richGridRow.getChildren().get(0);  
37:          RichTable theTable = (RichTable) richCell.getChildren().get(0);  
38:          theTable.setEmptyText("Really, this is something, but it works");  
39:          AdfFacesContext.getCurrentInstance().addPartialTarget(theTable);  
40:        }  
41:      }  
42:    }  

When you run the application now, the LOV will display the alternate text

If you know how to get into the internal structure of an inputListOfValues, or any other component, you will be able to manipulate these components. Note that there is no guarantee that these manipulation will work after a upgrade. Oracle can change the implementation. This is for instance what happened with the inputListOfValues when the panelGridComponent became available. The internal structure of the ListOfValues component switched from using PanelGroupLayout and PanelHeader to using the PanelGridLayout component.
The solution in this post is implemented in 12.1.3. I did not check if Oracle changed the inputListOfValues in 12.2.

The code for this post can be found here on GitHub.

Most credits go to Frank Nimphius for his Forum Harvest post

No comments: