Sunday, December 28, 2014

How to manage key strokes in ADF applications

The application that I am currently working on is a Next generation version of FORMS & REPORTS based application. One of the obvious questions from the end users is what is the support for navigating between the fields using key board keys in addition to mouse click. Well, in ADF there is no such thing as "tabindex" property that we can change during the UI design to get the order that we want. Since we could have multiple layouts in a single jsff, the order of the fields is decided by the ADF framework based on some internal logic. We often get requirements to navigate the focus when the users presses TAB+KEY in the key board from one field from one ADF Form Layout to another field  that is in placed different ADF Form Layout. i.e. navigation should cover the mandatory fields first.  But by default the TAB+KEY focus goes to the next field within the same form layout. In order to control this behavior we came with a java script function using which we can control the forward and reverse navigation based on pre-defined order of the fields. See the java script code below

//Initialize arrays (one per jsff) and put the IDs of the fields in the order that you want to navigate

var tradeIdArr = ['NptfDescId',
    'novalId',
    'ndepoDesc',
    'ncorrDId',
    'it9',
    'brokId',    
    'tradDaId',
    'seteDateId',
    'quantityId',
    'priceId',
    'tradeAmtId',
    'cdevDescId',
    'excRateId',
    'it26'];
    
var instIdArr = ['subtmplCodeDescId',
                 'cgtiId',
                 'cotlocaleId',
                 'scoId',
                 'cplaceId',
                 'nissuingd',
                 'nracineId',
                 'cpaysvalId',
                 'dc_it4',
                 'corcId'];   

//This function uses the initialized arrays and sets the focus on the UI field when the user presses "TAB" key in the key board. When the user presses SHIFT+TAB then the navigation goes in the reverse order. You need to pass the actual ID of the field in the first argument and the module/screen name in the second argument. In this example trade and instrument are two different screens

function tabOutOfLOVField(currentField, screen) {

  //currentField has the ID of the currently focused field. For e.g. trade_1 or instrument_1 etc
    
    return function tabOutOfLOVField(evt) {        
        var comp = evt.getSource(); //will return the component  
        var nextComp;
        var prevComp;
        
        if(screen == 'Trade'){
            genArr = tradeIdArr;
        }else if(screen == 'Instrument'){
            genArr = instIdArr;
        }
        
        var position = genArr.indexOf(currentField);               
         
        keyCode = evt.getKeyCode();
        keyModifiers = evt.getKeyModifiers();

        if (keyCode == AdfKeyStroke.TAB_KEY) {

            // Check if Shift + TAB is pressed
            if (keyModifiers == AdfKeyStroke.SHIFT_MASK) {
                if(position > 0){
                    position--;
                    prevComp = comp.findComponent(genArr[position]);
                    while(prevComp.getProperty("disabled") == true && position > 0)
                    {
                        position--;
                        prevComp = comp.findComponent(genArr[position]);                       
                    }        
                    setTimeout(function () {
                        prevComp.focus();
                    },10);
                    evt.cancel();
                }
            }
            else {
                    position++;
                    nextComp = comp.findComponent(genArr[position]);
                   while(nextComp.getProperty("disabled") == true && position < genArr.length)
                    {
                        position++;
                        nextComp = comp.findComponent(genArr[position]);
                    }        
        
                    setTimeout(function () {
                        nextComp.focus();
                    },10);
                    evt.cancel();
            }
        }
        else if (keyCode == AdfKeyStroke.F9_KEY) { //If the user presses F9 then open the search pop up
            var lovField = evt.getSource();
            AdfLaunchPopupEvent.queue(lovField, true);
            evt.cancel();
        }
    }

}

//From the jsff you can call the java script like below:

<af:clientListener method = "tabOutLOVField('brokId','Trade')"
type = "keyDown" />